You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

project_nested_set_test.rb 3.9KB

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