0
@@ -76,6 +76,20 @@ begin
0
+ include DataMapper::Resource
0
+ property :id, Fixnum, :serial => true
0
+ property :parent_id, Fixnum
0
+ property :name, String
0
+ repository(:sqlite3) do
0
+ one_to_many :children, :class_name => "Node", :child_key => [ :parent_id ]
0
+ many_to_one :parent, :class_name => "Node", :child_key => [ :parent_id ]
0
describe DataMapper::Associations do
0
describe "many to one associations" do
0
@@ -89,6 +103,7 @@ begin
0
Yard.auto_migrate!(:sqlite3)
0
@adapter.execute('INSERT INTO "yards" ("id", "name", "engine_id") values (?, ?, ?)', 1, 'yard1', 1)
0
+ @adapter.execute('INSERT INTO "yards" ("id", "name", "engine_id") values (?, ?, NULL)', 0, 'yard2')
0
it "should load without the parent"
0
@@ -138,6 +153,33 @@ begin
0
repository(:sqlite3).all(Engine, :id => 10).first.should_not be_nil
0
+ it 'should convert NULL parent ids into nils' do
0
+ y = repository(:sqlite3).all(Yard, :id => 0).first
0
+ y.engine.should be_nil
0
+ it 'should save nil parents as NULL ids' do
0
+ Broken. I'm guessing Resource#attributes= doesn't make any concessions for associations
0
+ (probably not what we want to do anyways), and more importantly, that many_to_one accessor=
0
+ methods don't properly handle nils.
0
+ repository(:sqlite3) do |r|
0
+ y1 = Yard.new(:id => 20, :name => "Yard20")
0
+ y2 = Yard.create!(:id => 30, :name => "Yard30", :engine => nil)
0
+ y1.engine_id.should be_nil
0
+ y2.engine_id.should be_nil
0
@adapter.execute('DROP TABLE "yards"')
0
@adapter.execute('DROP TABLE "engines"')
0
@@ -206,6 +248,22 @@ begin
0
repository(:sqlite3).first(Pie, :id => 10).should_not be_nil
0
+ it 'should save nil parents as NULL ids' do
0
+ repository(:sqlite3) do |r|
0
+ p1 = Pie.new(:id => 20, :name => "Pie20")
0
+ p2 = Pie.create!(:id => 30, :name => "Pie30", :sky => nil)
0
+ p1.sky_id.should be_nil
0
+ p2.sky_id.should be_nil
0
@adapter.execute('DROP TABLE "pies"')
0
@adapter.execute('DROP TABLE "skies"')
0
@@ -223,6 +281,7 @@ begin
0
Slice.auto_migrate!(:sqlite3)
0
+ @adapter.execute('INSERT INTO "slices" ("id", "name", "host_id") values (?, ?, NULL)', 0, 'slice0')
0
@adapter.execute('INSERT INTO "slices" ("id", "name", "host_id") values (?, ?, ?)', 1, 'slice1', 1)
0
@adapter.execute('INSERT INTO "slices" ("id", "name", "host_id") values (?, ?, ?)', 2, 'slice2', 1)
0
@@ -250,6 +309,10 @@ begin
0
h.slices.size.should == 2
0
h.slices.first.id.should == 1
0
h.slices.last.id.should == 2
0
+ s0 = repository(:sqlite3).all(Slice, :id => 0).first
0
+ s0.host_id.should be_nil
0
it "should add and save the associated instance" do
0
@@ -282,6 +345,64 @@ begin
0
+ describe "many-to-one and one-to-many associations combined" do
0
+ @adapter = repository(:sqlite3).adapter
0
+ Node.auto_migrate!(:sqlite3)
0
+ @adapter.execute('INSERT INTO "nodes" ("id", "name", "parent_id") values (?, ?, NULL)', 1, 'r1')
0
+ @adapter.execute('INSERT INTO "nodes" ("id", "name", "parent_id") values (?, ?, NULL)', 2, 'r2')
0
+ @adapter.execute('INSERT INTO "nodes" ("id", "name", "parent_id") values (?, ?, ?)', 3, 'r1c1', 1)
0
+ @adapter.execute('INSERT INTO "nodes" ("id", "name", "parent_id") values (?, ?, ?)', 4, 'r1c2', 1)
0
+ @adapter.execute('INSERT INTO "nodes" ("id", "name", "parent_id") values (?, ?, ?)', 5, 'r1c3', 1)
0
+ @adapter.execute('INSERT INTO "nodes" ("id", "name", "parent_id") values (?, ?, ?)', 6, 'r1c1c1', 3)
0
+ it "should properly set #parent" do
0
+ repository :sqlite3 do
0
+ r1.parent.should be_nil
0
+ n3.parent.should == r1
0
+ n6.parent.should == n3
0
+ it "should properly set #children" do
0
+ repository :sqlite3 do
0
+ off.include?(Node.get(3)).should be_true
0
+ off.include?(Node.get(4)).should be_true
0
+ off.include?(Node.get(5)).should be_true
0
+ it "should allow to create root nodes" do
0
+ repository :sqlite3 do
0
+ r = Node.create!(:name => "newroot")
0
+ r.parent.should be_nil
0
+ r.children.size.should == 0
0
+ it "should properly delete nodes" do
0
+ repository :sqlite3 do
0
+ r1.children.size.should == 3
0
+ r1.children.delete(Node.get(4))
0
+ Node.get(4).parent.should be_nil
0
+ r1.children.size.should == 2
Comments
No one has commented yet.