From 77f29deb2916555697fb940a34480766b2a8a713 Mon Sep 17 00:00:00 2001 From: Toshi MARUYAMA Date: Mon, 20 Jan 2014 02:19:08 +0000 Subject: svn propset svn:eol-style LF lib/plugins/awesome_nested_set/spec/* git-svn-id: http://svn.redmine.org/redmine/trunk@12676 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- .../spec/awesome_nested_set/helper_spec.rb | 134 +- .../spec/awesome_nested_set_spec.rb | 1682 ++++++++++---------- .../awesome_nested_set/spec/db/database.yml | 34 +- lib/plugins/awesome_nested_set/spec/db/schema.rb | 90 +- .../awesome_nested_set/spec/fixtures/brokens.yml | 4 +- .../spec/fixtures/categories.yml | 68 +- .../spec/fixtures/departments.yml | 4 +- .../awesome_nested_set/spec/fixtures/notes.yml | 76 +- .../awesome_nested_set/spec/fixtures/things.yml | 54 +- lib/plugins/awesome_nested_set/spec/spec_helper.rb | 64 +- .../awesome_nested_set/spec/support/models.rb | 142 +- 11 files changed, 1176 insertions(+), 1176 deletions(-) (limited to 'lib') diff --git a/lib/plugins/awesome_nested_set/spec/awesome_nested_set/helper_spec.rb b/lib/plugins/awesome_nested_set/spec/awesome_nested_set/helper_spec.rb index cc32a0d0a..1cca71ba8 100644 --- a/lib/plugins/awesome_nested_set/spec/awesome_nested_set/helper_spec.rb +++ b/lib/plugins/awesome_nested_set/spec/awesome_nested_set/helper_spec.rb @@ -1,67 +1,67 @@ -require 'spec_helper' - -describe "Helper" do - include CollectiveIdea::Acts::NestedSet::Helper - - before(:all) do - self.class.fixtures :categories - end - - describe "nested_set_options" do - it "test_nested_set_options" do - expected = [ - [" Top Level", 1], - ["- Child 1", 2], - ['- Child 2', 3], - ['-- Child 2.1', 4], - ['- Child 3', 5], - [" Top Level 2", 6] - ] - actual = nested_set_options(Category) do |c| - "#{'-' * c.level} #{c.name}" - end - actual.should == expected - end - - it "test_nested_set_options_with_mover" do - expected = [ - [" Top Level", 1], - ["- Child 1", 2], - ['- Child 3', 5], - [" Top Level 2", 6] - ] - actual = nested_set_options(Category, categories(:child_2)) do |c| - "#{'-' * c.level} #{c.name}" - end - actual.should == expected - end - - it "test_nested_set_options_with_array_as_argument_without_mover" do - expected = [ - [" Top Level", 1], - ["- Child 1", 2], - ['- Child 2', 3], - ['-- Child 2.1', 4], - ['- Child 3', 5], - [" Top Level 2", 6] - ] - actual = nested_set_options(Category.all) do |c| - "#{'-' * c.level} #{c.name}" - end - actual.should == expected - end - - it "test_nested_set_options_with_array_as_argument_with_mover" do - expected = [ - [" Top Level", 1], - ["- Child 1", 2], - ['- Child 3', 5], - [" Top Level 2", 6] - ] - actual = nested_set_options(Category.all, categories(:child_2)) do |c| - "#{'-' * c.level} #{c.name}" - end - actual.should == expected - end - end -end +require 'spec_helper' + +describe "Helper" do + include CollectiveIdea::Acts::NestedSet::Helper + + before(:all) do + self.class.fixtures :categories + end + + describe "nested_set_options" do + it "test_nested_set_options" do + expected = [ + [" Top Level", 1], + ["- Child 1", 2], + ['- Child 2', 3], + ['-- Child 2.1', 4], + ['- Child 3', 5], + [" Top Level 2", 6] + ] + actual = nested_set_options(Category) do |c| + "#{'-' * c.level} #{c.name}" + end + actual.should == expected + end + + it "test_nested_set_options_with_mover" do + expected = [ + [" Top Level", 1], + ["- Child 1", 2], + ['- Child 3', 5], + [" Top Level 2", 6] + ] + actual = nested_set_options(Category, categories(:child_2)) do |c| + "#{'-' * c.level} #{c.name}" + end + actual.should == expected + end + + it "test_nested_set_options_with_array_as_argument_without_mover" do + expected = [ + [" Top Level", 1], + ["- Child 1", 2], + ['- Child 2', 3], + ['-- Child 2.1', 4], + ['- Child 3', 5], + [" Top Level 2", 6] + ] + actual = nested_set_options(Category.all) do |c| + "#{'-' * c.level} #{c.name}" + end + actual.should == expected + end + + it "test_nested_set_options_with_array_as_argument_with_mover" do + expected = [ + [" Top Level", 1], + ["- Child 1", 2], + ['- Child 3', 5], + [" Top Level 2", 6] + ] + actual = nested_set_options(Category.all, categories(:child_2)) do |c| + "#{'-' * c.level} #{c.name}" + end + actual.should == expected + end + end +end diff --git a/lib/plugins/awesome_nested_set/spec/awesome_nested_set_spec.rb b/lib/plugins/awesome_nested_set/spec/awesome_nested_set_spec.rb index c1c0f7401..692b123a5 100644 --- a/lib/plugins/awesome_nested_set/spec/awesome_nested_set_spec.rb +++ b/lib/plugins/awesome_nested_set/spec/awesome_nested_set_spec.rb @@ -1,841 +1,841 @@ -require 'spec_helper' - -describe "AwesomeNestedSet" do - before(:all) do - self.class.fixtures :categories, :departments, :notes, :things, :brokens - end - - describe "defaults" do - it "should have left_column_default" do - Default.acts_as_nested_set_options[:left_column].should == 'lft' - end - - it "should have right_column_default" do - Default.acts_as_nested_set_options[:right_column].should == 'rgt' - end - - it "should have parent_column_default" do - Default.acts_as_nested_set_options[:parent_column].should == 'parent_id' - end - - it "should have scope_default" do - Default.acts_as_nested_set_options[:scope].should be_nil - end - - it "should have left_column_name" do - Default.left_column_name.should == 'lft' - Default.new.left_column_name.should == 'lft' - RenamedColumns.left_column_name.should == 'red' - RenamedColumns.new.left_column_name.should == 'red' - end - - it "should have right_column_name" do - Default.right_column_name.should == 'rgt' - Default.new.right_column_name.should == 'rgt' - RenamedColumns.right_column_name.should == 'black' - RenamedColumns.new.right_column_name.should == 'black' - end - - it "should have parent_column_name" do - Default.parent_column_name.should == 'parent_id' - Default.new.parent_column_name.should == 'parent_id' - RenamedColumns.parent_column_name.should == 'mother_id' - RenamedColumns.new.parent_column_name.should == 'mother_id' - end - end - - it "creation_with_altered_column_names" do - lambda { - RenamedColumns.create!() - }.should_not raise_exception - end - - it "creation when existing record has nil left column" do - assert_nothing_raised do - Broken.create! - end - end - - it "quoted_left_column_name" do - quoted = Default.connection.quote_column_name('lft') - Default.quoted_left_column_name.should == quoted - Default.new.quoted_left_column_name.should == quoted - end - - it "quoted_right_column_name" do - quoted = Default.connection.quote_column_name('rgt') - Default.quoted_right_column_name.should == quoted - Default.new.quoted_right_column_name.should == quoted - end - - it "left_column_protected_from_assignment" do - lambda { - Category.new.lft = 1 - }.should raise_exception(ActiveRecord::ActiveRecordError) - end - - it "right_column_protected_from_assignment" do - lambda { - Category.new.rgt = 1 - }.should raise_exception(ActiveRecord::ActiveRecordError) - end - - it "scoped_appends_id" do - ScopedCategory.acts_as_nested_set_options[:scope].should == :organization_id - end - - it "roots_class_method" do - Category.roots.should == Category.find_all_by_parent_id(nil) - end - - it "root_class_method" do - Category.root.should == categories(:top_level) - end - - it "root" do - categories(:child_3).root.should == categories(:top_level) - end - - it "root?" do - categories(:top_level).root?.should be_true - categories(:top_level_2).root?.should be_true - end - - it "leaves_class_method" do - Category.find(:all, :conditions => "#{Category.right_column_name} - #{Category.left_column_name} = 1").should == Category.leaves - Category.leaves.count.should == 4 - Category.leaves.should include(categories(:child_1)) - Category.leaves.should include(categories(:child_2_1)) - Category.leaves.should include(categories(:child_3)) - Category.leaves.should include(categories(:top_level_2)) - end - - it "leaf" do - categories(:child_1).leaf?.should be_true - categories(:child_2_1).leaf?.should be_true - categories(:child_3).leaf?.should be_true - categories(:top_level_2).leaf?.should be_true - - categories(:top_level).leaf?.should be_false - categories(:child_2).leaf?.should be_false - Category.new.leaf?.should be_false - end - - - it "parent" do - categories(:child_2_1).parent.should == categories(:child_2) - end - - it "self_and_ancestors" do - child = categories(:child_2_1) - self_and_ancestors = [categories(:top_level), categories(:child_2), child] - self_and_ancestors.should == child.self_and_ancestors - end - - it "ancestors" do - child = categories(:child_2_1) - ancestors = [categories(:top_level), categories(:child_2)] - ancestors.should == child.ancestors - end - - it "self_and_siblings" do - child = categories(:child_2) - self_and_siblings = [categories(:child_1), child, categories(:child_3)] - self_and_siblings.should == child.self_and_siblings - lambda do - tops = [categories(:top_level), categories(:top_level_2)] - assert_equal tops, categories(:top_level).self_and_siblings - end.should_not raise_exception - end - - it "siblings" do - child = categories(:child_2) - siblings = [categories(:child_1), categories(:child_3)] - siblings.should == child.siblings - end - - it "leaves" do - leaves = [categories(:child_1), categories(:child_2_1), categories(:child_3)] - categories(:top_level).leaves.should == leaves - end - - it "level" do - categories(:top_level).level.should == 0 - categories(:child_1).level.should == 1 - categories(:child_2_1).level.should == 2 - end - - it "has_children?" do - categories(:child_2_1).children.empty?.should be_true - categories(:child_2).children.empty?.should be_false - categories(:top_level).children.empty?.should be_false - end - - it "self_and_descendents" do - parent = categories(:top_level) - self_and_descendants = [parent, categories(:child_1), categories(:child_2), - categories(:child_2_1), categories(:child_3)] - self_and_descendants.should == parent.self_and_descendants - self_and_descendants.count.should == parent.self_and_descendants.count - end - - it "descendents" do - lawyers = Category.create!(:name => "lawyers") - us = Category.create!(:name => "United States") - us.move_to_child_of(lawyers) - patent = Category.create!(:name => "Patent Law") - patent.move_to_child_of(us) - lawyers.reload - - lawyers.children.size.should == 1 - us.children.size.should == 1 - lawyers.descendants.size.should == 2 - end - - it "self_and_descendents" do - parent = categories(:top_level) - descendants = [categories(:child_1), categories(:child_2), - categories(:child_2_1), categories(:child_3)] - descendants.should == parent.descendants - end - - it "children" do - category = categories(:top_level) - category.children.each {|c| category.id.should == c.parent_id } - end - - it "order_of_children" do - categories(:child_2).move_left - categories(:child_2).should == categories(:top_level).children[0] - categories(:child_1).should == categories(:top_level).children[1] - categories(:child_3).should == categories(:top_level).children[2] - end - - it "is_or_is_ancestor_of?" do - categories(:top_level).is_or_is_ancestor_of?(categories(:child_1)).should be_true - categories(:top_level).is_or_is_ancestor_of?(categories(:child_2_1)).should be_true - categories(:child_2).is_or_is_ancestor_of?(categories(:child_2_1)).should be_true - categories(:child_2_1).is_or_is_ancestor_of?(categories(:child_2)).should be_false - categories(:child_1).is_or_is_ancestor_of?(categories(:child_2)).should be_false - categories(:child_1).is_or_is_ancestor_of?(categories(:child_1)).should be_true - end - - it "is_ancestor_of?" do - categories(:top_level).is_ancestor_of?(categories(:child_1)).should be_true - categories(:top_level).is_ancestor_of?(categories(:child_2_1)).should be_true - categories(:child_2).is_ancestor_of?(categories(:child_2_1)).should be_true - categories(:child_2_1).is_ancestor_of?(categories(:child_2)).should be_false - categories(:child_1).is_ancestor_of?(categories(:child_2)).should be_false - categories(:child_1).is_ancestor_of?(categories(:child_1)).should be_false - end - - it "is_or_is_ancestor_of_with_scope" do - root = ScopedCategory.root - child = root.children.first - root.is_or_is_ancestor_of?(child).should be_true - child.update_attribute :organization_id, 'different' - root.is_or_is_ancestor_of?(child).should be_false - end - - it "is_or_is_descendant_of?" do - categories(:child_1).is_or_is_descendant_of?(categories(:top_level)).should be_true - categories(:child_2_1).is_or_is_descendant_of?(categories(:top_level)).should be_true - categories(:child_2_1).is_or_is_descendant_of?(categories(:child_2)).should be_true - categories(:child_2).is_or_is_descendant_of?(categories(:child_2_1)).should be_false - categories(:child_2).is_or_is_descendant_of?(categories(:child_1)).should be_false - categories(:child_1).is_or_is_descendant_of?(categories(:child_1)).should be_true - end - - it "is_descendant_of?" do - categories(:child_1).is_descendant_of?(categories(:top_level)).should be_true - categories(:child_2_1).is_descendant_of?(categories(:top_level)).should be_true - categories(:child_2_1).is_descendant_of?(categories(:child_2)).should be_true - categories(:child_2).is_descendant_of?(categories(:child_2_1)).should be_false - categories(:child_2).is_descendant_of?(categories(:child_1)).should be_false - categories(:child_1).is_descendant_of?(categories(:child_1)).should be_false - end - - it "is_or_is_descendant_of_with_scope" do - root = ScopedCategory.root - child = root.children.first - child.is_or_is_descendant_of?(root).should be_true - child.update_attribute :organization_id, 'different' - child.is_or_is_descendant_of?(root).should be_false - end - - it "same_scope?" do - root = ScopedCategory.root - child = root.children.first - child.same_scope?(root).should be_true - child.update_attribute :organization_id, 'different' - child.same_scope?(root).should be_false - end - - it "left_sibling" do - categories(:child_1).should == categories(:child_2).left_sibling - categories(:child_2).should == categories(:child_3).left_sibling - end - - it "left_sibling_of_root" do - categories(:top_level).left_sibling.should be_nil - end - - it "left_sibling_without_siblings" do - categories(:child_2_1).left_sibling.should be_nil - end - - it "left_sibling_of_leftmost_node" do - categories(:child_1).left_sibling.should be_nil - end - - it "right_sibling" do - categories(:child_3).should == categories(:child_2).right_sibling - categories(:child_2).should == categories(:child_1).right_sibling - end - - it "right_sibling_of_root" do - categories(:top_level_2).should == categories(:top_level).right_sibling - categories(:top_level_2).right_sibling.should be_nil - end - - it "right_sibling_without_siblings" do - categories(:child_2_1).right_sibling.should be_nil - end - - it "right_sibling_of_rightmost_node" do - categories(:child_3).right_sibling.should be_nil - end - - it "move_left" do - categories(:child_2).move_left - categories(:child_2).left_sibling.should be_nil - categories(:child_1).should == categories(:child_2).right_sibling - Category.valid?.should be_true - end - - it "move_right" do - categories(:child_2).move_right - categories(:child_2).right_sibling.should be_nil - categories(:child_3).should == categories(:child_2).left_sibling - Category.valid?.should be_true - end - - it "move_to_left_of" do - categories(:child_3).move_to_left_of(categories(:child_1)) - categories(:child_3).left_sibling.should be_nil - categories(:child_1).should == categories(:child_3).right_sibling - Category.valid?.should be_true - end - - it "move_to_right_of" do - categories(:child_1).move_to_right_of(categories(:child_3)) - categories(:child_1).right_sibling.should be_nil - categories(:child_3).should == categories(:child_1).left_sibling - Category.valid?.should be_true - end - - it "move_to_root" do - categories(:child_2).move_to_root - categories(:child_2).parent.should be_nil - categories(:child_2).level.should == 0 - categories(:child_2_1).level.should == 1 - categories(:child_2).left.should == 1 - categories(:child_2).right.should == 4 - Category.valid?.should be_true - end - - it "move_to_child_of" do - categories(:child_1).move_to_child_of(categories(:child_3)) - categories(:child_3).id.should == categories(:child_1).parent_id - Category.valid?.should be_true - end - - it "move_to_child_of_appends_to_end" do - child = Category.create! :name => 'New Child' - child.move_to_child_of categories(:top_level) - child.should == categories(:top_level).children.last - end - - it "subtree_move_to_child_of" do - categories(:child_2).left.should == 4 - categories(:child_2).right.should == 7 - - categories(:child_1).left.should == 2 - categories(:child_1).right.should == 3 - - categories(:child_2).move_to_child_of(categories(:child_1)) - Category.valid?.should be_true - categories(:child_1).id.should == categories(:child_2).parent_id - - categories(:child_2).left.should == 3 - categories(:child_2).right.should == 6 - categories(:child_1).left.should == 2 - categories(:child_1).right.should == 7 - end - - it "slightly_difficult_move_to_child_of" do - categories(:top_level_2).left.should == 11 - categories(:top_level_2).right.should == 12 - - # create a new top-level node and move single-node top-level tree inside it. - new_top = Category.create(:name => 'New Top') - new_top.left.should == 13 - new_top.right.should == 14 - - categories(:top_level_2).move_to_child_of(new_top) - - Category.valid?.should be_true - new_top.id.should == categories(:top_level_2).parent_id - - categories(:top_level_2).left.should == 12 - categories(:top_level_2).right.should == 13 - new_top.left.should == 11 - new_top.right.should == 14 - end - - it "difficult_move_to_child_of" do - categories(:top_level).left.should == 1 - categories(:top_level).right.should == 10 - categories(:child_2_1).left.should == 5 - categories(:child_2_1).right.should == 6 - - # create a new top-level node and move an entire top-level tree inside it. - new_top = Category.create(:name => 'New Top') - categories(:top_level).move_to_child_of(new_top) - categories(:child_2_1).reload - Category.valid?.should be_true - new_top.id.should == categories(:top_level).parent_id - - categories(:top_level).left.should == 4 - categories(:top_level).right.should == 13 - categories(:child_2_1).left.should == 8 - categories(:child_2_1).right.should == 9 - end - - #rebuild swaps the position of the 2 children when added using move_to_child twice onto same parent - it "move_to_child_more_than_once_per_parent_rebuild" do - root1 = Category.create(:name => 'Root1') - root2 = Category.create(:name => 'Root2') - root3 = Category.create(:name => 'Root3') - - root2.move_to_child_of root1 - root3.move_to_child_of root1 - - output = Category.roots.last.to_text - Category.update_all('lft = null, rgt = null') - Category.rebuild! - - Category.roots.last.to_text.should == output - end - - # doing move_to_child twice onto same parent from the furthest right first - it "move_to_child_more_than_once_per_parent_outside_in" do - node1 = Category.create(:name => 'Node-1') - node2 = Category.create(:name => 'Node-2') - node3 = Category.create(:name => 'Node-3') - - node2.move_to_child_of node1 - node3.move_to_child_of node1 - - output = Category.roots.last.to_text - Category.update_all('lft = null, rgt = null') - Category.rebuild! - - Category.roots.last.to_text.should == output - end - - it "should be able to rebuild without validating each record" do - root1 = Category.create(:name => 'Root1') - root2 = Category.create(:name => 'Root2') - root3 = Category.create(:name => 'Root3') - - root2.move_to_child_of root1 - root3.move_to_child_of root1 - - root2.name = nil - root2.save!(:validate => false) - - output = Category.roots.last.to_text - Category.update_all('lft = null, rgt = null') - Category.rebuild!(false) - - Category.roots.last.to_text.should == output - end - - it "valid_with_null_lefts" do - Category.valid?.should be_true - Category.update_all('lft = null') - Category.valid?.should be_false - end - - it "valid_with_null_rights" do - Category.valid?.should be_true - Category.update_all('rgt = null') - Category.valid?.should be_false - end - - it "valid_with_missing_intermediate_node" do - # Even though child_2_1 will still exist, it is a sign of a sloppy delete, not an invalid tree. - Category.valid?.should be_true - Category.delete(categories(:child_2).id) - Category.valid?.should be_true - end - - it "valid_with_overlapping_and_rights" do - Category.valid?.should be_true - categories(:top_level_2)['lft'] = 0 - categories(:top_level_2).save - Category.valid?.should be_false - end - - it "rebuild" do - Category.valid?.should be_true - before_text = Category.root.to_text - Category.update_all('lft = null, rgt = null') - Category.rebuild! - Category.valid?.should be_true - before_text.should == Category.root.to_text - end - - it "move_possible_for_sibling" do - categories(:child_2).move_possible?(categories(:child_1)).should be_true - end - - it "move_not_possible_to_self" do - categories(:top_level).move_possible?(categories(:top_level)).should be_false - end - - it "move_not_possible_to_parent" do - categories(:top_level).descendants.each do |descendant| - categories(:top_level).move_possible?(descendant).should be_false - descendant.move_possible?(categories(:top_level)).should be_true - end - end - - it "is_or_is_ancestor_of?" do - [:child_1, :child_2, :child_2_1, :child_3].each do |c| - categories(:top_level).is_or_is_ancestor_of?(categories(c)).should be_true - end - categories(:top_level).is_or_is_ancestor_of?(categories(:top_level_2)).should be_false - end - - it "left_and_rights_valid_with_blank_left" do - Category.left_and_rights_valid?.should be_true - categories(:child_2)[:lft] = nil - categories(:child_2).save(:validate => false) - Category.left_and_rights_valid?.should be_false - end - - it "left_and_rights_valid_with_blank_right" do - Category.left_and_rights_valid?.should be_true - categories(:child_2)[:rgt] = nil - categories(:child_2).save(:validate => false) - Category.left_and_rights_valid?.should be_false - end - - it "left_and_rights_valid_with_equal" do - Category.left_and_rights_valid?.should be_true - categories(:top_level_2)[:lft] = categories(:top_level_2)[:rgt] - categories(:top_level_2).save(:validate => false) - Category.left_and_rights_valid?.should be_false - end - - it "left_and_rights_valid_with_left_equal_to_parent" do - Category.left_and_rights_valid?.should be_true - categories(:child_2)[:lft] = categories(:top_level)[:lft] - categories(:child_2).save(:validate => false) - Category.left_and_rights_valid?.should be_false - end - - it "left_and_rights_valid_with_right_equal_to_parent" do - Category.left_and_rights_valid?.should be_true - categories(:child_2)[:rgt] = categories(:top_level)[:rgt] - categories(:child_2).save(:validate => false) - Category.left_and_rights_valid?.should be_false - end - - it "moving_dirty_objects_doesnt_invalidate_tree" do - r1 = Category.create :name => "Test 1" - r2 = Category.create :name => "Test 2" - r3 = Category.create :name => "Test 3" - r4 = Category.create :name => "Test 4" - nodes = [r1, r2, r3, r4] - - r2.move_to_child_of(r1) - Category.valid?.should be_true - - r3.move_to_child_of(r1) - Category.valid?.should be_true - - r4.move_to_child_of(r2) - Category.valid?.should be_true - end - - it "multi_scoped_no_duplicates_for_columns?" do - lambda { - Note.no_duplicates_for_columns? - }.should_not raise_exception - end - - it "multi_scoped_all_roots_valid?" do - lambda { - Note.all_roots_valid? - }.should_not raise_exception - end - - it "multi_scoped" do - note1 = Note.create!(:body => "A", :notable_id => 2, :notable_type => 'Category') - note2 = Note.create!(:body => "B", :notable_id => 2, :notable_type => 'Category') - note3 = Note.create!(:body => "C", :notable_id => 2, :notable_type => 'Default') - - [note1, note2].should == note1.self_and_siblings - [note3].should == note3.self_and_siblings - end - - it "multi_scoped_rebuild" do - root = Note.create!(:body => "A", :notable_id => 3, :notable_type => 'Category') - child1 = Note.create!(:body => "B", :notable_id => 3, :notable_type => 'Category') - child2 = Note.create!(:body => "C", :notable_id => 3, :notable_type => 'Category') - - child1.move_to_child_of root - child2.move_to_child_of root - - Note.update_all('lft = null, rgt = null') - Note.rebuild! - - Note.roots.find_by_body('A').should == root - [child1, child2].should == Note.roots.find_by_body('A').children - end - - it "same_scope_with_multi_scopes" do - lambda { - notes(:scope1).same_scope?(notes(:child_1)) - }.should_not raise_exception - notes(:scope1).same_scope?(notes(:child_1)).should be_true - notes(:child_1).same_scope?(notes(:scope1)).should be_true - notes(:scope1).same_scope?(notes(:scope2)).should be_false - end - - it "quoting_of_multi_scope_column_names" do - ["\"notable_id\"", "\"notable_type\""].should == Note.quoted_scope_column_names - end - - it "equal_in_same_scope" do - notes(:scope1).should == notes(:scope1) - notes(:scope1).should_not == notes(:child_1) - end - - it "equal_in_different_scopes" do - notes(:scope1).should_not == notes(:scope2) - end - - it "delete_does_not_invalidate" do - Category.acts_as_nested_set_options[:dependent] = :delete - categories(:child_2).destroy - Category.valid?.should be_true - end - - it "destroy_does_not_invalidate" do - Category.acts_as_nested_set_options[:dependent] = :destroy - categories(:child_2).destroy - Category.valid?.should be_true - end - - it "destroy_multiple_times_does_not_invalidate" do - Category.acts_as_nested_set_options[:dependent] = :destroy - categories(:child_2).destroy - categories(:child_2).destroy - Category.valid?.should be_true - end - - it "assigning_parent_id_on_create" do - category = Category.create!(:name => "Child", :parent_id => categories(:child_2).id) - categories(:child_2).should == category.parent - categories(:child_2).id.should == category.parent_id - category.left.should_not be_nil - category.right.should_not be_nil - Category.valid?.should be_true - end - - it "assigning_parent_on_create" do - category = Category.create!(:name => "Child", :parent => categories(:child_2)) - categories(:child_2).should == category.parent - categories(:child_2).id.should == category.parent_id - category.left.should_not be_nil - category.right.should_not be_nil - Category.valid?.should be_true - end - - it "assigning_parent_id_to_nil_on_create" do - category = Category.create!(:name => "New Root", :parent_id => nil) - category.parent.should be_nil - category.parent_id.should be_nil - category.left.should_not be_nil - category.right.should_not be_nil - Category.valid?.should be_true - end - - it "assigning_parent_id_on_update" do - category = categories(:child_2_1) - category.parent_id = categories(:child_3).id - category.save - category.reload - categories(:child_3).reload - categories(:child_3).should == category.parent - categories(:child_3).id.should == category.parent_id - Category.valid?.should be_true - end - - it "assigning_parent_on_update" do - category = categories(:child_2_1) - category.parent = categories(:child_3) - category.save - category.reload - categories(:child_3).reload - categories(:child_3).should == category.parent - categories(:child_3).id.should == category.parent_id - Category.valid?.should be_true - end - - it "assigning_parent_id_to_nil_on_update" do - category = categories(:child_2_1) - category.parent_id = nil - category.save - category.parent.should be_nil - category.parent_id.should be_nil - Category.valid?.should be_true - end - - it "creating_child_from_parent" do - category = categories(:child_2).children.create!(:name => "Child") - categories(:child_2).should == category.parent - categories(:child_2).id.should == category.parent_id - category.left.should_not be_nil - category.right.should_not be_nil - Category.valid?.should be_true - end - - def check_structure(entries, structure) - structure = structure.dup - Category.each_with_level(entries) do |category, level| - expected_level, expected_name = structure.shift - expected_name.should == category.name - expected_level.should == level - end - end - - it "each_with_level" do - levels = [ - [0, "Top Level"], - [1, "Child 1"], - [1, "Child 2"], - [2, "Child 2.1"], - [1, "Child 3" ]] - - check_structure(Category.root.self_and_descendants, levels) - - # test some deeper structures - category = Category.find_by_name("Child 1") - c1 = Category.new(:name => "Child 1.1") - c2 = Category.new(:name => "Child 1.1.1") - c3 = Category.new(:name => "Child 1.1.1.1") - c4 = Category.new(:name => "Child 1.2") - [c1, c2, c3, c4].each(&:save!) - - c1.move_to_child_of(category) - c2.move_to_child_of(c1) - c3.move_to_child_of(c2) - c4.move_to_child_of(category) - - levels = [ - [0, "Top Level"], - [1, "Child 1"], - [2, "Child 1.1"], - [3, "Child 1.1.1"], - [4, "Child 1.1.1.1"], - [2, "Child 1.2"], - [1, "Child 2"], - [2, "Child 2.1"], - [1, "Child 3" ]] - - check_structure(Category.root.self_and_descendants, levels) - end - - it "should not error on a model with attr_accessible" do - model = Class.new(ActiveRecord::Base) - model.table_name = 'categories' - model.attr_accessible :name - lambda { - model.acts_as_nested_set - model.new(:name => 'foo') - }.should_not raise_exception - end - - describe "before_move_callback" do - it "should fire the callback" do - categories(:child_2).should_receive(:custom_before_move) - categories(:child_2).move_to_root - end - - it "should stop move when callback returns false" do - Category.test_allows_move = false - categories(:child_3).move_to_root.should be_false - categories(:child_3).root?.should be_false - end - - it "should not halt save actions" do - Category.test_allows_move = false - categories(:child_3).parent_id = nil - categories(:child_3).save.should be_true - end - end - - describe "counter_cache" do - - it "should allow use of a counter cache for children" do - note1 = things(:parent1) - note1.children.count.should == 2 - end - - it "should increment the counter cache on create" do - note1 = things(:parent1) - note1.children.count.should == 2 - note1[:children_count].should == 2 - note1.children.create :body => 'Child 3' - note1.children.count.should == 3 - note1.reload - note1[:children_count].should == 3 - end - - it "should decrement the counter cache on destroy" do - note1 = things(:parent1) - note1.children.count.should == 2 - note1[:children_count].should == 2 - note1.children.last.destroy - note1.children.count.should == 1 - note1.reload - note1[:children_count].should == 1 - end - end - - describe "association callbacks on children" do - it "should call the appropriate callbacks on the children :has_many association " do - root = DefaultWithCallbacks.create - root.should_not be_new_record - - child = root.children.build - - root.before_add.should == child - root.after_add.should == child - - root.before_remove.should_not == child - root.after_remove.should_not == child - - child.save.should be_true - root.children.delete(child).should be_true - - root.before_remove.should == child - root.after_remove.should == child - end - end -end +require 'spec_helper' + +describe "AwesomeNestedSet" do + before(:all) do + self.class.fixtures :categories, :departments, :notes, :things, :brokens + end + + describe "defaults" do + it "should have left_column_default" do + Default.acts_as_nested_set_options[:left_column].should == 'lft' + end + + it "should have right_column_default" do + Default.acts_as_nested_set_options[:right_column].should == 'rgt' + end + + it "should have parent_column_default" do + Default.acts_as_nested_set_options[:parent_column].should == 'parent_id' + end + + it "should have scope_default" do + Default.acts_as_nested_set_options[:scope].should be_nil + end + + it "should have left_column_name" do + Default.left_column_name.should == 'lft' + Default.new.left_column_name.should == 'lft' + RenamedColumns.left_column_name.should == 'red' + RenamedColumns.new.left_column_name.should == 'red' + end + + it "should have right_column_name" do + Default.right_column_name.should == 'rgt' + Default.new.right_column_name.should == 'rgt' + RenamedColumns.right_column_name.should == 'black' + RenamedColumns.new.right_column_name.should == 'black' + end + + it "should have parent_column_name" do + Default.parent_column_name.should == 'parent_id' + Default.new.parent_column_name.should == 'parent_id' + RenamedColumns.parent_column_name.should == 'mother_id' + RenamedColumns.new.parent_column_name.should == 'mother_id' + end + end + + it "creation_with_altered_column_names" do + lambda { + RenamedColumns.create!() + }.should_not raise_exception + end + + it "creation when existing record has nil left column" do + assert_nothing_raised do + Broken.create! + end + end + + it "quoted_left_column_name" do + quoted = Default.connection.quote_column_name('lft') + Default.quoted_left_column_name.should == quoted + Default.new.quoted_left_column_name.should == quoted + end + + it "quoted_right_column_name" do + quoted = Default.connection.quote_column_name('rgt') + Default.quoted_right_column_name.should == quoted + Default.new.quoted_right_column_name.should == quoted + end + + it "left_column_protected_from_assignment" do + lambda { + Category.new.lft = 1 + }.should raise_exception(ActiveRecord::ActiveRecordError) + end + + it "right_column_protected_from_assignment" do + lambda { + Category.new.rgt = 1 + }.should raise_exception(ActiveRecord::ActiveRecordError) + end + + it "scoped_appends_id" do + ScopedCategory.acts_as_nested_set_options[:scope].should == :organization_id + end + + it "roots_class_method" do + Category.roots.should == Category.find_all_by_parent_id(nil) + end + + it "root_class_method" do + Category.root.should == categories(:top_level) + end + + it "root" do + categories(:child_3).root.should == categories(:top_level) + end + + it "root?" do + categories(:top_level).root?.should be_true + categories(:top_level_2).root?.should be_true + end + + it "leaves_class_method" do + Category.find(:all, :conditions => "#{Category.right_column_name} - #{Category.left_column_name} = 1").should == Category.leaves + Category.leaves.count.should == 4 + Category.leaves.should include(categories(:child_1)) + Category.leaves.should include(categories(:child_2_1)) + Category.leaves.should include(categories(:child_3)) + Category.leaves.should include(categories(:top_level_2)) + end + + it "leaf" do + categories(:child_1).leaf?.should be_true + categories(:child_2_1).leaf?.should be_true + categories(:child_3).leaf?.should be_true + categories(:top_level_2).leaf?.should be_true + + categories(:top_level).leaf?.should be_false + categories(:child_2).leaf?.should be_false + Category.new.leaf?.should be_false + end + + + it "parent" do + categories(:child_2_1).parent.should == categories(:child_2) + end + + it "self_and_ancestors" do + child = categories(:child_2_1) + self_and_ancestors = [categories(:top_level), categories(:child_2), child] + self_and_ancestors.should == child.self_and_ancestors + end + + it "ancestors" do + child = categories(:child_2_1) + ancestors = [categories(:top_level), categories(:child_2)] + ancestors.should == child.ancestors + end + + it "self_and_siblings" do + child = categories(:child_2) + self_and_siblings = [categories(:child_1), child, categories(:child_3)] + self_and_siblings.should == child.self_and_siblings + lambda do + tops = [categories(:top_level), categories(:top_level_2)] + assert_equal tops, categories(:top_level).self_and_siblings + end.should_not raise_exception + end + + it "siblings" do + child = categories(:child_2) + siblings = [categories(:child_1), categories(:child_3)] + siblings.should == child.siblings + end + + it "leaves" do + leaves = [categories(:child_1), categories(:child_2_1), categories(:child_3)] + categories(:top_level).leaves.should == leaves + end + + it "level" do + categories(:top_level).level.should == 0 + categories(:child_1).level.should == 1 + categories(:child_2_1).level.should == 2 + end + + it "has_children?" do + categories(:child_2_1).children.empty?.should be_true + categories(:child_2).children.empty?.should be_false + categories(:top_level).children.empty?.should be_false + end + + it "self_and_descendents" do + parent = categories(:top_level) + self_and_descendants = [parent, categories(:child_1), categories(:child_2), + categories(:child_2_1), categories(:child_3)] + self_and_descendants.should == parent.self_and_descendants + self_and_descendants.count.should == parent.self_and_descendants.count + end + + it "descendents" do + lawyers = Category.create!(:name => "lawyers") + us = Category.create!(:name => "United States") + us.move_to_child_of(lawyers) + patent = Category.create!(:name => "Patent Law") + patent.move_to_child_of(us) + lawyers.reload + + lawyers.children.size.should == 1 + us.children.size.should == 1 + lawyers.descendants.size.should == 2 + end + + it "self_and_descendents" do + parent = categories(:top_level) + descendants = [categories(:child_1), categories(:child_2), + categories(:child_2_1), categories(:child_3)] + descendants.should == parent.descendants + end + + it "children" do + category = categories(:top_level) + category.children.each {|c| category.id.should == c.parent_id } + end + + it "order_of_children" do + categories(:child_2).move_left + categories(:child_2).should == categories(:top_level).children[0] + categories(:child_1).should == categories(:top_level).children[1] + categories(:child_3).should == categories(:top_level).children[2] + end + + it "is_or_is_ancestor_of?" do + categories(:top_level).is_or_is_ancestor_of?(categories(:child_1)).should be_true + categories(:top_level).is_or_is_ancestor_of?(categories(:child_2_1)).should be_true + categories(:child_2).is_or_is_ancestor_of?(categories(:child_2_1)).should be_true + categories(:child_2_1).is_or_is_ancestor_of?(categories(:child_2)).should be_false + categories(:child_1).is_or_is_ancestor_of?(categories(:child_2)).should be_false + categories(:child_1).is_or_is_ancestor_of?(categories(:child_1)).should be_true + end + + it "is_ancestor_of?" do + categories(:top_level).is_ancestor_of?(categories(:child_1)).should be_true + categories(:top_level).is_ancestor_of?(categories(:child_2_1)).should be_true + categories(:child_2).is_ancestor_of?(categories(:child_2_1)).should be_true + categories(:child_2_1).is_ancestor_of?(categories(:child_2)).should be_false + categories(:child_1).is_ancestor_of?(categories(:child_2)).should be_false + categories(:child_1).is_ancestor_of?(categories(:child_1)).should be_false + end + + it "is_or_is_ancestor_of_with_scope" do + root = ScopedCategory.root + child = root.children.first + root.is_or_is_ancestor_of?(child).should be_true + child.update_attribute :organization_id, 'different' + root.is_or_is_ancestor_of?(child).should be_false + end + + it "is_or_is_descendant_of?" do + categories(:child_1).is_or_is_descendant_of?(categories(:top_level)).should be_true + categories(:child_2_1).is_or_is_descendant_of?(categories(:top_level)).should be_true + categories(:child_2_1).is_or_is_descendant_of?(categories(:child_2)).should be_true + categories(:child_2).is_or_is_descendant_of?(categories(:child_2_1)).should be_false + categories(:child_2).is_or_is_descendant_of?(categories(:child_1)).should be_false + categories(:child_1).is_or_is_descendant_of?(categories(:child_1)).should be_true + end + + it "is_descendant_of?" do + categories(:child_1).is_descendant_of?(categories(:top_level)).should be_true + categories(:child_2_1).is_descendant_of?(categories(:top_level)).should be_true + categories(:child_2_1).is_descendant_of?(categories(:child_2)).should be_true + categories(:child_2).is_descendant_of?(categories(:child_2_1)).should be_false + categories(:child_2).is_descendant_of?(categories(:child_1)).should be_false + categories(:child_1).is_descendant_of?(categories(:child_1)).should be_false + end + + it "is_or_is_descendant_of_with_scope" do + root = ScopedCategory.root + child = root.children.first + child.is_or_is_descendant_of?(root).should be_true + child.update_attribute :organization_id, 'different' + child.is_or_is_descendant_of?(root).should be_false + end + + it "same_scope?" do + root = ScopedCategory.root + child = root.children.first + child.same_scope?(root).should be_true + child.update_attribute :organization_id, 'different' + child.same_scope?(root).should be_false + end + + it "left_sibling" do + categories(:child_1).should == categories(:child_2).left_sibling + categories(:child_2).should == categories(:child_3).left_sibling + end + + it "left_sibling_of_root" do + categories(:top_level).left_sibling.should be_nil + end + + it "left_sibling_without_siblings" do + categories(:child_2_1).left_sibling.should be_nil + end + + it "left_sibling_of_leftmost_node" do + categories(:child_1).left_sibling.should be_nil + end + + it "right_sibling" do + categories(:child_3).should == categories(:child_2).right_sibling + categories(:child_2).should == categories(:child_1).right_sibling + end + + it "right_sibling_of_root" do + categories(:top_level_2).should == categories(:top_level).right_sibling + categories(:top_level_2).right_sibling.should be_nil + end + + it "right_sibling_without_siblings" do + categories(:child_2_1).right_sibling.should be_nil + end + + it "right_sibling_of_rightmost_node" do + categories(:child_3).right_sibling.should be_nil + end + + it "move_left" do + categories(:child_2).move_left + categories(:child_2).left_sibling.should be_nil + categories(:child_1).should == categories(:child_2).right_sibling + Category.valid?.should be_true + end + + it "move_right" do + categories(:child_2).move_right + categories(:child_2).right_sibling.should be_nil + categories(:child_3).should == categories(:child_2).left_sibling + Category.valid?.should be_true + end + + it "move_to_left_of" do + categories(:child_3).move_to_left_of(categories(:child_1)) + categories(:child_3).left_sibling.should be_nil + categories(:child_1).should == categories(:child_3).right_sibling + Category.valid?.should be_true + end + + it "move_to_right_of" do + categories(:child_1).move_to_right_of(categories(:child_3)) + categories(:child_1).right_sibling.should be_nil + categories(:child_3).should == categories(:child_1).left_sibling + Category.valid?.should be_true + end + + it "move_to_root" do + categories(:child_2).move_to_root + categories(:child_2).parent.should be_nil + categories(:child_2).level.should == 0 + categories(:child_2_1).level.should == 1 + categories(:child_2).left.should == 1 + categories(:child_2).right.should == 4 + Category.valid?.should be_true + end + + it "move_to_child_of" do + categories(:child_1).move_to_child_of(categories(:child_3)) + categories(:child_3).id.should == categories(:child_1).parent_id + Category.valid?.should be_true + end + + it "move_to_child_of_appends_to_end" do + child = Category.create! :name => 'New Child' + child.move_to_child_of categories(:top_level) + child.should == categories(:top_level).children.last + end + + it "subtree_move_to_child_of" do + categories(:child_2).left.should == 4 + categories(:child_2).right.should == 7 + + categories(:child_1).left.should == 2 + categories(:child_1).right.should == 3 + + categories(:child_2).move_to_child_of(categories(:child_1)) + Category.valid?.should be_true + categories(:child_1).id.should == categories(:child_2).parent_id + + categories(:child_2).left.should == 3 + categories(:child_2).right.should == 6 + categories(:child_1).left.should == 2 + categories(:child_1).right.should == 7 + end + + it "slightly_difficult_move_to_child_of" do + categories(:top_level_2).left.should == 11 + categories(:top_level_2).right.should == 12 + + # create a new top-level node and move single-node top-level tree inside it. + new_top = Category.create(:name => 'New Top') + new_top.left.should == 13 + new_top.right.should == 14 + + categories(:top_level_2).move_to_child_of(new_top) + + Category.valid?.should be_true + new_top.id.should == categories(:top_level_2).parent_id + + categories(:top_level_2).left.should == 12 + categories(:top_level_2).right.should == 13 + new_top.left.should == 11 + new_top.right.should == 14 + end + + it "difficult_move_to_child_of" do + categories(:top_level).left.should == 1 + categories(:top_level).right.should == 10 + categories(:child_2_1).left.should == 5 + categories(:child_2_1).right.should == 6 + + # create a new top-level node and move an entire top-level tree inside it. + new_top = Category.create(:name => 'New Top') + categories(:top_level).move_to_child_of(new_top) + categories(:child_2_1).reload + Category.valid?.should be_true + new_top.id.should == categories(:top_level).parent_id + + categories(:top_level).left.should == 4 + categories(:top_level).right.should == 13 + categories(:child_2_1).left.should == 8 + categories(:child_2_1).right.should == 9 + end + + #rebuild swaps the position of the 2 children when added using move_to_child twice onto same parent + it "move_to_child_more_than_once_per_parent_rebuild" do + root1 = Category.create(:name => 'Root1') + root2 = Category.create(:name => 'Root2') + root3 = Category.create(:name => 'Root3') + + root2.move_to_child_of root1 + root3.move_to_child_of root1 + + output = Category.roots.last.to_text + Category.update_all('lft = null, rgt = null') + Category.rebuild! + + Category.roots.last.to_text.should == output + end + + # doing move_to_child twice onto same parent from the furthest right first + it "move_to_child_more_than_once_per_parent_outside_in" do + node1 = Category.create(:name => 'Node-1') + node2 = Category.create(:name => 'Node-2') + node3 = Category.create(:name => 'Node-3') + + node2.move_to_child_of node1 + node3.move_to_child_of node1 + + output = Category.roots.last.to_text + Category.update_all('lft = null, rgt = null') + Category.rebuild! + + Category.roots.last.to_text.should == output + end + + it "should be able to rebuild without validating each record" do + root1 = Category.create(:name => 'Root1') + root2 = Category.create(:name => 'Root2') + root3 = Category.create(:name => 'Root3') + + root2.move_to_child_of root1 + root3.move_to_child_of root1 + + root2.name = nil + root2.save!(:validate => false) + + output = Category.roots.last.to_text + Category.update_all('lft = null, rgt = null') + Category.rebuild!(false) + + Category.roots.last.to_text.should == output + end + + it "valid_with_null_lefts" do + Category.valid?.should be_true + Category.update_all('lft = null') + Category.valid?.should be_false + end + + it "valid_with_null_rights" do + Category.valid?.should be_true + Category.update_all('rgt = null') + Category.valid?.should be_false + end + + it "valid_with_missing_intermediate_node" do + # Even though child_2_1 will still exist, it is a sign of a sloppy delete, not an invalid tree. + Category.valid?.should be_true + Category.delete(categories(:child_2).id) + Category.valid?.should be_true + end + + it "valid_with_overlapping_and_rights" do + Category.valid?.should be_true + categories(:top_level_2)['lft'] = 0 + categories(:top_level_2).save + Category.valid?.should be_false + end + + it "rebuild" do + Category.valid?.should be_true + before_text = Category.root.to_text + Category.update_all('lft = null, rgt = null') + Category.rebuild! + Category.valid?.should be_true + before_text.should == Category.root.to_text + end + + it "move_possible_for_sibling" do + categories(:child_2).move_possible?(categories(:child_1)).should be_true + end + + it "move_not_possible_to_self" do + categories(:top_level).move_possible?(categories(:top_level)).should be_false + end + + it "move_not_possible_to_parent" do + categories(:top_level).descendants.each do |descendant| + categories(:top_level).move_possible?(descendant).should be_false + descendant.move_possible?(categories(:top_level)).should be_true + end + end + + it "is_or_is_ancestor_of?" do + [:child_1, :child_2, :child_2_1, :child_3].each do |c| + categories(:top_level).is_or_is_ancestor_of?(categories(c)).should be_true + end + categories(:top_level).is_or_is_ancestor_of?(categories(:top_level_2)).should be_false + end + + it "left_and_rights_valid_with_blank_left" do + Category.left_and_rights_valid?.should be_true + categories(:child_2)[:lft] = nil + categories(:child_2).save(:validate => false) + Category.left_and_rights_valid?.should be_false + end + + it "left_and_rights_valid_with_blank_right" do + Category.left_and_rights_valid?.should be_true + categories(:child_2)[:rgt] = nil + categories(:child_2).save(:validate => false) + Category.left_and_rights_valid?.should be_false + end + + it "left_and_rights_valid_with_equal" do + Category.left_and_rights_valid?.should be_true + categories(:top_level_2)[:lft] = categories(:top_level_2)[:rgt] + categories(:top_level_2).save(:validate => false) + Category.left_and_rights_valid?.should be_false + end + + it "left_and_rights_valid_with_left_equal_to_parent" do + Category.left_and_rights_valid?.should be_true + categories(:child_2)[:lft] = categories(:top_level)[:lft] + categories(:child_2).save(:validate => false) + Category.left_and_rights_valid?.should be_false + end + + it "left_and_rights_valid_with_right_equal_to_parent" do + Category.left_and_rights_valid?.should be_true + categories(:child_2)[:rgt] = categories(:top_level)[:rgt] + categories(:child_2).save(:validate => false) + Category.left_and_rights_valid?.should be_false + end + + it "moving_dirty_objects_doesnt_invalidate_tree" do + r1 = Category.create :name => "Test 1" + r2 = Category.create :name => "Test 2" + r3 = Category.create :name => "Test 3" + r4 = Category.create :name => "Test 4" + nodes = [r1, r2, r3, r4] + + r2.move_to_child_of(r1) + Category.valid?.should be_true + + r3.move_to_child_of(r1) + Category.valid?.should be_true + + r4.move_to_child_of(r2) + Category.valid?.should be_true + end + + it "multi_scoped_no_duplicates_for_columns?" do + lambda { + Note.no_duplicates_for_columns? + }.should_not raise_exception + end + + it "multi_scoped_all_roots_valid?" do + lambda { + Note.all_roots_valid? + }.should_not raise_exception + end + + it "multi_scoped" do + note1 = Note.create!(:body => "A", :notable_id => 2, :notable_type => 'Category') + note2 = Note.create!(:body => "B", :notable_id => 2, :notable_type => 'Category') + note3 = Note.create!(:body => "C", :notable_id => 2, :notable_type => 'Default') + + [note1, note2].should == note1.self_and_siblings + [note3].should == note3.self_and_siblings + end + + it "multi_scoped_rebuild" do + root = Note.create!(:body => "A", :notable_id => 3, :notable_type => 'Category') + child1 = Note.create!(:body => "B", :notable_id => 3, :notable_type => 'Category') + child2 = Note.create!(:body => "C", :notable_id => 3, :notable_type => 'Category') + + child1.move_to_child_of root + child2.move_to_child_of root + + Note.update_all('lft = null, rgt = null') + Note.rebuild! + + Note.roots.find_by_body('A').should == root + [child1, child2].should == Note.roots.find_by_body('A').children + end + + it "same_scope_with_multi_scopes" do + lambda { + notes(:scope1).same_scope?(notes(:child_1)) + }.should_not raise_exception + notes(:scope1).same_scope?(notes(:child_1)).should be_true + notes(:child_1).same_scope?(notes(:scope1)).should be_true + notes(:scope1).same_scope?(notes(:scope2)).should be_false + end + + it "quoting_of_multi_scope_column_names" do + ["\"notable_id\"", "\"notable_type\""].should == Note.quoted_scope_column_names + end + + it "equal_in_same_scope" do + notes(:scope1).should == notes(:scope1) + notes(:scope1).should_not == notes(:child_1) + end + + it "equal_in_different_scopes" do + notes(:scope1).should_not == notes(:scope2) + end + + it "delete_does_not_invalidate" do + Category.acts_as_nested_set_options[:dependent] = :delete + categories(:child_2).destroy + Category.valid?.should be_true + end + + it "destroy_does_not_invalidate" do + Category.acts_as_nested_set_options[:dependent] = :destroy + categories(:child_2).destroy + Category.valid?.should be_true + end + + it "destroy_multiple_times_does_not_invalidate" do + Category.acts_as_nested_set_options[:dependent] = :destroy + categories(:child_2).destroy + categories(:child_2).destroy + Category.valid?.should be_true + end + + it "assigning_parent_id_on_create" do + category = Category.create!(:name => "Child", :parent_id => categories(:child_2).id) + categories(:child_2).should == category.parent + categories(:child_2).id.should == category.parent_id + category.left.should_not be_nil + category.right.should_not be_nil + Category.valid?.should be_true + end + + it "assigning_parent_on_create" do + category = Category.create!(:name => "Child", :parent => categories(:child_2)) + categories(:child_2).should == category.parent + categories(:child_2).id.should == category.parent_id + category.left.should_not be_nil + category.right.should_not be_nil + Category.valid?.should be_true + end + + it "assigning_parent_id_to_nil_on_create" do + category = Category.create!(:name => "New Root", :parent_id => nil) + category.parent.should be_nil + category.parent_id.should be_nil + category.left.should_not be_nil + category.right.should_not be_nil + Category.valid?.should be_true + end + + it "assigning_parent_id_on_update" do + category = categories(:child_2_1) + category.parent_id = categories(:child_3).id + category.save + category.reload + categories(:child_3).reload + categories(:child_3).should == category.parent + categories(:child_3).id.should == category.parent_id + Category.valid?.should be_true + end + + it "assigning_parent_on_update" do + category = categories(:child_2_1) + category.parent = categories(:child_3) + category.save + category.reload + categories(:child_3).reload + categories(:child_3).should == category.parent + categories(:child_3).id.should == category.parent_id + Category.valid?.should be_true + end + + it "assigning_parent_id_to_nil_on_update" do + category = categories(:child_2_1) + category.parent_id = nil + category.save + category.parent.should be_nil + category.parent_id.should be_nil + Category.valid?.should be_true + end + + it "creating_child_from_parent" do + category = categories(:child_2).children.create!(:name => "Child") + categories(:child_2).should == category.parent + categories(:child_2).id.should == category.parent_id + category.left.should_not be_nil + category.right.should_not be_nil + Category.valid?.should be_true + end + + def check_structure(entries, structure) + structure = structure.dup + Category.each_with_level(entries) do |category, level| + expected_level, expected_name = structure.shift + expected_name.should == category.name + expected_level.should == level + end + end + + it "each_with_level" do + levels = [ + [0, "Top Level"], + [1, "Child 1"], + [1, "Child 2"], + [2, "Child 2.1"], + [1, "Child 3" ]] + + check_structure(Category.root.self_and_descendants, levels) + + # test some deeper structures + category = Category.find_by_name("Child 1") + c1 = Category.new(:name => "Child 1.1") + c2 = Category.new(:name => "Child 1.1.1") + c3 = Category.new(:name => "Child 1.1.1.1") + c4 = Category.new(:name => "Child 1.2") + [c1, c2, c3, c4].each(&:save!) + + c1.move_to_child_of(category) + c2.move_to_child_of(c1) + c3.move_to_child_of(c2) + c4.move_to_child_of(category) + + levels = [ + [0, "Top Level"], + [1, "Child 1"], + [2, "Child 1.1"], + [3, "Child 1.1.1"], + [4, "Child 1.1.1.1"], + [2, "Child 1.2"], + [1, "Child 2"], + [2, "Child 2.1"], + [1, "Child 3" ]] + + check_structure(Category.root.self_and_descendants, levels) + end + + it "should not error on a model with attr_accessible" do + model = Class.new(ActiveRecord::Base) + model.table_name = 'categories' + model.attr_accessible :name + lambda { + model.acts_as_nested_set + model.new(:name => 'foo') + }.should_not raise_exception + end + + describe "before_move_callback" do + it "should fire the callback" do + categories(:child_2).should_receive(:custom_before_move) + categories(:child_2).move_to_root + end + + it "should stop move when callback returns false" do + Category.test_allows_move = false + categories(:child_3).move_to_root.should be_false + categories(:child_3).root?.should be_false + end + + it "should not halt save actions" do + Category.test_allows_move = false + categories(:child_3).parent_id = nil + categories(:child_3).save.should be_true + end + end + + describe "counter_cache" do + + it "should allow use of a counter cache for children" do + note1 = things(:parent1) + note1.children.count.should == 2 + end + + it "should increment the counter cache on create" do + note1 = things(:parent1) + note1.children.count.should == 2 + note1[:children_count].should == 2 + note1.children.create :body => 'Child 3' + note1.children.count.should == 3 + note1.reload + note1[:children_count].should == 3 + end + + it "should decrement the counter cache on destroy" do + note1 = things(:parent1) + note1.children.count.should == 2 + note1[:children_count].should == 2 + note1.children.last.destroy + note1.children.count.should == 1 + note1.reload + note1[:children_count].should == 1 + end + end + + describe "association callbacks on children" do + it "should call the appropriate callbacks on the children :has_many association " do + root = DefaultWithCallbacks.create + root.should_not be_new_record + + child = root.children.build + + root.before_add.should == child + root.after_add.should == child + + root.before_remove.should_not == child + root.after_remove.should_not == child + + child.save.should be_true + root.children.delete(child).should be_true + + root.before_remove.should == child + root.after_remove.should == child + end + end +end diff --git a/lib/plugins/awesome_nested_set/spec/db/database.yml b/lib/plugins/awesome_nested_set/spec/db/database.yml index c8c0aeeac..eaa5ed3a0 100644 --- a/lib/plugins/awesome_nested_set/spec/db/database.yml +++ b/lib/plugins/awesome_nested_set/spec/db/database.yml @@ -1,18 +1,18 @@ -sqlite3: - adapter: <%= "jdbc" if defined? JRUBY_VERSION %>sqlite3 - database: awesome_nested_set.sqlite3.db -sqlite3mem: - adapter: <%= "jdbc" if defined? JRUBY_VERSION %>sqlite3 - database: ":memory:" -postgresql: - adapter: postgresql - username: postgres - password: postgres - database: awesome_nested_set_plugin_test - min_messages: ERROR -mysql: - adapter: mysql2 - host: localhost - username: root - password: +sqlite3: + adapter: <%= "jdbc" if defined? JRUBY_VERSION %>sqlite3 + database: awesome_nested_set.sqlite3.db +sqlite3mem: + adapter: <%= "jdbc" if defined? JRUBY_VERSION %>sqlite3 + database: ":memory:" +postgresql: + adapter: postgresql + username: postgres + password: postgres + database: awesome_nested_set_plugin_test + min_messages: ERROR +mysql: + adapter: mysql2 + host: localhost + username: root + password: database: awesome_nested_set_plugin_test \ No newline at end of file diff --git a/lib/plugins/awesome_nested_set/spec/db/schema.rb b/lib/plugins/awesome_nested_set/spec/db/schema.rb index d3625fa3a..dcdcde814 100644 --- a/lib/plugins/awesome_nested_set/spec/db/schema.rb +++ b/lib/plugins/awesome_nested_set/spec/db/schema.rb @@ -1,45 +1,45 @@ -ActiveRecord::Schema.define(:version => 0) do - - create_table :categories, :force => true do |t| - t.column :name, :string - t.column :parent_id, :integer - t.column :lft, :integer - t.column :rgt, :integer - t.column :organization_id, :integer - end - - create_table :departments, :force => true do |t| - t.column :name, :string - end - - create_table :notes, :force => true do |t| - t.column :body, :text - t.column :parent_id, :integer - t.column :lft, :integer - t.column :rgt, :integer - t.column :notable_id, :integer - t.column :notable_type, :string - end - - create_table :renamed_columns, :force => true do |t| - t.column :name, :string - t.column :mother_id, :integer - t.column :red, :integer - t.column :black, :integer - end - - create_table :things, :force => true do |t| - t.column :body, :text - t.column :parent_id, :integer - t.column :lft, :integer - t.column :rgt, :integer - t.column :children_count, :integer - end - - create_table :brokens, :force => true do |t| - t.column :name, :string - t.column :parent_id, :integer - t.column :lft, :integer - t.column :rgt, :integer - end -end +ActiveRecord::Schema.define(:version => 0) do + + create_table :categories, :force => true do |t| + t.column :name, :string + t.column :parent_id, :integer + t.column :lft, :integer + t.column :rgt, :integer + t.column :organization_id, :integer + end + + create_table :departments, :force => true do |t| + t.column :name, :string + end + + create_table :notes, :force => true do |t| + t.column :body, :text + t.column :parent_id, :integer + t.column :lft, :integer + t.column :rgt, :integer + t.column :notable_id, :integer + t.column :notable_type, :string + end + + create_table :renamed_columns, :force => true do |t| + t.column :name, :string + t.column :mother_id, :integer + t.column :red, :integer + t.column :black, :integer + end + + create_table :things, :force => true do |t| + t.column :body, :text + t.column :parent_id, :integer + t.column :lft, :integer + t.column :rgt, :integer + t.column :children_count, :integer + end + + create_table :brokens, :force => true do |t| + t.column :name, :string + t.column :parent_id, :integer + t.column :lft, :integer + t.column :rgt, :integer + end +end diff --git a/lib/plugins/awesome_nested_set/spec/fixtures/brokens.yml b/lib/plugins/awesome_nested_set/spec/fixtures/brokens.yml index 45a8c0981..3cb6ce915 100644 --- a/lib/plugins/awesome_nested_set/spec/fixtures/brokens.yml +++ b/lib/plugins/awesome_nested_set/spec/fixtures/brokens.yml @@ -1,3 +1,3 @@ -one: - id: 1 +one: + id: 1 name: One \ No newline at end of file diff --git a/lib/plugins/awesome_nested_set/spec/fixtures/categories.yml b/lib/plugins/awesome_nested_set/spec/fixtures/categories.yml index 6cf7f3f41..bc8e078e8 100644 --- a/lib/plugins/awesome_nested_set/spec/fixtures/categories.yml +++ b/lib/plugins/awesome_nested_set/spec/fixtures/categories.yml @@ -1,34 +1,34 @@ -top_level: - id: 1 - name: Top Level - lft: 1 - rgt: 10 -child_1: - id: 2 - name: Child 1 - parent_id: 1 - lft: 2 - rgt: 3 -child_2: - id: 3 - name: Child 2 - parent_id: 1 - lft: 4 - rgt: 7 -child_2_1: - id: 4 - name: Child 2.1 - parent_id: 3 - lft: 5 - rgt: 6 -child_3: - id: 5 - name: Child 3 - parent_id: 1 - lft: 8 - rgt: 9 -top_level_2: - id: 6 - name: Top Level 2 - lft: 11 - rgt: 12 +top_level: + id: 1 + name: Top Level + lft: 1 + rgt: 10 +child_1: + id: 2 + name: Child 1 + parent_id: 1 + lft: 2 + rgt: 3 +child_2: + id: 3 + name: Child 2 + parent_id: 1 + lft: 4 + rgt: 7 +child_2_1: + id: 4 + name: Child 2.1 + parent_id: 3 + lft: 5 + rgt: 6 +child_3: + id: 5 + name: Child 3 + parent_id: 1 + lft: 8 + rgt: 9 +top_level_2: + id: 6 + name: Top Level 2 + lft: 11 + rgt: 12 diff --git a/lib/plugins/awesome_nested_set/spec/fixtures/departments.yml b/lib/plugins/awesome_nested_set/spec/fixtures/departments.yml index 31d65624a..e50a944f1 100644 --- a/lib/plugins/awesome_nested_set/spec/fixtures/departments.yml +++ b/lib/plugins/awesome_nested_set/spec/fixtures/departments.yml @@ -1,3 +1,3 @@ -top: - id: 1 +top: + id: 1 name: Top \ No newline at end of file diff --git a/lib/plugins/awesome_nested_set/spec/fixtures/notes.yml b/lib/plugins/awesome_nested_set/spec/fixtures/notes.yml index cb7a48faf..004a5335a 100644 --- a/lib/plugins/awesome_nested_set/spec/fixtures/notes.yml +++ b/lib/plugins/awesome_nested_set/spec/fixtures/notes.yml @@ -1,38 +1,38 @@ -scope1: - id: 1 - body: Top Level - lft: 1 - rgt: 10 - notable_id: 1 - notable_type: Category -child_1: - id: 2 - body: Child 1 - parent_id: 1 - lft: 2 - rgt: 3 - notable_id: 1 - notable_type: Category -child_2: - id: 3 - body: Child 2 - parent_id: 1 - lft: 4 - rgt: 7 - notable_id: 1 - notable_type: Category -child_3: - id: 4 - body: Child 3 - parent_id: 1 - lft: 8 - rgt: 9 - notable_id: 1 - notable_type: Category -scope2: - id: 5 - body: Top Level 2 - lft: 1 - rgt: 2 - notable_id: 1 - notable_type: Departments +scope1: + id: 1 + body: Top Level + lft: 1 + rgt: 10 + notable_id: 1 + notable_type: Category +child_1: + id: 2 + body: Child 1 + parent_id: 1 + lft: 2 + rgt: 3 + notable_id: 1 + notable_type: Category +child_2: + id: 3 + body: Child 2 + parent_id: 1 + lft: 4 + rgt: 7 + notable_id: 1 + notable_type: Category +child_3: + id: 4 + body: Child 3 + parent_id: 1 + lft: 8 + rgt: 9 + notable_id: 1 + notable_type: Category +scope2: + id: 5 + body: Top Level 2 + lft: 1 + rgt: 2 + notable_id: 1 + notable_type: Departments diff --git a/lib/plugins/awesome_nested_set/spec/fixtures/things.yml b/lib/plugins/awesome_nested_set/spec/fixtures/things.yml index b9c868831..207a682ed 100644 --- a/lib/plugins/awesome_nested_set/spec/fixtures/things.yml +++ b/lib/plugins/awesome_nested_set/spec/fixtures/things.yml @@ -1,27 +1,27 @@ -parent1: - id: 1 - body: Top Level - lft: 1 - rgt: 10 - children_count: 2 -child_1: - id: 2 - body: Child 1 - parent_id: 1 - lft: 2 - rgt: 3 - children_count: 0 -child_2: - id: 3 - body: Child 2 - parent_id: 1 - lft: 4 - rgt: 7 - children_count: 0 -child_2_1: - id: 4 - body: Child 2.1 - parent_id: 3 - lft: 8 - rgt: 9 - children_count: 0 +parent1: + id: 1 + body: Top Level + lft: 1 + rgt: 10 + children_count: 2 +child_1: + id: 2 + body: Child 1 + parent_id: 1 + lft: 2 + rgt: 3 + children_count: 0 +child_2: + id: 3 + body: Child 2 + parent_id: 1 + lft: 4 + rgt: 7 + children_count: 0 +child_2_1: + id: 4 + body: Child 2.1 + parent_id: 3 + lft: 8 + rgt: 9 + children_count: 0 diff --git a/lib/plugins/awesome_nested_set/spec/spec_helper.rb b/lib/plugins/awesome_nested_set/spec/spec_helper.rb index 3fa1ba3ad..c9cf43d88 100644 --- a/lib/plugins/awesome_nested_set/spec/spec_helper.rb +++ b/lib/plugins/awesome_nested_set/spec/spec_helper.rb @@ -1,32 +1,32 @@ -$:.unshift(File.dirname(__FILE__) + '/../lib') -plugin_test_dir = File.dirname(__FILE__) - -require 'rubygems' -require 'bundler/setup' - -require 'rspec' -require 'logger' - -require 'active_support' -require 'active_model' -require 'active_record' -require 'action_controller' - -require 'awesome_nested_set' - -ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log") - -require 'yaml' -require 'erb' -ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(plugin_test_dir + "/db/database.yml")).result) -ActiveRecord::Base.establish_connection(ENV["DB"] || "sqlite3mem") -ActiveRecord::Migration.verbose = false -load(File.join(plugin_test_dir, "db", "schema.rb")) - -require 'support/models' - -require 'rspec/rails' -RSpec.configure do |config| - config.fixture_path = "#{plugin_test_dir}/fixtures" - config.use_transactional_fixtures = true -end +$:.unshift(File.dirname(__FILE__) + '/../lib') +plugin_test_dir = File.dirname(__FILE__) + +require 'rubygems' +require 'bundler/setup' + +require 'rspec' +require 'logger' + +require 'active_support' +require 'active_model' +require 'active_record' +require 'action_controller' + +require 'awesome_nested_set' + +ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log") + +require 'yaml' +require 'erb' +ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(plugin_test_dir + "/db/database.yml")).result) +ActiveRecord::Base.establish_connection(ENV["DB"] || "sqlite3mem") +ActiveRecord::Migration.verbose = false +load(File.join(plugin_test_dir, "db", "schema.rb")) + +require 'support/models' + +require 'rspec/rails' +RSpec.configure do |config| + config.fixture_path = "#{plugin_test_dir}/fixtures" + config.use_transactional_fixtures = true +end diff --git a/lib/plugins/awesome_nested_set/spec/support/models.rb b/lib/plugins/awesome_nested_set/spec/support/models.rb index bb8776132..fadb0dc0b 100644 --- a/lib/plugins/awesome_nested_set/spec/support/models.rb +++ b/lib/plugins/awesome_nested_set/spec/support/models.rb @@ -1,72 +1,72 @@ -class Note < ActiveRecord::Base - acts_as_nested_set :scope => [:notable_id, :notable_type] -end - -class Default < ActiveRecord::Base - self.table_name = 'categories' - acts_as_nested_set -end - -class ScopedCategory < ActiveRecord::Base - self.table_name = 'categories' - acts_as_nested_set :scope => :organization -end - -class RenamedColumns < ActiveRecord::Base - acts_as_nested_set :parent_column => 'mother_id', :left_column => 'red', :right_column => 'black' -end - -class Category < ActiveRecord::Base - acts_as_nested_set - - validates_presence_of :name - - # Setup a callback that we can switch to true or false per-test - set_callback :move, :before, :custom_before_move - cattr_accessor :test_allows_move - @@test_allows_move = true - def custom_before_move - @@test_allows_move - end - - def to_s - name - end - - def recurse &block - block.call self, lambda{ - self.children.each do |child| - child.recurse &block - end - } - end -end - -class Thing < ActiveRecord::Base - acts_as_nested_set :counter_cache => 'children_count' -end - -class DefaultWithCallbacks < ActiveRecord::Base - - self.table_name = 'categories' - - attr_accessor :before_add, :after_add, :before_remove, :after_remove - - acts_as_nested_set :before_add => :do_before_add_stuff, - :after_add => :do_after_add_stuff, - :before_remove => :do_before_remove_stuff, - :after_remove => :do_after_remove_stuff - - private - - [ :before_add, :after_add, :before_remove, :after_remove ].each do |hook_name| - define_method "do_#{hook_name}_stuff" do |child_node| - self.send("#{hook_name}=", child_node) - end - end - -end - -class Broken < ActiveRecord::Base - acts_as_nested_set +class Note < ActiveRecord::Base + acts_as_nested_set :scope => [:notable_id, :notable_type] +end + +class Default < ActiveRecord::Base + self.table_name = 'categories' + acts_as_nested_set +end + +class ScopedCategory < ActiveRecord::Base + self.table_name = 'categories' + acts_as_nested_set :scope => :organization +end + +class RenamedColumns < ActiveRecord::Base + acts_as_nested_set :parent_column => 'mother_id', :left_column => 'red', :right_column => 'black' +end + +class Category < ActiveRecord::Base + acts_as_nested_set + + validates_presence_of :name + + # Setup a callback that we can switch to true or false per-test + set_callback :move, :before, :custom_before_move + cattr_accessor :test_allows_move + @@test_allows_move = true + def custom_before_move + @@test_allows_move + end + + def to_s + name + end + + def recurse &block + block.call self, lambda{ + self.children.each do |child| + child.recurse &block + end + } + end +end + +class Thing < ActiveRecord::Base + acts_as_nested_set :counter_cache => 'children_count' +end + +class DefaultWithCallbacks < ActiveRecord::Base + + self.table_name = 'categories' + + attr_accessor :before_add, :after_add, :before_remove, :after_remove + + acts_as_nested_set :before_add => :do_before_add_stuff, + :after_add => :do_after_add_stuff, + :before_remove => :do_before_remove_stuff, + :after_remove => :do_after_remove_stuff + + private + + [ :before_add, :after_add, :before_remove, :after_remove ].each do |hook_name| + define_method "do_#{hook_name}_stuff" do |child_node| + self.send("#{hook_name}=", child_node) + end + end + +end + +class Broken < ActiveRecord::Base + acts_as_nested_set end \ No newline at end of file -- cgit v1.2.3