mongo_mapper 0.13.1 → 0.15.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/LICENSE +1 -1
- data/README.md +61 -0
- data/examples/keys.rb +1 -1
- data/examples/modifiers/set.rb +1 -1
- data/examples/querying.rb +1 -1
- data/examples/safe.rb +2 -2
- data/examples/scopes.rb +1 -1
- data/lib/mongo_mapper.rb +4 -0
- data/lib/mongo_mapper/connection.rb +16 -38
- data/lib/mongo_mapper/document.rb +2 -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/plugins/accessible.rb +13 -12
- data/lib/mongo_mapper/plugins/associations.rb +7 -6
- data/lib/mongo_mapper/plugins/associations/base.rb +23 -14
- data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +1 -1
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +9 -8
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +18 -11
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +4 -4
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +60 -29
- data/lib/mongo_mapper/plugins/associations/in_foreign_array_proxy.rb +136 -0
- data/lib/mongo_mapper/plugins/associations/many_association.rb +4 -2
- 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 +14 -11
- data/lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +14 -13
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +12 -10
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +27 -26
- data/lib/mongo_mapper/plugins/associations/proxy.rb +38 -27
- data/lib/mongo_mapper/plugins/associations/single_association.rb +5 -4
- data/lib/mongo_mapper/plugins/callbacks.rb +13 -0
- data/lib/mongo_mapper/plugins/counter_cache.rb +23 -4
- 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 +2 -2
- data/lib/mongo_mapper/plugins/identity_map.rb +4 -2
- data/lib/mongo_mapper/plugins/indexes.rb +14 -7
- data/lib/mongo_mapper/plugins/keys.rb +164 -159
- data/lib/mongo_mapper/plugins/keys/key.rb +27 -16
- data/lib/mongo_mapper/plugins/keys/static.rb +45 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +64 -38
- 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 +20 -15
- data/lib/mongo_mapper/plugins/safe.rb +10 -4
- data/lib/mongo_mapper/plugins/scopes.rb +94 -7
- data/lib/mongo_mapper/plugins/stats.rb +1 -3
- data/lib/mongo_mapper/plugins/strong_parameters.rb +26 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +1 -0
- data/lib/mongo_mapper/plugins/validations.rb +0 -0
- data/lib/mongo_mapper/railtie.rb +1 -0
- 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 +1728 -0
- data/spec/functional/accessible_spec.rb +19 -13
- data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +13 -13
- data/spec/functional/associations/belongs_to_proxy_spec.rb +54 -20
- data/spec/functional/associations/in_array_proxy_spec.rb +145 -10
- data/spec/functional/associations/in_foreign_array_proxy_spec.rb +321 -0
- 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 +31 -3
- data/spec/functional/associations/one_proxy_spec.rb +21 -11
- 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 +89 -0
- data/spec/functional/dirty_spec.rb +84 -46
- data/spec/functional/dirty_with_callbacks_spec.rb +59 -0
- data/spec/functional/document_spec.rb +2 -5
- data/spec/functional/dumpable_spec.rb +1 -1
- data/spec/functional/embedded_document_spec.rb +17 -17
- data/spec/functional/identity_map_spec.rb +29 -16
- data/spec/functional/indexes_spec.rb +19 -18
- data/spec/functional/keys_spec.rb +55 -28
- data/spec/functional/logger_spec.rb +3 -3
- data/spec/functional/modifiers_spec.rb +81 -19
- data/spec/functional/partial_updates_spec.rb +577 -0
- data/spec/functional/protected_spec.rb +14 -14
- data/spec/functional/querying_spec.rb +77 -28
- data/spec/functional/safe_spec.rb +23 -27
- data/spec/functional/sci_spec.rb +9 -9
- data/spec/functional/scopes_spec.rb +323 -2
- data/spec/functional/static_keys_spec.rb +153 -0
- data/spec/functional/stats_spec.rb +28 -16
- data/spec/functional/strong_parameters_spec.rb +49 -0
- data/spec/functional/touch_spec.rb +1 -1
- data/spec/functional/validations_spec.rb +51 -57
- data/spec/quality_spec.rb +2 -2
- 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 +26 -20
- 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 +17 -17
- data/spec/unit/identity_map_middleware_spec.rb +65 -96
- data/spec/unit/key_spec.rb +28 -26
- data/spec/unit/keys_spec.rb +20 -11
- data/spec/unit/mongo_mapper_spec.rb +38 -85
- data/spec/unit/serialization_spec.rb +1 -1
- data/spec/unit/time_zones_spec.rb +2 -2
- data/spec/unit/validations_spec.rb +46 -33
- metadata +56 -32
- data/README.rdoc +0 -59
- data/lib/mongo_mapper/connections/10gen.rb +0 -0
- data/lib/mongo_mapper/connections/moped.rb +0 -0
- data/lib/mongo_mapper/extensions/ordered_hash.rb +0 -23
@@ -13,7 +13,11 @@ describe "Stats" do
|
|
13
13
|
|
14
14
|
context "with no documents present" do
|
15
15
|
it "should return nil" do
|
16
|
-
|
16
|
+
if Docs.stats == nil
|
17
|
+
Docs.stats.should == nil
|
18
|
+
else
|
19
|
+
Docs.stats['count'].should == 0
|
20
|
+
end
|
17
21
|
end
|
18
22
|
end
|
19
23
|
|
@@ -23,52 +27,60 @@ describe "Stats" do
|
|
23
27
|
Docs.create!
|
24
28
|
end
|
25
29
|
|
30
|
+
def get_stats
|
31
|
+
MongoMapper.database.command(:collstats => 'docs').documents[0]
|
32
|
+
end
|
33
|
+
|
26
34
|
it "should have the correct count" do
|
27
|
-
|
35
|
+
Docs.stats.count.should == get_stats['count']
|
28
36
|
end
|
29
37
|
|
30
38
|
it "should have the correct namespace" do
|
31
|
-
|
39
|
+
Docs.stats.ns.should == get_stats['ns']
|
32
40
|
end
|
33
41
|
|
34
42
|
it "should have the correct size" do
|
35
|
-
|
43
|
+
Docs.stats.size.should == get_stats['size']
|
36
44
|
end
|
37
45
|
|
38
46
|
it "should have the correct storage size" do
|
39
|
-
|
47
|
+
Docs.stats.storage_size.should == get_stats['storageSize']
|
40
48
|
end
|
41
49
|
|
42
50
|
it "should have the correct average object size" do
|
43
|
-
|
51
|
+
Docs.stats.avg_obj_size.should == get_stats['avgObjSize']
|
44
52
|
end
|
45
53
|
|
46
54
|
it "should have the correct number of extents" do
|
47
|
-
|
55
|
+
if get_stats['numExtents']
|
56
|
+
Docs.stats.num_extents.should == get_stats['numExtents']
|
57
|
+
end
|
48
58
|
end
|
49
59
|
|
50
60
|
it "should have the correct number of indexes" do
|
51
|
-
|
61
|
+
Docs.stats.nindexes.should == get_stats['nindexes']
|
52
62
|
end
|
53
63
|
|
54
64
|
it "should have the correct last extent size" do
|
55
|
-
|
65
|
+
if get_stats['lastExtentSize']
|
66
|
+
Docs.stats.last_extent_size.should == get_stats['lastExtentSize']
|
67
|
+
end
|
56
68
|
end
|
57
69
|
|
58
70
|
it "should have the correct padding factor" do
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
it "should have the correct system flags" do
|
63
|
-
expect(Docs.stats.system_flags).to eq(Docs.collection.stats['systemFlags'])
|
71
|
+
if get_stats['paddingFactor']
|
72
|
+
Docs.stats.padding_factor.should == get_stats['paddingFactor']
|
73
|
+
end
|
64
74
|
end
|
65
75
|
|
66
76
|
it "should have the correct user flags" do
|
67
|
-
|
77
|
+
if get_stats['userFlags']
|
78
|
+
Docs.stats.user_flags.should == get_stats['userFlags']
|
79
|
+
end
|
68
80
|
end
|
69
81
|
|
70
82
|
it "should have the correct total index size" do
|
71
|
-
|
83
|
+
Docs.stats.total_index_size.should == get_stats['totalIndexSize']
|
72
84
|
end
|
73
85
|
end
|
74
86
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Strong parameters" do
|
4
|
+
context 'A document with strong parameters protection' do
|
5
|
+
if ::ActiveModel.const_defined?(:ForbiddenAttributesProtection)
|
6
|
+
require "action_controller/metal/strong_parameters"
|
7
|
+
|
8
|
+
before do
|
9
|
+
@doc_class = Doc do
|
10
|
+
plugin MongoMapper::Plugins::StrongParameters
|
11
|
+
|
12
|
+
key :name, String
|
13
|
+
key :admin, Boolean, :default => false
|
14
|
+
end
|
15
|
+
|
16
|
+
@doc = @doc_class.create(:name => 'Steve Sloan')
|
17
|
+
end
|
18
|
+
|
19
|
+
let(:params) {
|
20
|
+
{name: "Permitted", admin: true}
|
21
|
+
}
|
22
|
+
|
23
|
+
let(:strong_params) {
|
24
|
+
ActionController::Parameters.new params
|
25
|
+
}
|
26
|
+
|
27
|
+
it "allows assignment of attribute hashes" do
|
28
|
+
@doc.attributes = params
|
29
|
+
@doc.name.should =="Permitted"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "doesn't allow mass assignment of ActionController::Parameters" do
|
33
|
+
lambda {
|
34
|
+
@doc.attributes = strong_params
|
35
|
+
}.should raise_error(ActiveModel::ForbiddenAttributesError)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "does not allow mass assignment of non-permitted attributes" do
|
39
|
+
@doc.attributes = strong_params.permit(:name)
|
40
|
+
@doc.admin.should == false
|
41
|
+
end
|
42
|
+
|
43
|
+
it "allows mass assignment of permitted attributes" do
|
44
|
+
@doc.attributes = strong_params.permit(:name)
|
45
|
+
@doc.name.should == "Permitted"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -61,7 +61,7 @@ describe "Touch" do
|
|
61
61
|
|
62
62
|
it 'should not be true by default' do
|
63
63
|
@comment_class.belongs_to :post, :class => @post_class
|
64
|
-
@comment_class.associations[:post].touch?.should_not
|
64
|
+
@comment_class.associations[:post].touch?.should_not be_truthy
|
65
65
|
end
|
66
66
|
|
67
67
|
context 'touch the parent when true' do
|
@@ -57,7 +57,7 @@ describe "Validations" do
|
|
57
57
|
|
58
58
|
it "should create a new document" do
|
59
59
|
instance = @document.create!(:name => "James")
|
60
|
-
instance.new_record?.should
|
60
|
+
instance.new_record?.should be_falsey
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
@@ -140,7 +140,7 @@ describe "Validations" do
|
|
140
140
|
validates_uniqueness_of :name, :scope => :adult
|
141
141
|
end
|
142
142
|
doc = document.new("name" => "joe", :adult => true)
|
143
|
-
doc.save.should
|
143
|
+
doc.save.should be_truthy
|
144
144
|
|
145
145
|
doc2 = document.new("name" => "joe", :adult => false)
|
146
146
|
doc2.should be_valid
|
@@ -149,31 +149,30 @@ describe "Validations" do
|
|
149
149
|
it "should work with i18n taken message" do
|
150
150
|
@document.create(:name => 'joe')
|
151
151
|
doc = @document.create(:name => 'joe')
|
152
|
-
doc.should have_error_on(:name
|
152
|
+
doc.should have_error_on(:name)
|
153
|
+
doc.errors[:name].should == ['has already been taken']
|
153
154
|
end
|
154
155
|
|
155
156
|
it "should allow to update an object" do
|
156
157
|
doc = @document.new("name" => "joe")
|
157
|
-
doc.save.should
|
158
|
+
doc.save.should be_truthy
|
158
159
|
|
159
|
-
@document
|
160
|
-
|
161
|
-
|
162
|
-
.and_return(doc)
|
160
|
+
@document.stub(:first).
|
161
|
+
with(:name => 'joe').
|
162
|
+
and_return(doc)
|
163
163
|
|
164
164
|
doc.name = "joe"
|
165
|
-
doc.valid?.should
|
165
|
+
doc.valid?.should be_truthy
|
166
166
|
doc.should_not have_error_on(:name)
|
167
167
|
end
|
168
168
|
|
169
169
|
it "should fail if object name is not unique" do
|
170
170
|
doc = @document.new("name" => "joe")
|
171
|
-
doc.save.should
|
171
|
+
doc.save.should be_truthy
|
172
172
|
|
173
|
-
@document
|
174
|
-
|
175
|
-
|
176
|
-
.and_return(doc)
|
173
|
+
@document.stub(:first).
|
174
|
+
with(:name => 'joe').
|
175
|
+
and_return(doc)
|
177
176
|
|
178
177
|
doc2 = @document.new("name" => "joe")
|
179
178
|
doc2.should have_error_on(:name)
|
@@ -186,12 +185,11 @@ describe "Validations" do
|
|
186
185
|
end
|
187
186
|
|
188
187
|
doc = document.new("name" => "")
|
189
|
-
doc.save.should
|
188
|
+
doc.save.should be_truthy
|
190
189
|
|
191
|
-
document
|
192
|
-
|
193
|
-
|
194
|
-
.and_return(doc)
|
190
|
+
@document.stub(:first).
|
191
|
+
with(:name => '').
|
192
|
+
and_return(doc)
|
195
193
|
|
196
194
|
doc2 = document.new("name" => "")
|
197
195
|
doc2.should_not have_error_on(:name)
|
@@ -204,7 +202,7 @@ describe "Validations" do
|
|
204
202
|
end
|
205
203
|
|
206
204
|
doc = document.new('name' => nil)
|
207
|
-
doc.save.should
|
205
|
+
doc.save.should be_truthy
|
208
206
|
|
209
207
|
doc2 = document.new('name' => nil)
|
210
208
|
doc2.should_not have_error_on(:name)
|
@@ -217,7 +215,7 @@ describe "Validations" do
|
|
217
215
|
end
|
218
216
|
|
219
217
|
doc = document.new("name" => "BLAMMO")
|
220
|
-
doc.save.should
|
218
|
+
doc.save.should be_truthy
|
221
219
|
|
222
220
|
doc2 = document.new("name" => "blammo")
|
223
221
|
doc2.should_not have_error_on(:name)
|
@@ -233,7 +231,7 @@ describe "Validations" do
|
|
233
231
|
|
234
232
|
it "should fail on entries that differ only in case" do
|
235
233
|
doc = @document.new("name" => "BLAMMO")
|
236
|
-
doc.save.should
|
234
|
+
doc.save.should be_truthy
|
237
235
|
|
238
236
|
doc2 = @document.new("name" => "blammo")
|
239
237
|
doc2.should have_error_on(:name)
|
@@ -251,10 +249,10 @@ describe "Validations" do
|
|
251
249
|
|
252
250
|
it "should check for uniqueness using entire string" do
|
253
251
|
doc = @document.new("name" => "John Doe")
|
254
|
-
doc.save.should
|
252
|
+
doc.save.should be_truthy
|
255
253
|
|
256
254
|
doc2 = @document.new("name" => "John")
|
257
|
-
doc2.valid?.should
|
255
|
+
doc2.valid?.should be_truthy
|
258
256
|
end
|
259
257
|
end
|
260
258
|
|
@@ -269,12 +267,11 @@ describe "Validations" do
|
|
269
267
|
|
270
268
|
it "should fail if the same name exists in the scope" do
|
271
269
|
doc = @document.new("name" => "joe", "scope" => "one")
|
272
|
-
doc.save.should
|
270
|
+
doc.save.should be_truthy
|
273
271
|
|
274
|
-
@document
|
275
|
-
|
276
|
-
|
277
|
-
.and_return(doc)
|
272
|
+
@document.stub(:first).
|
273
|
+
with(:name => 'joe', :scope => "one").
|
274
|
+
and_return(doc)
|
278
275
|
|
279
276
|
doc2 = @document.new("name" => "joe", "scope" => "one")
|
280
277
|
doc2.should have_error_on(:name)
|
@@ -282,12 +279,11 @@ describe "Validations" do
|
|
282
279
|
|
283
280
|
it "should pass if the same name exists in a different scope" do
|
284
281
|
doc = @document.new("name" => "joe", "scope" => "one")
|
285
|
-
doc.save.should
|
282
|
+
doc.save.should be_truthy
|
286
283
|
|
287
|
-
@document
|
288
|
-
|
289
|
-
|
290
|
-
.and_return(nil)
|
284
|
+
@document.stub(:first).
|
285
|
+
with(:name => 'joe', :scope => 'two').
|
286
|
+
and_return(nil)
|
291
287
|
|
292
288
|
doc2 = @document.new("name" => "joe", "scope" => "two")
|
293
289
|
doc2.should_not have_error_on(:name)
|
@@ -306,12 +302,11 @@ describe "Validations" do
|
|
306
302
|
|
307
303
|
it "should fail if the same name exists in the scope" do
|
308
304
|
doc = @document.new("name" => "joe", "first_scope" => "one", "second_scope" => "two")
|
309
|
-
doc.save.should
|
305
|
+
doc.save.should be_truthy
|
310
306
|
|
311
|
-
@document
|
312
|
-
|
313
|
-
|
314
|
-
.and_return(doc)
|
307
|
+
@document.stub(:first).
|
308
|
+
with(:name => 'joe', :first_scope => 'one', :second_scope => 'two').
|
309
|
+
and_return(doc)
|
315
310
|
|
316
311
|
doc2 = @document.new("name" => "joe", "first_scope" => "one", "second_scope" => "two")
|
317
312
|
doc2.should have_error_on(:name)
|
@@ -319,12 +314,11 @@ describe "Validations" do
|
|
319
314
|
|
320
315
|
it "should pass if the same name exists in a different scope" do
|
321
316
|
doc = @document.new("name" => "joe", "first_scope" => "one", "second_scope" => "two")
|
322
|
-
doc.save.should
|
317
|
+
doc.save.should be_truthy
|
323
318
|
|
324
|
-
@document
|
325
|
-
|
326
|
-
|
327
|
-
.and_return(nil)
|
319
|
+
@document.stub(:first).
|
320
|
+
with(:name => 'joe', :first_scope => 'one', :second_scope => 'one').
|
321
|
+
and_return(nil)
|
328
322
|
|
329
323
|
doc2 = @document.new("name" => "joe", "first_scope" => "one", "second_scope" => "one")
|
330
324
|
doc2.should_not have_error_on(:name)
|
@@ -345,21 +339,21 @@ describe "Validations" do
|
|
345
339
|
|
346
340
|
it "should pass if there are no associated docs" do
|
347
341
|
doc = @root_class.new
|
348
|
-
doc.save.should
|
342
|
+
doc.save.should be_truthy
|
349
343
|
end
|
350
344
|
|
351
345
|
it "should pass if the associated doc is valid" do
|
352
346
|
doc = @root_class.new
|
353
347
|
doc.children.build(:name => 'Joe')
|
354
|
-
doc.save.should
|
348
|
+
doc.save.should be_truthy
|
355
349
|
end
|
356
350
|
|
357
351
|
it "should fail if the associated doc is invalid" do
|
358
352
|
doc = @root_class.new
|
359
353
|
doc.children.build
|
360
|
-
doc.should have_error_on(:children
|
354
|
+
doc.should have_error_on(:children)
|
355
|
+
doc.errors[:children].should == ['are invalid']
|
361
356
|
end
|
362
|
-
|
363
357
|
end
|
364
358
|
|
365
359
|
context "validating associated docs with custom context" do
|
@@ -377,22 +371,22 @@ describe "Validations" do
|
|
377
371
|
|
378
372
|
it "should pass if there are no associated docs" do
|
379
373
|
doc = @root_class.new
|
380
|
-
doc.valid?(:custom_context).should
|
374
|
+
doc.valid?(:custom_context).should be_truthy
|
381
375
|
end
|
382
376
|
|
383
377
|
it "should pass if the associated doc is valid" do
|
384
378
|
doc = @root_class.new
|
385
379
|
doc.children.build(:name => 'George')
|
386
|
-
doc.valid?(:custom_context).should
|
380
|
+
doc.valid?(:custom_context).should be_truthy
|
387
381
|
end
|
388
382
|
|
389
383
|
it "should fail if the associated doc is invalid" do
|
390
384
|
doc = @root_class.new
|
391
385
|
doc.children.build(:name => 'Bob')
|
392
|
-
doc.valid?(:custom_context).should_not
|
386
|
+
doc.valid?(:custom_context).should_not be_truthy
|
393
387
|
end
|
394
|
-
|
395
388
|
end
|
389
|
+
|
396
390
|
# context "validates uniqueness of with :unique shortcut" do
|
397
391
|
# it "should work" do
|
398
392
|
# @document = Doc do
|
@@ -402,13 +396,13 @@ describe "Validations" do
|
|
402
396
|
# doc = @document.create(:name => 'John')
|
403
397
|
# doc.should_not have_error_on(:name)
|
404
398
|
#
|
405
|
-
# @document
|
406
|
-
#
|
407
|
-
#
|
408
|
-
# .and_return(doc)
|
399
|
+
# @document.stub(:first).
|
400
|
+
# with(:name => 'John').
|
401
|
+
# and_return(doc)
|
409
402
|
#
|
410
403
|
# second_john = @document.create(:name => 'John')
|
411
|
-
# second_john.should have_error_on(:name
|
404
|
+
# second_john.should have_error_on(:name)
|
405
|
+
# second_john.errors[:name].should == ['has already been taken']
|
412
406
|
# end
|
413
407
|
# end
|
414
408
|
end
|
data/spec/quality_spec.rb
CHANGED
@@ -27,7 +27,7 @@ describe "The library itself" do
|
|
27
27
|
end
|
28
28
|
|
29
29
|
RSpec::Matchers.define :be_well_formed do
|
30
|
-
|
30
|
+
failure_message do |actual|
|
31
31
|
actual.join("\n")
|
32
32
|
end
|
33
33
|
|
@@ -46,6 +46,6 @@ describe "The library itself" do
|
|
46
46
|
error_messages << check_for_extra_spaces(filename)
|
47
47
|
end
|
48
48
|
end
|
49
|
-
|
49
|
+
error_messages.compact.should be_well_formed
|
50
50
|
end
|
51
51
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,10 +2,15 @@ $:.unshift(File.expand_path('../../lib', __FILE__))
|
|
2
2
|
|
3
3
|
require 'rubygems'
|
4
4
|
require 'bundler/setup'
|
5
|
+
Bundler.require(:default)
|
5
6
|
require 'fileutils'
|
6
7
|
require 'timecop'
|
7
8
|
require "generator_spec/test_case"
|
8
9
|
|
10
|
+
if RUBY_ENGINE == "ruby" && RUBY_VERSION >= '2.3'
|
11
|
+
require 'byebug'
|
12
|
+
end
|
13
|
+
|
9
14
|
if RUBY_PLATFORM != "java"
|
10
15
|
if ENV['TRAVIS']
|
11
16
|
require 'coveralls'
|
@@ -39,7 +44,7 @@ def Doc(name='Class', &block)
|
|
39
44
|
end
|
40
45
|
|
41
46
|
klass.class_eval(&block) if block_given?
|
42
|
-
klass.collection.
|
47
|
+
klass.collection.drop
|
43
48
|
klass
|
44
49
|
end
|
45
50
|
|
@@ -58,24 +63,47 @@ def EDoc(name='Class', &block)
|
|
58
63
|
end
|
59
64
|
|
60
65
|
def drop_indexes(klass)
|
61
|
-
klass.collection.
|
66
|
+
klass.collection.indexes.drop_all if klass.database.collection_names.include?(klass.collection.name)
|
62
67
|
end
|
63
68
|
|
64
69
|
log_dir = File.expand_path('../../log', __FILE__)
|
65
70
|
FileUtils.mkdir_p(log_dir) unless File.exist?(log_dir)
|
66
71
|
logger = Logger.new(log_dir + '/test.log')
|
67
72
|
|
68
|
-
|
69
|
-
|
70
|
-
MongoMapper.
|
73
|
+
Mongo::Logger.logger = logger
|
74
|
+
|
75
|
+
MongoMapper.connection = Mongo::Client.new(['127.0.0.1:27017'], :database => 'test')
|
76
|
+
MongoMapper.database.collections.each { |c| c.indexes.drop_all }
|
71
77
|
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
|
72
78
|
|
73
|
-
RSpec.configure do |
|
74
|
-
|
75
|
-
|
79
|
+
RSpec.configure do |config|
|
80
|
+
config.example_status_persistence_file_path = "./spec/examples.txt"
|
81
|
+
|
82
|
+
config.expect_with :rspec do |c|
|
83
|
+
c.syntax = :should
|
84
|
+
end
|
85
|
+
|
86
|
+
config.mock_with :rspec do |mocks|
|
87
|
+
mocks.syntax = :should
|
88
|
+
end
|
89
|
+
|
90
|
+
config.around(:each, :without_connection) do |example|
|
76
91
|
old, MongoMapper.connection = MongoMapper.connection, nil
|
77
92
|
example.run
|
78
93
|
MongoMapper.connection = old
|
79
94
|
end
|
80
|
-
end
|
81
95
|
|
96
|
+
def suppress_stderr
|
97
|
+
begin
|
98
|
+
original_stderr = $stderr.clone
|
99
|
+
$stderr.reopen(File.new('/dev/null', 'w'))
|
100
|
+
retval = yield
|
101
|
+
rescue Exception => e
|
102
|
+
$stderr.reopen(original_stderr)
|
103
|
+
raise e
|
104
|
+
ensure
|
105
|
+
$stderr.reopen(original_stderr)
|
106
|
+
end
|
107
|
+
retval
|
108
|
+
end
|
109
|
+
end
|