annotate test/unit/project_nested_set_test.rb @ 1471:d65e60e20a50 bug_531

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