test/unit/test_content.rb in spontaneous-0.1.0.alpha1 vs test/unit/test_content.rb in spontaneous-0.2.0.alpha1

- old
+ new

@@ -1,18 +1,23 @@ # encoding: UTF-8 -require 'test_helper' +require File.expand_path('../../test_helper', __FILE__) class ContentTest < MiniTest::Spec context "Content:" do setup do - Spot::Schema.reset! - class C < Content; end + @site = setup_site + class Piece < Spontaneous::Piece; end + class C < Piece; end + C.box :things end + teardown do - ContentTest.send(:remove_const, :C) + teardown_site + ContentTest.send(:remove_const, :Piece) rescue nil + ContentTest.send(:remove_const, :C) rescue nil end context "Content instances" do should "evaluate instance code" do @instance = C.create({ :instance_code => "def monkey; 'magic'; end" @@ -35,50 +40,65 @@ @instance.pieces.should == [] end should "accept addition of child content" do e = C.new - @instance << e + @instance.things << e @instance.pieces.length.should == 1 + @instance.things.pieces.length.should == 1 @instance.pieces.first.should == e - @instance.pieces.first.container.should == @instance - e.content_path.should == "#{@instance.id}" - @instance.pieces.first.content_path.should == "#{@instance.id}" + @instance.pieces.first.container.should == @instance.things + @instance.pieces.first.owner.should == @instance + @instance.pieces.first.parent.should == @instance + e.visibility_path.should == "#{@instance.id}" + @instance.pieces.first.visibility_path.should == "#{@instance.id}" end should "accept addition of multiple children" do e = C.new f = C.new - @instance << e - @instance << f + @instance.things << e + @instance.things << f @instance.pieces.length.should == 2 + @instance.things.pieces.length.should == 2 @instance.pieces.first.should == e + @instance.things.pieces.first.should == e @instance.pieces.last.should == f - @instance.pieces.first.container.should == @instance - @instance.pieces.last.container.should == @instance - @instance.pieces.first.content_path.should == "#{@instance.id}" - @instance.pieces.last.content_path.should == "#{@instance.id}" + @instance.things.pieces.last.should == f + @instance.pieces.first.container.should == @instance.things + @instance.pieces.last.container.should == @instance.things + @instance.pieces.first.parent.should == @instance + @instance.pieces.last.parent.should == @instance + @instance.pieces.first.owner.should == @instance + @instance.pieces.last.owner.should == @instance + @instance.pieces.first.visibility_path.should == "#{@instance.id}" + @instance.pieces.last.visibility_path.should == "#{@instance.id}" end should "allow for a deep hierarchy" do e = C.new f = C.new - @instance << e - e << f + @instance.things << e + e.things << f @instance.pieces.length.should == 1 @instance.pieces.first.should == e - e.container.should == @instance - e.content_path.should == "#{@instance.id}" - f.container.id.should == e.id - f.content_path.should == "#{@instance.id}.#{e.id}" + e.owner.id.should == @instance.id + e.parent.id.should == @instance.id + e.container.should == @instance.things + e.visibility_path.should == "#{@instance.id}" + + f.owner.id.should == e.id + f.parent.id.should == e.id + f.container.should == e.things + f.visibility_path.should == "#{@instance.id}.#{e.id}" end should "persist hierarchy" do e = C.new f = C.new - e << f - @instance << e + e.things << f + @instance.things << e @instance.save e.save f.save i = C[@instance.id] @@ -86,22 +106,26 @@ f = C[f.id] i.pieces.length.should == 1 i.pieces.first.should == e - e.container.should == i - f.container.should == e + e.container.should == i.things + e.owner.should == i + e.parent.should == i + f.container.should == e.things + f.parent.should == e + f.owner.should == e e.entry.should == i.pieces.first f.entry.should == e.pieces.first e.pieces.first.should == f end should "have a list of child nodes" do e = C.new f = C.new - e << f - @instance << e + e.things << f + @instance.things << e @instance.save e.save f.save i = C[@instance.id] @@ -109,21 +133,61 @@ f = C[f.id] i.pieces.should == [e] e.pieces.should == [f] end + should "allow for testing of position" do + e = C.new + f = C.new + g = C.new + @instance.things << e + @instance.things << f + @instance.things << g + e.first?.should be_true + f.first?.should be_false + g.first?.should be_false + e.last?.should be_false + f.last?.should be_false + g.last?.should be_true + end + + should "know their next neighbour" do + e = C.new + f = C.new + g = C.new + @instance.things << e + @instance.things << f + @instance.things << g + e.next.should == f + f.next.should == g + g.next.should be_nil + end + + should "know their previous neighbour" do + e = C.new + f = C.new + g = C.new + @instance.things << e + @instance.things << f + @instance.things << g + e.previous.should be_nil + f.previous.should == e + g.previous.should == f + g.prev.should == f + end + should "record the depth of the nodes" do a = C.new b = C.new c = C.new a.depth.should == 0 b.depth.should == 0 c.depth.should == 0 - a << b - b << c + a.things << b + b.things << c b.depth.should == 1 c.depth.should == 2 end @@ -134,13 +198,13 @@ C.delete @a = C.new(:label => 'a') @b = C.new(:label => 'b') @c = C.new(:label => 'c') @d = C.new(:label => 'd') - @a << @b - @a << @d - @b << @c + @a.things << @b + @a.things << @d + @b.things << @c @a.save @b.save @c.save @d.save @a = C[@a.id] @@ -162,23 +226,78 @@ @a.pieces.length.should == 1 @a.pieces.first.should == @d.reload C.all.map { |c| c.id }.sort.should == [@a, @d].map { |c| c.id }.sort end - ## doesn't work due to should "work through pieces" do - @a.pieces.first.destroy + @a.things.length.should == 2 + @a.things.first.destroy C.count.should == 2 - @a.pieces.length.should == 1 + @a.things.length.should == 1 end end + context "Moving" do + setup do + C.delete + @r = C.new(:label => 'r') + @a = C.new(:label => 'a') + @b = C.new(:label => 'b') + @c = C.new(:label => 'c') + @d = C.new(:label => 'd') + @r.things << @a + @r.things << @c + @a.things << @b + @c.things << @d + [@r, @a, @b, @c, @d].each { |c| c.save; c.reload } + end + teardown do + C.delete + end + + should "default to adding at the end" do + @b.parent.should == @a + @r.things.adopt(@b) + @b.reload + @r.reload + @b.parent.should == @r + @b.container.should == @r.things + @b.depth.should == 1 + @a.reload + @a.things.count.should == 0 + @r.reload + @r.things.last.should == @b + end + + should "allow for adding in any position" do + @b.parent.should == @a + @r.things.adopt(@b, 1) + @b.reload + @r.reload + @b.parent.should == @r + @b.container.should == @r.things + @b.depth.should == 1 + @a.reload + @a.things.count.should == 0 + @r.reload + @r.things[1].should == @b + @r.things.adopt(@d, 0) + @d.reload + @r.reload + @r.things[0].should == @d + end + + should "re-set the visibility path" do + @r.things.adopt(@b) + @b.reload + @b.visibility_path.should == @r.id.to_s + end + end + + context "identity map" do setup do - instance = Spontaneous::Site.instantiate(Spontaneous.root, :test, :back) - Spontaneous.instance = instance - Spontaneous.instance.database = DB Content.delete Content.delete_all_revisions! class ::IdentitySubclass < C; end @c1 = C.create