From 03bde9fb42235af7147d6312e2cf6d928111dee1 Mon Sep 17 00:00:00 2001 From: hukl Date: Tue, 8 Sep 2009 11:30:11 +0200 Subject: updated awesome_nested_set plugin --- .../plugins/awesome_nested_set/test/application.rb | 1 + .../test/awesome_nested_set/helper_test.rb | 6 +- .../test/awesome_nested_set_test.rb | 165 ++++++++++++++++++--- .../plugins/awesome_nested_set/test/test_helper.rb | 14 +- 4 files changed, 154 insertions(+), 32 deletions(-) create mode 100644 vendor/plugins/awesome_nested_set/test/application.rb (limited to 'vendor/plugins/awesome_nested_set/test') diff --git a/vendor/plugins/awesome_nested_set/test/application.rb b/vendor/plugins/awesome_nested_set/test/application.rb new file mode 100644 index 0000000..0e6eacf --- /dev/null +++ b/vendor/plugins/awesome_nested_set/test/application.rb @@ -0,0 +1 @@ +# This file is here to satisfy test_help from Rails < 2.3 \ No newline at end of file diff --git a/vendor/plugins/awesome_nested_set/test/awesome_nested_set/helper_test.rb b/vendor/plugins/awesome_nested_set/test/awesome_nested_set/helper_test.rb index 6122a0e..888323c 100644 --- a/vendor/plugins/awesome_nested_set/test/awesome_nested_set/helper_test.rb +++ b/vendor/plugins/awesome_nested_set/test/awesome_nested_set/helper_test.rb @@ -1,9 +1,9 @@ -require File.dirname(__FILE__) + '/../test_helper' +require 'test_helper' module CollectiveIdea module Acts #:nodoc: module NestedSet #:nodoc: - class AwesomeNestedSetTest < Test::Unit::TestCase + class AwesomeNestedSetTest < TestCaseClass include Helper fixtures :categories @@ -38,4 +38,4 @@ module CollectiveIdea end end end -end \ No newline at end of file +end diff --git a/vendor/plugins/awesome_nested_set/test/awesome_nested_set_test.rb b/vendor/plugins/awesome_nested_set/test/awesome_nested_set_test.rb index 5252d80..7da4504 100644 --- a/vendor/plugins/awesome_nested_set/test/awesome_nested_set_test.rb +++ b/vendor/plugins/awesome_nested_set/test/awesome_nested_set_test.rb @@ -1,19 +1,18 @@ -require File.dirname(__FILE__) + '/test_helper' +require 'test_helper' class Note < ActiveRecord::Base acts_as_nested_set :scope => [:notable_id, :notable_type] end +class Default < ActiveRecord::Base + acts_as_nested_set + set_table_name 'categories' +end +class ScopedCategory < ActiveRecord::Base + acts_as_nested_set :scope => :organization + set_table_name 'categories' +end -class AwesomeNestedSetTest < Test::Unit::TestCase - - class Default < ActiveRecord::Base - acts_as_nested_set - set_table_name 'categories' - end - class Scoped < ActiveRecord::Base - acts_as_nested_set :scope => :organization - set_table_name 'categories' - end +class AwesomeNestedSetTest < TestCaseClass def test_left_column_default assert_equal 'lft', Default.acts_as_nested_set_options[:left_column] @@ -66,19 +65,14 @@ class AwesomeNestedSetTest < Test::Unit::TestCase assert_raises(ActiveRecord::ActiveRecordError) { Category.new.rgt = 1 } end - def test_parent_column_protected_from_assignment - assert_raises(ActiveRecord::ActiveRecordError) { Category.new.parent_id = 1 } - end - def test_colums_protected_on_initialize - c = Category.new(:lft => 1, :rgt => 2, :parent_id => 3) + c = Category.new(:lft => 1, :rgt => 2) assert_nil c.lft assert_nil c.rgt - assert_nil c.parent_id end def test_scoped_appends_id - assert_equal :organization_id, Scoped.acts_as_nested_set_options[:scope] + assert_equal :organization_id, ScopedCategory.acts_as_nested_set_options[:scope] end def test_roots_class_method @@ -115,7 +109,9 @@ class AwesomeNestedSetTest < Test::Unit::TestCase assert !categories(:top_level).leaf? assert !categories(:child_2).leaf? + assert !Category.new.leaf? end + def test_parent assert_equal categories(:child_2), categories(:child_2_1).parent @@ -218,7 +214,7 @@ class AwesomeNestedSetTest < Test::Unit::TestCase end def test_is_or_is_ancestor_of_with_scope - root = Scoped.root + root = ScopedCategory.root child = root.children.first assert root.is_or_is_ancestor_of?(child) child.update_attribute :organization_id, 'different' @@ -244,7 +240,7 @@ class AwesomeNestedSetTest < Test::Unit::TestCase end def test_is_or_is_descendant_of_with_scope - root = Scoped.root + root = ScopedCategory.root child = root.children.first assert child.is_or_is_descendant_of?(root) child.update_attribute :organization_id, 'different' @@ -252,7 +248,7 @@ class AwesomeNestedSetTest < Test::Unit::TestCase end def test_same_scope? - root = Scoped.root + root = ScopedCategory.root child = root.children.first assert child.same_scope?(root) child.update_attribute :organization_id, 'different' @@ -600,4 +596,131 @@ class AwesomeNestedSetTest < Test::Unit::TestCase assert_not_equal notes(:scope1), notes(:scope2) end + def test_delete_does_not_invalidate + Category.acts_as_nested_set_options[:dependent] = :delete + categories(:child_2).destroy + assert Category.valid? + end + + def test_destroy_does_not_invalidate + Category.acts_as_nested_set_options[:dependent] = :destroy + categories(:child_2).destroy + assert Category.valid? + end + + def test_destroy_multiple_times_does_not_invalidate + Category.acts_as_nested_set_options[:dependent] = :destroy + categories(:child_2).destroy + categories(:child_2).destroy + assert Category.valid? + end + + def test_assigning_parent_id_on_create + category = Category.create!(:name => "Child", :parent_id => categories(:child_2).id) + assert_equal categories(:child_2), category.parent + assert_equal categories(:child_2).id, category.parent_id + assert_not_nil category.left + assert_not_nil category.right + assert Category.valid? + end + + def test_assigning_parent_on_create + category = Category.create!(:name => "Child", :parent => categories(:child_2)) + assert_equal categories(:child_2), category.parent + assert_equal categories(:child_2).id, category.parent_id + assert_not_nil category.left + assert_not_nil category.right + assert Category.valid? + end + + def test_assigning_parent_id_to_nil_on_create + category = Category.create!(:name => "New Root", :parent_id => nil) + assert_nil category.parent + assert_nil category.parent_id + assert_not_nil category.left + assert_not_nil category.right + assert Category.valid? + end + + def test_assigning_parent_id_on_update + category = categories(:child_2_1) + category.parent_id = categories(:child_3).id + category.save + assert_equal categories(:child_3), category.parent + assert_equal categories(:child_3).id, category.parent_id + assert Category.valid? + end + + def test_assigning_parent_on_update + category = categories(:child_2_1) + category.parent = categories(:child_3) + category.save + assert_equal categories(:child_3), category.parent + assert_equal categories(:child_3).id, category.parent_id + assert Category.valid? + end + + def test_assigning_parent_id_to_nil_on_update + category = categories(:child_2_1) + category.parent_id = nil + category.save + assert_nil category.parent + assert_nil category.parent_id + assert Category.valid? + end + + def test_creating_child_from_parent + category = categories(:child_2).children.create!(:name => "Child") + assert_equal categories(:child_2), category.parent + assert_equal categories(:child_2).id, category.parent_id + assert_not_nil category.left + assert_not_nil category.right + assert Category.valid? + end + + def check_structure(entries, structure) + structure = structure.dup + Category.each_with_level(entries) do |category, level| + expected_level, expected_name = structure.shift + assert_equal expected_name, category.name, "wrong category" + assert_equal expected_level, level, "wrong level for #{category.name}" + end + end + + def test_each_with_level + 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 end diff --git a/vendor/plugins/awesome_nested_set/test/test_helper.rb b/vendor/plugins/awesome_nested_set/test/test_helper.rb index 6939822..05d8855 100644 --- a/vendor/plugins/awesome_nested_set/test/test_helper.rb +++ b/vendor/plugins/awesome_nested_set/test/test_helper.rb @@ -1,17 +1,16 @@ $:.unshift(File.dirname(__FILE__) + '/../lib') plugin_test_dir = File.dirname(__FILE__) +RAILS_ROOT = plugin_test_dir require 'rubygems' require 'test/unit' require 'multi_rails_init' -# gem 'activerecord', '>= 2.0' -require 'active_record' -require 'action_controller' -require 'action_view' -require 'active_record/fixtures' +require 'test_help' require plugin_test_dir + '/../init.rb' +TestCaseClass = ActiveSupport::TestCase rescue Test::Unit::TestCase + ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log") ActiveRecord::Base.configurations = YAML::load(IO.read(plugin_test_dir + "/db/database.yml")) @@ -21,11 +20,10 @@ load(File.join(plugin_test_dir, "db", "schema.rb")) Dir["#{plugin_test_dir}/fixtures/*.rb"].each {|file| require file } - -class Test::Unit::TestCase #:nodoc: +class TestCaseClass #:nodoc: self.fixture_path = File.dirname(__FILE__) + "/fixtures/" self.use_transactional_fixtures = true self.use_instantiated_fixtures = false fixtures :categories, :notes, :departments -end \ No newline at end of file +end -- cgit v1.3