annotate .svn/pristine/c7/c76b4386c82424294b50c9641cdeb22f3fba841c.svn-base @ 1296:038ba2d95de8 redmine-2.2

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