mongodoc 0.1.2 → 0.2.0
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.
- data/README.textile +143 -0
- data/Rakefile +35 -3
- data/VERSION +1 -1
- data/examples/simple_document.rb +35 -0
- data/examples/simple_object.rb +32 -0
- data/features/finders.feature +72 -0
- data/features/mongodoc_base.feature +12 -2
- data/features/named_scopes.feature +66 -0
- data/features/new_record.feature +36 -0
- data/features/partial_updates.feature +105 -0
- data/features/step_definitions/criteria_steps.rb +4 -41
- data/features/step_definitions/document_steps.rb +56 -5
- data/features/step_definitions/documents.rb +14 -3
- data/features/step_definitions/finder_steps.rb +15 -0
- data/features/step_definitions/named_scope_steps.rb +18 -0
- data/features/step_definitions/partial_update_steps.rb +32 -0
- data/features/step_definitions/query_steps.rb +51 -0
- data/features/using_criteria.feature +5 -1
- data/lib/mongodoc/attributes.rb +76 -63
- data/lib/mongodoc/collection.rb +9 -9
- data/lib/mongodoc/criteria.rb +152 -161
- data/lib/mongodoc/cursor.rb +7 -5
- data/lib/mongodoc/document.rb +95 -31
- data/lib/mongodoc/finders.rb +29 -0
- data/lib/mongodoc/named_scope.rb +68 -0
- data/lib/mongodoc/parent_proxy.rb +15 -6
- data/lib/mongodoc/proxy.rb +22 -13
- data/lib/mongodoc.rb +3 -3
- data/mongodoc.gemspec +42 -14
- data/perf/mongodoc_runner.rb +90 -0
- data/perf/ruby_driver_runner.rb +64 -0
- data/spec/attributes_spec.rb +46 -12
- data/spec/collection_spec.rb +23 -23
- data/spec/criteria_spec.rb +124 -187
- data/spec/cursor_spec.rb +21 -17
- data/spec/document_ext.rb +2 -2
- data/spec/document_spec.rb +187 -218
- data/spec/embedded_save_spec.rb +104 -0
- data/spec/finders_spec.rb +81 -0
- data/spec/hash_matchers.rb +27 -0
- data/spec/named_scope_spec.rb +82 -0
- data/spec/new_record_spec.rb +216 -0
- data/spec/parent_proxy_spec.rb +8 -6
- data/spec/proxy_spec.rb +80 -0
- data/spec/spec_helper.rb +2 -0
- metadata +35 -7
- data/README.rdoc +0 -75
data/spec/document_spec.rb
CHANGED
@@ -3,42 +3,67 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
3
3
|
describe "MongoDoc::Document" do
|
4
4
|
|
5
5
|
context "satisfies form_for requirements" do
|
6
|
-
class FormForTest
|
6
|
+
class FormForTest
|
7
|
+
include MongoDoc::Document
|
8
|
+
|
9
|
+
key :data
|
7
10
|
end
|
8
|
-
|
11
|
+
|
9
12
|
before do
|
10
13
|
@doc = FormForTest.new
|
11
14
|
@doc._id = '1'
|
12
15
|
end
|
13
|
-
|
16
|
+
|
14
17
|
it "#id returns the _id" do
|
15
18
|
@doc.id.should == @doc._id
|
16
19
|
end
|
17
|
-
|
20
|
+
|
18
21
|
it "#to_param returns the _id" do
|
19
22
|
@doc.to_param.should == @doc._id
|
20
23
|
end
|
21
|
-
|
24
|
+
|
22
25
|
context "#new_record?" do
|
23
26
|
it "is true when the object does not have an _id" do
|
24
27
|
@doc._id = nil
|
25
28
|
@doc.should be_new_record
|
26
29
|
end
|
27
|
-
|
30
|
+
|
28
31
|
it "is false when the object has an id" do
|
29
32
|
@doc.should_not be_new_record
|
30
33
|
end
|
31
34
|
end
|
35
|
+
|
36
|
+
context "attributes" do
|
37
|
+
it "has an initialize method that takes a hash" do
|
38
|
+
data = 'data'
|
39
|
+
FormForTest.new(:data => data).data.should == data
|
40
|
+
end
|
41
|
+
|
42
|
+
it "can set attributes from a hash" do
|
43
|
+
test = FormForTest.new
|
44
|
+
data = 'data'
|
45
|
+
test.attributes = {:data => data}
|
46
|
+
test.data.should == data
|
47
|
+
end
|
48
|
+
|
49
|
+
it "returns all its attributes" do
|
50
|
+
data = 'data'
|
51
|
+
test = FormForTest.new(:data => data)
|
52
|
+
test.attributes.should == {:data => data}
|
53
|
+
end
|
54
|
+
end
|
32
55
|
end
|
33
56
|
|
34
57
|
context "validations" do
|
35
|
-
class SimpleValidationTest
|
58
|
+
class SimpleValidationTest
|
59
|
+
include MongoDoc::Document
|
60
|
+
|
36
61
|
key :data
|
37
62
|
validates_presence_of :data
|
38
63
|
end
|
39
64
|
|
40
|
-
it "are
|
41
|
-
Validatable.should ===
|
65
|
+
it "are included by MongoDoc::Document" do
|
66
|
+
Validatable.should === SimpleValidationTest.new
|
42
67
|
end
|
43
68
|
|
44
69
|
it "valid? fails when a document is invalid" do
|
@@ -48,29 +73,19 @@ describe "MongoDoc::Document" do
|
|
48
73
|
end
|
49
74
|
end
|
50
75
|
|
51
|
-
context ".criteria" do
|
52
|
-
class CriteriaTest < MongoDoc::Document
|
53
|
-
key :data
|
54
|
-
end
|
55
|
-
|
56
|
-
it "creates a new criteria for the document" do
|
57
|
-
CriteriaTest.criteria.should be_a_kind_of(MongoDoc::Criteria)
|
58
|
-
end
|
59
|
-
|
60
|
-
it "sets the criteria klass" do
|
61
|
-
CriteriaTest.criteria.klass.should == CriteriaTest
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
76
|
context "saving" do
|
66
|
-
class SaveRoot
|
77
|
+
class SaveRoot
|
78
|
+
include MongoDoc::Document
|
79
|
+
|
67
80
|
has_many :save_children
|
68
81
|
end
|
69
|
-
|
70
|
-
class SaveChild
|
82
|
+
|
83
|
+
class SaveChild
|
84
|
+
include MongoDoc::Document
|
85
|
+
|
71
86
|
key :data
|
72
87
|
end
|
73
|
-
|
88
|
+
|
74
89
|
before do
|
75
90
|
@root = SaveRoot.new
|
76
91
|
@root.stub(:_save)
|
@@ -84,13 +99,13 @@ describe "MongoDoc::Document" do
|
|
84
99
|
@root.should_receive(:save).with(validate)
|
85
100
|
@child.save(validate)
|
86
101
|
end
|
87
|
-
|
102
|
+
|
88
103
|
context "when validating" do
|
89
104
|
it "validates" do
|
90
105
|
@root.should_receive(:valid?)
|
91
106
|
@root.save(true)
|
92
107
|
end
|
93
|
-
|
108
|
+
|
94
109
|
context "and valid" do
|
95
110
|
it "delegates to _save" do
|
96
111
|
@root.should_receive(:_save).with(false)
|
@@ -104,21 +119,21 @@ describe "MongoDoc::Document" do
|
|
104
119
|
@root.save(true).should == id
|
105
120
|
end
|
106
121
|
end
|
107
|
-
|
122
|
+
|
108
123
|
context "and invalid" do
|
109
124
|
it "does not call _save" do
|
110
125
|
@root.stub(:valid?).and_return(false)
|
111
126
|
@root.should_not_receive(:_save)
|
112
127
|
@root.save(true)
|
113
128
|
end
|
114
|
-
|
129
|
+
|
115
130
|
it "returns false" do
|
116
131
|
@root.stub(:valid?).and_return(false)
|
117
132
|
@root.save(true).should be_false
|
118
133
|
end
|
119
134
|
end
|
120
135
|
end
|
121
|
-
|
136
|
+
|
122
137
|
context "when not validating" do
|
123
138
|
it "does not validate" do
|
124
139
|
@root.should_not_receive(:valid?)
|
@@ -137,18 +152,18 @@ describe "MongoDoc::Document" do
|
|
137
152
|
end
|
138
153
|
end
|
139
154
|
end
|
140
|
-
|
155
|
+
|
141
156
|
context "#save!" do
|
142
157
|
it "delegates to the root" do
|
143
158
|
@root.should_receive(:save!)
|
144
159
|
@child.save!
|
145
160
|
end
|
146
|
-
|
161
|
+
|
147
162
|
it "validates" do
|
148
163
|
@root.should_receive(:valid?).and_return(true)
|
149
164
|
@root.save!
|
150
165
|
end
|
151
|
-
|
166
|
+
|
152
167
|
it "returns the result of _save if valid" do
|
153
168
|
id = 'id'
|
154
169
|
@root.stub(:valid?).and_return(true)
|
@@ -164,23 +179,24 @@ describe "MongoDoc::Document" do
|
|
164
179
|
end
|
165
180
|
end
|
166
181
|
end
|
167
|
-
|
182
|
+
|
168
183
|
context "#_save" do
|
169
|
-
class SaveTest
|
184
|
+
class SaveTest
|
185
|
+
include MongoDoc::Document
|
170
186
|
end
|
171
|
-
|
187
|
+
|
172
188
|
before do
|
173
189
|
@collection = stub('collection')
|
174
190
|
@doc = SaveTest.new
|
175
191
|
@doc.stub(:_collection).and_return(@collection)
|
176
192
|
end
|
177
|
-
|
193
|
+
|
178
194
|
it "delegates to the collection save" do
|
179
195
|
safe = true
|
180
196
|
@collection.should_receive(:save)
|
181
197
|
@doc.send(:_save, safe)
|
182
198
|
end
|
183
|
-
|
199
|
+
|
184
200
|
it "sets the _id of the document" do
|
185
201
|
id = 'id'
|
186
202
|
@collection.stub(:save).and_return(id)
|
@@ -196,7 +212,9 @@ describe "MongoDoc::Document" do
|
|
196
212
|
end
|
197
213
|
|
198
214
|
context "creating" do
|
199
|
-
class CreateTest
|
215
|
+
class CreateTest
|
216
|
+
include MongoDoc::Document
|
217
|
+
|
200
218
|
key :data
|
201
219
|
validates_presence_of :data
|
202
220
|
end
|
@@ -205,21 +223,21 @@ describe "MongoDoc::Document" do
|
|
205
223
|
@value = 'value'
|
206
224
|
CreateTest.stub(:_create).and_return(true)
|
207
225
|
end
|
208
|
-
|
226
|
+
|
209
227
|
context ".create" do
|
210
228
|
it "creates a new document" do
|
211
229
|
obj = CreateTest.new
|
212
230
|
CreateTest.should_receive(:new).and_return(obj)
|
213
231
|
CreateTest.create
|
214
232
|
end
|
215
|
-
|
233
|
+
|
216
234
|
it "delegates to _create with safe => false" do
|
217
235
|
obj = CreateTest.new(:data => @value)
|
218
236
|
CreateTest.stub(:new).and_return(obj)
|
219
237
|
CreateTest.should_receive(:_create).with(obj, false).and_return(true)
|
220
238
|
CreateTest.create(:data => @value)
|
221
239
|
end
|
222
|
-
|
240
|
+
|
223
241
|
it "sets the passed attributes" do
|
224
242
|
CreateTest.create(:data => @value).data.should == @value
|
225
243
|
end
|
@@ -227,7 +245,7 @@ describe "MongoDoc::Document" do
|
|
227
245
|
it "returns a valid document" do
|
228
246
|
CreateTest.should === CreateTest.create(:data => @value)
|
229
247
|
end
|
230
|
-
|
248
|
+
|
231
249
|
it "validates" do
|
232
250
|
CreateTest.create.errors.should_not be_empty
|
233
251
|
end
|
@@ -236,21 +254,21 @@ describe "MongoDoc::Document" do
|
|
236
254
|
CreateTest.should === CreateTest.create
|
237
255
|
end
|
238
256
|
end
|
239
|
-
|
257
|
+
|
240
258
|
context ".create!" do
|
241
259
|
it "creates a new document" do
|
242
260
|
obj = CreateTest.new
|
243
261
|
CreateTest.should_receive(:new).and_return(obj)
|
244
262
|
CreateTest.create! rescue nil
|
245
263
|
end
|
246
|
-
|
264
|
+
|
247
265
|
it "delegates to _create with safe => true" do
|
248
266
|
obj = CreateTest.new(:data => @value)
|
249
267
|
CreateTest.stub(:new).and_return(obj)
|
250
268
|
CreateTest.should_receive(:_create).with(obj, true).and_return(true)
|
251
269
|
CreateTest.create!(:data => @value)
|
252
270
|
end
|
253
|
-
|
271
|
+
|
254
272
|
it "sets the passed attributes" do
|
255
273
|
CreateTest.create!(:data => @value).data.should == @value
|
256
274
|
end
|
@@ -258,7 +276,7 @@ describe "MongoDoc::Document" do
|
|
258
276
|
it "returns a valid document" do
|
259
277
|
CreateTest.should === CreateTest.create!(:data => @value)
|
260
278
|
end
|
261
|
-
|
279
|
+
|
262
280
|
it "raises when invalid" do
|
263
281
|
expect do
|
264
282
|
CreateTest.create!
|
@@ -268,62 +286,68 @@ describe "MongoDoc::Document" do
|
|
268
286
|
end
|
269
287
|
|
270
288
|
context "#_create" do
|
271
|
-
class CreateTest
|
289
|
+
class CreateTest
|
290
|
+
include MongoDoc::Document
|
272
291
|
end
|
273
|
-
|
292
|
+
|
274
293
|
before do
|
275
294
|
@collection = stub('collection')
|
276
295
|
@collection.stub(:insert)
|
277
296
|
@doc = CreateTest.new
|
278
297
|
CreateTest.stub(:collection).and_return(@collection)
|
279
298
|
end
|
280
|
-
|
299
|
+
|
281
300
|
it "delegates to the collection insert with safe" do
|
282
301
|
safe = true
|
283
302
|
@collection.should_receive(:insert).with(@doc, hash_including(:safe => safe))
|
284
303
|
CreateTest.send(:_create, @doc, safe)
|
285
304
|
end
|
286
|
-
|
305
|
+
|
287
306
|
it "sets the _id of the document" do
|
288
307
|
id = 'id'
|
289
308
|
@collection.stub(:insert).and_return(id)
|
290
309
|
CreateTest.send(:_create, @doc, false)
|
291
310
|
@doc._id.should == id
|
292
311
|
end
|
293
|
-
|
312
|
+
|
294
313
|
it "returns the _id" do
|
295
314
|
id = 'id'
|
296
315
|
@collection.stub(:insert).and_return(id)
|
297
316
|
CreateTest.send(:_create, @doc, false).should == id
|
298
|
-
end
|
317
|
+
end
|
299
318
|
end
|
300
319
|
|
301
320
|
context "updating attributes" do
|
302
|
-
class UpdateAttributesRoot
|
321
|
+
class UpdateAttributesRoot
|
322
|
+
include MongoDoc::Document
|
323
|
+
|
303
324
|
has_one :update_attribute_child
|
304
325
|
end
|
305
|
-
|
306
|
-
class UpdateAttributesChild
|
326
|
+
|
327
|
+
class UpdateAttributesChild
|
328
|
+
include MongoDoc::Document
|
329
|
+
|
307
330
|
key :data
|
308
331
|
end
|
309
|
-
|
332
|
+
|
310
333
|
before do
|
311
334
|
@data = 'data'
|
312
335
|
@doc = UpdateAttributesChild.new
|
313
336
|
UpdateAttributesRoot.new.update_attribute_child = @doc
|
314
337
|
@attrs = {:data => @data}
|
315
338
|
@path_attrs = {'update_attribute_child.data' => @data}
|
316
|
-
@doc.stub(:
|
339
|
+
@doc.stub(:_naive_update_attributes)
|
317
340
|
end
|
318
341
|
|
319
342
|
context "#update_attributes" do
|
343
|
+
|
320
344
|
it "sets the attributes" do
|
321
345
|
@doc.update_attributes(@attrs)
|
322
346
|
@doc.data.should == @data
|
323
347
|
end
|
324
348
|
|
325
349
|
it "normalizes the attributes to the parent" do
|
326
|
-
@doc.should_receive(:
|
350
|
+
@doc.should_receive(:_path_to_root)
|
327
351
|
@doc.update_attributes(@attrs)
|
328
352
|
end
|
329
353
|
|
@@ -331,26 +355,36 @@ describe "MongoDoc::Document" do
|
|
331
355
|
@doc.should_receive(:valid?)
|
332
356
|
@doc.update_attributes(@attrs)
|
333
357
|
end
|
334
|
-
|
358
|
+
|
335
359
|
it "returns false if the object is not valid" do
|
336
360
|
@doc.stub(:valid?).and_return(false)
|
337
361
|
@doc.update_attributes(@attrs).should be_false
|
338
362
|
end
|
339
|
-
|
363
|
+
|
340
364
|
context "if valid" do
|
341
|
-
|
342
|
-
|
343
|
-
|
365
|
+
context "and strict" do
|
366
|
+
it "delegates to _strict_update_attributes" do
|
367
|
+
strict_attrs = @attrs.merge(:__strict__ => true)
|
368
|
+
@doc.should_receive(:_strict_update_attributes).with(@path_attrs, false)
|
369
|
+
@doc.update_attributes(strict_attrs)
|
370
|
+
end
|
344
371
|
end
|
345
372
|
|
346
|
-
|
373
|
+
context "and naive" do
|
374
|
+
it "delegates to _naive_update_attributes" do
|
375
|
+
@doc.should_receive(:_naive_update_attributes).with(@path_attrs, false)
|
376
|
+
@doc.update_attributes(@attrs)
|
377
|
+
end
|
378
|
+
end
|
379
|
+
|
380
|
+
it "returns the result of _naive_update_attributes" do
|
347
381
|
result = 'check'
|
348
|
-
@doc.stub(:
|
382
|
+
@doc.stub(:_naive_update_attributes).and_return(result)
|
349
383
|
@doc.update_attributes(@attrs).should == result
|
350
384
|
end
|
351
385
|
end
|
352
386
|
end
|
353
|
-
|
387
|
+
|
354
388
|
context "#update_attributes!" do
|
355
389
|
it "sets the attributes" do
|
356
390
|
@doc.update_attributes!(@attrs)
|
@@ -358,7 +392,7 @@ describe "MongoDoc::Document" do
|
|
358
392
|
end
|
359
393
|
|
360
394
|
it "normalizes the attributes to the parent" do
|
361
|
-
@doc.should_receive(:
|
395
|
+
@doc.should_receive(:_path_to_root)
|
362
396
|
@doc.update_attributes!(@attrs)
|
363
397
|
end
|
364
398
|
|
@@ -375,186 +409,120 @@ describe "MongoDoc::Document" do
|
|
375
409
|
end
|
376
410
|
|
377
411
|
context "if valid" do
|
378
|
-
|
379
|
-
|
380
|
-
|
412
|
+
context "and strict" do
|
413
|
+
it "delegates to _strict_update_attributes with safe == true" do
|
414
|
+
strict_attrs = @attrs.merge(:__strict__ => true)
|
415
|
+
@doc.should_receive(:_strict_update_attributes).with(@path_attrs, true)
|
416
|
+
@doc.update_attributes!(strict_attrs)
|
417
|
+
end
|
418
|
+
end
|
419
|
+
|
420
|
+
context "and naive" do
|
421
|
+
it "delegates to _naive_update_attributes with safe == true" do
|
422
|
+
@doc.should_receive(:_naive_update_attributes).with(@path_attrs, true)
|
423
|
+
@doc.update_attributes!(@attrs)
|
424
|
+
end
|
381
425
|
end
|
382
|
-
|
383
|
-
it "returns the result of
|
426
|
+
|
427
|
+
it "returns the result of _naive_update_attributes" do
|
384
428
|
result = 'check'
|
385
|
-
@doc.stub(:
|
429
|
+
@doc.stub(:_naive_update_attributes).and_return(result)
|
386
430
|
@doc.update_attributes!(@attrs).should == result
|
387
431
|
end
|
388
432
|
end
|
389
433
|
end
|
390
434
|
end
|
391
|
-
|
392
|
-
context "#_propose_update_attributes" do
|
393
|
-
class ProposeUpdateAttributes < MongoDoc::Document
|
394
|
-
end
|
395
435
|
|
396
|
-
|
397
|
-
|
398
|
-
|
436
|
+
context "#_naive_update_attributes" do
|
437
|
+
class NaiveUpdateAttributes
|
438
|
+
include MongoDoc::Document
|
399
439
|
end
|
400
|
-
|
401
|
-
context "when not a child" do
|
402
|
-
before do
|
403
|
-
@obj = ProposeUpdateAttributes.new
|
404
|
-
end
|
405
|
-
|
406
|
-
it "delegates to _update_attributes when not a child" do
|
407
|
-
@obj.should_receive(:_update_attributes).with(@attrs, @safe)
|
408
|
-
@obj.send(:_propose_update_attributes, @obj, @attrs, @safe)
|
409
|
-
end
|
410
|
-
|
411
|
-
it "returns the results of _update_attributes" do
|
412
|
-
result = 'check'
|
413
|
-
@obj.stub(:_update_attributes).and_return(result)
|
414
|
-
@obj.send(:_propose_update_attributes, @obj, @attrs, @safe).should == result
|
415
|
-
end
|
416
|
-
end
|
417
|
-
|
418
|
-
context "when a child" do
|
419
|
-
before do
|
420
|
-
@obj = ProposeUpdateAttributes.new
|
421
|
-
@parent = stub('parent')
|
422
|
-
@obj._parent = @parent
|
423
|
-
end
|
424
|
-
|
425
|
-
it "delegates to the parent when a child" do
|
426
|
-
@parent.should_receive(:_propose_update_attributes).with(@obj, @attrs, @safe)
|
427
|
-
@obj.send(:_propose_update_attributes, @obj, @attrs, @safe)
|
428
|
-
end
|
429
|
-
|
430
|
-
it "returns the results of the parent's _propose_update_attributes" do
|
431
|
-
result = 'check'
|
432
|
-
@parent.stub(:_propose_update_attributes).and_return(result)
|
433
|
-
@obj.send(:_propose_update_attributes, @obj, @attrs, @safe).should == result
|
434
|
-
end
|
435
|
-
end
|
436
|
-
end
|
437
440
|
|
438
|
-
context "#_update_attributes" do
|
439
|
-
class UpdateAttributes < MongoDoc::Document
|
440
|
-
end
|
441
|
-
|
442
441
|
before do
|
443
|
-
@
|
444
|
-
@
|
445
|
-
@
|
442
|
+
@id = 'id'
|
443
|
+
@attrs = {:data => 'data'}
|
444
|
+
@safe = false
|
445
|
+
@doc = NaiveUpdateAttributes.new
|
446
446
|
@doc.stub(:_id).and_return(@id)
|
447
|
-
|
448
|
-
@attrs = {:data => 'value'}
|
449
|
-
end
|
450
|
-
|
451
|
-
it "uses the set modifier for the attributes" do
|
452
|
-
safe = true
|
453
|
-
MongoDoc::Query.should_receive(:set_modifier).with(@attrs)
|
447
|
+
@collection = stub('collection')
|
454
448
|
@collection.stub(:update)
|
455
|
-
@doc.
|
449
|
+
@doc.stub(:_collection).and_return(@collection)
|
456
450
|
end
|
457
451
|
|
458
|
-
it "
|
459
|
-
safe
|
460
|
-
@
|
461
|
-
@doc.send(:_update_attributes, @attrs, safe)
|
452
|
+
it "calls update on the collection without a root" do
|
453
|
+
@collection.should_receive(:update).with({'_id' => @id}, MongoDoc::Query.set_modifier(@attrs), {:safe => @safe})
|
454
|
+
@doc.send(:_naive_update_attributes, @attrs, @safe)
|
462
455
|
end
|
463
456
|
|
464
|
-
it "
|
465
|
-
|
466
|
-
@
|
467
|
-
|
468
|
-
|
469
|
-
end
|
470
|
-
|
471
|
-
context "from a nested document" do
|
472
|
-
class NestedDocsRoot < MongoDoc::Document
|
473
|
-
has_many :nested_children
|
457
|
+
it "with a root, calls _naive_update_attributes on the root" do
|
458
|
+
root = NaiveUpdateAttributes.new
|
459
|
+
@doc.stub(:_root).and_return(root)
|
460
|
+
root.should_receive(:_naive_update_attributes).with(@attrs, @safe)
|
461
|
+
@doc.send(:_naive_update_attributes, @attrs, @safe)
|
474
462
|
end
|
463
|
+
end
|
475
464
|
|
476
|
-
|
477
|
-
|
465
|
+
context "#_strict_update_attributes" do
|
466
|
+
class StrictUpdateAttributes
|
467
|
+
include MongoDoc::Document
|
478
468
|
end
|
479
469
|
|
480
|
-
|
481
|
-
|
470
|
+
before do
|
471
|
+
@id = 'id'
|
472
|
+
@attrs = {:data => 'data'}
|
473
|
+
@selector = {'selector' => 'selector'}
|
474
|
+
@safe = false
|
475
|
+
@doc = StrictUpdateAttributes.new
|
476
|
+
@doc.stub(:_id).and_return(@id)
|
477
|
+
@collection = stub('collection')
|
478
|
+
@collection.stub(:update)
|
479
|
+
@doc.stub(:_collection).and_return(@collection)
|
482
480
|
end
|
483
481
|
|
484
|
-
context "
|
485
|
-
|
486
|
-
@
|
487
|
-
@
|
488
|
-
end
|
489
|
-
|
490
|
-
it "calls the root document's save" do
|
491
|
-
@root.should_receive(:save).with(true)
|
492
|
-
@leaf.save
|
493
|
-
end
|
494
|
-
|
495
|
-
it "(with bang!) calls the root documents save!" do
|
496
|
-
@root.should_receive(:save!)
|
497
|
-
@leaf.save!
|
482
|
+
context "without a root" do
|
483
|
+
it "calls update on the collection" do
|
484
|
+
@collection.should_receive(:update).with({'_id' => @id}.merge(@selector), MongoDoc::Query.set_modifier(@attrs), :safe => @safe)
|
485
|
+
@doc.send(:_strict_update_attributes, @attrs, @safe, @selector)
|
498
486
|
end
|
499
487
|
end
|
500
488
|
|
501
|
-
context "with
|
489
|
+
context "with a root" do
|
502
490
|
before do
|
503
|
-
@
|
504
|
-
@root
|
491
|
+
@root = StrictUpdateAttributes.new
|
492
|
+
@root.stub(:_collection).and_return(@collection)
|
493
|
+
@doc.stub(:_root).and_return(@root)
|
494
|
+
@doc.stub(:_selector_path_to_root).and_return({'path._id' => @id})
|
505
495
|
end
|
506
496
|
|
507
|
-
it "calls
|
508
|
-
@
|
509
|
-
@
|
497
|
+
it "calls _selector_path_to_root on our id" do
|
498
|
+
@doc.should_receive(:_selector_path_to_root).with('_id' => @id).and_return({'path._id' => @id})
|
499
|
+
@doc.send(:_strict_update_attributes, @attrs, @safe)
|
510
500
|
end
|
511
501
|
|
512
|
-
it "
|
513
|
-
@root.should_receive(:
|
514
|
-
@
|
502
|
+
it "calls _strict_update_attributes on the root with our selector" do
|
503
|
+
@root.should_receive(:_strict_update_attributes).with(@attrs, @safe, 'path._id' => @id)
|
504
|
+
@doc.send(:_strict_update_attributes, @attrs, @safe)
|
515
505
|
end
|
516
506
|
end
|
517
|
-
|
518
|
-
context "with has_many, update_attributes" do
|
519
|
-
before do
|
520
|
-
@leaf = LeafDoc.new
|
521
|
-
NestedDocsRoot.new(:nested_children => [NestedChild.new(:leaf => @leaf)])
|
522
|
-
end
|
523
|
-
|
524
|
-
it "returns false" do
|
525
|
-
@leaf.update_attributes(:data => 'data').should be_false
|
526
|
-
end
|
527
|
-
|
528
|
-
it "sets an error on base" do
|
529
|
-
@leaf.update_attributes(:data => 'data')
|
530
|
-
@leaf.errors[:base].should_not be_nil
|
531
|
-
end
|
532
|
-
|
533
|
-
it "(with bang!) returns false" do
|
534
|
-
@leaf.update_attributes!(:data => 'data').should be_false
|
535
|
-
end
|
536
|
-
|
537
|
-
it "(with bang!) sets an error on base" do
|
538
|
-
@leaf.update_attributes(:data => 'data')
|
539
|
-
@leaf.errors[:base].should_not be_nil
|
540
|
-
end
|
541
|
-
end
|
542
|
-
|
543
507
|
end
|
544
508
|
|
545
509
|
describe "bson" do
|
546
|
-
class BSONTest
|
510
|
+
class BSONTest
|
511
|
+
include MongoDoc::Document
|
512
|
+
|
547
513
|
key :other
|
548
514
|
end
|
549
515
|
|
550
516
|
class BSONDerived < BSONTest
|
517
|
+
include MongoDoc::Document
|
518
|
+
|
551
519
|
key :derived
|
552
520
|
end
|
553
|
-
|
521
|
+
|
554
522
|
class OtherObject
|
555
523
|
attr_accessor :value
|
556
524
|
end
|
557
|
-
|
525
|
+
|
558
526
|
before do
|
559
527
|
@value = 'value'
|
560
528
|
@other = OtherObject.new
|
@@ -565,7 +533,7 @@ describe "MongoDoc::Document" do
|
|
565
533
|
it "encodes the class for the object" do
|
566
534
|
@doc.to_bson[MongoDoc::BSON::CLASS_KEY].should == BSONTest.name
|
567
535
|
end
|
568
|
-
|
536
|
+
|
569
537
|
it "renders a json representation of the object" do
|
570
538
|
@doc.to_bson.should be_bson_eql({MongoDoc::BSON::CLASS_KEY => BSONTest.name, "other" => {MongoDoc::BSON::CLASS_KEY => OtherObject.name, "value" => @value}})
|
571
539
|
end
|
@@ -594,11 +562,15 @@ describe "MongoDoc::Document" do
|
|
594
562
|
|
595
563
|
context "associations" do
|
596
564
|
context "has_one" do
|
597
|
-
class TestHasOneBsonDoc
|
565
|
+
class TestHasOneBsonDoc
|
566
|
+
include MongoDoc::Document
|
567
|
+
|
598
568
|
has_one :subdoc
|
599
569
|
end
|
600
570
|
|
601
|
-
class SubHasOneBsonDoc
|
571
|
+
class SubHasOneBsonDoc
|
572
|
+
include MongoDoc::Document
|
573
|
+
|
602
574
|
key :attr
|
603
575
|
end
|
604
576
|
|
@@ -621,11 +593,14 @@ describe "MongoDoc::Document" do
|
|
621
593
|
|
622
594
|
context "has_many" do
|
623
595
|
|
624
|
-
class SubHasManyBsonDoc
|
596
|
+
class SubHasManyBsonDoc
|
597
|
+
include MongoDoc::Document
|
598
|
+
|
625
599
|
key :attr
|
626
600
|
end
|
627
601
|
|
628
|
-
class TestHasManyBsonDoc
|
602
|
+
class TestHasManyBsonDoc
|
603
|
+
include MongoDoc::Document
|
629
604
|
has_many :subdoc, :class_name => 'SubHasManyBsonDoc'
|
630
605
|
end
|
631
606
|
|
@@ -654,14 +629,8 @@ describe "MongoDoc::Document" do
|
|
654
629
|
end
|
655
630
|
|
656
631
|
context "misc class methods" do
|
657
|
-
class ClassMethods
|
658
|
-
|
659
|
-
|
660
|
-
it ".count calls the collection count" do
|
661
|
-
collection = stub('collection')
|
662
|
-
ClassMethods.stub(:collection).and_return(collection)
|
663
|
-
collection.should_receive(:count).and_return(1)
|
664
|
-
ClassMethods.count
|
632
|
+
class ClassMethods
|
633
|
+
include MongoDoc::Document
|
665
634
|
end
|
666
635
|
|
667
636
|
it ".collection_name returns the name of the collection for this class" do
|