annotate test/unit/project_nested_set_test.rb @ 1452:d6b9fd02bb89 feature_36_js_refactoring

Deprecated develoment branch.
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Fri, 11 Oct 2013 17:01:24 +0100
parents af80e5618e9b
children cbb26bc654de
rev   line source
Chris@0 1 # Redmine - project management software
Chris@0 2 # Copyright (C) 2006-2010 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@0 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@0 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@117 18 require File.expand_path('../../test_helper', __FILE__)
Chris@0 19
Chris@0 20 class ProjectNestedSetTest < ActiveSupport::TestCase
Chris@0 21
Chris@117 22 context "nested set" do
Chris@117 23 setup do
Chris@117 24 Project.delete_all
Chris@117 25
Chris@117 26 @a = Project.create!(:name => 'Project A', :identifier => 'projecta')
Chris@117 27 @a1 = Project.create!(:name => 'Project A1', :identifier => 'projecta1')
Chris@117 28 @a1.set_parent!(@a)
Chris@117 29 @a2 = Project.create!(:name => 'Project A2', :identifier => 'projecta2')
Chris@117 30 @a2.set_parent!(@a)
Chris@117 31
Chris@117 32 @b = Project.create!(:name => 'Project B', :identifier => 'projectb')
Chris@117 33 @b1 = Project.create!(:name => 'Project B1', :identifier => 'projectb1')
Chris@117 34 @b1.set_parent!(@b)
Chris@117 35 @b11 = Project.create!(:name => 'Project B11', :identifier => 'projectb11')
Chris@117 36 @b11.set_parent!(@b1)
Chris@117 37 @b2 = Project.create!(:name => 'Project B2', :identifier => 'projectb2')
Chris@117 38 @b2.set_parent!(@b)
Chris@117 39
Chris@117 40 @c = Project.create!(:name => 'Project C', :identifier => 'projectc')
Chris@117 41 @c1 = Project.create!(:name => 'Project C1', :identifier => 'projectc1')
Chris@117 42 @c1.set_parent!(@c)
Chris@117 43
Chris@117 44 [@a, @a1, @a2, @b, @b1, @b11, @b2, @c, @c1].each(&:reload)
Chris@117 45 end
Chris@117 46
Chris@117 47 context "#create" do
Chris@117 48 should "build valid tree" do
Chris@117 49 assert_nested_set_values({
Chris@117 50 @a => [nil, 1, 6],
Chris@117 51 @a1 => [@a.id, 2, 3],
Chris@117 52 @a2 => [@a.id, 4, 5],
Chris@117 53 @b => [nil, 7, 14],
Chris@117 54 @b1 => [@b.id, 8, 11],
Chris@117 55 @b11 => [@b1.id,9, 10],
Chris@117 56 @b2 => [@b.id,12, 13],
Chris@117 57 @c => [nil, 15, 18],
Chris@117 58 @c1 => [@c.id,16, 17]
Chris@117 59 })
Chris@117 60 end
Chris@117 61 end
Chris@117 62
Chris@117 63 context "#set_parent!" do
Chris@117 64 should "keep valid tree" do
Chris@117 65 assert_no_difference 'Project.count' do
Chris@117 66 Project.find_by_name('Project B1').set_parent!(Project.find_by_name('Project A2'))
Chris@117 67 end
Chris@117 68 assert_nested_set_values({
Chris@117 69 @a => [nil, 1, 10],
Chris@117 70 @a2 => [@a.id, 4, 9],
Chris@117 71 @b1 => [@a2.id,5, 8],
Chris@117 72 @b11 => [@b1.id,6, 7],
Chris@117 73 @b => [nil, 11, 14],
Chris@117 74 @c => [nil, 15, 18]
Chris@117 75 })
Chris@117 76 end
Chris@117 77 end
Chris@117 78
Chris@117 79 context "#destroy" do
Chris@117 80 context "a root with children" do
Chris@117 81 should "not mess up the tree" do
Chris@117 82 assert_difference 'Project.count', -4 do
Chris@117 83 Project.find_by_name('Project B').destroy
Chris@117 84 end
Chris@117 85 assert_nested_set_values({
Chris@117 86 @a => [nil, 1, 6],
Chris@117 87 @a1 => [@a.id, 2, 3],
Chris@117 88 @a2 => [@a.id, 4, 5],
Chris@117 89 @c => [nil, 7, 10],
Chris@117 90 @c1 => [@c.id, 8, 9]
Chris@117 91 })
Chris@117 92 end
Chris@117 93 end
Chris@117 94
Chris@117 95 context "a child with children" do
Chris@117 96 should "not mess up the tree" do
Chris@117 97 assert_difference 'Project.count', -2 do
Chris@117 98 Project.find_by_name('Project B1').destroy
Chris@117 99 end
Chris@117 100 assert_nested_set_values({
Chris@117 101 @a => [nil, 1, 6],
Chris@117 102 @b => [nil, 7, 10],
Chris@117 103 @b2 => [@b.id, 8, 9],
Chris@117 104 @c => [nil, 11, 14]
Chris@117 105 })
Chris@117 106 end
Chris@117 107 end
Chris@117 108 end
Chris@0 109 end
Chris@0 110
Chris@117 111 def assert_nested_set_values(h)
Chris@117 112 assert Project.valid?
Chris@117 113 h.each do |project, expected|
Chris@117 114 project.reload
Chris@117 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@117 118 end