Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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