jnunemaker-mongomapper 0.2.0 → 0.3.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.
Files changed (55) hide show
  1. data/.gitignore +1 -0
  2. data/History +17 -0
  3. data/README.rdoc +6 -3
  4. data/Rakefile +3 -2
  5. data/VERSION +1 -1
  6. data/bin/mmconsole +56 -0
  7. data/lib/mongomapper.rb +48 -17
  8. data/lib/mongomapper/associations.rb +31 -39
  9. data/lib/mongomapper/associations/base.rb +40 -22
  10. data/lib/mongomapper/associations/belongs_to_polymorphic_proxy.rb +33 -0
  11. data/lib/mongomapper/associations/belongs_to_proxy.rb +10 -14
  12. data/lib/mongomapper/associations/many_embedded_polymorphic_proxy.rb +34 -0
  13. data/lib/mongomapper/associations/{has_many_embedded_proxy.rb → many_embedded_proxy.rb} +5 -5
  14. data/lib/mongomapper/associations/many_proxy.rb +55 -0
  15. data/lib/mongomapper/associations/proxy.rb +21 -14
  16. data/lib/mongomapper/callbacks.rb +1 -1
  17. data/lib/mongomapper/document.rb +82 -59
  18. data/lib/mongomapper/embedded_document.rb +121 -130
  19. data/lib/mongomapper/finder_options.rb +21 -6
  20. data/lib/mongomapper/key.rb +5 -7
  21. data/lib/mongomapper/observing.rb +1 -41
  22. data/lib/mongomapper/pagination.rb +52 -0
  23. data/lib/mongomapper/rails_compatibility/document.rb +15 -0
  24. data/lib/mongomapper/rails_compatibility/embedded_document.rb +25 -0
  25. data/lib/mongomapper/serialization.rb +1 -1
  26. data/mongomapper.gemspec +62 -36
  27. data/test/NOTE_ON_TESTING +1 -0
  28. data/test/functional/test_associations.rb +485 -0
  29. data/test/{test_callbacks.rb → functional/test_callbacks.rb} +2 -1
  30. data/test/functional/test_document.rb +636 -0
  31. data/test/functional/test_pagination.rb +82 -0
  32. data/test/functional/test_rails_compatibility.rb +31 -0
  33. data/test/functional/test_validations.rb +172 -0
  34. data/test/models.rb +92 -0
  35. data/test/test_helper.rb +5 -0
  36. data/test/{serializers → unit/serializers}/test_json_serializer.rb +0 -0
  37. data/test/unit/test_association_base.rb +131 -0
  38. data/test/unit/test_document.rb +115 -0
  39. data/test/{test_embedded_document.rb → unit/test_embedded_document.rb} +158 -66
  40. data/test/{test_finder_options.rb → unit/test_finder_options.rb} +66 -0
  41. data/test/{test_key.rb → unit/test_key.rb} +13 -1
  42. data/test/unit/test_mongo_id.rb +35 -0
  43. data/test/{test_mongomapper.rb → unit/test_mongomapper.rb} +0 -0
  44. data/test/{test_observing.rb → unit/test_observing.rb} +0 -0
  45. data/test/unit/test_pagination.rb +113 -0
  46. data/test/unit/test_rails_compatibility.rb +34 -0
  47. data/test/{test_serializations.rb → unit/test_serializations.rb} +0 -2
  48. data/test/{test_validations.rb → unit/test_validations.rb} +0 -134
  49. metadata +68 -36
  50. data/lib/mongomapper/associations/has_many_proxy.rb +0 -28
  51. data/lib/mongomapper/associations/polymorphic_belongs_to_proxy.rb +0 -31
  52. data/lib/mongomapper/rails_compatibility.rb +0 -23
  53. data/test/test_associations.rb +0 -149
  54. data/test/test_document.rb +0 -944
  55. data/test/test_rails_compatibility.rb +0 -29
@@ -29,7 +29,8 @@ class CallbacksTest < Test::Unit::TestCase
29
29
  @history = nil
