diff options
| author | hukl <contact@smyck.org> | 2009-09-08 11:30:11 +0200 |
|---|---|---|
| committer | hukl <contact@smyck.org> | 2009-09-08 11:30:11 +0200 |
| commit | 03bde9fb42235af7147d6312e2cf6d928111dee1 (patch) | |
| tree | 973056e923d5c1b147c16cc9ee5b50a0180c5630 /vendor/plugins/awesome_nested_set/test | |
| parent | 1ab4397274dfbed2cd35bcad0c023cefc25c7365 (diff) | |
updated awesome_nested_set plugin
Diffstat (limited to 'vendor/plugins/awesome_nested_set/test')
4 files changed, 154 insertions, 32 deletions
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 @@ | |||
| 1 | require File.dirname(__FILE__) + '/../test_helper' | 1 | require 'test_helper' |
| 2 | 2 | ||
| 3 | module CollectiveIdea | 3 | module CollectiveIdea |
| 4 | module Acts #:nodoc: | 4 | module Acts #:nodoc: |
| 5 | module NestedSet #:nodoc: | 5 | module NestedSet #:nodoc: |
| 6 | class AwesomeNestedSetTest < Test::Unit::TestCase | 6 | class AwesomeNestedSetTest < TestCaseClass |
| 7 | include Helper | 7 | include Helper |
| 8 | fixtures :categories | 8 | fixtures :categories |
| 9 | 9 | ||
| @@ -38,4 +38,4 @@ module CollectiveIdea | |||
| 38 | end | 38 | end |
| 39 | end | 39 | end |
| 40 | end | 40 | end |
| 41 | end \ No newline at end of file | 41 | 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 @@ | |||
| 1 | require File.dirname(__FILE__) + '/test_helper' | 1 | require 'test_helper' |
| 2 | 2 | ||
| 3 | class Note < ActiveRecord::Base | 3 | class Note < ActiveRecord::Base |
| 4 | acts_as_nested_set :scope => [:notable_id, :notable_type] | 4 | acts_as_nested_set :scope => [:notable_id, :notable_type] |
| 5 | end | 5 | end |
| 6 | class Default < ActiveRecord::Base | ||
| 7 | acts_as_nested_set | ||
| 8 | set_table_name 'categories' | ||
| 9 | end | ||
| 10 | class ScopedCategory < ActiveRecord::Base | ||
| 11 | acts_as_nested_set :scope => :organization | ||
| 12 | set_table_name 'categories' | ||
| 13 | end | ||
| 6 | 14 | ||
| 7 | class AwesomeNestedSetTest < Test::Unit::TestCase | 15 | class AwesomeNestedSetTest < TestCaseClass |
| 8 | |||
| 9 | class Default < ActiveRecord::Base | ||
| 10 | acts_as_nested_set | ||
| 11 | set_table_name 'categories' | ||
| 12 | end | ||
| 13 | class Scoped < ActiveRecord::Base | ||
| 14 | acts_as_nested_set :scope => :organization | ||
| 15 | set_table_name 'categories' | ||
| 16 | end | ||
| 17 | 16 | ||
| 18 | def test_left_column_default | 17 | def test_left_column_default |
| 19 | assert_equal 'lft', Default.acts_as_nested_set_options[:left_column] | 18 | assert_equal 'lft', Default.acts_as_nested_set_options[:left_column] |
| @@ -66,19 +65,14 @@ class AwesomeNestedSetTest < Test::Unit::TestCase | |||
| 66 | assert_raises(ActiveRecord::ActiveRecordError) { Category.new.rgt = 1 } | 65 | assert_raises(ActiveRecord::ActiveRecordError) { Category.new.rgt = 1 } |
| 67 | end | 66 | end |
| 68 | 67 | ||
| 69 | def test_parent_column_protected_from_assignment | ||
| 70 | assert_raises(ActiveRecord::ActiveRecordError) { Category.new.parent_id = 1 } | ||
| 71 | end | ||
| 72 | |||
| 73 | def test_colums_protected_on_initialize | 68 | def test_colums_protected_on_initialize |
| 74 | c = Category.new(:lft => 1, :rgt => 2, :parent_id => 3) | 69 | c = Category.new(:lft => 1, :rgt => 2) |
| 75 | assert_nil c.lft | 70 | assert_nil c.lft |
| 76 | assert_nil c.rgt | 71 | assert_nil c.rgt |
| 77 | assert_nil c.parent_id | ||
| 78 | end | 72 | end |
| 79 | 73 | ||
| 80 | def test_scoped_appends_id | 74 | def test_scoped_appends_id |
| 81 | assert_equal :organization_id, Scoped.acts_as_nested_set_options[:scope] | 75 | assert_equal :organization_id, ScopedCategory.acts_as_nested_set_options[:scope] |
| 82 | end | 76 | end |
| 83 | 77 | ||
| 84 | def test_roots_class_method | 78 | def test_roots_class_method |
| @@ -115,7 +109,9 @@ class AwesomeNestedSetTest < Test::Unit::TestCase | |||
| 115 | 109 | ||
| 116 | assert !categories(:top_level).leaf? | 110 | assert !categories(:top_level).leaf? |
| 117 | assert !categories(:child_2).leaf? | 111 | assert !categories(:child_2).leaf? |
| 112 | assert !Category.new.leaf? | ||
| 118 | end | 113 | end |
| 114 | |||
| 119 | 115 | ||
| 120 | def test_parent | 116 | def test_parent |
| 121 | assert_equal categories(:child_2), categories(:child_2_1).parent | 117 | assert_equal categories(:child_2), categories(:child_2_1).parent |
| @@ -218,7 +214,7 @@ class AwesomeNestedSetTest < Test::Unit::TestCase | |||
| 218 | end | 214 | end |
| 219 | 215 | ||
| 220 | def test_is_or_is_ancestor_of_with_scope | 216 | def test_is_or_is_ancestor_of_with_scope |
| 221 | root = Scoped.root | 217 | root = ScopedCategory.root |
| 222 | child = root.children.first | 218 | child = root.children.first |
| 223 | assert root.is_or_is_ancestor_of?(child) | 219 | assert root.is_or_is_ancestor_of?(child) |
| 224 | child.update_attribute :organization_id, 'different' | 220 | child.update_attribute :organization_id, 'different' |
| @@ -244,7 +240,7 @@ class AwesomeNestedSetTest < Test::Unit::TestCase | |||
| 244 | end | 240 | end |
| 245 | 241 | ||
| 246 | def test_is_or_is_descendant_of_with_scope | 242 | def test_is_or_is_descendant_of_with_scope |
| 247 | root = Scoped.root | 243 | root = ScopedCategory.root |
| 248 | child = root.children.first | 244 | child = root.children.first |
| 249 | assert child.is_or_is_descendant_of?(root) | 245 | assert child.is_or_is_descendant_of?(root) |
| 250 | child.update_attribute :organization_id, 'different' | 246 | child.update_attribute :organization_id, 'different' |
| @@ -252,7 +248,7 @@ class AwesomeNestedSetTest < Test::Unit::TestCase | |||
| 252 | end | 248 | end |
| 253 | 249 | ||
| 254 | def test_same_scope? | 250 | def test_same_scope? |
| 255 | root = Scoped.root | 251 | root = ScopedCategory.root |
| 256 | child = root.children.first | 252 | child = root.children.first |
| 257 | assert child.same_scope?(root) | 253 | assert child.same_scope?(root) |
| 258 | child.update_attribute :organization_id, 'different' | 254 | child.update_attribute :organization_id, 'different' |
| @@ -600,4 +596,131 @@ class AwesomeNestedSetTest < Test::Unit::TestCase | |||
| 600 | assert_not_equal notes(:scope1), notes(:scope2) | 596 | assert_not_equal notes(:scope1), notes(:scope2) |
| 601 | end | 597 | end |
| 602 | 598 | ||
| 599 | def test_delete_does_not_invalidate | ||
| 600 | Category.acts_as_nested_set_options[:dependent] = :delete | ||
| 601 | categories(:child_2).destroy | ||
| 602 | assert Category.valid? | ||
| 603 | end | ||
| 604 | |||
| 605 | def test_destroy_does_not_invalidate | ||
| 606 | Category.acts_as_nested_set_options[:dependent] = :destroy | ||
| 607 | categories(:child_2).destroy | ||
| 608 | assert Category.valid? | ||
| 609 | end | ||
| 610 | |||
| 611 | def test_destroy_multiple_times_does_not_invalidate | ||
| 612 | Category.acts_as_nested_set_options[:dependent] = :destroy | ||
| 613 | categories(:child_2).destroy | ||
| 614 | categories(:child_2).destroy | ||
| 615 | assert Category.valid? | ||
| 616 | end | ||
| 617 | |||
| 618 | def test_assigning_parent_id_on_create | ||
| 619 | category = Category.create!(:name => "Child", :parent_id => categories(:child_2).id) | ||
| 620 | assert_equal categories(:child_2), category.parent | ||
| 621 | assert_equal categories(:child_2).id, category.parent_id | ||
| 622 | assert_not_nil category.left | ||
| 623 | assert_not_nil category.right | ||
| 624 | assert Category.valid? | ||
| 625 | end | ||
| 626 | |||
| 627 | def test_assigning_parent_on_create | ||
| 628 | category = Category.create!(:name => "Child", :parent => categories(:child_2)) | ||
| 629 | assert_equal categories(:child_2), category.parent | ||
| 630 | assert_equal categories(:child_2).id, category.parent_id | ||
| 631 | assert_not_nil category.left | ||
| 632 | assert_not_nil category.right | ||
| 633 | assert Category.valid? | ||
| 634 | end | ||
| 635 | |||
| 636 | def test_assigning_parent_id_to_nil_on_create | ||
| 637 | category = Category.create!(:name => "New Root", :parent_id => nil) | ||
| 638 | assert_nil category.parent | ||
| 639 | assert_nil category.parent_id | ||
| 640 | assert_not_nil category.left | ||
| 641 | assert_not_nil category.right | ||
| 642 | assert Category.valid? | ||
| 643 | end | ||
| 644 | |||
| 645 | def test_assigning_parent_id_on_update | ||
| 646 | category = categories(:child_2_1) | ||
| 647 | category.parent_id = categories(:child_3).id | ||
| 648 | category.save | ||
| 649 | assert_equal categories(:child_3), category.parent | ||
| 650 | assert_equal categories(:child_3).id, category.parent_id | ||
| 651 | assert Category.valid? | ||
| 652 | end | ||
| 653 | |||
| 654 | def test_assigning_parent_on_update | ||
| 655 | category = categories(:child_2_1) | ||
| 656 | category.parent = categories(:child_3) | ||
| 657 | category.save | ||
| 658 | assert_equal categories(:child_3), category.parent | ||
| 659 | assert_equal categories(:child_3).id, category.parent_id | ||
| 660 | assert Category.valid? | ||
| 661 | end | ||
| 662 | |||
| 663 | def test_assigning_parent_id_to_nil_on_update | ||
| 664 | category = categories(:child_2_1) | ||
| 665 | category.parent_id = nil | ||
| 666 | category.save | ||
| 667 | assert_nil category.parent | ||
| 668 | assert_nil category.parent_id | ||
| 669 | assert Category.valid? | ||
| 670 | end | ||
| 671 | |||
| 672 | def test_creating_child_from_parent | ||
| 673 | category = categories(:child_2).children.create!(:name => "Child") | ||
| 674 | assert_equal categories(:child_2), category.parent | ||
| 675 | assert_equal categories(:child_2).id, category.parent_id | ||
| 676 | assert_not_nil category.left | ||
| 677 | assert_not_nil category.right | ||
| 678 | assert Category.valid? | ||
| 679 | end | ||
| 680 | |||
| 681 | def check_structure(entries, structure) | ||
| 682 | structure = structure.dup | ||
| 683 | Category.each_with_level(entries) do |category, level| | ||
| 684 | expected_level, expected_name = structure.shift | ||
| 685 | assert_equal expected_name, category.name, "wrong category" | ||
| 686 | assert_equal expected_level, level, "wrong level for #{category.name}" | ||
| 687 | end | ||
| 688 | end | ||
| 689 | |||
| 690 | def test_each_with_level | ||
| 691 | levels = [ | ||
| 692 | [0, "Top Level"], | ||
| 693 | [1, "Child 1"], | ||
| 694 | [1, "Child 2"], | ||
| 695 | [2, "Child 2.1"], | ||
| 696 | [1, "Child 3" ]] | ||
| 697 | |||
| 698 | check_structure(Category.root.self_and_descendants, levels) | ||
| 699 | |||
| 700 | # test some deeper structures | ||
| 701 | category = Category.find_by_name("Child 1") | ||
| 702 | c1 = Category.new(:name => "Child 1.1") | ||
| 703 | c2 = Category.new(:name => "Child 1.1.1") | ||
| 704 | c3 = Category.new(:name => "Child 1.1.1.1") | ||
| 705 | c4 = Category.new(:name => "Child 1.2") | ||
| 706 | [c1, c2, c3, c4].each(&:save!) | ||
| 707 | |||
| 708 | c1.move_to_child_of(category) | ||
| 709 | c2.move_to_child_of(c1) | ||
| 710 | c3.move_to_child_of(c2) | ||
| 711 | c4.move_to_child_of(category) | ||
| 712 | |||
| 713 | levels = [ | ||
| 714 | [0, "Top Level"], | ||
| 715 | [1, "Child 1"], | ||
| 716 | [2, "Child 1.1"], | ||
| 717 | [3, "Child 1.1.1"], | ||
| 718 | [4, "Child 1.1.1.1"], | ||
| 719 | [2, "Child 1.2"], | ||
| 720 | [1, "Child 2"], | ||
| 721 | [2, "Child 2.1"], | ||
| 722 | [1, "Child 3" ]] | ||
| 723 | |||
| 724 | check_structure(Category.root.self_and_descendants, levels) | ||
| 725 | end | ||
| 603 | end | 726 | 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 @@ | |||
| 1 | $:.unshift(File.dirname(__FILE__) + '/../lib') | 1 | $:.unshift(File.dirname(__FILE__) + '/../lib') |
| 2 | plugin_test_dir = File.dirname(__FILE__) | 2 | plugin_test_dir = File.dirname(__FILE__) |
| 3 | RAILS_ROOT = plugin_test_dir | ||
| 3 | 4 | ||
| 4 | require 'rubygems' | 5 | require 'rubygems' |
| 5 | require 'test/unit' | 6 | require 'test/unit' |
| 6 | require 'multi_rails_init' | 7 | require 'multi_rails_init' |
| 7 | # gem 'activerecord', '>= 2.0' | 8 | require 'test_help' |
| 8 | require 'active_record' | ||
| 9 | require 'action_controller' | ||
| 10 | require 'action_view' | ||
| 11 | require 'active_record/fixtures' | ||
| 12 | 9 | ||
| 13 | require plugin_test_dir + '/../init.rb' | 10 | require plugin_test_dir + '/../init.rb' |
| 14 | 11 | ||
| 12 | TestCaseClass = ActiveSupport::TestCase rescue Test::Unit::TestCase | ||
| 13 | |||
| 15 | ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log") | 14 | ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log") |
| 16 | 15 | ||
| 17 | ActiveRecord::Base.configurations = YAML::load(IO.read(plugin_test_dir + "/db/database.yml")) | 16 | 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")) | |||
| 21 | 20 | ||
| 22 | Dir["#{plugin_test_dir}/fixtures/*.rb"].each {|file| require file } | 21 | Dir["#{plugin_test_dir}/fixtures/*.rb"].each {|file| require file } |
| 23 | 22 | ||
| 24 | 23 | class TestCaseClass #:nodoc: | |
| 25 | class Test::Unit::TestCase #:nodoc: | ||
| 26 | self.fixture_path = File.dirname(__FILE__) + "/fixtures/" | 24 | self.fixture_path = File.dirname(__FILE__) + "/fixtures/" |
| 27 | self.use_transactional_fixtures = true | 25 | self.use_transactional_fixtures = true |
| 28 | self.use_instantiated_fixtures = false | 26 | self.use_instantiated_fixtures = false |
| 29 | 27 | ||
| 30 | fixtures :categories, :notes, :departments | 28 | fixtures :categories, :notes, :departments |
| 31 | end \ No newline at end of file | 29 | end |
