require File.join(File.dirname(__FILE__), 'CONFIG.rb') # $DBG = true require 'rubygems' require 'facets' require 'test/unit' require 'og' class TC_OgInheritance < Test::Unit::TestCase # :nodoc: all include Og class Document property :title, String schema_inheritance def initialize(title) @title = title end end class Article < Document property :body, String def initialize(title, body) @title, @body = title, body end end class Photo < Document property :url, String def initialize(title, url) @title, @url = title, url end end class Car property :name, String belongs_to :admin end class User property :login, String many_to_many Car schema_inheritance end class Admin < User property :password, String #has_one Car end $og1.manage_classes(Document, Article, Document, Photo, Car, User, Admin) def test_all assert_equal Document, Photo.superclass descendents = Document.descendents assert_equal(2, descendents.size) assert descendents.include?(Photo) assert descendents.include?(Article) assert Document.ann.self.schema_inheritance # propagate schema_inheritance flag. assert Photo.ann.self.schema_inheritance # subclasses reuse the same table. assert_equal Document.table, Photo.table doc = Document.create('doc1') Photo.create('photo1', 'http:/www.gmosx.com/photo/1') Photo.create('photo2', 'http:/www.gmosx.com/photo/2') Article.create('art1', 'here comes the body') Article.create('art2', 'this is cool') Article.create('art3', 'this is cooler') docs = Document.all assert_equal 6, docs.size assert_equal 'art2', docs[4].title assert_equal Document, docs[0].class assert_equal Photo, docs[1].class assert_equal Article, docs[4].class photos = Photo.all assert_equal 2, photos.size assert_equal 'photo2', photos[1].title assert_equal 'http:/www.gmosx.com/photo/1', photos[0].url articles = Article.all assert_equal 3, articles.size assert_equal 'art3', articles[2].title articles = Article.all(:limit => 2) assert_equal 2, articles.size # Bug report. # This happens when creating a has_one Car in Admin, which is wrong # because it overrides the joins_many from User. #Admin.create #Admin.create.car end end