forked from mongomapper/mongomapper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_document.rb
761 lines (628 loc) · 23.5 KB
/
test_document.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
require 'test_helper'
require 'models'
class DocumentTest < Test::Unit::TestCase
def setup
@document = Class.new do
include MongoMapper::Document
collection 'users'
key :first_name, String
key :last_name, String
key :age, Integer
end
@document.collection.clear
end
context "Saving a document with a custom id" do
should "clear custom id flag when saved" do
doc = @document.new(:id => '1234')
doc.using_custom_id?.should be_true
doc.save.should be_true
doc.using_custom_id?.should be_false
end
end
context "Document Class Methods" do
context "Using key with type Array" do
setup do
@document.key :tags, Array
end
should "give correct default" do
doc = @document.new
doc.tags.should == []
end
should "work with assignment" do
doc = @document.new
doc.tags = %w(foo bar)
doc.tags.should == %w(foo bar)
end
should "work with assignment after saving" do
doc = @document.new
doc.tags = %w(foo bar)
doc.save
doc.tags.should == %w(foo bar)
@document.find(doc.id).tags.should == %w(foo bar)
end
should "work with assignment then <<" do
doc = @document.new
doc.tags = []
doc.tags << "foo"
doc.tags.should == ["foo"]
end
should "work with <<" do
doc = @document.new
doc.tags << "foo"
doc.tags.should == ["foo"]
end
should "work with << then save" do
doc = @document.new
doc.tags << "foo"
doc.tags << "bar"
doc.save
doc.tags.should == %w(foo bar)
@document.find(doc.id).tags.should == %w(foo bar)
end
end
context "Using key with type Hash" do
setup do
@document.key :foo, Hash
end
should "give correct default" do
doc = @document.new
doc.foo.should == {}
end
should "work with []=" do
doc = @document.new
doc.foo["quux"] = "bar"
doc.foo["quux"].should == "bar"
doc.foo.should == { "quux" => "bar" }
end
should "work with indifferent access" do
doc = @document.new
doc.foo = {:baz => 'bar'}
doc.foo[:baz].should == 'bar'
doc.foo['baz'].should == 'bar'
end
should "work with indifferent access after save" do
doc = @document.new
doc.foo = {:baz => 'bar'}
doc.save
doc = @document.find(doc.id)
doc.foo[:baz].should == 'bar'
doc.foo['baz'].should == 'bar'
end
end
context "Saving a document with an embedded document" do
setup do
@document.class_eval do
key :foo, Address
end
end
should "embed embedded document" do
address = Address.new(:city => 'South Bend', :state => 'IN')
doc = @document.new(:foo => address)
doc.save
doc.foo.city.should == 'South Bend'
doc.foo.state.should == 'IN'
from_db = @document.find(doc.id)
from_db.foo.city.should == 'South Bend'
from_db.foo.state.should == 'IN'
end
end
context "Creating a single document" do
setup do
@doc_instance = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
end
should "create a document in correct collection" do
@document.count.should == 1
end
should "automatically set id" do
@doc_instance.id.should_not be_nil
@doc_instance.id.size.should == 24
end
should "no longer be new?" do
@doc_instance.new?.should be_false
end
should "return instance of document" do
@doc_instance.should be_instance_of(@document)
@doc_instance.first_name.should == 'John'
@doc_instance.last_name.should == 'Nunemaker'
@doc_instance.age.should == 27
end
end
context "Creating a document with no attributes provided" do
setup do
@document = Class.new do
include MongoMapper::Document
end
@document.collection.clear
end
should "create the document" do
lambda {
@document.create
}.should change { @document.count }.by(1)
end
end
context "Creating multiple documents" do
setup do
@doc_instances = @document.create([
{:first_name => 'John', :last_name => 'Nunemaker', :age => '27'},
{:first_name => 'Steve', :last_name => 'Smith', :age => '28'},
])
end
should "create multiple documents" do
@document.count.should == 2
end
should "return an array of doc instances" do
@doc_instances.map do |doc_instance|
doc_instance.should be_instance_of(@document)
end
end
end
context "Updating a document" do
setup do
doc = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
@doc_instance = @document.update(doc.id, {:age => 40})
end
should "update attributes provided" do
@doc_instance.age.should == 40
end
should "not update existing attributes that were not set to update" do
@doc_instance.first_name.should == 'John'
@doc_instance.last_name.should == 'Nunemaker'
end
should "not create new document" do
@document.count.should == 1
end
end
should "raise error when updating single doc if not provided id and attributes" do
doc = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
lambda { @document.update }.should raise_error(ArgumentError)
lambda { @document.update(doc.id) }.should raise_error(ArgumentError)
lambda { @document.update(doc.id, [1]) }.should raise_error(ArgumentError)
end
context "Updating multiple documents" do
setup do
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
@doc_instances = @document.update({
@doc1.id => {:age => 30},
@doc2.id => {:age => 30},
})
end
should "not create any new documents" do
@document.count.should == 2
end
should "should return an array of doc instances" do
@doc_instances.map do |doc_instance|
doc_instance.should be_instance_of(@document)
end
end
should "update the documents" do
@document.find(@doc1.id).age.should == 30
@document.find(@doc2.id).age.should == 30
end
end
should "raise error when updating multiple documents if not a hash" do
lambda { @document.update([1, 2]) }.should raise_error(ArgumentError)
end
context "Finding documents" do
setup do
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
end
should "raise document not found if nothing provided" do
lambda { @document.find }.should raise_error(MongoMapper::DocumentNotFound)
end
context "with a single id" do
should "work" do
@document.find(@doc1.id).should == @doc1
end
should "raise error if document not found" do
lambda { @document.find(123) }.should raise_error(MongoMapper::DocumentNotFound)
end
end
context "with multiple id's" do
should "work as arguments" do
@document.find(@doc1.id, @doc2.id).should == [@doc1, @doc2]
end
should "work as array" do
@document.find([@doc1.id, @doc2.id]).should == [@doc1, @doc2]
end
end
context "with :all" do
should "find all documents" do
@document.find(:all, :order => 'first_name').should == [@doc1, @doc3, @doc2]
end
should "be able to add conditions" do
@document.find(:all, :conditions => {:first_name => 'John'}).should == [@doc1]
end
end
context "with #all" do
should "find all documents based on criteria" do
@document.all(:order => 'first_name').should == [@doc1, @doc3, @doc2]
@document.all(:conditions => {:last_name => 'Nunemaker'}).should == [@doc1, @doc3]
end
end
context "with :first" do
should "find first document" do
@document.find(:first, :order => 'first_name').should == @doc1
end
end
context "with #first" do
should "find first document based on criteria" do
@document.first(:order => 'first_name').should == @doc1
@document.first(:conditions => {:age => 28}).should == @doc2
end
end
context "with :last" do
should "find last document" do
@document.find(:last).should == @doc3
end
end
context "with #last" do
should "find last document based on criteria" do
@document.last.should == @doc3
@document.last(:conditions => {:age => 28}).should == @doc2
end
end
context "with :find_by" do
should "find document based on argument" do
@document.find_by_first_name('John').should == @doc1
@document.find_by_last_name('Nunemaker').should == @doc1
@document.find_by_age(27).should == @doc1
end
should "not raise error" do
@document.find_by_first_name('Mongo').should be_nil
end
should "define a method for each key" do
@document.methods(false).select { |e| e =~ /^find_by_/ }.size == @document.keys.size
end
end
context "with dynamic finders" do
should "find document based on all arguments" do
@document.find_by_first_name_and_last_name_and_age('John', 'Nunemaker', 27).should == @doc1
end
should "not find the document if an argument is wrong" do
@document.find_by_first_name_and_last_name_and_age('John', 'Nunemaker', 28).should be_nil
end
should "find all documents based on arguments" do
docs = @document.find_all_by_last_name('Nunemaker')
docs.should be_kind_of(Array)
docs.should include(@doc1)
docs.should include(@doc3)
end
should "find last document based on arguments" do
doc = @document.find_last_by_last_name('Nunemaker')
doc.should == @doc3
end
should "initialize document with given arguments" do
doc = @document.find_or_initialize_by_first_name_and_last_name('David', 'Cuadrado')
doc.should be_new
doc.first_name.should == 'David'
end
should "not initialize document if document is found" do
doc = @document.find_or_initialize_by_first_name('John')
doc.should_not be_new
end
should "create document with given arguments" do
doc = @document.find_or_create_by_first_name_and_last_name('David', 'Cuadrado')
doc.should_not be_new
doc.first_name.should == 'David'
end
should "raise error if document is not found" do
lambda {@document.find_by_first_name_and_last_name!(1,2)}.should raise_error(MongoMapper::DocumentNotFound)
end
end
end # finding documents
context "Finding document by id" do
setup do
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
end
should "be able to find by id" do
@document.find_by_id(@doc1.id).should == @doc1
@document.find_by_id(@doc2.id).should == @doc2
end
should "return nil if document not found" do
@document.find_by_id(1234).should be(nil)
end
end
context "Deleting a document" do
setup do
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
@document.delete(@doc1.id)
end
should "remove document from collection" do
@document.count.should == 1
end
should "not remove other documents" do
@document.find(@doc2.id).should_not be(nil)
end
end
context "Deleting multiple documents" do
should "work with multiple arguments" do
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
@document.delete(@doc1.id, @doc2.id)
@document.count.should == 1
end
should "work with array as argument" do
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
@document.delete([@doc1.id, @doc2.id])
@document.count.should == 1
end
end
context "Deleting all documents" do
setup do
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
end
should "remove all documents when given no conditions" do
@document.delete_all
@document.count.should == 0
end
should "only remove matching documents when given conditions" do
@document.delete_all({:first_name => 'John'})
@document.count.should == 2
end
should "convert the conditions to mongo criteria" do
@document.delete_all(:age => [26, 27])
@document.count.should == 1
end
end
context "Destroying a document" do
setup do
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
@document.destroy(@doc1.id)
end
should "remove document from collection" do
@document.count.should == 1
end
should "not remove other documents" do
@document.find(@doc2.id).should_not be(nil)
end
end
context "Destroying multiple documents" do
should "work with multiple arguments" do
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
@document.destroy(@doc1.id, @doc2.id)
@document.count.should == 1
end
should "work with array as argument" do
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
@document.destroy([@doc1.id, @doc2.id])
@document.count.should == 1
end
end
context "Destroying all documents" do
setup do
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
end
should "remove all documents when given no conditions" do
@document.destroy_all
@document.count.should == 0
end
should "only remove matching documents when given conditions" do
@document.destroy_all(:first_name => 'John')
@document.count.should == 2
@document.destroy_all(:age => 26)
@document.count.should == 1
end
should "convert the conditions to mongo criteria" do
@document.destroy_all(:age => [26, 27])
@document.count.should == 1
end
end
context "Counting documents in collection" do
setup do
@doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
@doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
@doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
end
should "count all with no arguments" do
@document.count.should == 3
end
should "return 0 if there are no documents in the collection" do
@document.delete_all
@document.count.should == 0
end
should "return 0 if the collection does not exist" do
klass = Class.new do
include MongoMapper::Document
collection 'foobarbazwickdoesnotexist'
end
klass.count.should == 0
end
should "return count for matching documents if conditions provided" do
@document.count(:age => 27).should == 1
end
should "convert the conditions to mongo criteria" do
@document.count(:age => [26, 27]).should == 2
end
end
context "Indexing" do
setup do
@document.collection.drop_indexes
end
should "allow creating index for a key" do
index_name = nil
lambda {
index_name = @document.ensure_index :first_name
}.should change { @document.collection.index_information.size }.by(1)
index_name.should == 'first_name_1'
index = @document.collection.index_information[index_name]
index.should_not be_nil
index.should include(['first_name', 1])
end
should "allow creating unique index for a key" do
@document.collection.expects(:create_index).with(:first_name, true)
@document.ensure_index :first_name, :unique => true
end
should "allow creating index on multiple keys" do
index_name = nil
lambda {
index_name = @document.ensure_index [[:first_name, 1], [:last_name, -1]]
}.should change { @document.collection.index_information.size }.by(1)
index_name.should == 'first_name_1_last_name_-1'
index = @document.collection.index_information[index_name]
index.should_not be_nil
index.should include(['first_name', 1])
index.should include(['last_name', -1])
end
should "work with :index shortcut when defining key" do
@document.expects(:ensure_index).with('father').returns(nil)
@document.key :father, String, :index => true
end
end
end # Document Class Methods
context "Saving a new document" do
setup do
@doc = @document.new(:first_name => 'John', :age => '27')
@doc.save
end
should "insert document into the collection" do
@document.count.should == 1
end
should "assign an id for the document" do
@doc.id.should_not be(nil)
@doc.id.size.should == 24
end
should "save attributes" do
@doc.first_name.should == 'John'
@doc.age.should == 27
end
should "update attributes in the database" do
from_db = @document.find(@doc.id)
from_db.should == @doc
from_db.first_name.should == 'John'
from_db.age.should == 27
end
end
context "Saving an existing document" do
setup do
@doc = @document.create(:first_name => 'John', :age => '27')
@doc.first_name = 'Johnny'
@doc.age = 30
@doc.save
end
should "not insert document into collection" do
@document.count.should == 1
end
should "update attributes" do
@doc.first_name.should == 'Johnny'
@doc.age.should == 30
end
should "update attributes in the database" do
from_db = @document.find(@doc.id)
from_db.first_name.should == 'Johnny'
from_db.age.should == 30
end
end
context "Calling update attributes on a new document" do
setup do
@doc = @document.new(:first_name => 'John', :age => '27')
@doc.update_attributes(:first_name => 'Johnny', :age => 30)
end
should "insert document into the collection" do
@document.count.should == 1
end
should "assign an id for the document" do
@doc.id.should_not be(nil)
@doc.id.size.should == 24
end
should "save attributes" do
@doc.first_name.should == 'Johnny'
@doc.age.should == 30
end
should "update attributes in the database" do
from_db = @document.find(@doc.id)
from_db.should == @doc
from_db.first_name.should == 'Johnny'
from_db.age.should == 30
end
end
context "Updating an existing document using update attributes" do
setup do
@doc = @document.create(:first_name => 'John', :age => '27')
@doc.update_attributes(:first_name => 'Johnny', :age => 30)
end
should "not insert document into collection" do
@document.count.should == 1
end
should "update attributes" do
@doc.first_name.should == 'Johnny'
@doc.age.should == 30
end
should "update attributes in the database" do
from_db = @document.find(@doc.id)
from_db.first_name.should == 'Johnny'
from_db.age.should == 30
end
end
context "Destroying a document that exists" do
setup do
@doc = @document.create(:first_name => 'John', :age => '27')
@doc.destroy
end
should "remove the document from the collection" do
@document.count.should == 0
end
should "raise error if assignment is attempted" do
lambda { @doc.first_name = 'Foo' }.should raise_error(TypeError)
end
end
context "Destroying a document that is a new" do
setup do
setup do
@doc = @document.new(:first_name => 'John Nunemaker', :age => '27')
@doc.destroy
end
should "not affect collection count" do
@document.collection.count.should == 0
end
should "raise error if assignment is attempted" do
lambda { @doc.first_name = 'Foo' }.should raise_error(TypeError)
end
end
end
context "timestamping" do
should "set created_at and updated_at on create" do
doc = @document.new(:first_name => 'John', :age => 27)
doc.created_at.should be(nil)
doc.updated_at.should be(nil)
doc.save
doc.created_at.should_not be(nil)
doc.updated_at.should_not be(nil)
end
should "set updated_at on field update but leave created_at alone" do
doc = @document.create(:first_name => 'John', :age => 27)
old_created_at = doc.created_at
old_updated_at = doc.updated_at
doc.first_name = 'Johnny'
doc.save
doc.created_at.should == old_created_at
doc.updated_at.should_not == old_updated_at
end
should "set updated_at on document update but leave created_at alone" do
doc = @document.create(:first_name => 'John', :age => 27)
old_created_at = doc.created_at
old_updated_at = doc.updated_at
sleep 1 # this annoys me
@document.update(doc._id, { :first_name => 'Johnny' })
from_db = @document.find(doc.id)
from_db.created_at.to_i.should == old_created_at.to_i
from_db.updated_at.to_i.should_not == old_updated_at.to_i
end
end
end