annotate .svn/pristine/50/50daa16908a24d5782527b2a9378dbefcd260eae.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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