annotate .svn/pristine/c7/c76b4386c82424294b50c9641cdeb22f3fba841c.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 require File.expand_path('../../test_helper', __FILE__)
Chris@1295 19
Chris@1295 20 class ProjectNestedSetTest < ActiveSupport::TestCase
Chris@1295 21
Chris@1295 22 def setup
Chris@1295 23 Project.delete_all
Chris@1295 24
Chris@1295 25 @a = Project.create!(:name => 'A', :identifier => 'projecta')
Chris@1295 26 @a1 = Project.create!(:name => 'A1', :identifier => 'projecta1')
Chris@1295 27 @a1.set_parent!(@a)
Chris@1295 28 @a2 = Project.create!(:name => 'A2', :identifier => 'projecta2')
Chris@1295 29 @a2.set_parent!(@a)
Chris@1295 30
Chris@1295 31 @c = Project.create!(:name => 'C', :identifier => 'projectc')
Chris@1295 32 @c1 = Project.create!(:name => 'C1', :identifier => 'projectc1')
Chris@1295 33 @c1.set_parent!(@c)
Chris@1295 34
Chris@1295 35 @b = Project.create!(:name => 'B', :identifier => 'projectb')
Chris@1295 36 @b2 = Project.create!(:name => 'B2', :identifier => 'projectb2')
Chris@1295 37 @b2.set_parent!(@b)
Chris@1295 38 @b1 = Project.create!(:name => 'B1', :identifier => 'projectb1')
Chris@1295 39 @b1.set_parent!(@b)
Chris@1295 40 @b11 = Project.create!(:name => 'B11', :identifier => 'projectb11')
Chris@1295 41 @b11.set_parent!(@b1)
Chris@1295 42
Chris@1295 43 @a, @a1, @a2, @b, @b1, @b11, @b2, @c, @c1 = *(Project.all.sort_by(&:name))
Chris@1295 44 end
Chris@1295 45
Chris@1295 46 def test_valid_tree
Chris@1295 47 assert_valid_nested_set
Chris@1295 48 end
Chris@1295 49
Chris@1295 50 def test_rebuild_should_build_valid_tree
Chris@1295 51 Project.update_all "lft = NULL, rgt = NULL"
Chris@1295 52
Chris@1295 53 Project.rebuild!
Chris@1295 54 assert_valid_nested_set
Chris@1295 55 end
Chris@1295 56
Chris@1295 57 def test_rebuild_tree_should_build_valid_tree_even_with_valid_lft_rgt_values
Chris@1295 58 Project.update_all "name = 'YY'", {:id => @a.id }
Chris@1295 59 # lft and rgt values are still valid (Project.rebuild! would not update anything)
Chris@1295 60 # but projects are not ordered properly (YY is in the first place)
Chris@1295 61
Chris@1295 62 Project.rebuild_tree!
Chris@1295 63 assert_valid_nested_set
Chris@1295 64 end
Chris@1295 65
Chris@1295 66 def test_moving_a_child_to_a_different_parent_should_keep_valid_tree
Chris@1295 67 assert_no_difference 'Project.count' do
Chris@1295 68 Project.find_by_name('B1').set_parent!(Project.find_by_name('A2'))
Chris@1295 69 end
Chris@1295 70 assert_valid_nested_set
Chris@1295 71 end
Chris@1295 72
Chris@1295 73 def test_renaming_a_root_to_first_position_should_update_nested_set_order
Chris@1295 74 @c.name = '1'
Chris@1295 75 @c.save!
Chris@1295 76 assert_valid_nested_set
Chris@1295 77 end
Chris@1295 78
Chris@1295 79 def test_renaming_a_root_to_middle_position_should_update_nested_set_order
Chris@1295 80 @a.name = 'BA'
Chris@1295 81 @a.save!
Chris@1295 82 assert_valid_nested_set
Chris@1295 83 end
Chris@1295 84
Chris@1295 85 def test_renaming_a_root_to_last_position_should_update_nested_set_order
Chris@1295 86 @a.name = 'D'
Chris@1295 87 @a.save!
Chris@1295 88 assert_valid_nested_set
Chris@1295 89 end
Chris@1295 90
Chris@1295 91 def test_renaming_a_root_to_same_position_should_update_nested_set_order
Chris@1295 92 @c.name = 'D'
Chris@1295 93 @c.save!
Chris@1295 94 assert_valid_nested_set
Chris@1295 95 end
Chris@1295 96
Chris@1295 97 def test_renaming_a_child_should_update_nested_set_order
Chris@1295 98 @a1.name = 'A3'
Chris@1295 99 @a1.save!
Chris@1295 100 assert_valid_nested_set
Chris@1295 101 end
Chris@1295 102
Chris@1295 103 def test_renaming_a_child_with_child_should_update_nested_set_order
Chris@1295 104 @b1.name = 'B3'
Chris@1295 105 @b1.save!
Chris@1295 106 assert_valid_nested_set
Chris@1295 107 end
Chris@1295 108
Chris@1295 109 def test_adding_a_root_to_first_position_should_update_nested_set_order
Chris@1295 110 project = Project.create!(:name => '1', :identifier => 'projectba')
Chris@1295 111 assert_valid_nested_set
Chris@1295 112 end
Chris@1295 113
Chris@1295 114 def test_adding_a_root_to_middle_position_should_update_nested_set_order
Chris@1295 115 project = Project.create!(:name => 'BA', :identifier => 'projectba')
Chris@1295 116 assert_valid_nested_set
Chris@1295 117 end
Chris@1295 118
Chris@1295 119 def test_adding_a_root_to_last_position_should_update_nested_set_order
Chris@1295 120 project = Project.create!(:name => 'Z', :identifier => 'projectba')
Chris@1295 121 assert_valid_nested_set
Chris@1295 122 end
Chris@1295 123
Chris@1295 124 def test_destroying_a_root_with_children_should_keep_valid_tree
Chris@1295 125 assert_difference 'Project.count', -4 do
Chris@1295 126 Project.find_by_name('B').destroy
Chris@1295 127 end
Chris@1295 128 assert_valid_nested_set
Chris@1295 129 end
Chris@1295 130
Chris@1295 131 def test_destroying_a_child_with_children_should_keep_valid_tree
Chris@1295 132 assert_difference 'Project.count', -2 do
Chris@1295 133 Project.find_by_name('B1').destroy
Chris@1295 134 end
Chris@1295 135 assert_valid_nested_set
Chris@1295 136 end
Chris@1295 137
Chris@1295 138 private
Chris@1295 139
Chris@1295 140 def assert_nested_set_values(h)
Chris@1295 141 assert Project.valid?
Chris@1295 142 h.each do |project, expected|
Chris@1295 143 project.reload
Chris@1295 144 assert_equal expected, [project.parent_id, project.lft, project.rgt], "Unexpected nested set values for #{project.name}"
Chris@1295 145 end
Chris@1295 146 end
Chris@1295 147
Chris@1295 148 def assert_valid_nested_set
Chris@1295 149 projects = Project.all
Chris@1295 150 lft_rgt = projects.map {|p| [p.lft, p.rgt]}.flatten
Chris@1295 151 assert_equal projects.size * 2, lft_rgt.uniq.size
Chris@1295 152 assert_equal 1, lft_rgt.min
Chris@1295 153 assert_equal projects.size * 2, lft_rgt.max
Chris@1295 154
Chris@1295 155 projects.each do |project|
Chris@1295 156 # lft should always be < rgt
Chris@1295 157 assert project.lft < project.rgt, "lft=#{project.lft} was not < rgt=#{project.rgt} for project #{project.name}"
Chris@1295 158 if project.parent_id
Chris@1295 159 # child lft/rgt values must be greater/lower
Chris@1295 160 assert_not_nil project.parent, "parent was nil for project #{project.name}"
Chris@1295 161 assert project.lft > project.parent.lft, "lft=#{project.lft} was not > parent.lft=#{project.parent.lft} for project #{project.name}"
Chris@1295 162 assert project.rgt < project.parent.rgt, "rgt=#{project.rgt} was not < parent.rgt=#{project.parent.rgt} for project #{project.name}"
Chris@1295 163 end
Chris@1295 164 # no overlapping lft/rgt values
Chris@1295 165 overlapping = projects.detect {|other|
Chris@1295 166 other != project && (
Chris@1295 167 (other.lft > project.lft && other.lft < project.rgt && other.rgt > project.rgt) ||
Chris@1295 168 (other.rgt > project.lft && other.rgt < project.rgt && other.lft < project.lft)
Chris@1295 169 )
Chris@1295 170 }
Chris@1295 171 assert_nil overlapping, (overlapping && "Project #{overlapping.name} (#{overlapping.lft}/#{overlapping.rgt}) overlapped #{project.name} (#{project.lft}/#{project.rgt})")
Chris@1295 172 end
Chris@1295 173
Chris@1295 174 # root projects sorted alphabetically
Chris@1295 175 assert_equal Project.roots.map(&:name).sort, Project.roots.sort_by(&:lft).map(&:name), "Root projects were not properly sorted"
Chris@1295 176 projects.each do |project|
Chris@1295 177 if project.children.any?
Chris@1295 178 # sibling projects sorted alphabetically
Chris@1295 179 assert_equal project.children.map(&:name).sort, project.children.order('lft').map(&:name), "Project #{project.name}'s children were not properly sorted"
Chris@1295 180 end
Chris@1295 181 end
Chris@1295 182 end
Chris@1295 183 end