30
30
  end
31
31
  end
32
- @document.collection.clear
32
+
33
+ clear_all_collections
33
34
  end
34
35
 
35
36
  should "get the order right for creating documents" do
@@ -0,0 +1,636 @@
1
+ require 'test_helper'
2
+
3
+ class Address
4
+ include MongoMapper::EmbeddedDocument
5
+ key :city, String
6
+ key :state, String
7
+ end
8
+
9
+ class DocumentTest < Test::Unit::TestCase
10
+ def setup
11
+ @document = Class.new do
12
+ include MongoMapper::Document
13
+ collection 'users'
14
+
15
+ key :first_name, String
16
+ key :last_name, String
17
+ key :age, Integer
18
+ end
19
+
20
+ @document.collection.clear
21
+ end
22
+
23
+ context "Document Class Methods" do
24
+ context "Using key with type Array" do
25
+ setup do
26
+ @document.key :tags, Array
27
+ end
28
+
29
+ should "work" do
30
+ doc = @document.new
31
+ doc.tags.should == []
32
+ doc.tags = %w(foo bar)
33
+ doc.save
34
+ doc.tags.should == %w(foo bar)
35
+ @document.find(doc.id).tags.should == %w(foo bar)
36
+ end
37
+ end
38
+
39
+ context "Using key with type Hash" do
40
+ setup do
41
+ @document.key :foo, Hash
42
+ end
43
+
44
+ should "work with indifferent access" do
45
+ doc = @document.new
46
+ doc.foo = {:baz => 'bar'}
47
+ doc.save
48
+
49
+ doc = @document.find(doc.id)
50
+ doc.foo[:baz].should == 'bar'
51
+ doc.foo['baz'].should == 'bar'
52
+ end
53
+ end
54
+
55
+ context "Saving a document with an embedded document" do
56
+ setup do
57
+ @document.class_eval do
58
+ key :foo, Address
59
+ end
60
+ end
61
+
62
+ should "embed embedded document" do
63
+ address = Address.new(:city => 'South Bend', :state => 'IN')
64
+ doc = @document.new(:foo => address)
65
+ doc.save
66
+ doc.foo.city.should == 'South Bend'
67
+ doc.foo.state.should == 'IN'
68
+
69
+ from_db = @document.find(doc.id)
70
+ from_db.foo.city.should == 'South Bend'
71
+ from_db.foo.state.should == 'IN'
72
+ end
73
+ end
74
+
75
+ context "Creating a single document" do
76
+ setup do
77
+ @doc_instance = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
78
+ end
79
+
80
+ should "create a document in correct collection" do
81
+ @document.count.should == 1
82
+ end
83
+
84
+ should "automatically set id" do
85
+ @doc_instance.id.should_not be_nil
86
+ @doc_instance.id.size.should == 24
87
+ end
88
+
89
+ should "return instance of document" do
90
+ @doc_instance.should be_instance_of(@document)
91
+ @doc_instance.first_name.should == 'John'
92
+ @doc_instance.last_name.should == 'Nunemaker'
93
+ @doc_instance.age.should == 27
94
+ end
95
+ end
96
+
97
+ context "Creating a document with no attributes provided" do
98
+ setup do
99
+ @document = Class.new do
100
+ include MongoMapper::Document
101
+ end
102
+ @document.collection.clear
103
+ end
104
+
105
+ should "create the document" do
106
+ lambda {
107
+ @document.create
108
+ }.should change { @document.count }.by(1)
109
+ end
110
+ end
111
+
112
+
113
+ context "Creating multiple documents" do
114
+ setup do
115
+ @doc_instances = @document.create([
116
+ {:first_name => 'John', :last_name => 'Nunemaker', :age => '27'},
117
+ {:first_name => 'Steve', :last_name => 'Smith', :age => '28'},
118
+ ])
119
+ end
120
+
121
+ should "create multiple documents" do
122
+ @document.count.should == 2
123
+ end
124
+
125
+ should "return an array of doc instances" do
126
+ @doc_instances.map do |doc_instance|
127
+ doc_instance.should be_instance_of(@document)
128
+ end
129
+ end
130
+ end
131
+
132
+ context "Updating a document" do
133
+ setup do
134
+ doc = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
135
+ @doc_instance = @document.update(doc.id, {:age => 40})
136
+ end
137
+
138
+ should "update attributes provided" do
139
+ @doc_instance.age.should == 40
140
+ end
141
+
142
+ should "not update existing attributes that were not set to update" do
143
+ @doc_instance.first_name.should == 'John'
144
+ @doc_instance.last_name.should == 'Nunemaker'
145
+ end
146
+
147
+ should "not create new document" do
148
+ @document.count.should == 1
149
+ end
150
+ end
151
+
152
+ should "raise error when updating single doc if not provided id and attributes" do
153
+ doc = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
154
+ lambda { @document.update }.should raise_error(ArgumentError)
155
+ lambda { @document.update(doc.id) }.should raise_error(ArgumentError)
156
+ lambda { @document.update(doc.id, [1]) }.should raise_error(ArgumentError)
157
+ end
158
+
159
+ context "Updating multiple documents" do
160
+ setup do
161
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
162
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
163
+
164
+ @doc_instances = @document.update({
165
+ @doc1.id => {:age => 30},
166
+ @doc2.id => {:age => 30},
167
+ })
168
+ end
169
+
170
+ should "not create any new documents" do
171
+ @document.count.should == 2
172
+ end
173
+
174
+ should "should return an array of doc instances" do
175
+ @doc_instances.map do |doc_instance|
176
+ doc_instance.should be_instance_of(@document)
177
+ end
178
+ end
179
+
180
+ should "update the documents" do
181
+ @document.find(@doc1.id).age.should == 30
182
+ @document.find(@doc2.id).age.should == 30
183
+ end
184
+ end
185
+
186
+ should "raise error when updating multiple documents if not a hash" do
187
+ lambda { @document.update([1, 2]) }.should raise_error(ArgumentError)
188
+ end
189
+
190
+ context "Finding documents" do
191
+ setup do
192
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
193
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
194
+ @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
195
+ end
196
+
197
+ should "raise document not found if nothing provided" do
198
+ lambda { @document.find }.should raise_error(MongoMapper::DocumentNotFound)
199
+ end
200
+
201
+ context "with a single id" do
202
+ should "work" do
203
+ @document.find(@doc1.id).should == @doc1
204
+ end
205
+
206
+ should "raise error if document not found" do
207
+ lambda { @document.find(MongoID.new) }.should raise_error(MongoMapper::DocumentNotFound)
208
+ end
209
+
210
+ should "raise error if id is illegal" do
211
+ lambda { @document.find(1) }.should raise_error(MongoMapper::DocumentNotFound)
212
+ end
213
+ end
214
+
215
+ context "with multiple id's" do
216
+ should "work as arguments" do
217
+ @document.find(@doc1.id, @doc2.id).should == [@doc1, @doc2]
218
+ end
219
+
220
+ should "work as array" do
221
+ @document.find([@doc1.id, @doc2.id]).should == [@doc1, @doc2]
222
+ end
223
+ end
224
+
225
+ context "with :all" do
226
+ should "find all documents" do
227
+ @document.find(:all).should == [@doc1, @doc2, @doc3]
228
+ end
229
+
230
+ should "be able to add conditions" do
231
+ @document.find(:all, :conditions => {:first_name => 'John'}).should == [@doc1]
232
+ end
233
+ end
234
+
235
+ context "with #all" do
236
+ should "find all documents based on criteria" do
237
+ @document.all.should == [@doc1, @doc2, @doc3]
238
+ @document.all(:conditions => {:last_name => 'Nunemaker'}).should == [@doc1, @doc3]
239
+ end
240
+ end
241
+
242
+ context "with :first" do
243
+ should "find first document" do
244
+ @document.find(:first).should == @doc1
245
+ end
246
+ end
247
+
248
+ context "with #first" do
249
+ should "find first document based on criteria" do
250
+ @document.first.should == @doc1
251
+ @document.first(:conditions => {:age => 28}).should == @doc2
252
+ end
253
+ end
254
+
255
+ context "with :last" do
256
+ should "find last document" do
257
+ @document.find(:last).should == @doc3
258
+ end
259
+ end
260
+
261
+ context "with #last" do
262
+ should "find last document based on criteria" do
263
+ @document.last.should == @doc3
264
+ @document.last(:conditions => {:age => 28}).should == @doc2
265
+ end
266
+ end
267
+ end # finding documents
268
+
269
+ context "Finding document by id" do
270
+ setup do
271
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
272
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
273
+ end
274
+
275
+ should "be able to find by id" do
276
+ @document.find_by_id(@doc1.id).should == @doc1
277
+ @document.find_by_id(@doc2.id).should == @doc2
278
+ end
279
+
280
+ should "return nil if document not found" do
281
+ @document.find_by_id(MongoID.new).should be(nil)
282
+ end
283
+ end
284
+
285
+ context "Deleting a document" do
286
+ setup do
287
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
288
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
289
+ @document.delete(@doc1.id)
290
+ end
291
+
292
+ should "remove document from collection" do
293
+ @document.count.should == 1
294
+ end
295
+
296
+ should "not remove other documents" do
297
+ @document.find(@doc2.id).should_not be(nil)
298
+ end
299
+ end
300
+
301
+ context "Deleting multiple documents" do
302
+ should "work with multiple arguments" do
303
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
304
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
305
+ @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
306
+ @document.delete(@doc1.id, @doc2.id)
307
+
308
+ @document.count.should == 1
309
+ end
310
+
311
+ should "work with array as argument" do
312
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
313
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
314
+ @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
315
+ @document.delete([@doc1.id, @doc2.id])
316
+
317
+ @document.count.should == 1
318
+ end
319
+ end
320
+
321
+ context "Deleting all documents" do
322
+ setup do
323
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
324
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
325
+ @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
326
+ end
327
+
328
+ should "remove all documents when given no conditions" do
329
+ @document.delete_all
330
+ @document.count.should == 0
331
+ end
332
+
333
+ should "only remove matching documents when given conditions" do
334
+ @document.delete_all({:first_name => 'John'})
335
+ @document.count.should == 2
336
+ end
337
+
338
+ should "convert the conditions to mongo criteria" do
339
+ @document.delete_all(:age => [26, 27])
340
+ @document.count.should == 1
341
+ end
342
+ end
343
+
344
+ context "Destroying a document" do
345
+ setup do
346
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
347
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
348
+ @document.destroy(@doc1.id)
349
+ end
350
+
351
+ should "remove document from collection" do
352
+ @document.count.should == 1
353
+ end
354
+
355
+ should "not remove other documents" do
356
+ @document.find(@doc2.id).should_not be(nil)
357
+ end
358
+ end
359
+
360
+ context "Destroying multiple documents" do
361
+ should "work with multiple arguments" do
362
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
363
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
364
+ @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
365
+ @document.destroy(@doc1.id, @doc2.id)
366
+
367
+ @document.count.should == 1
368
+ end
369
+
370
+ should "work with array as argument" do
371
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
372
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
373
+ @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
374
+ @document.destroy([@doc1.id, @doc2.id])
375
+
376
+ @document.count.should == 1
377
+ end
378
+ end
379
+
380
+ context "Destroying all documents" do
381
+ setup do
382
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
383
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
384
+ @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
385
+ end
386
+
387
+ should "remove all documents when given no conditions" do
388
+ @document.destroy_all
389
+ @document.count.should == 0
390
+ end
391
+
392
+ should "only remove matching documents when given conditions" do
393
+ @document.destroy_all(:first_name => 'John')
394
+ @document.count.should == 2
395
+ @document.destroy_all(:age => 26)
396
+ @document.count.should == 1
397
+ end
398
+
399
+ should "convert the conditions to mongo criteria" do
400
+ @document.destroy_all(:age => [26, 27])
401
+ @document.count.should == 1
402
+ end
403
+ end
404
+
405
+ context "Counting documents in collection" do
406
+ setup do
407
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
408
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
409
+ @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
410
+ end
411
+
412
+ should "count all with no arguments" do
413
+ @document.count.should == 3
414
+ end
415
+
416
+ should "return 0 if there are no documents in the collection" do
417
+ @document.delete_all
418
+ @document.count.should == 0
419
+ end
420
+
421
+ should "return 0 if the collection does not exist" do
422
+ klass = Class.new do
423
+ include MongoMapper::Document
424
+ collection 'foobarbazwickdoesnotexist'
425
+ end
426
+
427
+ klass.count.should == 0
428
+ end
429
+
430
+ should "return count for matching documents if conditions provided" do
431
+ @document.count(:age => 27).should == 1
432
+ end
433
+
434
+ should "convert the conditions to mongo criteria" do
435
+ @document.count(:age => [26, 27]).should == 2
436
+ end
437
+ end
438
+
439
+ context "Indexing" do
440
+ setup do
441
+ @document.collection.drop_indexes
442
+ end
443
+
444
+ should "allow creating index for a key" do
445
+ index_name = nil
446
+ lambda {
447
+ index_name = @document.ensure_index :first_name
448
+ }.should change { @document.collection.index_information.size }.by(1)
449
+
450
+ index_name.should == 'first_name_1'
451
+ index = @document.collection.index_information[index_name]
452
+ index.should_not be_nil
453
+ index.should include(['first_name', 1])
454
+ end
455
+
456
+ should "allow creating unique index for a key" do
457
+ @document.collection.expects(:create_index).with(:first_name, true)
458
+ @document.ensure_index :first_name, :unique => true
459
+ end
460
+
461
+ should "allow creating index on multiple keys" do
462
+ index_name = nil
463
+ lambda {
464
+ index_name = @document.ensure_index [[:first_name, 1], [:last_name, -1]]
465
+ }.should change { @document.collection.index_information.size }.by(1)
466
+
467
+ index_name.should == 'first_name_1_last_name_-1'
468
+
469
+ index = @document.collection.index_information[index_name]
470
+ index.should_not be_nil
471
+ index.should include(['first_name', 1])
472
+ index.should include(['last_name', -1])
473
+ end
474
+
475
+ should "work with :index shortcut when defining key" do
476
+ @document.expects(:ensure_index).with('father').returns(nil)
477
+ @document.key :father, String, :index => true
478
+ end
479
+ end
480
+ end # Document Class Methods
481
+
482
+ context "Saving a new document" do
483
+ setup do
484
+ @doc = @document.new(:first_name => 'John', :age => '27')
485
+ @doc.save
486
+ end
487
+
488
+ should "insert document into the collection" do
489
+ @document.count.should == 1
490
+ end
491
+
492
+ should "assign an id for the document" do
493
+ @doc.id.should_not be(nil)
494
+ @doc.id.size.should == 24
495
+ end
496
+
497
+ should "save attributes" do
498
+ @doc.first_name.should == 'John'
499
+ @doc.age.should == 27
500
+ end
501
+
502
+ should "update attributes in the database" do
503
+ from_db = @document.find(@doc.id)
504
+ from_db.should == @doc
505
+ from_db.first_name.should == 'John'
506
+ from_db.age.should == 27
507
+ end
508
+ end
509
+
510
+ context "Saving an existing document" do
511
+ setup do
512
+ @doc = @document.create(:first_name => 'John', :age => '27')
513
+ @doc.first_name = 'Johnny'
514
+ @doc.age = 30
515
+ @doc.save
516
+ end
517
+
518
+ should "not insert document into collection" do
519
+ @document.count.should == 1
520
+ end
521
+
522
+ should "update attributes" do
523
+ @doc.first_name.should == 'Johnny'
524
+ @doc.age.should == 30
525
+ end
526
+
527
+ should "update attributes in the database" do
528
+ from_db = @document.find(@doc.id)
529
+ from_db.first_name.should == 'Johnny'
530
+ from_db.age.should == 30
531
+ end
532
+ end
533
+
534
+ context "Calling update attributes on a new document" do
535
+ setup do
536
+ @doc = @document.new(:first_name => 'John', :age => '27')
537
+ @doc.update_attributes(:first_name => 'Johnny', :age => 30)
538
+ end
539
+
540
+ should "insert document into the collection" do
541
+ @document.count.should == 1
542
+ end
543
+
544
+ should "assign an id for the document" do
545
+ @doc.id.should_not be(nil)
546
+ @doc.id.size.should == 24
547
+ end
548
+
549
+ should "save attributes" do
550
+ @doc.first_name.should == 'Johnny'
551
+ @doc.age.should == 30
552
+ end
553
+
554
+ should "update attributes in the database" do
555
+ from_db = @document.find(@doc.id)
556
+ from_db.should == @doc
557
+ from_db.first_name.should == 'Johnny'
558
+ from_db.age.should == 30
559
+ end
560
+ end
561
+
562
+ context "Updating an existing document using update attributes" do
563
+ setup do
564
+ @doc = @document.create(:first_name => 'John', :age => '27')
565
+ @doc.update_attributes(:first_name => 'Johnny', :age => 30)
566
+ end
567
+
568
+ should "not insert document into collection" do
569
+ @document.count.should == 1
570
+ end
571
+
572
+ should_eventually "update attributes" do
573
+ @doc.first_name.should == 'Johnny'
574
+ @doc.age.should == 30
575
+ end
576
+
577
+ should_eventually "update attributes in the database" do
578
+ from_db = @document.find(@doc.id)
579
+ from_db.first_name.should == 'Johnny'
580
+ from_db.age.should == 30
581
+ end
582
+ end
583
+
584
+ context "Destroying a document that exists" do
585
+ setup do
586
+ @doc = @document.create(:first_name => 'John', :age => '27')
587
+ @doc.destroy
588
+ end
589
+
590
+ should "remove the document from the collection" do
591
+ @document.count.should == 0
592
+ end
593
+
594
+ should "raise error if assignment is attempted" do
595
+ lambda { @doc.first_name = 'Foo' }.should raise_error(TypeError)
596
+ end
597
+ end
598
+
599
+ context "Destroying a document that is a new" do
600
+ setup do
601
+ setup do
602
+ @doc = @document.new(:first_name => 'John Nunemaker', :age => '27')
603
+ @doc.destroy
604
+ end
605
+
606
+ should "not affect collection count" do
607
+ @document.collection.count.should == 0
608
+ end
609
+
610
+ should "raise error if assignment is attempted" do
611
+ lambda { @doc.first_name = 'Foo' }.should raise_error(TypeError)
612
+ end
613
+ end
614
+ end
615
+
616
+ context "timestamping" do
617
+ should "set created_at and updated_at on create" do
618
+ doc = @document.new(:first_name => 'John', :age => 27)
619
+ doc.created_at.should be(nil)
620
+ doc.updated_at.should be(nil)
621
+ doc.save
622
+ doc.created_at.should_not be(nil)
623
+ doc.updated_at.should_not be(nil)
624
+ end
625
+
626
+ should "set updated_at on update but leave created_at alone" do
627
+ doc = @document.create(:first_name => 'John', :age => 27)
628
+ old_created_at = doc.created_at
629
+ old_updated_at = doc.updated_at
630
+ doc.first_name = 'Johnny'
631
+ doc.save
632
+ doc.created_at.should == old_created_at
633
+ doc.updated_at.should_not == old_updated_at
634
+ end
635
+ end
636
+ end