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