PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/plugins/acts_as_tree/test/acts_as_tree_test.rb

https://bitbucket.org/sqctest01/redmine
Ruby | 219 lines | 178 code | 40 blank | 1 comment | 0 complexity | 7bd3d48984d0e563b9f5e20ca8fdff81 MD5 | raw file
Possible License(s): GPL-2.0
  1. require 'test/unit'
  2. require 'rubygems'
  3. require 'active_record'
  4. $:.unshift File.dirname(__FILE__) + '/../lib'
  5. require File.dirname(__FILE__) + '/../init'
  6. class Test::Unit::TestCase
  7. def assert_queries(num = 1)
  8. $query_count = 0
  9. yield
  10. ensure
  11. assert_equal num, $query_count, "#{$query_count} instead of #{num} queries were executed."
  12. end
  13. def assert_no_queries(&block)
  14. assert_queries(0, &block)
  15. end
  16. end
  17. ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
  18. # AR keeps printing annoying schema statements
  19. $stdout = StringIO.new
  20. def setup_db
  21. ActiveRecord::Base.logger
  22. ActiveRecord::Schema.define(:version => 1) do
  23. create_table :mixins do |t|
  24. t.column :type, :string
  25. t.column :parent_id, :integer
  26. end
  27. end
  28. end
  29. def teardown_db
  30. ActiveRecord::Base.connection.tables.each do |table|
  31. ActiveRecord::Base.connection.drop_table(table)
  32. end
  33. end
  34. class Mixin < ActiveRecord::Base
  35. end
  36. class TreeMixin < Mixin
  37. acts_as_tree :foreign_key => "parent_id", :order => "id"
  38. end
  39. class TreeMixinWithoutOrder < Mixin
  40. acts_as_tree :foreign_key => "parent_id"
  41. end
  42. class RecursivelyCascadedTreeMixin < Mixin
  43. acts_as_tree :foreign_key => "parent_id"
  44. has_one :first_child, :class_name => 'RecursivelyCascadedTreeMixin', :foreign_key => :parent_id
  45. end
  46. class TreeTest < Test::Unit::TestCase
  47. def setup
  48. setup_db
  49. @root1 = TreeMixin.create!
  50. @root_child1 = TreeMixin.create! :parent_id => @root1.id
  51. @child1_child = TreeMixin.create! :parent_id => @root_child1.id
  52. @root_child2 = TreeMixin.create! :parent_id => @root1.id
  53. @root2 = TreeMixin.create!
  54. @root3 = TreeMixin.create!
  55. end
  56. def teardown
  57. teardown_db
  58. end
  59. def test_children
  60. assert_equal @root1.children, [@root_child1, @root_child2]
  61. assert_equal @root_child1.children, [@child1_child]
  62. assert_equal @child1_child.children, []
  63. assert_equal @root_child2.children, []
  64. end
  65. def test_parent
  66. assert_equal @root_child1.parent, @root1
  67. assert_equal @root_child1.parent, @root_child2.parent
  68. assert_nil @root1.parent
  69. end
  70. def test_delete
  71. assert_equal 6, TreeMixin.count
  72. @root1.destroy
  73. assert_equal 2, TreeMixin.count
  74. @root2.destroy
  75. @root3.destroy
  76. assert_equal 0, TreeMixin.count
  77. end
  78. def test_insert
  79. @extra = @root1.children.create
  80. assert @extra
  81. assert_equal @extra.parent, @root1
  82. assert_equal 3, @root1.children.size
  83. assert @root1.children.include?(@extra)
  84. assert @root1.children.include?(@root_child1)
  85. assert @root1.children.include?(@root_child2)
  86. end
  87. def test_ancestors
  88. assert_equal [], @root1.ancestors
  89. assert_equal [@root1], @root_child1.ancestors
  90. assert_equal [@root_child1, @root1], @child1_child.ancestors
  91. assert_equal [@root1], @root_child2.ancestors
  92. assert_equal [], @root2.ancestors
  93. assert_equal [], @root3.ancestors
  94. end
  95. def test_root
  96. assert_equal @root1, TreeMixin.root
  97. assert_equal @root1, @root1.root
  98. assert_equal @root1, @root_child1.root
  99. assert_equal @root1, @child1_child.root
  100. assert_equal @root1, @root_child2.root
  101. assert_equal @root2, @root2.root
  102. assert_equal @root3, @root3.root
  103. end
  104. def test_roots
  105. assert_equal [@root1, @root2, @root3], TreeMixin.roots
  106. end
  107. def test_siblings
  108. assert_equal [@root2, @root3], @root1.siblings
  109. assert_equal [@root_child2], @root_child1.siblings
  110. assert_equal [], @child1_child.siblings
  111. assert_equal [@root_child1], @root_child2.siblings
  112. assert_equal [@root1, @root3], @root2.siblings
  113. assert_equal [@root1, @root2], @root3.siblings
  114. end
  115. def test_self_and_siblings
  116. assert_equal [@root1, @root2, @root3], @root1.self_and_siblings
  117. assert_equal [@root_child1, @root_child2], @root_child1.self_and_siblings
  118. assert_equal [@child1_child], @child1_child.self_and_siblings
  119. assert_equal [@root_child1, @root_child2], @root_child2.self_and_siblings
  120. assert_equal [@root1, @root2, @root3], @root2.self_and_siblings
  121. assert_equal [@root1, @root2, @root3], @root3.self_and_siblings
  122. end
  123. end
  124. class TreeTestWithEagerLoading < Test::Unit::TestCase
  125. def setup
  126. teardown_db
  127. setup_db
  128. @root1 = TreeMixin.create!
  129. @root_child1 = TreeMixin.create! :parent_id => @root1.id
  130. @child1_child = TreeMixin.create! :parent_id => @root_child1.id
  131. @root_child2 = TreeMixin.create! :parent_id => @root1.id
  132. @root2 = TreeMixin.create!
  133. @root3 = TreeMixin.create!
  134. @rc1 = RecursivelyCascadedTreeMixin.create!
  135. @rc2 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc1.id
  136. @rc3 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc2.id
  137. @rc4 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc3.id
  138. end
  139. def teardown
  140. teardown_db
  141. end
  142. def test_eager_association_loading
  143. roots = TreeMixin.find(:all, :include => :children, :conditions => "mixins.parent_id IS NULL", :order => "mixins.id")
  144. assert_equal [@root1, @root2, @root3], roots
  145. assert_no_queries do
  146. assert_equal 2, roots[0].children.size
  147. assert_equal 0, roots[1].children.size
  148. assert_equal 0, roots[2].children.size
  149. end
  150. end
  151. def test_eager_association_loading_with_recursive_cascading_three_levels_has_many
  152. root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :children => { :children => :children } }, :order => 'mixins.id')
  153. assert_equal @rc4, assert_no_queries { root_node.children.first.children.first.children.first }
  154. end
  155. def test_eager_association_loading_with_recursive_cascading_three_levels_has_one
  156. root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :first_child => { :first_child => :first_child } }, :order => 'mixins.id')
  157. assert_equal @rc4, assert_no_queries { root_node.first_child.first_child.first_child }
  158. end
  159. def test_eager_association_loading_with_recursive_cascading_three_levels_belongs_to
  160. leaf_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :parent => { :parent => :parent } }, :order => 'mixins.id DESC')
  161. assert_equal @rc1, assert_no_queries { leaf_node.parent.parent.parent }
  162. end
  163. end
  164. class TreeTestWithoutOrder < Test::Unit::TestCase
  165. def setup
  166. setup_db
  167. @root1 = TreeMixinWithoutOrder.create!
  168. @root2 = TreeMixinWithoutOrder.create!
  169. end
  170. def teardown
  171. teardown_db
  172. end
  173. def test_root
  174. assert [@root1, @root2].include?(TreeMixinWithoutOrder.root)
  175. end
  176. def test_roots
  177. assert_equal [], [@root1, @root2] - TreeMixinWithoutOrder.roots
  178. end
  179. end