summaryrefslogtreecommitdiffstats
path: root/test/unit/project_nested_set_test.rb
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2011-01-06 21:17:08 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2011-01-06 21:17:08 +0000
commit306a47023ce13d5c18d11470bf1ba7df0b1317e8 (patch)
treee6fd1da6c0bc7e908640b12cd1a07388001a5aec /test/unit/project_nested_set_test.rb
parent88d65c514927094e348010d31fa17fcd2a60bd69 (diff)
downloadredmine-306a47023ce13d5c18d11470bf1ba7df0b1317e8.tar.gz
redmine-306a47023ce13d5c18d11470bf1ba7df0b1317e8.zip
Additional unit tests for project tree.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4650 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'test/unit/project_nested_set_test.rb')
-rw-r--r--test/unit/project_nested_set_test.rb126
1 files changed, 93 insertions, 33 deletions
diff --git a/test/unit/project_nested_set_test.rb b/test/unit/project_nested_set_test.rb
index d6bc528a9..9e9666529 100644
--- a/test/unit/project_nested_set_test.rb
+++ b/test/unit/project_nested_set_test.rb
@@ -19,40 +19,100 @@ require File.expand_path('../../test_helper', __FILE__)
class ProjectNestedSetTest < ActiveSupport::TestCase
- def setup
- Project.delete_all
- end
-
- def test_destroy_root_and_chldren_should_not_mess_up_the_tree
- a = Project.create!(:name => 'Project A', :identifier => 'projecta')
- a1 = Project.create!(:name => 'Project A1', :identifier => 'projecta1')
- a2 = Project.create!(:name => 'Project A2', :identifier => 'projecta2')
- a1.set_parent!(a)
- a2.set_parent!(a)
- b = Project.create!(:name => 'Project B', :identifier => 'projectb')
- b1 = Project.create!(:name => 'Project B1', :identifier => 'projectb1')
- b1.set_parent!(b)
-
- a.reload
- a1.reload
- a2.reload
- b.reload
- b1.reload
-
- assert_equal [nil, 1, 6], [a.parent_id, a.lft, a.rgt]
- assert_equal [a.id, 2, 3], [a1.parent_id, a1.lft, a1.rgt]
- assert_equal [a.id, 4, 5], [a2.parent_id, a2.lft, a2.rgt]
- assert_equal [nil, 7, 10], [b.parent_id, b.lft, b.rgt]
- assert_equal [b.id, 8, 9], [b1.parent_id, b1.lft, b1.rgt]
+ context "nested set" do
+ setup do
+ Project.delete_all
+
+ @a = Project.create!(:name => 'Project A', :identifier => 'projecta')
+ @a1 = Project.create!(:name => 'Project A1', :identifier => 'projecta1')
+ @a1.set_parent!(@a)
+ @a2 = Project.create!(:name => 'Project A2', :identifier => 'projecta2')
+ @a2.set_parent!(@a)
+
+ @b = Project.create!(:name => 'Project B', :identifier => 'projectb')
+ @b1 = Project.create!(:name => 'Project B1', :identifier => 'projectb1')
+ @b1.set_parent!(@b)
+ @b11 = Project.create!(:name => 'Project B11', :identifier => 'projectb11')
+ @b11.set_parent!(@b1)
+ @b2 = Project.create!(:name => 'Project B2', :identifier => 'projectb2')
+ @b2.set_parent!(@b)
+
+ @c = Project.create!(:name => 'Project C', :identifier => 'projectc')
+ @c1 = Project.create!(:name => 'Project C1', :identifier => 'projectc1')
+ @c1.set_parent!(@c)
+
+ [@a, @a1, @a2, @b, @b1, @b11, @b2, @c, @c1].each(&:reload)
+ end
- assert_difference 'Project.count', -3 do
- a.destroy
+ context "#create" do
+ should "build valid tree" do
+ assert_nested_set_values({
+ @a => [nil, 1, 6],
+ @a1 => [@a.id, 2, 3],
+ @a2 => [@a.id, 4, 5],
+ @b => [nil, 7, 14],
+ @b1 => [@b.id, 8, 11],
+ @b11 => [@b1.id,9, 10],
+ @b2 => [@b.id,12, 13],
+ @c => [nil, 15, 18],
+ @c1 => [@c.id,16, 17]
+ })
+ end
end
- b.reload
- b1.reload
-
- assert_equal [nil, 1, 4], [b.parent_id, b.lft, b.rgt]
- assert_equal [b.id, 2, 3], [b1.parent_id, b1.lft, b1.rgt]
+ context "#set_parent!" do
+ should "keep valid tree" do
+ assert_no_difference 'Project.count' do
+ Project.find_by_name('Project B1').set_parent!(Project.find_by_name('Project A2'))
+ end
+ assert_nested_set_values({
+ @a => [nil, 1, 10],
+ @a2 => [@a.id, 4, 9],
+ @b1 => [@a2.id,5, 8],
+ @b11 => [@b1.id,6, 7],
+ @b => [nil, 11, 14],
+ @c => [nil, 15, 18]
+ })
+ end
+ end
+
+ context "#destroy" do
+ context "a root with children" do
+ should "not mess up the tree" do
+ assert_difference 'Project.count', -4 do
+ Project.find_by_name('Project B').destroy
+ end
+ assert_nested_set_values({
+ @a => [nil, 1, 6],
+ @a1 => [@a.id, 2, 3],
+ @a2 => [@a.id, 4, 5],
+ @c => [nil, 7, 10],
+ @c1 => [@c.id, 8, 9]
+ })
+ end
+ end
+
+ context "a child with children" do
+ should "not mess up the tree" do
+ assert_difference 'Project.count', -2 do
+ Project.find_by_name('Project B1').destroy
+ end
+ assert_nested_set_values({
+ @a => [nil, 1, 6],
+ @b => [nil, 7, 10],
+ @b2 => [@b.id, 8, 9],
+ @c => [nil, 11, 14]
+ })
+ end
+ end
+ end
+ end
+
+ def assert_nested_set_values(h)
+ assert Project.valid?
+ h.each do |project, expected|
+ project.reload
+ assert_equal expected, [project.parent_id, project.lft, project.rgt], "Unexpected nested set values for #{project.name}"
+ end
end
-end \ No newline at end of file
+end