@@ -1,118 +1,118 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 |
# Copyright (C) 2006-201 |
|
2 | # Copyright (C) 2006-2011 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 |
# |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 |
# |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require File.expand_path('../../test_helper', __FILE__) |
|
18 | require File.expand_path('../../test_helper', __FILE__) | |
19 |
|
19 | |||
20 | class ProjectNestedSetTest < ActiveSupport::TestCase |
|
20 | class ProjectNestedSetTest < ActiveSupport::TestCase | |
21 |
|
21 | |||
22 | context "nested set" do |
|
22 | context "nested set" do | |
23 | setup do |
|
23 | setup do | |
24 | Project.delete_all |
|
24 | Project.delete_all | |
25 |
|
25 | |||
26 | @a = Project.create!(:name => 'Project A', :identifier => 'projecta') |
|
26 | @a = Project.create!(:name => 'Project A', :identifier => 'projecta') | |
27 | @a1 = Project.create!(:name => 'Project A1', :identifier => 'projecta1') |
|
27 | @a1 = Project.create!(:name => 'Project A1', :identifier => 'projecta1') | |
28 | @a1.set_parent!(@a) |
|
28 | @a1.set_parent!(@a) | |
29 | @a2 = Project.create!(:name => 'Project A2', :identifier => 'projecta2') |
|
29 | @a2 = Project.create!(:name => 'Project A2', :identifier => 'projecta2') | |
30 | @a2.set_parent!(@a) |
|
30 | @a2.set_parent!(@a) | |
31 |
|
31 | |||
32 | @b = Project.create!(:name => 'Project B', :identifier => 'projectb') |
|
32 | @b = Project.create!(:name => 'Project B', :identifier => 'projectb') | |
33 | @b1 = Project.create!(:name => 'Project B1', :identifier => 'projectb1') |
|
33 | @b1 = Project.create!(:name => 'Project B1', :identifier => 'projectb1') | |
34 | @b1.set_parent!(@b) |
|
34 | @b1.set_parent!(@b) | |
35 | @b11 = Project.create!(:name => 'Project B11', :identifier => 'projectb11') |
|
35 | @b11 = Project.create!(:name => 'Project B11', :identifier => 'projectb11') | |
36 | @b11.set_parent!(@b1) |
|
36 | @b11.set_parent!(@b1) | |
37 | @b2 = Project.create!(:name => 'Project B2', :identifier => 'projectb2') |
|
37 | @b2 = Project.create!(:name => 'Project B2', :identifier => 'projectb2') | |
38 | @b2.set_parent!(@b) |
|
38 | @b2.set_parent!(@b) | |
39 |
|
39 | |||
40 | @c = Project.create!(:name => 'Project C', :identifier => 'projectc') |
|
40 | @c = Project.create!(:name => 'Project C', :identifier => 'projectc') | |
41 | @c1 = Project.create!(:name => 'Project C1', :identifier => 'projectc1') |
|
41 | @c1 = Project.create!(:name => 'Project C1', :identifier => 'projectc1') | |
42 | @c1.set_parent!(@c) |
|
42 | @c1.set_parent!(@c) | |
43 |
|
43 | |||
44 | [@a, @a1, @a2, @b, @b1, @b11, @b2, @c, @c1].each(&:reload) |
|
44 | [@a, @a1, @a2, @b, @b1, @b11, @b2, @c, @c1].each(&:reload) | |
45 | end |
|
45 | end | |
46 |
|
46 | |||
47 | context "#create" do |
|
47 | context "#create" do | |
48 | should "build valid tree" do |
|
48 | should "build valid tree" do | |
49 | assert_nested_set_values({ |
|
49 | assert_nested_set_values({ | |
50 | @a => [nil, 1, 6], |
|
50 | @a => [nil, 1, 6], | |
51 | @a1 => [@a.id, 2, 3], |
|
51 | @a1 => [@a.id, 2, 3], | |
52 | @a2 => [@a.id, 4, 5], |
|
52 | @a2 => [@a.id, 4, 5], | |
53 | @b => [nil, 7, 14], |
|
53 | @b => [nil, 7, 14], | |
54 | @b1 => [@b.id, 8, 11], |
|
54 | @b1 => [@b.id, 8, 11], | |
55 | @b11 => [@b1.id,9, 10], |
|
55 | @b11 => [@b1.id,9, 10], | |
56 | @b2 => [@b.id,12, 13], |
|
56 | @b2 => [@b.id,12, 13], | |
57 | @c => [nil, 15, 18], |
|
57 | @c => [nil, 15, 18], | |
58 | @c1 => [@c.id,16, 17] |
|
58 | @c1 => [@c.id,16, 17] | |
59 | }) |
|
59 | }) | |
60 | end |
|
60 | end | |
61 | end |
|
61 | end | |
62 |
|
62 | |||
63 | context "#set_parent!" do |
|
63 | context "#set_parent!" do | |
64 | should "keep valid tree" do |
|
64 | should "keep valid tree" do | |
65 | assert_no_difference 'Project.count' do |
|
65 | assert_no_difference 'Project.count' do | |
66 | Project.find_by_name('Project B1').set_parent!(Project.find_by_name('Project A2')) |
|
66 | Project.find_by_name('Project B1').set_parent!(Project.find_by_name('Project A2')) | |
67 | end |
|
67 | end | |
68 | assert_nested_set_values({ |
|
68 | assert_nested_set_values({ | |
69 | @a => [nil, 1, 10], |
|
69 | @a => [nil, 1, 10], | |
70 | @a2 => [@a.id, 4, 9], |
|
70 | @a2 => [@a.id, 4, 9], | |
71 | @b1 => [@a2.id,5, 8], |
|
71 | @b1 => [@a2.id,5, 8], | |
72 | @b11 => [@b1.id,6, 7], |
|
72 | @b11 => [@b1.id,6, 7], | |
73 | @b => [nil, 11, 14], |
|
73 | @b => [nil, 11, 14], | |
74 | @c => [nil, 15, 18] |
|
74 | @c => [nil, 15, 18] | |
75 | }) |
|
75 | }) | |
76 | end |
|
76 | end | |
77 | end |
|
77 | end | |
78 |
|
78 | |||
79 | context "#destroy" do |
|
79 | context "#destroy" do | |
80 | context "a root with children" do |
|
80 | context "a root with children" do | |
81 | should "not mess up the tree" do |
|
81 | should "not mess up the tree" do | |
82 | assert_difference 'Project.count', -4 do |
|
82 | assert_difference 'Project.count', -4 do | |
83 | Project.find_by_name('Project B').destroy |
|
83 | Project.find_by_name('Project B').destroy | |
84 | end |
|
84 | end | |
85 | assert_nested_set_values({ |
|
85 | assert_nested_set_values({ | |
86 | @a => [nil, 1, 6], |
|
86 | @a => [nil, 1, 6], | |
87 | @a1 => [@a.id, 2, 3], |
|
87 | @a1 => [@a.id, 2, 3], | |
88 | @a2 => [@a.id, 4, 5], |
|
88 | @a2 => [@a.id, 4, 5], | |
89 | @c => [nil, 7, 10], |
|
89 | @c => [nil, 7, 10], | |
90 | @c1 => [@c.id, 8, 9] |
|
90 | @c1 => [@c.id, 8, 9] | |
91 | }) |
|
91 | }) | |
92 | end |
|
92 | end | |
93 | end |
|
93 | end | |
94 |
|
94 | |||
95 | context "a child with children" do |
|
95 | context "a child with children" do | |
96 | should "not mess up the tree" do |
|
96 | should "not mess up the tree" do | |
97 | assert_difference 'Project.count', -2 do |
|
97 | assert_difference 'Project.count', -2 do | |
98 | Project.find_by_name('Project B1').destroy |
|
98 | Project.find_by_name('Project B1').destroy | |
99 | end |
|
99 | end | |
100 | assert_nested_set_values({ |
|
100 | assert_nested_set_values({ | |
101 | @a => [nil, 1, 6], |
|
101 | @a => [nil, 1, 6], | |
102 | @b => [nil, 7, 10], |
|
102 | @b => [nil, 7, 10], | |
103 | @b2 => [@b.id, 8, 9], |
|
103 | @b2 => [@b.id, 8, 9], | |
104 | @c => [nil, 11, 14] |
|
104 | @c => [nil, 11, 14] | |
105 | }) |
|
105 | }) | |
106 | end |
|
106 | end | |
107 | end |
|
107 | end | |
108 | end |
|
108 | end | |
109 | end |
|
109 | end | |
110 |
|
110 | |||
111 | def assert_nested_set_values(h) |
|
111 | def assert_nested_set_values(h) | |
112 | assert Project.valid? |
|
112 | assert Project.valid? | |
113 | h.each do |project, expected| |
|
113 | h.each do |project, expected| | |
114 | project.reload |
|
114 | project.reload | |
115 | assert_equal expected, [project.parent_id, project.lft, project.rgt], "Unexpected nested set values for #{project.name}" |
|
115 | assert_equal expected, [project.parent_id, project.lft, project.rgt], "Unexpected nested set values for #{project.name}" | |
116 | end |
|
116 | end | |
117 | end |
|
117 | end | |
118 | end |
|
118 | end |
General Comments 0
You need to be logged in to leave comments.
Login now