Chris@0: # Redmine - project management software Chris@0: # Copyright (C) 2006-2010 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@117: require File.expand_path('../../test_helper', __FILE__) Chris@0: Chris@0: class ProjectNestedSetTest < ActiveSupport::TestCase Chris@0: Chris@117: context "nested set" do Chris@117: setup do Chris@117: Project.delete_all Chris@117: Chris@117: @a = Project.create!(:name => 'Project A', :identifier => 'projecta') Chris@117: @a1 = Project.create!(:name => 'Project A1', :identifier => 'projecta1') Chris@117: @a1.set_parent!(@a) Chris@117: @a2 = Project.create!(:name => 'Project A2', :identifier => 'projecta2') Chris@117: @a2.set_parent!(@a) Chris@117: Chris@117: @b = Project.create!(:name => 'Project B', :identifier => 'projectb') Chris@117: @b1 = Project.create!(:name => 'Project B1', :identifier => 'projectb1') Chris@117: @b1.set_parent!(@b) Chris@117: @b11 = Project.create!(:name => 'Project B11', :identifier => 'projectb11') Chris@117: @b11.set_parent!(@b1) Chris@117: @b2 = Project.create!(:name => 'Project B2', :identifier => 'projectb2') Chris@117: @b2.set_parent!(@b) Chris@117: Chris@117: @c = Project.create!(:name => 'Project C', :identifier => 'projectc') Chris@117: @c1 = Project.create!(:name => 'Project C1', :identifier => 'projectc1') Chris@117: @c1.set_parent!(@c) Chris@117: Chris@117: [@a, @a1, @a2, @b, @b1, @b11, @b2, @c, @c1].each(&:reload) Chris@117: end Chris@117: Chris@117: context "#create" do Chris@117: should "build valid tree" do Chris@117: assert_nested_set_values({ Chris@117: @a => [nil, 1, 6], Chris@117: @a1 => [@a.id, 2, 3], Chris@117: @a2 => [@a.id, 4, 5], Chris@117: @b => [nil, 7, 14], Chris@117: @b1 => [@b.id, 8, 11], Chris@117: @b11 => [@b1.id,9, 10], Chris@117: @b2 => [@b.id,12, 13], Chris@117: @c => [nil, 15, 18], Chris@117: @c1 => [@c.id,16, 17] Chris@117: }) Chris@117: end Chris@117: end Chris@117: Chris@117: context "#set_parent!" do Chris@117: should "keep valid tree" do Chris@117: assert_no_difference 'Project.count' do Chris@117: Project.find_by_name('Project B1').set_parent!(Project.find_by_name('Project A2')) Chris@117: end Chris@117: assert_nested_set_values({ Chris@117: @a => [nil, 1, 10], Chris@117: @a2 => [@a.id, 4, 9], Chris@117: @b1 => [@a2.id,5, 8], Chris@117: @b11 => [@b1.id,6, 7], Chris@117: @b => [nil, 11, 14], Chris@117: @c => [nil, 15, 18] Chris@117: }) Chris@117: end Chris@117: end Chris@117: Chris@117: context "#destroy" do Chris@117: context "a root with children" do Chris@117: should "not mess up the tree" do Chris@117: assert_difference 'Project.count', -4 do Chris@117: Project.find_by_name('Project B').destroy Chris@117: end Chris@117: assert_nested_set_values({ Chris@117: @a => [nil, 1, 6], Chris@117: @a1 => [@a.id, 2, 3], Chris@117: @a2 => [@a.id, 4, 5], Chris@117: @c => [nil, 7, 10], Chris@117: @c1 => [@c.id, 8, 9] Chris@117: }) Chris@117: end Chris@117: end Chris@117: Chris@117: context "a child with children" do Chris@117: should "not mess up the tree" do Chris@117: assert_difference 'Project.count', -2 do Chris@117: Project.find_by_name('Project B1').destroy Chris@117: end Chris@117: assert_nested_set_values({ Chris@117: @a => [nil, 1, 6], Chris@117: @b => [nil, 7, 10], Chris@117: @b2 => [@b.id, 8, 9], Chris@117: @c => [nil, 11, 14] Chris@117: }) Chris@117: end Chris@117: end Chris@117: end Chris@0: end Chris@0: Chris@117: def assert_nested_set_values(h) Chris@117: assert Project.valid? Chris@117: h.each do |project, expected| Chris@117: project.reload Chris@117: assert_equal expected, [project.parent_id, project.lft, project.rgt], "Unexpected nested set values for #{project.name}" Chris@0: end Chris@0: end Chris@117: end