comparison .svn/pristine/38/384178c4f05b1a0eb059a8f4d3cd70f8c0aec17d.svn-base @ 909:cbb26bc654de redmine-1.3

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