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