mongo_mapper 0.13.0.beta2 → 0.15.0
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 +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
@@ -16,7 +16,7 @@ describe "Touch" do
|
|
16
16
|
end
|
17
17
|
|
18
18
|
doc.reload
|
19
|
-
doc.updated_at.should_not == old_updated_at
|
19
|
+
doc.updated_at.should_not == old_updated_at
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -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
|
@@ -79,7 +79,7 @@ describe "Touch" do
|
|
79
79
|
|
80
80
|
@post.reload.updated_at.should_not == orig_updated_at
|
81
81
|
end
|
82
|
-
|
82
|
+
|
83
83
|
it "should when the child is updated" do
|
84
84
|
@comment.save
|
85
85
|
old_updated_at = @post.updated_at
|
@@ -88,7 +88,7 @@ describe "Touch" do
|
|
88
88
|
end
|
89
89
|
@post.reload.updated_at.should_not == old_updated_at
|
90
90
|
end
|
91
|
-
|
91
|
+
|
92
92
|
it "should when the child is touched" do
|
93
93
|
@comment.save
|
94
94
|
old_updated_at = @post.updated_at
|
@@ -117,9 +117,9 @@ describe "Touch" do
|
|
117
117
|
Timecop.freeze(Time.now + 1.day) do
|
118
118
|
comment.save
|
119
119
|
end
|
120
|
-
|
120
|
+
|
121
121
|
post.reload.updated_at.should == post.created_at
|
122
122
|
end
|
123
123
|
end
|
124
124
|
end
|
125
|
-
end
|
125
|
+
end
|
@@ -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
|
@@ -0,0 +1,51 @@
|
|
1
|
+
if defined?(Encoding) && Encoding.default_external != "UTF-8"
|
2
|
+
Encoding.default_external = "UTF-8"
|
3
|
+
end
|
4
|
+
|
5
|
+
describe "The library itself" do
|
6
|
+
def check_for_tab_characters(filename)
|
7
|
+
failing_lines = []
|
8
|
+
File.readlines(filename).each_with_index do |line,number|
|
9
|
+
failing_lines << number + 1 if line =~ /\t/
|
10
|
+
end
|
11
|
+
|
12
|
+
unless failing_lines.empty?
|
13
|
+
"#{filename} has tab characters on lines #{failing_lines.join(', ')}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def check_for_extra_spaces(filename)
|
18
|
+
failing_lines = []
|
19
|
+
File.readlines(filename).each_with_index do |line,number|
|
20
|
+
next if line =~ /^\s+#.*\s+\n$/
|
21
|
+
failing_lines << number + 1 if line =~ /\s+\n$/
|
22
|
+
end
|
23
|
+
|
24
|
+
unless failing_lines.empty?
|
25
|
+
"#{filename} has spaces on the EOL on lines #{failing_lines.join(', ')}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
RSpec::Matchers.define :be_well_formed do
|
30
|
+
failure_message do |actual|
|
31
|
+
actual.join("\n")
|
32
|
+
end
|
33
|
+
|
34
|
+
match do |actual|
|
35
|
+
actual.empty?
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it "has no malformed whitespace" do
|
40
|
+
exempt = /\.gitmodules|\.marshal|fixtures|vendor|ssl_certs|kill|LICENSE/
|
41
|
+
error_messages = []
|
42
|
+
Dir.chdir(File.expand_path("../..", __FILE__)) do
|
43
|
+
`git ls-files`.split("\n").each do |filename|
|
44
|
+
next if filename =~ exempt
|
45
|
+
error_messages << check_for_tab_characters(filename)
|
46
|
+
error_messages << check_for_extra_spaces(filename)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
error_messages.compact.should be_well_formed
|
50
|
+
end
|
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
|
data/spec/support/matchers.rb
CHANGED
@@ -1,24 +1,15 @@
|
|
1
1
|
RSpec::Matchers.define :have_error_on do |*args|
|
2
|
-
@message = nil
|
3
|
-
@attributes = [args]
|
4
|
-
|
5
|
-
chain :or do |*args|
|
6
|
-
@attributes << args
|
7
|
-
end
|
8
|
-
|
9
2
|
match do |model|
|
3
|
+
field = args.dup.shift
|
4
|
+
error_messages = args
|
5
|
+
|
10
6
|
model.valid?
|
11
|
-
|
12
|
-
if @message
|
13
|
-
!!@has_errors && model.errors[@has_errors[0]].include?(@has_errors[1])
|
14
|
-
else
|
15
|
-
!!@has_errors
|
16
|
-
end
|
7
|
+
model.errors[field].any?
|
17
8
|
end
|
18
9
|
end
|
19
10
|
|
20
11
|
RSpec::Matchers.define :have_index do |index_name|
|
21
12
|
match do |model|
|
22
|
-
model.collection.
|
13
|
+
model.collection.indexes.get(index_name).present?
|
23
14
|
end
|
24
15
|
end
|