forked from mongomapper/mongomapper
/
test_document.rb
1157 lines (952 loc) · 35.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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'test_helper'
require 'models'
class DocumentTest < Test::Unit::TestCase
def setup
@document = Class.new do
include MongoMapper::Document
set_collection_name 'users'
key :first_name, String
key :last_name, String
key :age, Integer
key :date, Date
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 "Saving a document with a blank binary value" do
setup do
@document.key :file, Binary
end
should "not fail" do
doc = @document.new(:file => nil)
doc.save
end
end
context "Loading a document from the database with keys that are not defined" do
setup do
@id = Mongo::ObjectID.new.to_s
@document.collection.insert({
:_id => @id,
:first_name => 'John',
:last_name => 'Nunemaker',
:age => 27,
:favorite_color => 'red',
:skills => ['ruby', 'rails', 'javascript', 'xhtml', 'css']
})
end
should "assign all keys from database" do
doc = @document.find(@id)
doc.first_name.should == 'John'
doc.last_name.should == 'Nunemaker'
doc.age.should == 27
doc.favorite_color.should == 'red'
doc.skills.should == ['ruby', 'rails', 'javascript', 'xhtml', 'css']
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 "Using key with custom type with default" do
setup do
@document.key :window, WindowSize, :default => WindowSize.new(600, 480)
end
should "default to default" do
doc = @document.new
doc.window.should == WindowSize.new(600, 480)
end
should "save and load from mongo" do
doc = @document.new
doc.save
from_db = @document.find(doc.id)
from_db.window.should == WindowSize.new(600, 480)
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
set_collection_name 'test'
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
should "return array if array only has one element" do
@document.find([@doc1.id]).should == [@doc1]
end
end
should "be able to find using condition auto-detection" do
@document.first(:first_name => 'John').should == @doc1
@document.all(:last_name => 'Nunemaker', :order => 'age desc').should == [@doc1, @doc3]
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'}, :order => 'age desc').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, :order => 'age').should == @doc2
end
end
context "with #last" do
should "find last document based on criteria" do
@document.last(:order => 'age').should == @doc2
@document.last(:order => 'age', :conditions => {:age => 28}).should == @doc2
end
should "raise error if no order provided" do
lambda { @document.last() }.should raise_error
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', :order => 'age desc').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 "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 when using !" 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 ":dependent" do
setup do
# FIXME: make use of already defined models
class ::Property
include MongoMapper::Document
end
Property.collection.clear
class ::Thing
include MongoMapper::Document
key :name, String
end
Thing.collection.clear
end
teardown do
Object.send :remove_const, 'Property' if defined?(::Property)
Object.send :remove_const, 'Thing' if defined?(::Thing)
end
context "many" do
context "=> destroy" do
setup do
Property.key :thing_id, String
Property.belongs_to :thing, :dependent => :destroy
Thing.many :properties, :dependent => :destroy
@thing = Thing.create(:name => "Tree")
@property1 = Property.create
@property2 = Property.create
@property3 = Property.create
@thing.properties << @property1
@thing.properties << @property2
@thing.properties << @property3
end
should "should destroy the associated documents" do
@thing.properties.count.should == 3
@thing.destroy
@thing.properties.count.should == 0
Property.count.should == 0
end
end
context "=> delete_all" do
setup do
Property.key :thing_id, String
Property.belongs_to :thing
Thing.has_many :properties, :dependent => :delete_all
@thing = Thing.create(:name => "Tree")
@property1 = Property.create
@property2 = Property.create
@property3 = Property.create
@thing.properties << @property1
@thing.properties << @property2
@thing.properties << @property3
end
should "should delete associated documents" do
@thing.properties.count.should == 3
@thing.destroy
@thing.properties.count.should == 0
Property.count.should == 0
end
end
context "=> nullify" do
setup do
Property.key :thing_id, String
Property.belongs_to :thing
Thing.has_many :properties, :dependent => :nullify
@thing = Thing.create(:name => "Tree")
@property1 = Property.create
@property2 = Property.create
@property3 = Property.create
@thing.properties << @property1
@thing.properties << @property2
@thing.properties << @property3
end
should "should nullify relationship but not destroy associated documents" do
@thing.properties.count.should == 3
@thing.destroy
@thing.properties.count.should == 0
Property.count.should == 3
end
end
end
context "belongs_to" do
context "=> destroy" do
setup do
Property.key :thing_id, String
Property.belongs_to :thing, :dependent => :destroy
Thing.has_many :properties
@thing = Thing.create(:name => "Tree")
@property1 = Property.create
@property2 = Property.create
@property3 = Property.create
@thing.properties << @property1
@thing.properties << @property2
@thing.properties << @property3
end
should "not execute on a belongs_to association" do
Thing.count.should == 1
@property1.destroy
Thing.count.should == 1
@property1.should be_frozen
end
end
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
set_collection_name 'foobarbazwickdoesnotexist'
end
@document.collection.clear
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
@document.ensure_index :first_name
MongoMapper.ensure_indexes!
@document.should have_index('first_name_1')
end
should "allow creating unique index for a key" do
@document.ensure_index :first_name, :unique => true
MongoMapper.ensure_indexes!
@document.should have_index('first_name_1')
end
should "allow creating index on multiple keys" do
@document.ensure_index [[:first_name, 1], [:last_name, -1]]
MongoMapper.ensure_indexes!
# order is different for different versions of ruby so instead of
# just checking have_index('first_name_1_last_name_-1') I'm checking
# the values of the indexes to make sure the index creation was successful
@document.collection.index_information.detect do |index|
keys = index[1]
keys.include?(['first_name', 1]) && keys.include?(['last_name', -1])
end.should_not be_nil
end
should "work with :index shortcut when defining key" do
@document.key :father, String, :index => true
MongoMapper.ensure_indexes!
@document.should have_index('father_1')
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
should "allow to add custom attributes to the document" do
@doc = @document.new(:first_name => 'David', :age => '26', :gender => 'male', :tags => [1, "2"])
@doc.save
from_db = @document.find(@doc.id)
from_db.gender.should == 'male'
from_db.tags.should == [1, "2"]
end
should "allow to use custom methods to assign properties" do
person = RealPerson.new(:realname => "David")
person.save
from_db = RealPerson.find(person.id)
from_db.name.should == "David"
end
context "with key of type date" do
should "save the date value as a Time object" do
doc = @document.new(:first_name => 'John', :age => '27', :date => "12/01/2009")
doc.save
doc.date.should == Date.new(2009, 12, 1)
end
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
should "allow updating custom attributes" do
@doc = @document.new(:first_name => 'David', :age => '26', :gender => 'male')
@doc.gender = 'Male'
@doc.save
from_db = @document.find(@doc.id)
from_db.gender.should == 'Male'
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
should "allow updating custom attributes" do
@doc.update_attributes(:gender => 'mALe')
from_db = @document.find(@doc.id)
from_db.gender.should == 'mALe'
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 "update_attributes" do
setup do
@document.key :foo, String, :required => true
end
should "return true if document valid" do
@document.new.update_attributes(:foo => 'bar').should be_true
end
should "return false if document not valid" do
@document.new.update_attributes({}).should be_false
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
should "do nothing if destroy is called again" do
@doc.destroy.should be_false
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 "Single collection inheritance" do
setup do
class ::DocParent
include MongoMapper::Document
key :_type, String
key :name, String
end
DocParent.collection.clear
class ::DocDaughter < ::DocParent; end
class ::DocSon < ::DocParent; end
class ::DocGrandSon < ::DocSon; end
@parent = DocParent.new({:name => "Daddy Warbucks"})
@daughter = DocDaughter.new({:name => "Little Orphan Annie"})
end
teardown do
Object.send :remove_const, 'DocParent' if defined?(::DocParent)
Object.send :remove_const, 'DocDaughter' if defined?(::DocDaughter)
Object.send :remove_const, 'DocSon' if defined?(::DocSon)
Object.send :remove_const, 'DocGrandSon' if defined?(::DocGrandSon)
end
should "use the same collection in the subclass" do
DocDaughter.collection.name.should == DocParent.collection.name
end
should "assign the class name into the _type property" do
@parent._type.should == 'DocParent'
@daughter._type.should == 'DocDaughter'
end
should "load the document with the assigned type" do
@parent.save
@daughter.save
collection = DocParent.find(:all)
collection.size.should == 2
collection.first.should be_kind_of(DocParent)
collection.first.name.should == "Daddy Warbucks"
collection.last.should be_kind_of(DocDaughter)
collection.last.name.should == "Little Orphan Annie"
end
should "gracefully handle when the type can't be constantized" do
doc = DocParent.new(:name => 'Nunes')
doc._type = 'FoobarBaz'
doc.save
collection = DocParent.all
collection.last.should == doc
collection.last.should be_kind_of(DocParent)
end