annotate .svn/pristine/38/384178c4f05b1a0eb059a8f4d3cd70f8c0aec17d.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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