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