annotate lib/plugins/acts_as_tree/test/acts_as_tree_test.rb @ 1517:dffacf8a6908 redmine-2.5

Update to Redmine SVN revision 13367 on 2.5-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:29:00 +0100
parents 433d4f72a19b
children
rev   line source
Chris@0 1 require 'test/unit'
Chris@0 2
Chris@0 3 require 'rubygems'
Chris@0 4 require 'active_record'
Chris@0 5
Chris@0 6 $:.unshift File.dirname(__FILE__) + '/../lib'
Chris@0 7 require File.dirname(__FILE__) + '/../init'
Chris@0 8
Chris@0 9 class Test::Unit::TestCase
Chris@0 10 def assert_queries(num = 1)
Chris@0 11 $query_count = 0
Chris@0 12 yield
Chris@0 13 ensure
Chris@0 14 assert_equal num, $query_count, "#{$query_count} instead of #{num} queries were executed."
Chris@0 15 end
Chris@0 16
Chris@0 17 def assert_no_queries(&block)
Chris@0 18 assert_queries(0, &block)
Chris@0 19 end
Chris@0 20 end
Chris@0 21
Chris@0 22 ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
Chris@0 23
Chris@0 24 # AR keeps printing annoying schema statements
Chris@0 25 $stdout = StringIO.new
Chris@0 26
Chris@0 27 def setup_db
Chris@0 28 ActiveRecord::Base.logger
Chris@0 29 ActiveRecord::Schema.define(:version => 1) do
Chris@0 30 create_table :mixins do |t|
Chris@0 31 t.column :type, :string
Chris@0 32 t.column :parent_id, :integer
Chris@0 33 end
Chris@0 34 end
Chris@0 35 end
Chris@0 36
Chris@0 37 def teardown_db
Chris@0 38 ActiveRecord::Base.connection.tables.each do |table|
Chris@0 39 ActiveRecord::Base.connection.drop_table(table)
Chris@0 40 end
Chris@0 41 end
Chris@0 42
Chris@0 43 class Mixin < ActiveRecord::Base
Chris@0 44 end
Chris@0 45
Chris@0 46 class TreeMixin < Mixin
Chris@0 47 acts_as_tree :foreign_key => "parent_id", :order => "id"
Chris@0 48 end
Chris@0 49
Chris@0 50 class TreeMixinWithoutOrder < Mixin
Chris@0 51 acts_as_tree :foreign_key => "parent_id"
Chris@0 52 end
Chris@0 53
Chris@0 54 class RecursivelyCascadedTreeMixin < Mixin
Chris@0 55 acts_as_tree :foreign_key => "parent_id"
Chris@0 56 has_one :first_child, :class_name => 'RecursivelyCascadedTreeMixin', :foreign_key => :parent_id
Chris@0 57 end
Chris@0 58
Chris@0 59 class TreeTest < Test::Unit::TestCase
Chris@0 60
Chris@0 61 def setup
Chris@0 62 setup_db
Chris@0 63 @root1 = TreeMixin.create!
Chris@0 64 @root_child1 = TreeMixin.create! :parent_id => @root1.id
Chris@0 65 @child1_child = TreeMixin.create! :parent_id => @root_child1.id
Chris@0 66 @root_child2 = TreeMixin.create! :parent_id => @root1.id
Chris@0 67 @root2 = TreeMixin.create!
Chris@0 68 @root3 = TreeMixin.create!
Chris@0 69 end
Chris@0 70
Chris@0 71 def teardown
Chris@0 72 teardown_db
Chris@0 73 end
Chris@0 74
Chris@0 75 def test_children
Chris@0 76 assert_equal @root1.children, [@root_child1, @root_child2]
Chris@0 77 assert_equal @root_child1.children, [@child1_child]
Chris@0 78 assert_equal @child1_child.children, []
Chris@0 79 assert_equal @root_child2.children, []
Chris@0 80 end
Chris@0 81
Chris@0 82 def test_parent
Chris@0 83 assert_equal @root_child1.parent, @root1
Chris@0 84 assert_equal @root_child1.parent, @root_child2.parent
Chris@0 85 assert_nil @root1.parent
Chris@0 86 end
Chris@0 87
Chris@0 88 def test_delete
Chris@0 89 assert_equal 6, TreeMixin.count
Chris@0 90 @root1.destroy
Chris@0 91 assert_equal 2, TreeMixin.count
Chris@0 92 @root2.destroy
Chris@0 93 @root3.destroy
Chris@0 94 assert_equal 0, TreeMixin.count
Chris@0 95 end
Chris@0 96
Chris@0 97 def test_insert
Chris@0 98 @extra = @root1.children.create
Chris@0 99
Chris@0 100 assert @extra
Chris@0 101
Chris@0 102 assert_equal @extra.parent, @root1
Chris@0 103
Chris@0 104 assert_equal 3, @root1.children.size
Chris@0 105 assert @root1.children.include?(@extra)
Chris@0 106 assert @root1.children.include?(@root_child1)
Chris@0 107 assert @root1.children.include?(@root_child2)
Chris@0 108 end
Chris@0 109
Chris@0 110 def test_ancestors
Chris@0 111 assert_equal [], @root1.ancestors
Chris@0 112 assert_equal [@root1], @root_child1.ancestors
Chris@0 113 assert_equal [@root_child1, @root1], @child1_child.ancestors
Chris@0 114 assert_equal [@root1], @root_child2.ancestors
Chris@0 115 assert_equal [], @root2.ancestors
Chris@0 116 assert_equal [], @root3.ancestors
Chris@0 117 end
Chris@0 118
Chris@0 119 def test_root
Chris@0 120 assert_equal @root1, TreeMixin.root
Chris@0 121 assert_equal @root1, @root1.root
Chris@0 122 assert_equal @root1, @root_child1.root
Chris@0 123 assert_equal @root1, @child1_child.root
Chris@0 124 assert_equal @root1, @root_child2.root
Chris@0 125 assert_equal @root2, @root2.root
Chris@0 126 assert_equal @root3, @root3.root
Chris@0 127 end
Chris@0 128
Chris@0 129 def test_roots
Chris@0 130 assert_equal [@root1, @root2, @root3], TreeMixin.roots
Chris@0 131 end
Chris@0 132
Chris@0 133 def test_siblings
Chris@0 134 assert_equal [@root2, @root3], @root1.siblings
Chris@0 135 assert_equal [@root_child2], @root_child1.siblings
Chris@0 136 assert_equal [], @child1_child.siblings
Chris@0 137 assert_equal [@root_child1], @root_child2.siblings
Chris@0 138 assert_equal [@root1, @root3], @root2.siblings
Chris@0 139 assert_equal [@root1, @root2], @root3.siblings
Chris@0 140 end
Chris@0 141
Chris@0 142 def test_self_and_siblings
Chris@0 143 assert_equal [@root1, @root2, @root3], @root1.self_and_siblings
Chris@0 144 assert_equal [@root_child1, @root_child2], @root_child1.self_and_siblings
Chris@0 145 assert_equal [@child1_child], @child1_child.self_and_siblings
Chris@0 146 assert_equal [@root_child1, @root_child2], @root_child2.self_and_siblings
Chris@0 147 assert_equal [@root1, @root2, @root3], @root2.self_and_siblings
Chris@0 148 assert_equal [@root1, @root2, @root3], @root3.self_and_siblings
Chris@0 149 end
Chris@0 150 end
Chris@0 151
Chris@0 152 class TreeTestWithEagerLoading < Test::Unit::TestCase
Chris@0 153
Chris@0 154 def setup
Chris@0 155 teardown_db
Chris@0 156 setup_db
Chris@0 157 @root1 = TreeMixin.create!
Chris@0 158 @root_child1 = TreeMixin.create! :parent_id => @root1.id
Chris@0 159 @child1_child = TreeMixin.create! :parent_id => @root_child1.id
Chris@0 160 @root_child2 = TreeMixin.create! :parent_id => @root1.id
Chris@0 161 @root2 = TreeMixin.create!
Chris@0 162 @root3 = TreeMixin.create!
Chris@0 163
Chris@0 164 @rc1 = RecursivelyCascadedTreeMixin.create!
Chris@0 165 @rc2 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc1.id
Chris@0 166 @rc3 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc2.id
Chris@0 167 @rc4 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc3.id
Chris@0 168 end
Chris@0 169
Chris@0 170 def teardown
Chris@0 171 teardown_db
Chris@0 172 end
Chris@0 173
Chris@0 174 def test_eager_association_loading
Chris@0 175 roots = TreeMixin.find(:all, :include => :children, :conditions => "mixins.parent_id IS NULL", :order => "mixins.id")
Chris@0 176 assert_equal [@root1, @root2, @root3], roots
Chris@0 177 assert_no_queries do
Chris@0 178 assert_equal 2, roots[0].children.size
Chris@0 179 assert_equal 0, roots[1].children.size
Chris@0 180 assert_equal 0, roots[2].children.size
Chris@0 181 end
Chris@0 182 end
Chris@0 183
Chris@0 184 def test_eager_association_loading_with_recursive_cascading_three_levels_has_many
Chris@0 185 root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :children => { :children => :children } }, :order => 'mixins.id')
Chris@0 186 assert_equal @rc4, assert_no_queries { root_node.children.first.children.first.children.first }
Chris@0 187 end
Chris@0 188
Chris@0 189 def test_eager_association_loading_with_recursive_cascading_three_levels_has_one
Chris@0 190 root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :first_child => { :first_child => :first_child } }, :order => 'mixins.id')
Chris@0 191 assert_equal @rc4, assert_no_queries { root_node.first_child.first_child.first_child }
Chris@0 192 end
Chris@0 193
Chris@0 194 def test_eager_association_loading_with_recursive_cascading_three_levels_belongs_to
Chris@0 195 leaf_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :parent => { :parent => :parent } }, :order => 'mixins.id DESC')
Chris@0 196 assert_equal @rc1, assert_no_queries { leaf_node.parent.parent.parent }
Chris@0 197 end
Chris@0 198 end
Chris@0 199
Chris@0 200 class TreeTestWithoutOrder < Test::Unit::TestCase
Chris@0 201
Chris@0 202 def setup
Chris@0 203 setup_db
Chris@0 204 @root1 = TreeMixinWithoutOrder.create!
Chris@0 205 @root2 = TreeMixinWithoutOrder.create!
Chris@0 206 end
Chris@0 207
Chris@0 208 def teardown
Chris@0 209 teardown_db
Chris@0 210 end
Chris@0 211
Chris@0 212 def test_root
Chris@0 213 assert [@root1, @root2].include?(TreeMixinWithoutOrder.root)
Chris@0 214 end
Chris@0 215
Chris@0 216 def test_roots
Chris@0 217 assert_equal [], [@root1, @root2] - TreeMixinWithoutOrder.roots
Chris@0 218 end
Chris@0 219 end