Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 718 lines (647 sloc) 28.319 kb
34a13a9 Stefan Kroes Fixes for 1.2.1
authored
1 require File.join(File.expand_path(File.dirname(__FILE__)), "environment")
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
2
3 class HasAncestryTreeTest < ActiveSupport::TestCase
1282d2e Stefan Kroes Initial commit
authored
4 def test_default_ancestry_column
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
5 AncestryTestDatabase.with_model do |model|
6 assert_equal :ancestry, model.ancestry_column
7 end
1282d2e Stefan Kroes Initial commit
authored
8 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
9
1282d2e Stefan Kroes Initial commit
authored
10 def test_non_default_ancestry_column
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
11 AncestryTestDatabase.with_model :ancestry_column => :alternative_ancestry do |model|
12 assert_equal :alternative_ancestry, model.ancestry_column
13 end
1282d2e Stefan Kroes Initial commit
authored
14 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
15
1282d2e Stefan Kroes Initial commit
authored
16 def test_setting_ancestry_column
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
17 AncestryTestDatabase.with_model do |model|
18 model.ancestry_column = :ancestors
19 assert_equal :ancestors, model.ancestry_column
20 model.ancestry_column = :ancestry
21 assert_equal :ancestry, model.ancestry_column
22 end
1282d2e Stefan Kroes Initial commit
authored
23 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
24
1282d2e Stefan Kroes Initial commit
authored
25 def test_default_orphan_strategy
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
26 AncestryTestDatabase.with_model do |model|
27 assert_equal :destroy, model.orphan_strategy
28 end
1282d2e Stefan Kroes Initial commit
authored
29 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
30
1282d2e Stefan Kroes Initial commit
authored
31 def test_non_default_orphan_strategy
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
32 AncestryTestDatabase.with_model :orphan_strategy => :rootify do |model|
33 assert_equal :rootify, model.orphan_strategy
34 end
1282d2e Stefan Kroes Initial commit
authored
35 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
36
1282d2e Stefan Kroes Initial commit
authored
37 def test_setting_orphan_strategy
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
38 AncestryTestDatabase.with_model do |model|
39 model.orphan_strategy = :rootify
40 assert_equal :rootify, model.orphan_strategy
41 model.orphan_strategy = :destroy
42 assert_equal :destroy, model.orphan_strategy
43 end
1282d2e Stefan Kroes Initial commit
authored
44 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
45
1282d2e Stefan Kroes Initial commit
authored
46 def test_setting_invalid_orphan_strategy
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
47 AncestryTestDatabase.with_model do |model|
48 assert_raise Ancestry::AncestryException do
49 model.orphan_strategy = :non_existent_orphan_strategy
50 end
1282d2e Stefan Kroes Initial commit
authored
51 end
52 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
53
1282d2e Stefan Kroes Initial commit
authored
54 def test_setup_test_nodes
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
55 AncestryTestDatabase.with_model :depth => 3, :width => 3 do |model, roots|
1282d2e Stefan Kroes Initial commit
authored
56 assert_equal Array, roots.class
57 assert_equal 3, roots.length
58 roots.each do |node, children|
59 assert_equal model, node.class
60 assert_equal Array, children.class
61 assert_equal 3, children.length
62 children.each do |node, children|
63 assert_equal model, node.class
64 assert_equal Array, children.class
65 assert_equal 3, children.length
66 children.each do |node, children|
67 assert_equal model, node.class
68 assert_equal Array, children.class
69 assert_equal 0, children.length
70 end
71 end
72 end
73 end
74 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
75
1282d2e Stefan Kroes Initial commit
authored
76 def test_tree_navigation
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
77 AncestryTestDatabase.with_model :depth => 3, :width => 3 do |model, roots|
78 roots.each do |lvl0_node, lvl0_children|
1282d2e Stefan Kroes Initial commit
authored
79 # Ancestors assertions
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
80 assert_equal [], lvl0_node.ancestor_ids
81 assert_equal [], lvl0_node.ancestors
82 assert_equal [lvl0_node.id], lvl0_node.path_ids
83 assert_equal [lvl0_node], lvl0_node.path
84 assert_equal 0, lvl0_node.depth
1282d2e Stefan Kroes Initial commit
authored
85 # Parent assertions
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
86 assert_equal nil, lvl0_node.parent_id
87 assert_equal nil, lvl0_node.parent
1282d2e Stefan Kroes Initial commit
authored
88 # Root assertions
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
89 assert_equal lvl0_node.id, lvl0_node.root_id
90 assert_equal lvl0_node, lvl0_node.root
91 assert lvl0_node.is_root?
1282d2e Stefan Kroes Initial commit
authored
92 # Children assertions
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
93 assert_equal lvl0_children.map(&:first).map(&:id), lvl0_node.child_ids
94 assert_equal lvl0_children.map(&:first), lvl0_node.children
95 assert lvl0_node.has_children?
96 assert !lvl0_node.is_childless?
1282d2e Stefan Kroes Initial commit
authored
97 # Siblings assertions
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
98 assert_equal roots.map(&:first).map(&:id), lvl0_node.sibling_ids
99 assert_equal roots.map(&:first), lvl0_node.siblings
100 assert lvl0_node.has_siblings?
101 assert !lvl0_node.is_only_child?
1282d2e Stefan Kroes Initial commit
authored
102 # Descendants assertions
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
103 descendants = model.all.find_all do |node|
104 node.ancestor_ids.include? lvl0_node.id
1282d2e Stefan Kroes Initial commit
authored
105 end
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
106 assert_equal descendants.map(&:id), lvl0_node.descendant_ids
107 assert_equal descendants, lvl0_node.descendants
108 assert_equal [lvl0_node] + descendants, lvl0_node.subtree
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
109
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
110 lvl0_children.each do |lvl1_node, lvl1_children|
1282d2e Stefan Kroes Initial commit
authored
111 # Ancestors assertions
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
112 assert_equal [lvl0_node.id], lvl1_node.ancestor_ids
113 assert_equal [lvl0_node], lvl1_node.ancestors
114 assert_equal [lvl0_node.id, lvl1_node.id], lvl1_node.path_ids
115 assert_equal [lvl0_node, lvl1_node], lvl1_node.path
116 assert_equal 1, lvl1_node.depth
1282d2e Stefan Kroes Initial commit
authored
117 # Parent assertions
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
118 assert_equal lvl0_node.id, lvl1_node.parent_id
119 assert_equal lvl0_node, lvl1_node.parent
1282d2e Stefan Kroes Initial commit
authored
120 # Root assertions
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
121 assert_equal lvl0_node.id, lvl1_node.root_id
122 assert_equal lvl0_node, lvl1_node.root
123 assert !lvl1_node.is_root?
1282d2e Stefan Kroes Initial commit
authored
124 # Children assertions
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
125 assert_equal lvl1_children.map(&:first).map(&:id), lvl1_node.child_ids
126 assert_equal lvl1_children.map(&:first), lvl1_node.children
127 assert lvl1_node.has_children?
128 assert !lvl1_node.is_childless?
1282d2e Stefan Kroes Initial commit
authored
129 # Siblings assertions
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
130 assert_equal lvl0_children.map(&:first).map(&:id), lvl1_node.sibling_ids
131 assert_equal lvl0_children.map(&:first), lvl1_node.siblings
132 assert lvl1_node.has_siblings?
133 assert !lvl1_node.is_only_child?
1282d2e Stefan Kroes Initial commit
authored
134 # Descendants assertions
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
135 descendants = model.all.find_all do |node|
136 node.ancestor_ids.include? lvl1_node.id
137 end
138 assert_equal descendants.map(&:id), lvl1_node.descendant_ids
139 assert_equal descendants, lvl1_node.descendants
140 assert_equal [lvl1_node] + descendants, lvl1_node.subtree
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
141
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
142 lvl1_children.each do |lvl2_node, lvl2_children|
143 # Ancestors assertions
144 assert_equal [lvl0_node.id, lvl1_node.id], lvl2_node.ancestor_ids
145 assert_equal [lvl0_node, lvl1_node], lvl2_node.ancestors
146 assert_equal [lvl0_node.id, lvl1_node.id, lvl2_node.id], lvl2_node.path_ids
147 assert_equal [lvl0_node, lvl1_node, lvl2_node], lvl2_node.path
148 assert_equal 2, lvl2_node.depth
149 # Parent assertions
150 assert_equal lvl1_node.id, lvl2_node.parent_id
151 assert_equal lvl1_node, lvl2_node.parent
152 # Root assertions
153 assert_equal lvl0_node.id, lvl2_node.root_id
154 assert_equal lvl0_node, lvl2_node.root
155 assert !lvl2_node.is_root?
156 # Children assertions
157 assert_equal [], lvl2_node.child_ids
158 assert_equal [], lvl2_node.children
159 assert !lvl2_node.has_children?
160 assert lvl2_node.is_childless?
161 # Siblings assertions
162 assert_equal lvl1_children.map(&:first).map(&:id), lvl2_node.sibling_ids
163 assert_equal lvl1_children.map(&:first), lvl2_node.siblings
164 assert lvl2_node.has_siblings?
165 assert !lvl2_node.is_only_child?
166 # Descendants assertions
167 descendants = model.all.find_all do |node|
168 node.ancestor_ids.include? lvl2_node.id
169 end
170 assert_equal descendants.map(&:id), lvl2_node.descendant_ids
171 assert_equal descendants, lvl2_node.descendants
172 assert_equal [lvl2_node] + descendants, lvl2_node.subtree
1282d2e Stefan Kroes Initial commit
authored
173 end
174 end
175 end
176 end
177 end
178
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
179 def test_ancestors_with_string_primary_keys
180 AncestryTestDatabase.with_model :depth => 3, :width => 3, :primary_key_type => :string, :primary_key_format => /[a-z0-9]+/ do |model, roots|
181 roots.each do |lvl0_node, lvl0_children|
182 # Ancestors assertions
183 assert_equal [], lvl0_node.ancestor_ids
184 assert_equal [], lvl0_node.ancestors
185 assert_equal [lvl0_node.id], lvl0_node.path_ids
186 assert_equal [lvl0_node], lvl0_node.path
187 assert_equal 0, lvl0_node.depth
188 lvl0_children.each do |lvl1_node, lvl1_children|
189 # Ancestors assertions
190 assert_equal [lvl0_node.id], lvl1_node.ancestor_ids
191 assert_equal [lvl0_node], lvl1_node.ancestors
192 assert_equal [lvl0_node.id, lvl1_node.id], lvl1_node.path_ids
193 assert_equal [lvl0_node, lvl1_node], lvl1_node.path
194 assert_equal 1, lvl1_node.depth
195 lvl1_children.each do |lvl2_node, lvl2_children|
196 # Ancestors assertions
197 assert_equal [lvl0_node.id, lvl1_node.id], lvl2_node.ancestor_ids
198 assert_equal [lvl0_node, lvl1_node], lvl2_node.ancestors
199 assert_equal [lvl0_node.id, lvl1_node.id, lvl2_node.id], lvl2_node.path_ids
200 assert_equal [lvl0_node, lvl1_node, lvl2_node], lvl2_node.path
201 assert_equal 2, lvl2_node.depth
202 end
203 end
204 end
205 end
206 end
207
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
208 def test_scopes
209 AncestryTestDatabase.with_model :depth => 3, :width => 3 do |model, roots|
210 # Roots assertion
211 assert_equal roots.map(&:first), model.roots.all
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
212
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
213 model.all.each do |test_node|
214 # Assertions for ancestors_of named scope
215 assert_equal test_node.ancestors.all, model.ancestors_of(test_node).all
216 assert_equal test_node.ancestors.all, model.ancestors_of(test_node.id).all
217 # Assertions for children_of named scope
218 assert_equal test_node.children.all, model.children_of(test_node).all
219 assert_equal test_node.children.all, model.children_of(test_node.id).all
220 # Assertions for descendants_of named scope
221 assert_equal test_node.descendants.all, model.descendants_of(test_node).all
222 assert_equal test_node.descendants.all, model.descendants_of(test_node.id).all
223 # Assertions for subtree_of named scope
224 assert_equal test_node.subtree.all, model.subtree_of(test_node).all
225 assert_equal test_node.subtree.all, model.subtree_of(test_node.id).all
226 # Assertions for siblings_of named scope
227 assert_equal test_node.siblings.all, model.siblings_of(test_node).all
228 assert_equal test_node.siblings.all, model.siblings_of(test_node.id).all
229 end
1282d2e Stefan Kroes Initial commit
authored
230 end
231 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
232
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
233 def test_ancestry_column_validation
234 AncestryTestDatabase.with_model do |model|
235 node = model.create
236 ['3', '10/2', '1/4/30', nil].each do |value|
237 node.send :write_attribute, model.ancestry_column, value
238 node.valid?; assert node.errors[model.ancestry_column].blank?
239 end
240 ['1/3/', '/2/3', 'a', 'a/b', '-34', '/54'].each do |value|
241 node.send :write_attribute, model.ancestry_column, value
242 node.valid?; assert !node.errors[model.ancestry_column].blank?
243 end
1282d2e Stefan Kroes Initial commit
authored
244 end
bbd4b55 Allow specification of primary key format, to facility non-integer prima...
Rolf Timmermans authored
245
246 AncestryTestDatabase.with_model :primary_key_format => /[0-9a-z]+/ do |model|
247 node = model.create
248 ['xk7', '9x1/l4n', 'r1c/4z9/8ps', nil].each do |value|
249 node.send :write_attribute, model.ancestry_column, value
250 node.valid?; assert node.errors[model.ancestry_column].blank?
251 end
252 ['s9a/xk2/', '/s92/d92', 'X', 'X/Y', 'S23', '/xk2'].each do |value|
253 node.send :write_attribute, model.ancestry_column, value
254 node.valid?; assert !node.errors[model.ancestry_column].blank?
255 end
256 end
1282d2e Stefan Kroes Initial commit
authored
257 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
258
1282d2e Stefan Kroes Initial commit
authored
259 def test_descendants_move_with_node
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
260 AncestryTestDatabase.with_model :depth => 3, :width => 3 do |model, roots|
261 root1, root2, root3 = roots.map(&:first)
262 assert_no_difference 'root1.descendants.size' do
263 assert_difference 'root2.descendants.size', root1.subtree.size do
264 root1.parent = root2
265 root1.save!
266 end
1282d2e Stefan Kroes Initial commit
authored
267 end
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
268 assert_no_difference 'root2.descendants.size' do
269 assert_difference 'root3.descendants.size', root2.subtree.size do
270 root2.parent = root3
271 root2.save!
272 end
1282d2e Stefan Kroes Initial commit
authored
273 end
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
274 assert_no_difference 'root1.descendants.size' do
275 assert_difference 'root2.descendants.size', -root1.subtree.size do
276 assert_difference 'root3.descendants.size', -root1.subtree.size do
277 root1.parent = nil
278 root1.save!
279 end
1282d2e Stefan Kroes Initial commit
authored
280 end
281 end
282 end
283 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
284
1282d2e Stefan Kroes Initial commit
authored
285 def test_orphan_rootify_strategy
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
286 AncestryTestDatabase.with_model :depth => 3, :width => 3 do |model, roots|
287 model.orphan_strategy = :rootify
288 root = roots.first.first
289 children = root.children.all
290 root.destroy
291 children.each do |child|
292 child.reload
293 assert child.is_root?
294 assert_equal 3, child.children.size
295 end
1282d2e Stefan Kroes Initial commit
authored
296 end
297 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
298
1282d2e Stefan Kroes Initial commit
authored
299 def test_orphan_destroy_strategy
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
300 AncestryTestDatabase.with_model :depth => 3, :width => 3 do |model, roots|
301 model.orphan_strategy = :destroy
302 root = roots.first.first
303 assert_difference 'model.count', -root.subtree.size do
304 root.destroy
305 end
306 node = model.roots.first.children.first
307 assert_difference 'model.count', -node.subtree.size do
308 node.destroy
309 end
1282d2e Stefan Kroes Initial commit
authored
310 end
311 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
312
1282d2e Stefan Kroes Initial commit
authored
313 def test_orphan_restrict_strategy
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
314 AncestryTestDatabase.with_model :depth => 3, :width => 3 do |model, roots|
315 model.orphan_strategy = :restrict
316 root = roots.first.first
317 assert_raise Ancestry::AncestryException do
318 root.destroy
319 end
320 assert_nothing_raised Ancestry::AncestryException do
321 root.children.first.children.first.destroy
322 end
1282d2e Stefan Kroes Initial commit
authored
323 end
324 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
325
1282d2e Stefan Kroes Initial commit
authored
326 def test_integrity_checking
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
327 AncestryTestDatabase.with_model :width => 3, :depth => 3 do |model, roots|
328 # Check that there are no errors on a valid tree
329 assert_nothing_raised do
330 model.check_ancestry_integrity!
331 end
34a13a9 Stefan Kroes Fixes for 1.2.1
authored
332 assert_equal 0, model.check_ancestry_integrity!(:report => :list).size
1282d2e Stefan Kroes Initial commit
authored
333 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
334
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
335 AncestryTestDatabase.with_model :width => 3, :depth => 3 do |model, roots|
336 # Check detection of invalid format for ancestry column
337 roots.first.first.update_attribute model.ancestry_column, 'invalid_ancestry'
338 assert_raise Ancestry::AncestryIntegrityException do
339 model.check_ancestry_integrity!
340 end
34a13a9 Stefan Kroes Fixes for 1.2.1
authored
341 assert_equal 1, model.check_ancestry_integrity!(:report => :list).size
1282d2e Stefan Kroes Initial commit
authored
342 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
343
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
344 AncestryTestDatabase.with_model :width => 3, :depth => 3 do |model, roots|
345 # Check detection of non-existent ancestor
346 roots.first.first.update_attribute model.ancestry_column, 35
347 assert_raise Ancestry::AncestryIntegrityException do
348 model.check_ancestry_integrity!
349 end
34a13a9 Stefan Kroes Fixes for 1.2.1
authored
350 assert_equal 1, model.check_ancestry_integrity!(:report => :list).size
1282d2e Stefan Kroes Initial commit
authored
351 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
352
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
353 AncestryTestDatabase.with_model :width => 3, :depth => 3 do |model, roots|
354 # Check detection of cyclic ancestry
355 node = roots.first.first
356 node.update_attribute model.ancestry_column, node.id
357 assert_raise Ancestry::AncestryIntegrityException do
358 model.check_ancestry_integrity!
359 end
34a13a9 Stefan Kroes Fixes for 1.2.1
authored
360 assert_equal 1, model.check_ancestry_integrity!(:report => :list).size
1282d2e Stefan Kroes Initial commit
authored
361 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
362
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
363 AncestryTestDatabase.with_model do |model|
364 # Check detection of conflicting parent id
365 model.destroy_all
366 model.create!(model.ancestry_column => model.create!(model.ancestry_column => model.create!(model.ancestry_column => nil).id).id)
367 assert_raise Ancestry::AncestryIntegrityException do
368 model.check_ancestry_integrity!
369 end
34a13a9 Stefan Kroes Fixes for 1.2.1
authored
370 assert_equal 1, model.check_ancestry_integrity!(:report => :list).size
1282d2e Stefan Kroes Initial commit
authored
371 end
372 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
373
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
374 def assert_integrity_restoration model
1282d2e Stefan Kroes Initial commit
authored
375 assert_raise Ancestry::AncestryIntegrityException do
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
376 model.check_ancestry_integrity!
1282d2e Stefan Kroes Initial commit
authored
377 end
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
378 model.restore_ancestry_integrity!
1282d2e Stefan Kroes Initial commit
authored
379 assert_nothing_raised do
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
380 model.check_ancestry_integrity!
1282d2e Stefan Kroes Initial commit
authored
381 end
770f2e3 Fix bug in restore_ancestry_integrity!, added test to verify we didn't r...
Arthur Holstvoogd authored
382 assert model.all.any? {|node| node.ancestry.present? }, "Expected some nodes not to be roots"
383 assert_equal model.count, model.roots.collect {|node| node.descendants.count + 1 }.sum
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
384 end
385
1282d2e Stefan Kroes Initial commit
authored
386 def test_integrity_restoration
770f2e3 Fix bug in restore_ancestry_integrity!, added test to verify we didn't r...
Arthur Holstvoogd authored
387 width, depth = 3, 3
1282d2e Stefan Kroes Initial commit
authored
388 # Check that integrity is restored for invalid format for ancestry column
770f2e3 Fix bug in restore_ancestry_integrity!, added test to verify we didn't r...
Arthur Holstvoogd authored
389 AncestryTestDatabase.with_model :width => width, :depth => depth do |model, roots|
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
390 roots.first.first.update_attribute model.ancestry_column, 'invalid_ancestry'
391 assert_integrity_restoration model
392 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
393
1282d2e Stefan Kroes Initial commit
authored
394 # Check that integrity is restored for non-existent ancestor
770f2e3 Fix bug in restore_ancestry_integrity!, added test to verify we didn't r...
Arthur Holstvoogd authored
395 AncestryTestDatabase.with_model :width => width, :depth => depth do |model, roots|
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
396 roots.first.first.update_attribute model.ancestry_column, 35
397 assert_integrity_restoration model
398 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
399
1282d2e Stefan Kroes Initial commit
authored
400 # Check that integrity is restored for cyclic ancestry
770f2e3 Fix bug in restore_ancestry_integrity!, added test to verify we didn't r...
Arthur Holstvoogd authored
401 AncestryTestDatabase.with_model :width => width, :depth => depth do |model, roots|
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
402 node = roots.first.first
403 node.update_attribute model.ancestry_column, node.id
404 assert_integrity_restoration model
405 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
406
1282d2e Stefan Kroes Initial commit
authored
407 # Check that integrity is restored for conflicting parent id
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
408 AncestryTestDatabase.with_model do |model|
409 model.destroy_all
410 model.create!(model.ancestry_column => model.create!(model.ancestry_column => model.create!(model.ancestry_column => nil).id).id)
411 assert_integrity_restoration model
412 end
1282d2e Stefan Kroes Initial commit
authored
413 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
414
1282d2e Stefan Kroes Initial commit
authored
415 def test_arrangement
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
416 AncestryTestDatabase.with_model :depth => 3, :width => 3 do |model, roots|
417 id_sorter = Proc.new do |a, b|; a.id <=> b.id; end
418 arranged_nodes = model.arrange
419 assert_equal 3, arranged_nodes.size
420 arranged_nodes.each do |node, children|
1282d2e Stefan Kroes Initial commit
authored
421 assert_equal node.children.sort(&id_sorter), children.keys.sort(&id_sorter)
422 children.each do |node, children|
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
423 assert_equal node.children.sort(&id_sorter), children.keys.sort(&id_sorter)
424 children.each do |node, children|
425 assert_equal 0, children.size
426 end
1282d2e Stefan Kroes Initial commit
authored
427 end
428 end
429 end
430 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
431
22a1984 - Added some tests for node creation through scopes
Stefan Kroes authored
432 def test_node_creation_though_scope
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
433 AncestryTestDatabase.with_model do |model|
434 node = model.create!
435 child = node.children.create
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
436 assert_equal node, child.parent
437
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
438 other_child = child.siblings.create!
439 assert_equal node, other_child.parent
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
440
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
441 grandchild = model.children_of(child).new
442 grandchild.save
443 assert_equal child, grandchild.parent
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
444
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
445 other_grandchild = model.siblings_of(grandchild).new
446 other_grandchild.save!
447 assert_equal child, other_grandchild.parent
448 end
22a1984 - Added some tests for node creation through scopes
Stefan Kroes authored
449 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
450
488e752 Stefan Kroes Added validation to detect cycles in record ancestry
authored
451 def test_validate_ancestry_exclude_self
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
452 AncestryTestDatabase.with_model do |model|
453 parent = model.create!
454 child = parent.children.create!
455 assert_raise ActiveRecord::RecordInvalid do
456 parent.update_attributes! :parent => child
457 end
488e752 Stefan Kroes Added validation to detect cycles in record ancestry
authored
458 end
459 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
460
df705c3 Stefan Kroes Version 1.1.0 done!
authored
461 def test_depth_caching
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
462 AncestryTestDatabase.with_model :depth => 3, :width => 3, :cache_depth => true, :depth_cache_column => :depth_cache do |model, roots|
463 roots.each do |lvl0_node, lvl0_children|
464 assert_equal 0, lvl0_node.depth_cache
465 lvl0_children.each do |lvl1_node, lvl1_children|
466 assert_equal 1, lvl1_node.depth_cache
467 lvl1_children.each do |lvl2_node, lvl2_children|
468 assert_equal 2, lvl2_node.depth_cache
469 end
df705c3 Stefan Kroes Version 1.1.0 done!
authored
470 end
471 end
472 end
473 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
474
df705c3 Stefan Kroes Version 1.1.0 done!
authored
475 def test_depth_scopes
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
476 AncestryTestDatabase.with_model :depth => 4, :width => 2, :cache_depth => true do |model, roots|
477 model.before_depth(2).all? { |node| assert node.depth < 2 }
478 model.to_depth(2).all? { |node| assert node.depth <= 2 }
479 model.at_depth(2).all? { |node| assert node.depth == 2 }
480 model.from_depth(2).all? { |node| assert node.depth >= 2 }
481 model.after_depth(2).all? { |node| assert node.depth > 2 }
482 end
df705c3 Stefan Kroes Version 1.1.0 done!
authored
483 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
484
df705c3 Stefan Kroes Version 1.1.0 done!
authored
485 def test_depth_scopes_unavailable
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
486 AncestryTestDatabase.with_model do |model|
487 assert_raise Ancestry::AncestryException do
488 model.before_depth(1)
489 end
490 assert_raise Ancestry::AncestryException do
491 model.to_depth(1)
492 end
493 assert_raise Ancestry::AncestryException do
494 model.at_depth(1)
495 end
496 assert_raise Ancestry::AncestryException do
497 model.from_depth(1)
498 end
499 assert_raise Ancestry::AncestryException do
500 model.after_depth(1)
501 end
df705c3 Stefan Kroes Version 1.1.0 done!
authored
502 end
503 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
504
ecea76f Stefan Kroes Version 1.2.0
authored
505 def test_invalid_has_ancestry_options
df705c3 Stefan Kroes Version 1.1.0 done!
authored
506 assert_raise Ancestry::AncestryException do
ecea76f Stefan Kroes Version 1.2.0
authored
507 Class.new(ActiveRecord::Base).has_ancestry :this_option_doesnt_exist => 42
df705c3 Stefan Kroes Version 1.1.0 done!
authored
508 end
509 assert_raise Ancestry::AncestryException do
ecea76f Stefan Kroes Version 1.2.0
authored
510 Class.new(ActiveRecord::Base).has_ancestry :not_a_hash
df705c3 Stefan Kroes Version 1.1.0 done!
authored
511 end
512 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
513
df705c3 Stefan Kroes Version 1.1.0 done!
authored
514 def test_build_ancestry_from_parent_ids
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
515 AncestryTestDatabase.with_model :skip_ancestry => true, :extra_columns => {:parent_id => :integer} do |model|
516 [model.create!].each do |parent|
517 (Array.new(5) { model.create! :parent_id => parent.id }).each do |parent|
518 (Array.new(5) { model.create! :parent_id => parent.id }).each do |parent|
519 (Array.new(5) { model.create! :parent_id => parent.id })
520 end
df705c3 Stefan Kroes Version 1.1.0 done!
authored
521 end
522 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
523
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
524 # Assert all nodes where created
34a13a9 Stefan Kroes Fixes for 1.2.1
authored
525 assert_equal (0..3).map { |n| 5 ** n }.sum, model.count
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
526
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
527 model.has_ancestry
528 model.build_ancestry_from_parent_ids!
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
529
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
530 # Assert ancestry integrity
531 assert_nothing_raised do
532 model.check_ancestry_integrity!
533 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
534
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
535 roots = model.roots.all
536 # Assert single root node
537 assert_equal 1, roots.size
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
538
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
539 # Assert it has 5 children
540 roots.each do |parent|
34a13a9 Stefan Kroes Fixes for 1.2.1
authored
541 assert_equal 5, parent.children.count
df705c3 Stefan Kroes Version 1.1.0 done!
authored
542 parent.children.each do |parent|
34a13a9 Stefan Kroes Fixes for 1.2.1
authored
543 assert_equal 5, parent.children.count
df705c3 Stefan Kroes Version 1.1.0 done!
authored
544 parent.children.each do |parent|
34a13a9 Stefan Kroes Fixes for 1.2.1
authored
545 assert_equal 5, parent.children.count
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
546 parent.children.each do |parent|
34a13a9 Stefan Kroes Fixes for 1.2.1
authored
547 assert_equal 0, parent.children.count
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
548 end
df705c3 Stefan Kroes Version 1.1.0 done!
authored
549 end
550 end
551 end
552 end
553 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
554
df705c3 Stefan Kroes Version 1.1.0 done!
authored
555 def test_rebuild_depth_cache
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
556 AncestryTestDatabase.with_model :depth => 3, :width => 3, :cache_depth => true, :depth_cache_column => :depth_cache do |model, roots|
557 model.connection.execute("update test_nodes set depth_cache = null;")
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
558
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
559 # Assert cache was emptied correctly
560 model.all.each do |test_node|
561 assert_equal nil, test_node.depth_cache
562 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
563
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
564 # Rebuild cache
565 model.rebuild_depth_cache!
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
566
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
567 # Assert cache was rebuild correctly
568 model.all.each do |test_node|
569 assert_equal test_node.depth, test_node.depth_cache
570 end
df705c3 Stefan Kroes Version 1.1.0 done!
authored
571 end
572 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
573
df705c3 Stefan Kroes Version 1.1.0 done!
authored
574 def test_exception_when_rebuilding_depth_cache_for_model_without_depth_caching
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
575 AncestryTestDatabase.with_model do |model|
576 assert_raise Ancestry::AncestryException do
577 model.rebuild_depth_cache!
578 end
df705c3 Stefan Kroes Version 1.1.0 done!
authored
579 end
580 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
581
df705c3 Stefan Kroes Version 1.1.0 done!
authored
582 def test_descendants_with_depth_constraints
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
583 AncestryTestDatabase.with_model :depth => 4, :width => 4, :cache_depth => true do |model, roots|
584 assert_equal 4, model.roots.first.descendants(:before_depth => 2).count
585 assert_equal 20, model.roots.first.descendants(:to_depth => 2).count
586 assert_equal 16, model.roots.first.descendants(:at_depth => 2).count
587 assert_equal 80, model.roots.first.descendants(:from_depth => 2).count
588 assert_equal 64, model.roots.first.descendants(:after_depth => 2).count
589 end
df705c3 Stefan Kroes Version 1.1.0 done!
authored
590 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
591
df705c3 Stefan Kroes Version 1.1.0 done!
authored
592 def test_subtree_with_depth_constraints
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
593 AncestryTestDatabase.with_model :depth => 4, :width => 4, :cache_depth => true do |model, roots|
594 assert_equal 5, model.roots.first.subtree(:before_depth => 2).count
595 assert_equal 21, model.roots.first.subtree(:to_depth => 2).count
596 assert_equal 16, model.roots.first.subtree(:at_depth => 2).count
597 assert_equal 80, model.roots.first.subtree(:from_depth => 2).count
598 assert_equal 64, model.roots.first.subtree(:after_depth => 2).count
599 end
df705c3 Stefan Kroes Version 1.1.0 done!
authored
600 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
601
602
df705c3 Stefan Kroes Version 1.1.0 done!
authored
603 def test_ancestors_with_depth_constraints
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
604 AncestryTestDatabase.with_model :cache_depth => true do |model|
605 node1 = model.create!
606 node2 = node1.children.create!
607 node3 = node2.children.create!
608 node4 = node3.children.create!
609 node5 = node4.children.create!
610 leaf = node5.children.create!
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
611
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
612 assert_equal [node1, node2, node3], leaf.ancestors(:before_depth => -2)
613 assert_equal [node1, node2, node3, node4], leaf.ancestors(:to_depth => -2)
614 assert_equal [node4], leaf.ancestors(:at_depth => -2)
615 assert_equal [node4, node5], leaf.ancestors(:from_depth => -2)
616 assert_equal [node5], leaf.ancestors(:after_depth => -2)
617 end
df705c3 Stefan Kroes Version 1.1.0 done!
authored
618 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
619
df705c3 Stefan Kroes Version 1.1.0 done!
authored
620 def test_path_with_depth_constraints
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
621 AncestryTestDatabase.with_model :cache_depth => true do |model|
622 node1 = model.create!
623 node2 = node1.children.create!
624 node3 = node2.children.create!
625 node4 = node3.children.create!
626 node5 = node4.children.create!
627 leaf = node5.children.create!
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
628
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
629 assert_equal [node1, node2, node3], leaf.path(:before_depth => -2)
630 assert_equal [node1, node2, node3, node4], leaf.path(:to_depth => -2)
631 assert_equal [node4], leaf.path(:at_depth => -2)
632 assert_equal [node4, node5, leaf], leaf.path(:from_depth => -2)
633 assert_equal [node5, leaf], leaf.path(:after_depth => -2)
634 end
df705c3 Stefan Kroes Version 1.1.0 done!
authored
635 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
636
df705c3 Stefan Kroes Version 1.1.0 done!
authored
637 def test_exception_on_unknown_depth_column
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
638 AncestryTestDatabase.with_model :cache_depth => true do |model|
639 assert_raise Ancestry::AncestryException do
640 model.create!.subtree(:this_is_not_a_valid_depth_option => 42)
641 end
df705c3 Stefan Kroes Version 1.1.0 done!
authored
642 end
643 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
644
e3ea26a Stefan Kroes - Version 1.1.2 (2009-10-29)
authored
645 def test_sti_support
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
646 AncestryTestDatabase.with_model :extra_columns => {:type => :string} do |model|
647 subclass1 = Object.const_set 'Subclass1', Class.new(model)
648 (class << subclass1; self; end).send :define_method, :model_name do; Struct.new(:human, :underscore).new 'Subclass1', 'subclass1'; end
649 subclass2 = Object.const_set 'Subclass2', Class.new(model)
650 (class << subclass2; self; end).send :define_method, :model_name do; Struct.new(:human, :underscore).new 'Subclass1', 'subclass1'; end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
651
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
652 node1 = subclass1.create!
653 node2 = subclass2.create! :parent => node1
654 node3 = subclass1.create! :parent => node2
655 node4 = subclass2.create! :parent => node3
656 node5 = subclass1.create! :parent => node4
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
657
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
658 model.all.each do |node|
659 assert [subclass1, subclass2].include?(node.class)
660 end
661
662 assert_equal [node2.id, node3.id, node4.id, node5.id], node1.descendants.map(&:id)
663 assert_equal [node1.id, node2.id, node3.id, node4.id, node5.id], node1.subtree.map(&:id)
664 assert_equal [node1.id, node2.id, node3.id, node4.id], node5.ancestors.map(&:id)
665 assert_equal [node1.id, node2.id, node3.id, node4.id, node5.id], node5.path.map(&:id)
666 end
e3ea26a Stefan Kroes - Version 1.1.2 (2009-10-29)
authored
667 end
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
668
ecea76f Stefan Kroes Version 1.2.0
authored
669 def test_arrange_order_option
f1955bb Stefan Kroes Changed test suite to run independently of Rails application
authored
670 AncestryTestDatabase.with_model :width => 3, :depth => 3 do |model, roots|
671 descending_nodes_lvl0 = model.arrange :order => 'id desc'
672 ascending_nodes_lvl0 = model.arrange :order => 'id asc'
d8cd386 Automatically determine primary key column type, and cast to integer onl...
Rolf Timmermans authored
673
ecea76f Stefan Kroes Version 1.2.0
authored
674 descending_nodes_lvl0.keys.zip(ascending_nodes_lvl0.keys.reverse).each do |descending_node, ascending_node|
675 assert_equal descending_node, ascending_node
676 descending_nodes_lvl1 = descending_nodes_lvl0[descending_node]
677 ascending_nodes_lvl1 = ascending_nodes_lvl0[ascending_node]
678 descending_nodes_lvl1.keys.zip(ascending_nodes_lvl1.keys.reverse).each do |descending_node, ascending_node|
679 assert_equal descending_node, ascending_node
680 descending_nodes_lvl2 = descending_nodes_lvl1[descending_node]
681 ascending_nodes_lvl2 = ascending_nodes_lvl1[ascending_node]
682 descending_nodes_lvl2.keys.zip(ascending_nodes_lvl2.keys.reverse).each do |descending_node, ascending_node|
683 assert_equal descending_node, ascending_node
684 descending_nodes_lvl3 = descending_nodes_lvl2[descending_node]
685 ascending_nodes_lvl3 = ascending_nodes_lvl2[ascending_node]
686 descending_nodes_lvl3.keys.zip(ascending_nodes_lvl3.keys.reverse).each do |descending_node, ascending_node|
687 assert_equal descending_node, ascending_node
688 end
689 end
690 end
691 end
692 end
693 end
5614a3b Ken-ichi Added class method to sort nodes by ancestry.
kueda authored
694
695 def test_sort_by_ancestry
696 AncestryTestDatabase.with_model do |model|
697 n1 = model.create!
698 n2 = model.create!(:parent => n1)
699 n3 = model.create!(:parent => n2)
700 n4 = model.create!(:parent => n2)
701 n5 = model.create!(:parent => n1)
702
703 arranged = model.sort_by_ancestry(model.all.sort_by(&:id).reverse)
704 assert_equal [n1, n2, n4, n3, n5].map(&:id), arranged.map(&:id)
705 end
706 end
b5fd912 Fixed it for Rails 3.2 and broke it for 2.x, changed version 1.2.6 and a...
Stefan Kroes authored
707
708 def test_arrangement_nesting
709 AncestryTestDatabase.with_model :extra_columns => {:name => :string} do |model|
710 model.send :default_scope, model.order('name')
711
712 model.create!(:name => 'Linux').children.create! :name => 'Debian'
713
714 assert_equal 1, model.arrange.count
715 end
716 end
1282d2e Stefan Kroes Initial commit
authored
717 end
Something went wrong with that request. Please try again.