Skip to content

Instantly share code, notes, and snippets.

@lancecarlson
Created January 6, 2010 09:18
Show Gist options
  • Save lancecarlson/270157 to your computer and use it in GitHub Desktop.
Save lancecarlson/270157 to your computer and use it in GitHub Desktop.
require File.join(File.dirname(__FILE__), 'spec_helper')
describe "Single Table Inheritance" do
before(:all) do
cleanup_models :Person, :Male, :Father, :Son
$db.drop_collection('people')
class ::Person
include DataMapper::Mongo::Resource
property :id, ObjectID
property :name, String
property :job, String
property :type, Discriminator
end
class ::Male < Person; end
class ::Father < Male; end
class ::Son < Male; end
end
it "should have a type property that reflects the class" do
[Person, Male, Father, Son].each do |model|
object = model.create
object.reload
object.type.should == model
end
end
it "should parent should return an instance of the child when type is explicitly specified" do
[Person, Male, Father, Son].each do |model|
object = model.create
object.reload
object.should be_instance_of(model)
end
end
it "should discriminate types during reads" do
person = Person.all(:type => Person)
p person
Son.create
father1 = Father.create
father2 = Father.create
fathers = Father.all(:type => "Father")
fathers.should == [father1, father2]
fathers.each do |father|
father.should be_instance_of(Father)
end
end
end
Single Table Inheritance
READ
"(id = 4b4455437d209c2819000001)"
READ
"(id = 4b4455437d209c2819000002)"
READ
"(id = 4b4455437d209c2819000003)"
READ
"(id = 4b4455437d209c2819000004)"
- should have a type property that reflects the class
- should parent should return an instance of the child when type is explicitly specified
[]
- should discriminate types during reads (FAILED - 1)
1)
'Single Table Inheritance should discriminate types during reads' FAILED
expected: [#<Father @id="4b4455437d209c281900000a" @name=nil @job=nil @type=Father>, #<Father @id="4b4455437d209c281900000b" @name=nil @job=nil @type=Father>],
got: [] (using ==)
./spec/sti_spec.rb:45:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment