mongoid-ancestry-fixes 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,241 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe MongoidAncestry do
4
-
5
- subject { MongoidAncestry }
6
-
7
- it "should have tree navigation" do
8
- subject.with_model :depth => 3, :width => 3 do |model, roots|
9
- roots.each do |lvl0_node, lvl0_children|
10
- # Ancestors assertions
11
- lvl0_node.ancestor_ids.should eql([])
12
- lvl0_node.ancestors.to_a.should eql([])
13
- lvl0_node.path_ids.should eql([lvl0_node.id])
14
- lvl0_node.path.to_a.should eql([lvl0_node])
15
- lvl0_node.depth.should eql(0)
16
- # Parent assertions
17
- lvl0_node.parent_id.should be_nil
18
- lvl0_node.parent.should be_nil
19
- # Root assertions
20
- lvl0_node.root_id.should eql(lvl0_node.id)
21
- lvl0_node.root.should eql(lvl0_node)
22
- lvl0_node.is_root?.should be_true
23
- # Children assertions
24
- lvl0_node.child_ids.should eql(lvl0_children.map(&:first).map(&:id))
25
- lvl0_node.children.to_a.should eql(lvl0_children.map(&:first))
26
- lvl0_node.has_children?.should be_true
27
- lvl0_node.is_childless?.should be_false
28
- # Siblings assertions
29
- lvl0_node.sibling_ids.should eql(roots.map(&:first).map(&:id))
30
- lvl0_node.siblings.to_a.should eql(roots.map(&:first))
31
- lvl0_node.has_siblings?.should be_true
32
- lvl0_node.is_only_child?.should be_false
33
- # Descendants assertions
34
- descendants = model.all.find_all do |node|
35
- node.ancestor_ids.include?(lvl0_node.id)
36
- end
37
- lvl0_node.descendant_ids.should eql(descendants.map(&:id))
38
- lvl0_node.descendants.to_a.should eql(descendants)
39
- lvl0_node.subtree.to_a.should eql([lvl0_node] + descendants)
40
-
41
- lvl0_children.each do |lvl1_node, lvl1_children|
42
- # Ancestors assertions
43
- lvl1_node.ancestor_ids.should eql([lvl0_node.id])
44
- lvl1_node.ancestors.to_a.should eql([lvl0_node])
45
- lvl1_node.path_ids.should eql([lvl0_node.id, lvl1_node.id])
46
- lvl1_node.path.to_a.should eql([lvl0_node, lvl1_node])
47
- lvl1_node.depth.should eql(1)
48
- # Parent assertions
49
- lvl1_node.parent_id.should eql(lvl0_node.id)
50
- lvl1_node.parent.should eql(lvl0_node)
51
- # Root assertions
52
- lvl1_node.root_id.should eql(lvl0_node.id)
53
- lvl1_node.root.should eql(lvl0_node)
54
- lvl1_node.is_root?.should be_false
55
- # Children assertions
56
- lvl1_node.child_ids.should eql(lvl1_children.map(&:first).map(&:id))
57
- lvl1_node.children.to_a.should eql(lvl1_children.map(&:first))
58
- lvl1_node.has_children?.should be_true
59
- lvl1_node.is_childless?.should be_false
60
- # Siblings assertions
61
- lvl1_node.sibling_ids.should eql(lvl0_children.map(&:first).map(&:id))
62
- lvl1_node.siblings.to_a.should eql(lvl0_children.map(&:first))
63
- lvl1_node.has_siblings?.should be_true
64
- lvl1_node.is_only_child?.should be_false
65
- # Descendants assertions
66
- descendants = model.all.find_all do |node|
67
- node.ancestor_ids.include? lvl1_node.id
68
- end
69
-
70
- lvl1_node.descendant_ids.should eql(descendants.map(&:id))
71
- lvl1_node.descendants.to_a.should eql(descendants)
72
- lvl1_node.subtree.to_a.should eql([lvl1_node] + descendants)
73
-
74
- lvl1_children.each do |lvl2_node, lvl2_children|
75
- # Ancestors assertions
76
- lvl2_node.ancestor_ids.should eql([lvl0_node.id, lvl1_node.id])
77
- lvl2_node.ancestors.to_a.should eql([lvl0_node, lvl1_node])
78
- lvl2_node.path_ids.should eql([lvl0_node.id, lvl1_node.id, lvl2_node.id])
79
- lvl2_node.path.to_a.should eql([lvl0_node, lvl1_node, lvl2_node])
80
- lvl2_node.depth.should eql(2)
81
- # Parent assertions
82
- lvl2_node.parent_id.should eql(lvl1_node.id)
83
- lvl2_node.parent.should eql(lvl1_node)
84
- # Root assertions
85
- lvl2_node.root_id.should eql(lvl0_node.id)
86
- lvl2_node.root.should eql(lvl0_node)
87
- lvl2_node.is_root?.should be_false
88
- # Children assertions
89
- lvl2_node.child_ids.should eql([])
90
- lvl2_node.children.to_a.should eql([])
91
- lvl2_node.has_children?.should be_false
92
- lvl2_node.is_childless?.should be_true
93
- # Siblings assertions
94
- lvl2_node.sibling_ids.should eql(lvl1_children.map(&:first).map(&:id))
95
- lvl2_node.siblings.to_a.should eql(lvl1_children.map(&:first))
96
- lvl2_node.has_siblings?.should be_true
97
- lvl2_node.is_only_child?.should be_false
98
- # Descendants assertions
99
- descendants = model.all.find_all do |node|
100
- node.ancestor_ids.include? lvl2_node.id
101
- end
102
- lvl2_node.descendant_ids.should eql(descendants.map(&:id))
103
- lvl2_node.descendants.to_a.should eql(descendants)
104
- lvl2_node.subtree.to_a.should eql([lvl2_node] + descendants)
105
- end
106
- end
107
- end
108
- end
109
- end
110
-
111
- it "should validate ancestry field" do
112
- subject.with_model do |model|
113
- node = model.create
114
- ['3', '10/2', '1/4/30', nil].each do |value|
115
- node.send :write_attribute, model.ancestry_field, value
116
- node.should be_valid
117
- node.errors[model.ancestry_field].blank?.should be_true
118
- end
119
- ['1/3/', '/2/3', 'A/b', '-34', '/54'].each do |value|
120
- node.send :write_attribute, model.ancestry_field, value
121
- node.should_not be_valid
122
- node.errors[model.ancestry_field].blank?.should be_false
123
- end
124
- end
125
- end
126
-
127
- it "should move descendants with node" do
128
- subject.with_model :depth => 3, :width => 3 do |model, roots|
129
- root1, root2, root3 = roots.map(&:first)
130
-
131
- descendants = root1.descendants.asc(:_id).map(&:to_param)
132
- expect {
133
- root1.parent = root2
134
- root1.save!
135
- root1.descendants.asc(:_id).map(&:to_param).should eql(descendants)
136
- }.to change(root2.descendants, 'size').by(root1.subtree.size)
137
-
138
- descendants = root2.descendants.asc(:_id).map(&:to_param)
139
- expect {
140
- root2.parent = root3
141
- root2.save!
142
- root2.descendants.asc(:_id).map(&:to_param).should eql(descendants)
143
- }.to change(root3.descendants, 'size').by(root2.subtree.size)
144
-
145
- descendants = root1.descendants.asc(:_id).map(&:to_param)
146
- expect {
147
- expect {
148
- root1.parent = nil
149
- root1.save!
150
- root1.descendants.asc(:_id).map(&:to_param).should eql(descendants)
151
- }.to change(root3.descendants, 'size').by(-root1.subtree.size)
152
- }.to change(root2.descendants, 'size').by(-root1.subtree.size)
153
- end
154
- end
155
-
156
- it "should validate ancestry exclude self" do
157
- subject.with_model do |model|
158
- parent = model.create!
159
- child = parent.children.create
160
- expect { parent.update_attributes! :parent => child }.to raise_error(Mongoid::Errors::Validations)
161
- end
162
- end
163
-
164
- it "should have depth caching" do
165
- subject.with_model :depth => 3, :width => 3, :cache_depth => true, :depth_cache_field => :depth_cache do |model, roots|
166
- roots.each do |lvl0_node, lvl0_children|
167
- lvl0_node.depth_cache.should eql(0)
168
- lvl0_children.each do |lvl1_node, lvl1_children|
169
- lvl1_node.depth_cache.should eql(1)
170
- lvl1_children.each do |lvl2_node, lvl2_children|
171
- lvl2_node.depth_cache.should eql(2)
172
- end
173
- end
174
- end
175
- end
176
- end
177
-
178
- it "should have descendants with depth constraints" do
179
- subject.with_model :depth => 4, :width => 4, :cache_depth => true do |model, roots|
180
- model.roots.first.descendants(:before_depth => 2).count.should eql(4)
181
- model.roots.first.descendants(:to_depth => 2).count.should eql(20)
182
- model.roots.first.descendants(:at_depth => 2).count.should eql(16)
183
- model.roots.first.descendants(:from_depth => 2).count.should eql(80)
184
- model.roots.first.descendants(:after_depth => 2).count.should eql(64)
185
- end
186
- end
187
-
188
- it "should have subtree with depth constraints" do
189
- subject.with_model :depth => 4, :width => 4, :cache_depth => true do |model, roots|
190
- model.roots.first.subtree(:before_depth => 2).count.should eql(5)
191
- model.roots.first.subtree(:to_depth => 2).count.should eql(21)
192
- model.roots.first.subtree(:at_depth => 2).count.should eql(16)
193
- model.roots.first.subtree(:from_depth => 2).count.should eql(80)
194
- model.roots.first.subtree(:after_depth => 2).count.should eql(64)
195
- end
196
- end
197
-
198
- it "should have ancestors with depth constraints" do
199
- subject.with_model :cache_depth => true do |model|
200
- node1 = model.create!
201
- node2 = node1.children.create
202
- node3 = node2.children.create
203
- node4 = node3.children.create
204
- node5 = node4.children.create
205
- leaf = node5.children.create
206
-
207
- leaf.ancestors(:before_depth => -2).to_a.should eql([node1, node2, node3])
208
- leaf.ancestors(:to_depth => -2).to_a.should eql([node1, node2, node3, node4])
209
- leaf.ancestors(:at_depth => -2).to_a.should eql([node4])
210
- leaf.ancestors(:from_depth => -2).to_a.should eql([node4, node5])
211
- leaf.ancestors(:after_depth => -2).to_a.should eql([node5])
212
- end
213
- end
214
-
215
- it "should have path with depth constraints" do
216
- subject.with_model :cache_depth => true do |model|
217
- node1 = model.create!
218
- node2 = node1.children.create
219
- node3 = node2.children.create
220
- node4 = node3.children.create
221
- node5 = node4.children.create
222
- leaf = node5.children.create
223
-
224
- leaf.path(:before_depth => -2).to_a.should eql([node1, node2, node3])
225
- leaf.path(:to_depth => -2).to_a.should eql([node1, node2, node3, node4])
226
- leaf.path(:at_depth => -2).to_a.should eql([node4])
227
- leaf.path(:from_depth => -2).to_a.should eql([node4, node5, leaf])
228
- leaf.path(:after_depth => -2).to_a.should eql([node5, leaf])
229
- end
230
- end
231
-
232
- it "should raise exception on unknown depth field" do
233
- subject.with_model :cache_depth => true do |model|
234
- expect {
235
- model.create!.subtree(:this_is_not_a_valid_depth_option => 42)
236
- }.to raise_error(Mongoid::Ancestry::Error)
237
- end
238
- end
239
-
240
-
241
- end
data/spec/spec_helper.rb DELETED
@@ -1,22 +0,0 @@
1
- require 'rubygems'
2
- require 'bundler/setup'
3
-
4
- require 'mongoid'
5
- require 'rspec'
6
-
7
- require 'mongoid-ancestry'
8
-
9
- Mongoid.configure do |config|
10
- logger = Logger.new('log/test.log')
11
- config.master = Mongo::Connection.new('localhost', 27017,
12
- :logger => logger).db('ancestry_test')
13
- config.logger = logger
14
- end
15
-
16
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
17
-
18
- RSpec.configure do |config|
19
- config.after :each do
20
- Mongoid.master.collections.reject { |c| c.name =~ /^system\./ }.each(&:drop)
21
- end
22
- end
@@ -1,40 +0,0 @@
1
- class MongoidAncestry
2
-
3
- def self.with_model options = {}
4
- depth = options.delete(:depth) || 0
5
- width = options.delete(:width) || 0
6
- extra_columns = options.delete(:extra_columns)
7
- skip_ancestry = options.delete(:skip_ancestry)
8
-
9
- begin
10
- model = Class.new
11
- (class << model; self; end).send :define_method, :model_name do; Struct.new(:human, :underscore, :i18n_key).new 'TestNode', 'test_node', 'key'; end
12
- const_set 'TestNode', model
13
- TestNode.send(:include, Mongoid::Document)
14
- TestNode.send(:include, Mongoid::Ancestry) unless skip_ancestry
15
-
16
- extra_columns.each do |name, type|
17
- TestNode.send :field, name, :type => type.to_s.capitalize.constantize
18
- end unless extra_columns.nil?
19
-
20
- TestNode.has_ancestry options unless skip_ancestry
21
-
22
- if depth > 0
23
- yield TestNode, create_test_nodes(TestNode, depth, width)
24
- else
25
- yield TestNode
26
- end
27
- ensure
28
- remove_const "TestNode"
29
- end
30
- end
31
-
32
- def self.create_test_nodes model, depth, width, parent = nil
33
- unless depth == 0
34
- Array.new width do
35
- node = model.create!(:parent => parent)
36
- [node, create_test_nodes(model, depth - 1, width, node)]
37
- end
38
- else; []; end
39
- end
40
- end