mongo_mapper 0.13.0.beta2 → 0.15.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.
- checksums.yaml +5 -5
- data/LICENSE +1 -1
- data/README.md +64 -0
- data/examples/keys.rb +3 -3
- data/examples/modifiers/set.rb +2 -2
- data/examples/querying.rb +3 -3
- data/examples/safe.rb +2 -2
- data/examples/scopes.rb +1 -1
- data/lib/mongo_mapper.rb +5 -0
- data/lib/mongo_mapper/connection.rb +16 -37
- data/lib/mongo_mapper/document.rb +4 -0
- data/lib/mongo_mapper/extensions/array.rb +14 -6
- data/lib/mongo_mapper/extensions/hash.rb +15 -3
- data/lib/mongo_mapper/extensions/object.rb +4 -0
- data/lib/mongo_mapper/extensions/object_id.rb +5 -1
- data/lib/mongo_mapper/extensions/string.rb +13 -5
- data/lib/mongo_mapper/extensions/symbol.rb +18 -0
- data/lib/mongo_mapper/plugins/accessible.rb +14 -4
- data/lib/mongo_mapper/plugins/associations.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/base.rb +18 -13
- data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +10 -1
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +9 -8
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +12 -11
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +4 -4
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +24 -23
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +18 -16
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +55 -48
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +14 -13
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +7 -5
- data/lib/mongo_mapper/plugins/associations/one_as_proxy.rb +17 -14
- data/lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +14 -13
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +9 -9
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +27 -26
- data/lib/mongo_mapper/plugins/associations/proxy.rb +31 -28
- data/lib/mongo_mapper/plugins/callbacks.rb +14 -1
- data/lib/mongo_mapper/plugins/counter_cache.rb +97 -0
- data/lib/mongo_mapper/plugins/dirty.rb +29 -37
- data/lib/mongo_mapper/plugins/document.rb +1 -1
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +10 -9
- data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +18 -17
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +2 -1
- data/lib/mongo_mapper/plugins/embedded_document.rb +1 -1
- data/lib/mongo_mapper/plugins/identity_map.rb +1 -1
- data/lib/mongo_mapper/plugins/indexes.rb +37 -2
- data/lib/mongo_mapper/plugins/keys.rb +202 -142
- data/lib/mongo_mapper/plugins/keys/key.rb +22 -13
- data/lib/mongo_mapper/plugins/keys/static.rb +45 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +59 -28
- data/lib/mongo_mapper/plugins/partial_updates.rb +86 -0
- data/lib/mongo_mapper/plugins/persistence.rb +13 -8
- data/lib/mongo_mapper/plugins/protected.rb +6 -5
- data/lib/mongo_mapper/plugins/querying.rb +85 -42
- data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +32 -9
- data/lib/mongo_mapper/plugins/rails.rb +1 -0
- data/lib/mongo_mapper/plugins/safe.rb +10 -4
- data/lib/mongo_mapper/plugins/sci.rb +4 -1
- data/lib/mongo_mapper/plugins/scopes.rb +78 -7
- data/lib/mongo_mapper/plugins/stats.rb +17 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +1 -0
- data/lib/mongo_mapper/plugins/touch.rb +1 -1
- data/lib/mongo_mapper/plugins/validations.rb +7 -2
- data/lib/mongo_mapper/railtie.rb +20 -0
- data/lib/mongo_mapper/railtie/database.rake +1 -1
- data/lib/mongo_mapper/utils.rb +2 -2
- data/lib/mongo_mapper/version.rb +1 -1
- data/lib/rails/generators/mongo_mapper/config/config_generator.rb +12 -13
- data/lib/rails/generators/mongo_mapper/model/model_generator.rb +9 -9
- data/spec/examples.txt +1643 -0
- data/spec/functional/accessible_spec.rb +13 -13
- data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +13 -13
- data/spec/functional/associations/belongs_to_proxy_spec.rb +18 -19
- data/spec/functional/associations/in_array_proxy_spec.rb +10 -10
- data/spec/functional/associations/many_documents_as_proxy_spec.rb +6 -6
- data/spec/functional/associations/many_documents_proxy_spec.rb +85 -14
- data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +13 -13
- data/spec/functional/associations/many_embedded_proxy_spec.rb +1 -1
- data/spec/functional/associations/many_polymorphic_proxy_spec.rb +4 -4
- data/spec/functional/associations/one_as_proxy_spec.rb +10 -10
- data/spec/functional/associations/one_embedded_polymorphic_proxy_spec.rb +9 -9
- data/spec/functional/associations/one_embedded_proxy_spec.rb +3 -3
- data/spec/functional/associations/one_proxy_spec.rb +10 -10
- data/spec/functional/associations_spec.rb +3 -3
- data/spec/functional/binary_spec.rb +2 -2
- data/spec/functional/caching_spec.rb +8 -15
- data/spec/functional/callbacks_spec.rb +89 -2
- data/spec/functional/counter_cache_spec.rb +235 -0
- data/spec/functional/dirty_spec.rb +63 -46
- data/spec/functional/document_spec.rb +30 -2
- data/spec/functional/dumpable_spec.rb +1 -1
- data/spec/functional/embedded_document_spec.rb +18 -18
- data/spec/functional/identity_map_spec.rb +27 -14
- data/spec/functional/indexes_spec.rb +44 -19
- data/spec/functional/keys_spec.rb +117 -15
- data/spec/functional/logger_spec.rb +3 -3
- data/spec/functional/modifiers_spec.rb +67 -19
- data/spec/functional/partial_updates_spec.rb +577 -0
- data/spec/functional/protected_spec.rb +14 -14
- data/spec/functional/querying_spec.rb +55 -28
- data/spec/functional/safe_spec.rb +23 -27
- data/spec/functional/sci_spec.rb +49 -14
- data/spec/functional/scopes_spec.rb +235 -2
- data/spec/functional/static_keys_spec.rb +153 -0
- data/spec/functional/stats_spec.rb +86 -0
- data/spec/functional/touch_spec.rb +6 -6
- data/spec/functional/validations_spec.rb +51 -57
- data/spec/quality_spec.rb +51 -0
- data/spec/spec_helper.rb +37 -9
- data/spec/support/matchers.rb +5 -14
- data/spec/unit/associations/base_spec.rb +12 -12
- data/spec/unit/associations/belongs_to_association_spec.rb +2 -2
- data/spec/unit/associations/many_association_spec.rb +2 -2
- data/spec/unit/associations/one_association_spec.rb +2 -2
- data/spec/unit/associations/proxy_spec.rb +19 -16
- data/spec/unit/clone_spec.rb +1 -1
- data/spec/unit/document_spec.rb +8 -8
- data/spec/unit/dynamic_finder_spec.rb +8 -8
- data/spec/unit/embedded_document_spec.rb +18 -19
- data/spec/unit/extensions_spec.rb +41 -17
- data/spec/unit/identity_map_middleware_spec.rb +65 -96
- data/spec/unit/inspect_spec.rb +4 -4
- data/spec/unit/key_spec.rb +28 -26
- data/spec/unit/keys_spec.rb +10 -10
- data/spec/unit/model_generator_spec.rb +2 -4
- data/spec/unit/mongo_mapper_spec.rb +38 -85
- data/spec/unit/rails_spec.rb +5 -0
- data/spec/unit/serialization_spec.rb +1 -1
- data/spec/unit/time_zones_spec.rb +2 -2
- data/spec/unit/validations_spec.rb +28 -15
- metadata +188 -161
- data/README.rdoc +0 -55
- data/lib/mongo_mapper/extensions/ordered_hash.rb +0 -23
@@ -44,12 +44,12 @@ describe 'A document with protected attributes' do
|
|
44
44
|
|
45
45
|
it "should assign protected attribute through accessor" do
|
46
46
|
@doc.admin = true
|
47
|
-
@doc.admin.should
|
47
|
+
@doc.admin.should be_truthy
|
48
48
|
end
|
49
49
|
|
50
50
|
it "should ignore protected attribute on #initialize" do
|
51
51
|
doc = @doc_class.new(:name => 'John', :admin => true)
|
52
|
-
doc.admin.should
|
52
|
+
doc.admin.should be_falsey
|
53
53
|
doc.name.should == 'John'
|
54
54
|
end
|
55
55
|
|
@@ -59,7 +59,7 @@ describe 'A document with protected attributes' do
|
|
59
59
|
doc.save!
|
60
60
|
|
61
61
|
doc = @doc_class.first(:name => 'John')
|
62
|
-
doc.admin.should
|
62
|
+
doc.admin.should be_truthy
|
63
63
|
doc.name.should == 'John'
|
64
64
|
end
|
65
65
|
|
@@ -69,37 +69,37 @@ describe 'A document with protected attributes' do
|
|
69
69
|
doc.save!
|
70
70
|
|
71
71
|
doc.reload
|
72
|
-
doc.admin.should
|
72
|
+
doc.admin.should be_truthy
|
73
73
|
doc.name.should == 'John'
|
74
74
|
end
|
75
75
|
|
76
76
|
it "should ignore protected attribute on #update_attribute" do
|
77
77
|
@doc.update_attribute('admin', true)
|
78
|
-
@doc.admin.should
|
78
|
+
@doc.admin.should be_truthy
|
79
79
|
end
|
80
80
|
|
81
81
|
it "should ignore protected attribute on #update_attributes" do
|
82
82
|
@doc.update_attributes(:name => 'Ren Hoek', :admin => true)
|
83
83
|
@doc.name.should == 'Ren Hoek'
|
84
|
-
@doc.admin.should
|
84
|
+
@doc.admin.should be_falsey
|
85
85
|
end
|
86
86
|
|
87
87
|
it "should ignore protected attribute on #update_attributes!" do
|
88
88
|
@doc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
|
89
89
|
@doc.name.should == 'Stimpson J. Cat'
|
90
|
-
@doc.admin.should
|
90
|
+
@doc.admin.should be_falsey
|
91
91
|
end
|
92
92
|
|
93
93
|
it "should ignore protecteds attribute on #attributes=" do
|
94
94
|
@doc.attributes = {:name => 'Stimpson J. Cat', :admin => true}
|
95
95
|
@doc.name.should == 'Stimpson J. Cat'
|
96
|
-
@doc.admin.should
|
96
|
+
@doc.admin.should be_falsey
|
97
97
|
end
|
98
98
|
|
99
99
|
it "should be indifferent to whether the protected keys are strings or symbols" do
|
100
100
|
@doc.update_attributes!("name" => 'Stimpson J. Cat', "admin" => true)
|
101
101
|
@doc.name.should == 'Stimpson J. Cat'
|
102
|
-
@doc.admin.should
|
102
|
+
@doc.admin.should be_falsey
|
103
103
|
end
|
104
104
|
|
105
105
|
it "should accept nil as constructor's argument without raising exception" do
|
@@ -115,7 +115,7 @@ describe "Single collection inherited protected attributes" do
|
|
115
115
|
key :site_id, ObjectId
|
116
116
|
attr_protected :site_id
|
117
117
|
end
|
118
|
-
GrandParent.collection.
|
118
|
+
GrandParent.collection.drop
|
119
119
|
|
120
120
|
class ::Child < ::GrandParent
|
121
121
|
key :position, Integer
|
@@ -177,23 +177,23 @@ describe 'An embedded document with protected attributes' do
|
|
177
177
|
|
178
178
|
it "should assign protected attribute through accessor" do
|
179
179
|
@edoc.admin = true
|
180
|
-
@edoc.admin.should
|
180
|
+
@edoc.admin.should be_truthy
|
181
181
|
end
|
182
182
|
|
183
183
|
it "should not ignore protected attribute on #update_attribute" do
|
184
184
|
@edoc.update_attribute('admin', true)
|
185
|
-
@edoc.admin.should
|
185
|
+
@edoc.admin.should be_truthy
|
186
186
|
end
|
187
187
|
|
188
188
|
it "should ignore protected attribute on #update_attributes" do
|
189
189
|
@edoc.update_attributes(:name => 'Ren Hoek', :admin => true)
|
190
190
|
@edoc.name.should == 'Ren Hoek'
|
191
|
-
@edoc.admin.should
|
191
|
+
@edoc.admin.should be_falsey
|
192
192
|
end
|
193
193
|
|
194
194
|
it "should ignore protected attribute on #update_attributes!" do
|
195
195
|
@edoc.update_attributes!(:name => 'Stimpson J. Cat', :admin => true)
|
196
196
|
@edoc.name.should == 'Stimpson J. Cat'
|
197
|
-
@edoc.admin.should
|
197
|
+
@edoc.admin.should be_falsey
|
198
198
|
end
|
199
199
|
end
|
@@ -55,7 +55,7 @@ describe "Querying" do
|
|
55
55
|
end
|
56
56
|
|
57
57
|
it "should no longer be new?" do
|
58
|
-
doc.new?.should
|
58
|
+
doc.new?.should be_falsey
|
59
59
|
end
|
60
60
|
|
61
61
|
it "should return instance of document" do
|
@@ -164,7 +164,7 @@ describe "Querying" do
|
|
164
164
|
end
|
165
165
|
|
166
166
|
it "should raise document not found if nothing provided for find!" do
|
167
|
-
|
167
|
+
lambda { document.find! }.should raise_error(MongoMapper::DocumentNotFound)
|
168
168
|
end
|
169
169
|
|
170
170
|
context "(with a single id)" do
|
@@ -177,7 +177,7 @@ describe "Querying" do
|
|
177
177
|
end
|
178
178
|
|
179
179
|
it "should raise error if document not found with find!" do
|
180
|
-
|
180
|
+
lambda { document.find!(123) }.should raise_error(MongoMapper::DocumentNotFound)
|
181
181
|
end
|
182
182
|
end
|
183
183
|
|
@@ -203,15 +203,15 @@ describe "Querying" do
|
|
203
203
|
end
|
204
204
|
|
205
205
|
it "should raise error if not all found when using find!" do
|
206
|
-
|
206
|
+
lambda {
|
207
207
|
document.find!(@doc1._id, BSON::ObjectId.new.to_s)
|
208
|
-
}.
|
208
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
209
209
|
end
|
210
210
|
|
211
211
|
it "should raise error if not all found when using find!" do
|
212
|
-
|
212
|
+
lambda {
|
213
213
|
document.find!([@doc1._id, BSON::ObjectId.new.to_s])
|
214
|
-
}.
|
214
|
+
}.should raise_error(MongoMapper::DocumentNotFound)
|
215
215
|
end
|
216
216
|
|
217
217
|
it "should return array if array with one element" do
|
@@ -292,12 +292,12 @@ describe "Querying" do
|
|
292
292
|
end
|
293
293
|
|
294
294
|
it "should disregard non-keys when creating, but use them in the query" do
|
295
|
-
|
295
|
+
lambda {
|
296
296
|
document.create(:first_name => 'John', :age => 9)
|
297
297
|
lambda {
|
298
298
|
document.first_or_create(:first_name => 'John', :age.gt => 10).first_name.should == 'John'
|
299
299
|
}.should change { document.count }.by(1)
|
300
|
-
}.
|
300
|
+
}.should_not raise_error
|
301
301
|
end
|
302
302
|
end
|
303
303
|
|
@@ -320,10 +320,10 @@ describe "Querying" do
|
|
320
320
|
end
|
321
321
|
|
322
322
|
it "should disregard non-keys when initializing, but use them in the query" do
|
323
|
-
|
323
|
+
lambda {
|
324
324
|
document.create(:first_name => 'John', :age => 9)
|
325
325
|
document.first_or_new(:first_name => 'John', :age.gt => 10).first_name.should == 'John'
|
326
|
-
}.
|
326
|
+
}.should_not raise_error
|
327
327
|
end
|
328
328
|
end
|
329
329
|
|
@@ -495,12 +495,12 @@ describe "Querying" do
|
|
495
495
|
|
496
496
|
context ".empty?" do
|
497
497
|
it "should be true if no documents" do
|
498
|
-
document.empty?.should
|
498
|
+
document.empty?.should be_truthy
|
499
499
|
end
|
500
500
|
|
501
501
|
it "should be false if documents present" do
|
502
502
|
@doc = document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
|
503
|
-
document.empty?.should
|
503
|
+
document.empty?.should be_falsey
|
504
504
|
end
|
505
505
|
end
|
506
506
|
|
@@ -616,6 +616,32 @@ describe "Querying" do
|
|
616
616
|
it "should be chainable" do
|
617
617
|
@query.sort(:age).all.map(&:age).should == [26, 27, 28]
|
618
618
|
end
|
619
|
+
|
620
|
+
it "supports hash" do
|
621
|
+
@query = document.fields({:age => 0})
|
622
|
+
docs = @query.all
|
623
|
+
docs.should include(@doc1)
|
624
|
+
docs.should include(@doc3)
|
625
|
+
docs.should include(@doc2)
|
626
|
+
docs.each do |doc|
|
627
|
+
doc.age.should be_nil # key was not loaded
|
628
|
+
doc.first_name.should_not be_nil
|
629
|
+
doc.last_name.should_not be_nil # key was not loaded
|
630
|
+
end
|
631
|
+
end
|
632
|
+
|
633
|
+
it "supports array" do
|
634
|
+
@query = document.fields([:age,:first_name])
|
635
|
+
docs = @query.all
|
636
|
+
docs.should include(@doc1)
|
637
|
+
docs.should include(@doc3)
|
638
|
+
docs.should include(@doc2)
|
639
|
+
docs.each do |doc|
|
640
|
+
doc.age.should_not be_nil
|
641
|
+
doc.first_name.should_not be_nil
|
642
|
+
doc.last_name.should be_nil # key was not loaded
|
643
|
+
end
|
644
|
+
end
|
619
645
|
end
|
620
646
|
|
621
647
|
context ".limit" do
|
@@ -737,11 +763,11 @@ describe "Querying" do
|
|
737
763
|
end
|
738
764
|
|
739
765
|
it "should be true if document valid" do
|
740
|
-
document.new.update_attributes(:foo => 'bar').should
|
766
|
+
document.new.update_attributes(:foo => 'bar').should be_truthy
|
741
767
|
end
|
742
768
|
|
743
769
|
it "should be false if document not valid" do
|
744
|
-
document.new.update_attributes({}).should
|
770
|
+
document.new.update_attributes({}).should be_falsey
|
745
771
|
end
|
746
772
|
end
|
747
773
|
|
@@ -751,12 +777,12 @@ describe "Querying" do
|
|
751
777
|
end
|
752
778
|
|
753
779
|
it "should accept symbols as keys" do
|
754
|
-
@doc.update_attribute(:first_name, 'Chris').should
|
780
|
+
@doc.update_attribute(:first_name, 'Chris').should be_truthy
|
755
781
|
@doc.reload.first_name.should == 'Chris'
|
756
782
|
end
|
757
783
|
|
758
784
|
it "should update the attribute" do
|
759
|
-
@doc.update_attribute('first_name', 'Chris').should
|
785
|
+
@doc.update_attribute('first_name', 'Chris').should be_truthy
|
760
786
|
@doc.reload.first_name.should == 'Chris'
|
761
787
|
end
|
762
788
|
|
@@ -764,7 +790,8 @@ describe "Querying" do
|
|
764
790
|
document.key :name, String, :required => true
|
765
791
|
|
766
792
|
@doc.should_receive(:valid?).never
|
767
|
-
@doc.update_attribute('name', '').should
|
793
|
+
@doc.update_attribute('name', '').should be_truthy
|
794
|
+
|
768
795
|
@doc.reload.name.should == ''
|
769
796
|
document.count.should == 1
|
770
797
|
end
|
@@ -884,15 +911,15 @@ describe "Querying" do
|
|
884
911
|
|
885
912
|
it "should allow passing safe" do
|
886
913
|
@document.create(:name => 'John')
|
887
|
-
|
914
|
+
lambda {
|
888
915
|
@document.new(:name => 'John').save(:safe => true)
|
889
|
-
}.
|
916
|
+
}.should raise_error(Mongo::Error::OperationFailure)
|
890
917
|
end
|
891
918
|
|
892
919
|
it "should raise argument error if options has unsupported key" do
|
893
|
-
|
920
|
+
lambda {
|
894
921
|
@document.new.save(:foo => true)
|
895
|
-
}.
|
922
|
+
}.should raise_error(ArgumentError)
|
896
923
|
end
|
897
924
|
end
|
898
925
|
|
@@ -905,21 +932,21 @@ describe "Querying" do
|
|
905
932
|
|
906
933
|
it "should allow passing safe" do
|
907
934
|
@document.create(:name => 'John')
|
908
|
-
|
935
|
+
lambda {
|
909
936
|
@document.new(:name => 'John').save!(:safe => true)
|
910
|
-
}.
|
937
|
+
}.should raise_error(Mongo::Error::OperationFailure)
|
911
938
|
end
|
912
939
|
|
913
940
|
it "should raise argument error if options has unsupported key" do
|
914
|
-
|
941
|
+
lambda {
|
915
942
|
@document.new.save!(:foo => true)
|
916
|
-
}.
|
943
|
+
}.should raise_error(ArgumentError)
|
917
944
|
end
|
918
945
|
|
919
946
|
it "should raise argument error if using validate as that would be pointless with save!" do
|
920
|
-
|
947
|
+
lambda {
|
921
948
|
@document.new.save!(:validate => false)
|
922
|
-
}.
|
949
|
+
}.should raise_error(ArgumentError)
|
923
950
|
end
|
924
951
|
end
|
925
952
|
|
@@ -42,7 +42,7 @@ describe "Safe" do
|
|
42
42
|
|
43
43
|
it "should not raise an error on duplicate IDs" do
|
44
44
|
k = @klass.create
|
45
|
-
|
45
|
+
lambda { j = @klass.create(:_id => k.id) }.should_not raise_error
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
@@ -61,46 +61,44 @@ describe "Safe" do
|
|
61
61
|
|
62
62
|
it "should raise an error on duplicate IDs" do
|
63
63
|
k = @klass.create
|
64
|
-
|
64
|
+
lambda { j = @klass.create(:_id => k.id) }.should raise_error(Mongo::Error::OperationFailure)
|
65
65
|
end
|
66
66
|
|
67
67
|
context "using safe setting from class" do
|
68
|
-
it "should pass :w => 1 option to
|
69
|
-
|
70
|
-
Mongo::Collection.any_instance.should_receive(:insert).once.with({'_id' => instance.id, 'email' => 'john@doe.com'}, {:w => 1})
|
71
|
-
instance.save!
|
68
|
+
it "should pass :w => 1 option to the collection" do
|
69
|
+
@klass.collection.write_concern.options.should == { w: 1 }
|
72
70
|
end
|
73
71
|
|
74
72
|
it "should work fine when all is well" do
|
75
|
-
|
73
|
+
lambda {
|
76
74
|
@klass.new(:email => 'john@doe.com').save
|
77
|
-
}.
|
75
|
+
}.should_not raise_error
|
78
76
|
end
|
79
77
|
|
80
78
|
it "should raise error when operation fails" do
|
81
|
-
|
79
|
+
lambda {
|
82
80
|
2.times do
|
83
81
|
@klass.new(:email => 'john@doe.com').save
|
84
82
|
end
|
85
|
-
}.
|
83
|
+
}.should raise_error(Mongo::Error::OperationFailure)
|
86
84
|
end
|
87
85
|
end
|
88
86
|
|
89
87
|
context "overriding safe setting" do
|
90
88
|
it "should raise error if safe is true" do
|
91
|
-
|
89
|
+
lambda {
|
92
90
|
2.times do
|
93
91
|
@klass.new(:email => 'john@doe.com').save(:safe => true)
|
94
92
|
end
|
95
|
-
}.
|
93
|
+
}.should raise_error(Mongo::Error::OperationFailure)
|
96
94
|
end
|
97
95
|
|
98
96
|
it "should not raise error if safe is false" do
|
99
|
-
|
97
|
+
lambda {
|
100
98
|
2.times do
|
101
99
|
@klass.new(:email => 'john@doe.com').save(:safe => false)
|
102
100
|
end
|
103
|
-
}.
|
101
|
+
}.should_not raise_error
|
104
102
|
end
|
105
103
|
end
|
106
104
|
end
|
@@ -120,44 +118,42 @@ describe "Safe" do
|
|
120
118
|
end
|
121
119
|
|
122
120
|
context "using safe setting from class" do
|
123
|
-
it "should pass :safe => options_hash to
|
124
|
-
|
125
|
-
Mongo::Collection.any_instance.should_receive(:insert).once.with({'_id' => instance.id, 'email' => 'john@doe.com'}, {:j => true})
|
126
|
-
instance.save!
|
121
|
+
it "should pass :safe => options_hash to the collection" do
|
122
|
+
@klass.collection.write_concern.options.should == { j: true }
|
127
123
|
end
|
128
124
|
|
129
125
|
it "should work fine when all is well" do
|
130
|
-
|
126
|
+
lambda {
|
131
127
|
@klass.new(:email => 'john@doe.com').save
|
132
|
-
}.
|
128
|
+
}.should_not raise_error
|
133
129
|
end
|
134
130
|
|
135
131
|
it "should raise error when operation fails" do
|
136
|
-
|
132
|
+
lambda {
|
137
133
|
2.times do
|
138
134
|
@klass.new(:email => 'john@doe.com').save
|
139
135
|
end
|
140
|
-
}.
|
136
|
+
}.should raise_error(Mongo::Error::OperationFailure)
|
141
137
|
end
|
142
138
|
end
|
143
139
|
|
144
140
|
context "overriding safe setting" do
|
145
141
|
it "should raise error if safe is true" do
|
146
|
-
|
142
|
+
lambda {
|
147
143
|
2.times do
|
148
144
|
@klass.new(:email => 'john@doe.com').save(:safe => true)
|
149
145
|
end
|
150
|
-
}.
|
146
|
+
}.should raise_error(Mongo::Error::OperationFailure)
|
151
147
|
end
|
152
148
|
|
153
149
|
it "should not raise error if safe is false" do
|
154
|
-
|
150
|
+
lambda {
|
155
151
|
2.times do
|
156
152
|
@klass.new(:email => 'john@doe.com').save(:safe => false)
|
157
153
|
end
|
158
|
-
}.
|
154
|
+
}.should_not raise_error
|
159
155
|
end
|
160
156
|
end
|
161
157
|
end
|
162
158
|
end
|
163
|
-
end
|
159
|
+
end
|
data/spec/functional/sci_spec.rb
CHANGED
@@ -3,12 +3,12 @@ require 'spec_helper'
|
|
3
3
|
describe "Single collection inheritance (document)" do
|
4
4
|
context "without a connection", :without_connection => true do
|
5
5
|
it "should attempt to create a connection during inheritance" do
|
6
|
-
Mongo::
|
6
|
+
Mongo::Client.should_not_receive(:new)
|
7
7
|
doc = Class.new
|
8
8
|
doc.send(:include, MongoMapper::Document)
|
9
|
-
|
9
|
+
lambda {
|
10
10
|
Class.new(doc)
|
11
|
-
}.
|
11
|
+
}.should_not raise_error
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should pick up a connection if one wasn't set" do
|
@@ -17,7 +17,7 @@ describe "Single collection inheritance (document)" do
|
|
17
17
|
klass = Class.new(doc)
|
18
18
|
klass.connection.should be_nil
|
19
19
|
MongoMapper.connection
|
20
|
-
klass.connection.should be_a Mongo::
|
20
|
+
klass.connection.should be_a Mongo::Client
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -27,7 +27,7 @@ describe "Single collection inheritance (document)" do
|
|
27
27
|
include MongoMapper::Document
|
28
28
|
key :name, String
|
29
29
|
end
|
30
|
-
DocParent.collection.
|
30
|
+
DocParent.collection.drop
|
31
31
|
|
32
32
|
class ::DocDaughter < ::DocParent; end
|
33
33
|
class ::DocSon < ::DocParent; end
|
@@ -49,7 +49,7 @@ describe "Single collection inheritance (document)" do
|
|
49
49
|
end
|
50
50
|
|
51
51
|
it "should automatically add _type key to store class" do
|
52
|
-
DocParent.key?(:_type).should
|
52
|
+
DocParent.key?(:_type).should be_truthy
|
53
53
|
end
|
54
54
|
|
55
55
|
it "should use modifiers properly" do
|
@@ -61,7 +61,7 @@ describe "Single collection inheritance (document)" do
|
|
61
61
|
it "should use the same connection in the subclass" do
|
62
62
|
parent_class = Class.new do
|
63
63
|
include MongoMapper::Document
|
64
|
-
connection Mongo::
|
64
|
+
connection Mongo::Client.new(['127.0.0.1:27017'])
|
65
65
|
end
|
66
66
|
|
67
67
|
child_class = Class.new(parent_class) do
|
@@ -88,12 +88,47 @@ describe "Single collection inheritance (document)" do
|
|
88
88
|
DocDaughter.collection.name.should == DocParent.collection.name
|
89
89
|
end
|
90
90
|
|
91
|
-
|
92
|
-
|
93
|
-
|
91
|
+
context "if the subclass changes its collection" do
|
92
|
+
before do
|
93
|
+
class ::DocSCIOrphan < ::DocParent
|
94
|
+
set_collection_name "foobars"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
after do
|
99
|
+
Object.send :remove_const, 'DocSCIOrphan' if defined?(::DocSCIOrphan)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should negate SCI" do
|
103
|
+
DocSCIOrphan.collection.name.should == "foobars"
|
104
|
+
DocSCIOrphan.should_not be_single_collection_inherited
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should remove the _type key" do
|
108
|
+
DocParent.keys.should_not have_key "_type"
|
109
|
+
DocSCIOrphan.keys.should_not have_key "_type"
|
110
|
+
end
|
111
|
+
|
112
|
+
context "and then is subclassed again" do
|
113
|
+
before do
|
114
|
+
class ::DocSCIOrphanChild < ::DocSCIOrphan
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
after do
|
119
|
+
Object.send :remove_const, 'DocSCIOrphanChild' if defined?(::DocSCIOrphanChild)
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should reinstate SCI" do
|
123
|
+
DocSCIOrphan.should_not be_single_collection_inherited
|
124
|
+
DocSCIOrphanChild.should be_single_collection_inherited
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should have the _type key" do
|
128
|
+
DocSCIOrphan.keys.should have_key "_type"
|
129
|
+
DocSCIOrphanChild.keys.should have_key "_type"
|
130
|
+
end
|
94
131
|
end
|
95
|
-
klass.collection.name.should == "foobars"
|
96
|
-
klass.should_not be_single_collection_inherited
|
97
132
|
end
|
98
133
|
|
99
134
|
it "should know single_collection_parent" do
|
@@ -288,7 +323,7 @@ describe "Single collection inheritance (document)" do
|
|
288
323
|
end
|
289
324
|
|
290
325
|
it "should automatically add _type key" do
|
291
|
-
Grandparent.key?(:_type).should
|
326
|
+
Grandparent.key?(:_type).should be_truthy
|
292
327
|
end
|
293
328
|
|
294
329
|
context ".single_collection_inherited?" do
|
@@ -342,4 +377,4 @@ describe "Single collection inheritance (document)" do
|
|
342
377
|
p.reload.article_parent.sci_polymorphic_posts.all.should include(p)
|
343
378
|
end
|
344
379
|
end
|
345
|
-
end
|
380
|
+
end
|