mongo_mapper 0.14.0.rc1 → 0.15.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/LICENSE +1 -1
- data/{README.rdoc → README.md} +26 -21
- 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 +3 -0
- data/lib/mongo_mapper/connection.rb +16 -38
- data/lib/mongo_mapper/extensions/object_id.rb +5 -1
- data/lib/mongo_mapper/plugins/accessible.rb +1 -1
- data/lib/mongo_mapper/plugins/associations/base.rb +10 -2
- data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +1 -1
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +6 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +36 -6
- 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/one_embedded_proxy.rb +3 -1
- data/lib/mongo_mapper/plugins/associations/proxy.rb +11 -3
- data/lib/mongo_mapper/plugins/associations/single_association.rb +5 -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/dynamic_finder.rb +1 -1
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +1 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +2 -2
- data/lib/mongo_mapper/plugins/identity_map.rb +3 -1
- data/lib/mongo_mapper/plugins/indexes.rb +13 -6
- data/lib/mongo_mapper/plugins/keys.rb +12 -7
- data/lib/mongo_mapper/plugins/keys/key.rb +13 -8
- data/lib/mongo_mapper/plugins/modifiers.rb +39 -14
- data/lib/mongo_mapper/plugins/persistence.rb +6 -2
- data/lib/mongo_mapper/plugins/querying.rb +9 -3
- data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +6 -6
- data/lib/mongo_mapper/plugins/safe.rb +10 -4
- data/lib/mongo_mapper/plugins/scopes.rb +19 -3
- data/lib/mongo_mapper/plugins/stats.rb +1 -3
- data/lib/mongo_mapper/plugins/strong_parameters.rb +26 -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/spec/examples.txt +1729 -0
- data/spec/functional/accessible_spec.rb +7 -1
- data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +2 -2
- data/spec/functional/associations/belongs_to_proxy_spec.rb +55 -5
- data/spec/functional/associations/in_array_proxy_spec.rb +149 -14
- 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 +22 -22
- data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +2 -2
- data/spec/functional/associations/many_polymorphic_proxy_spec.rb +4 -4
- data/spec/functional/associations/one_as_proxy_spec.rb +8 -8
- data/spec/functional/associations/one_embedded_proxy_spec.rb +28 -0
- data/spec/functional/associations/one_proxy_spec.rb +19 -9
- data/spec/functional/associations_spec.rb +3 -3
- data/spec/functional/binary_spec.rb +2 -2
- data/spec/functional/caching_spec.rb +15 -22
- data/spec/functional/callbacks_spec.rb +2 -2
- data/spec/functional/counter_cache_spec.rb +10 -10
- data/spec/functional/dirty_spec.rb +48 -10
- data/spec/functional/dirty_with_callbacks_spec.rb +59 -0
- data/spec/functional/document_spec.rb +5 -8
- data/spec/functional/dumpable_spec.rb +1 -1
- data/spec/functional/embedded_document_spec.rb +5 -5
- data/spec/functional/identity_map_spec.rb +8 -8
- data/spec/functional/indexes_spec.rb +19 -18
- data/spec/functional/keys_spec.rb +51 -33
- data/spec/functional/logger_spec.rb +2 -2
- data/spec/functional/modifiers_spec.rb +81 -19
- data/spec/functional/partial_updates_spec.rb +8 -8
- data/spec/functional/protected_spec.rb +1 -1
- data/spec/functional/querying_spec.rb +70 -22
- data/spec/functional/safe_spec.rb +23 -27
- data/spec/functional/sci_spec.rb +7 -7
- data/spec/functional/scopes_spec.rb +89 -1
- data/spec/functional/static_keys_spec.rb +2 -2
- data/spec/functional/stats_spec.rb +28 -12
- data/spec/functional/strong_parameters_spec.rb +49 -0
- data/spec/functional/validations_spec.rb +8 -16
- data/spec/quality_spec.rb +1 -1
- data/spec/spec_helper.rb +39 -8
- data/spec/support/matchers.rb +1 -1
- data/spec/unit/associations/proxy_spec.rb +13 -5
- data/spec/unit/clone_spec.rb +1 -1
- data/spec/unit/document_spec.rb +3 -3
- data/spec/unit/embedded_document_spec.rb +4 -5
- data/spec/unit/extensions_spec.rb +2 -2
- data/spec/unit/identity_map_middleware_spec.rb +65 -96
- data/spec/unit/key_spec.rb +16 -17
- data/spec/unit/keys_spec.rb +17 -8
- data/spec/unit/mongo_mapper_spec.rb +41 -88
- data/spec/unit/rails_spec.rb +2 -2
- data/spec/unit/validations_spec.rb +18 -18
- metadata +53 -31
- data/lib/mongo_mapper/extensions/ordered_hash.rb +0 -23
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "Logger" do
|
4
|
-
context "with connection that has logger" do
|
4
|
+
context "with connection that has logger", without_connection: true do
|
5
5
|
before do
|
6
6
|
@output = StringIO.new
|
7
7
|
@logger = Logger.new(@output)
|
8
|
-
MongoMapper.connection = Mongo::
|
8
|
+
MongoMapper.connection = Mongo::Client.new(['127.0.0.1:27017'], :logger => @logger)
|
9
9
|
end
|
10
10
|
|
11
11
|
it "should be able to get access to that logger" do
|
@@ -4,7 +4,7 @@ module Modifiers
|
|
4
4
|
describe "Modifiers" do
|
5
5
|
let(:page_class_with_compound_key) {
|
6
6
|
Doc do
|
7
|
-
key :_id,
|
7
|
+
key :_id, Hash, :default => lambda { Hash['n', 42, 'i', BSON::ObjectId.new] }
|
8
8
|
key :title, String
|
9
9
|
key :day_count, Integer, :default => 0
|
10
10
|
key :week_count, Integer, :default => 0
|
@@ -24,7 +24,7 @@ module Modifiers
|
|
24
24
|
}
|
25
25
|
|
26
26
|
def assert_page_counts(page, day_count, week_count, month_count)
|
27
|
-
doc = page.collection.
|
27
|
+
doc = page.collection.find({:_id => page.id}).first
|
28
28
|
doc.should be_present, "Could not find document"
|
29
29
|
doc.fetch('day_count').should == day_count
|
30
30
|
doc.fetch('week_count').should == week_count
|
@@ -32,7 +32,7 @@ module Modifiers
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def assert_keys_removed(page, *keys)
|
35
|
-
page.class.collection.
|
35
|
+
page.class.collection.find({:_id => page.id}).first.tap do |doc|
|
36
36
|
doc.should be_present, "Could not find document"
|
37
37
|
(doc.keys & keys).should be_empty, "Expected to not have keys #{keys.inspect}, got #{(keys & doc.keys).inspect}"
|
38
38
|
end
|
@@ -69,10 +69,10 @@ module Modifiers
|
|
69
69
|
it "should be able to pass safe option" do
|
70
70
|
page_class.create(:title => "Better Be Safe than Sorry")
|
71
71
|
|
72
|
-
|
72
|
+
Mongo::Collection.any_instance.should_receive(:update_many).with(
|
73
73
|
{:title => "Better Be Safe than Sorry"},
|
74
74
|
{'$unset' => {:tags => 1}},
|
75
|
-
{:w => 1
|
75
|
+
{:w => 1}
|
76
76
|
)
|
77
77
|
page_class.unset({:title => "Better Be Safe than Sorry"}, :tags, {:w => 1})
|
78
78
|
end
|
@@ -166,17 +166,17 @@ module Modifiers
|
|
166
166
|
it "should typecast values before querying" do
|
167
167
|
page_class.key :tags, Set
|
168
168
|
|
169
|
-
|
169
|
+
lambda {
|
170
170
|
page_class.set(page.id, :tags => ['foo', 'bar'].to_set)
|
171
171
|
page.reload
|
172
172
|
page.tags.should == Set.new(['foo', 'bar'])
|
173
|
-
}.
|
173
|
+
}.should_not raise_error
|
174
174
|
end
|
175
175
|
|
176
|
-
it "should not typecast keys that are not defined in document" do
|
177
|
-
|
176
|
+
it "should not typecast keys that are not defined in document and have no default typecasing" do
|
177
|
+
lambda {
|
178
178
|
page_class.set(page.id, :colors => ['red', 'green'].to_set)
|
179
|
-
}.
|
179
|
+
}.should raise_error(BSON::Error::UnserializableClass, /does not define its BSON serialized type/)
|
180
180
|
end
|
181
181
|
|
182
182
|
it "should set keys that are not defined in document" do
|
@@ -196,10 +196,10 @@ module Modifiers
|
|
196
196
|
it "should be able to pass safe option" do
|
197
197
|
page_class.create(:title => "Better Be Safe than Sorry")
|
198
198
|
|
199
|
-
|
199
|
+
Mongo::Collection.any_instance.should_receive(:update_many).with(
|
200
200
|
{:title => "Better Be Safe than Sorry"},
|
201
201
|
{'$set' => {:title => "I like safety."}},
|
202
|
-
{:w => 1
|
202
|
+
{:w => 1}
|
203
203
|
)
|
204
204
|
page_class.set({:title => "Better Be Safe than Sorry"}, {:title => "I like safety."}, {:safe => true})
|
205
205
|
end
|
@@ -238,6 +238,15 @@ module Modifiers
|
|
238
238
|
context "push_all" do
|
239
239
|
let(:tags) { %w(foo bar) }
|
240
240
|
|
241
|
+
before do
|
242
|
+
Kernel.stub(:warn)
|
243
|
+
end
|
244
|
+
|
245
|
+
it "should issue a warning" do
|
246
|
+
Kernel.should_receive(:warn).with("push_all no longer supported. use $push with $each")
|
247
|
+
page_class.push_all({:title => 'Home'}, :tags => tags)
|
248
|
+
end
|
249
|
+
|
241
250
|
it "should work with criteria and modifier hashes" do
|
242
251
|
page_class.push_all({:title => 'Home'}, :tags => tags)
|
243
252
|
|
@@ -385,9 +394,9 @@ module Modifiers
|
|
385
394
|
page_class.create(:title => "Better Be Safe than Sorry")
|
386
395
|
|
387
396
|
# We are trying to increment a key of type string here which should fail
|
388
|
-
|
397
|
+
lambda {
|
389
398
|
page_class.increment({:title => "Better Be Safe than Sorry"}, {:title => 1}, {:safe => true})
|
390
|
-
}.
|
399
|
+
}.should raise_error(Mongo::Error::OperationFailure)
|
391
400
|
end
|
392
401
|
|
393
402
|
it "should be able to pass both safe and upsert options" do
|
@@ -397,19 +406,51 @@ module Modifiers
|
|
397
406
|
page_class.first(:title => new_key_value).day_count.should == 1
|
398
407
|
end
|
399
408
|
end
|
409
|
+
|
410
|
+
context "upsert" do
|
411
|
+
it "should insert document if not present" do
|
412
|
+
lambda {
|
413
|
+
page_class.upsert({:title => 'A new story'}, {:title => 'A new story', :day_count => 1})
|
414
|
+
}.should change {page_class.count}
|
415
|
+
page_class.first(title: 'A new story').day_count.should == 1
|
416
|
+
end
|
417
|
+
|
418
|
+
it "should update documents if present" do
|
419
|
+
lambda {
|
420
|
+
page_class.upsert({:_id => page2.id}, {tags: %w(foo bar)})
|
421
|
+
}.should_not change {page_class.count}
|
422
|
+
page2.reload.tags.should == %w(foo bar)
|
423
|
+
end
|
424
|
+
end
|
425
|
+
|
426
|
+
context "find_and_modify" do
|
427
|
+
it "should retrieve the document without the changes" do
|
428
|
+
page_class.find_and_modify(query: {_id: page2.id}, update: {title: 'A new title'})['title'].should == 'Home'
|
429
|
+
page2.reload.title.should == 'A new title'
|
430
|
+
end
|
431
|
+
|
432
|
+
it "should allow find_and_modify options" do
|
433
|
+
page_class.find_and_modify(query: {_id: page2.id}, update: {title: 'A new title'}, return_document: :after)['title'].should == 'A new title'
|
434
|
+
end
|
435
|
+
end
|
436
|
+
|
400
437
|
end
|
401
438
|
|
402
439
|
context "compound keys" do
|
403
440
|
it "should create a document" do
|
404
|
-
|
441
|
+
lambda {
|
405
442
|
page_class_with_compound_key.create(:title => 'Foo', :tags => %w(foo))
|
406
|
-
}.
|
443
|
+
}.should change { page_class_with_compound_key.count }.by(1)
|
407
444
|
doc = page_class_with_compound_key.first
|
408
445
|
page_class_with_compound_key.find(doc._id).should == doc
|
409
446
|
end
|
410
447
|
end
|
411
448
|
|
412
449
|
context "instance methods" do
|
450
|
+
before do
|
451
|
+
Kernel.stub(:warn)
|
452
|
+
end
|
453
|
+
|
413
454
|
{
|
414
455
|
:page_class_with_standard_key => "with standard key",
|
415
456
|
:page_class_with_compound_key => "with compound key",
|
@@ -430,6 +471,13 @@ module Modifiers
|
|
430
471
|
assert_page_counts page, 1, 2, 3
|
431
472
|
end
|
432
473
|
|
474
|
+
it "should be able to increment with just the field name" do
|
475
|
+
page = page_class.create
|
476
|
+
page.increment :day_count
|
477
|
+
|
478
|
+
assert_page_counts page, 1, 0, 0
|
479
|
+
end
|
480
|
+
|
433
481
|
it "should be able to decrement with modifier hashes" do
|
434
482
|
page = page_class.create(:day_count => 1, :week_count => 2, :month_count => 3)
|
435
483
|
page.decrement(:day_count => 1, :week_count => 2, :month_count => 3)
|
@@ -437,6 +485,13 @@ module Modifiers
|
|
437
485
|
assert_page_counts page, 0, 0, 0
|
438
486
|
end
|
439
487
|
|
488
|
+
it "should be able to decrement with just the field name" do
|
489
|
+
page = page_class.create
|
490
|
+
page.decrement :day_count
|
491
|
+
|
492
|
+
assert_page_counts page, -1, 0, 0
|
493
|
+
end
|
494
|
+
|
440
495
|
it "should always decrement when decrement is called whether number is positive or negative" do
|
441
496
|
page = page_class.create(:day_count => 1, :week_count => 2, :month_count => 3)
|
442
497
|
page.decrement(:day_count => -1, :week_count => 2, :month_count => -3)
|
@@ -467,6 +522,13 @@ module Modifiers
|
|
467
522
|
page.tags.should == %w(foo bar)
|
468
523
|
end
|
469
524
|
|
525
|
+
it "should issue a warning with push_all" do
|
526
|
+
Kernel.should_receive(:warn).with("push_all no longer supported. use $push with $each")
|
527
|
+
|
528
|
+
page = page_class.create
|
529
|
+
page.push_all(:tags => %w(foo bar))
|
530
|
+
end
|
531
|
+
|
470
532
|
it "should be able to pull with criteria and modifier hashes" do
|
471
533
|
page = page_class.create(:tags => %w(foo bar))
|
472
534
|
page.pull(:tags => 'foo')
|
@@ -531,9 +593,9 @@ module Modifiers
|
|
531
593
|
page = page_class.create(:title => "Safe Page")
|
532
594
|
|
533
595
|
# We are trying to increment a key of type string here which should fail
|
534
|
-
|
596
|
+
lambda {
|
535
597
|
page.increment({:title => 1}, {:safe => true})
|
536
|
-
}.
|
598
|
+
}.should raise_error(Mongo::Error::OperationFailure)
|
537
599
|
end
|
538
600
|
|
539
601
|
it "should be able to pass upsert and safe options" do
|
@@ -547,4 +609,4 @@ module Modifiers
|
|
547
609
|
end
|
548
610
|
end
|
549
611
|
end
|
550
|
-
end
|
612
|
+
end
|
@@ -85,9 +85,9 @@ describe "Partial Updates" do
|
|
85
85
|
@obj.save!
|
86
86
|
|
87
87
|
mock_collection = double 'collection'
|
88
|
-
|
88
|
+
@obj.stub(:collection).and_return mock_collection
|
89
89
|
|
90
|
-
|
90
|
+
@obj.collection.should_receive(:update_one).with({:_id => @obj.id}, {
|
91
91
|
'$set' => {
|
92
92
|
"string_field" => "bar",
|
93
93
|
"updated_at" => kind_of(Time),
|
@@ -126,16 +126,16 @@ describe "Partial Updates" do
|
|
126
126
|
@obj.save!
|
127
127
|
|
128
128
|
mock_collection = double 'collection'
|
129
|
-
|
129
|
+
@obj.stub(:collection).and_return mock_collection
|
130
130
|
|
131
|
-
|
131
|
+
@obj.collection.should_receive(:update_one).with({:_id => @obj.id}, {
|
132
132
|
"_id" => @obj.id,
|
133
133
|
"string_field" => "bar",
|
134
134
|
"array_field" => [],
|
135
135
|
"hash_field" => {},
|
136
136
|
"created_at" => kind_of(Time),
|
137
137
|
"updated_at" => kind_of(Time),
|
138
|
-
}, {})
|
138
|
+
}, {upsert: true})
|
139
139
|
|
140
140
|
@obj.string_field = "bar"
|
141
141
|
@obj.save!
|
@@ -234,7 +234,7 @@ describe "Partial Updates" do
|
|
234
234
|
attrs = @obj.attributes.dup
|
235
235
|
attrs.delete("foo")
|
236
236
|
|
237
|
-
|
237
|
+
@obj.stub(:attributes).and_return(attrs)
|
238
238
|
|
239
239
|
@obj.fields_for_partial_update.should == {
|
240
240
|
:set_fields => [],
|
@@ -566,11 +566,11 @@ describe "Partial Updates" do
|
|
566
566
|
updates[:unset_fields].should == []
|
567
567
|
|
568
568
|
mock_collection = double('collection')
|
569
|
-
|
569
|
+
obj.stub(:collection).and_return(mock_collection)
|
570
570
|
|
571
571
|
update_query_expectation = hash_including("$set"=> hash_including("boolean_field"))
|
572
572
|
|
573
|
-
obj.collection.
|
573
|
+
obj.collection.should_not_receive(:update_one).with(anything, update_query_expectation, anything)
|
574
574
|
obj.save!
|
575
575
|
end
|
576
576
|
end
|
@@ -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
|
@@ -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
|
@@ -219,6 +219,16 @@ describe "Querying" do
|
|
219
219
|
end
|
220
220
|
end
|
221
221
|
|
222
|
+
context "(with array of single id)" do
|
223
|
+
it "should return an array" do
|
224
|
+
document.find([@doc1._id]).should == [@doc1]
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should return an array for find!" do
|
228
|
+
document.find!([@doc1._id]).should == [@doc1]
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
222
232
|
it "should be able to find using condition auto-detection" do
|
223
233
|
document.first(:first_name => 'John').should == @doc1
|
224
234
|
document.all(:last_name => 'Nunemaker', :order => 'age desc').should == [@doc1, @doc3]
|
@@ -255,6 +265,18 @@ describe "Querying" do
|
|
255
265
|
document.find_each(:last_name => 'Nunemaker', :order => 'age desc') {|doc| yield_documents << doc }
|
256
266
|
yield_documents.should == [@doc1, @doc3]
|
257
267
|
end
|
268
|
+
|
269
|
+
it "should return an enumerator when no block is given" do
|
270
|
+
yield_documents = []
|
271
|
+
enum = document.find_each(:order => "first_name")
|
272
|
+
enum.with_index {|doc, idx| yield_documents << [doc, idx] }
|
273
|
+
yield_documents.should == [[@doc1, 0], [@doc3, 1], [@doc2, 2]]
|
274
|
+
|
275
|
+
yield_documents = []
|
276
|
+
enum = document.find_each(:last_name => 'Nunemaker', :order => 'age desc')
|
277
|
+
enum.with_index {|doc, idx| yield_documents << [doc, idx] }
|
278
|
+
yield_documents.should == [[@doc1, 0], [@doc3, 1]]
|
279
|
+
end
|
258
280
|
end
|
259
281
|
end # finding documents
|
260
282
|
|
@@ -292,12 +314,12 @@ describe "Querying" do
|
|
292
314
|
end
|
293
315
|
|
294
316
|
it "should disregard non-keys when creating, but use them in the query" do
|
295
|
-
|
317
|
+
lambda {
|
296
318
|
document.create(:first_name => 'John', :age => 9)
|
297
319
|
lambda {
|
298
320
|
document.first_or_create(:first_name => 'John', :age.gt => 10).first_name.should == 'John'
|
299
321
|
}.should change { document.count }.by(1)
|
300
|
-
}.
|
322
|
+
}.should_not raise_error
|
301
323
|
end
|
302
324
|
end
|
303
325
|
|
@@ -320,10 +342,10 @@ describe "Querying" do
|
|
320
342
|
end
|
321
343
|
|
322
344
|
it "should disregard non-keys when initializing, but use them in the query" do
|
323
|
-
|
345
|
+
lambda {
|
324
346
|
document.create(:first_name => 'John', :age => 9)
|
325
347
|
document.first_or_new(:first_name => 'John', :age.gt => 10).first_name.should == 'John'
|
326
|
-
}.
|
348
|
+
}.should_not raise_error
|
327
349
|
end
|
328
350
|
end
|
329
351
|
|
@@ -616,6 +638,32 @@ describe "Querying" do
|
|
616
638
|
it "should be chainable" do
|
617
639
|
@query.sort(:age).all.map(&:age).should == [26, 27, 28]
|
618
640
|
end
|
641
|
+
|
642
|
+
it "supports hash" do
|
643
|
+
@query = document.fields({:age => 0})
|
644
|
+
docs = @query.all
|
645
|
+
docs.should include(@doc1)
|
646
|
+
docs.should include(@doc3)
|
647
|
+
docs.should include(@doc2)
|
648
|
+
docs.each do |doc|
|
649
|
+
doc.age.should be_nil # key was not loaded
|
650
|
+
doc.first_name.should_not be_nil
|
651
|
+
doc.last_name.should_not be_nil # key was not loaded
|
652
|
+
end
|
653
|
+
end
|
654
|
+
|
655
|
+
it "supports array" do
|
656
|
+
@query = document.fields([:age,:first_name])
|
657
|
+
docs = @query.all
|
658
|
+
docs.should include(@doc1)
|
659
|
+
docs.should include(@doc3)
|
660
|
+
docs.should include(@doc2)
|
661
|
+
docs.each do |doc|
|
662
|
+
doc.age.should_not be_nil
|
663
|
+
doc.first_name.should_not be_nil
|
664
|
+
doc.last_name.should be_nil # key was not loaded
|
665
|
+
end
|
666
|
+
end
|
619
667
|
end
|
620
668
|
|
621
669
|
context ".limit" do
|
@@ -763,7 +811,7 @@ describe "Querying" do
|
|
763
811
|
it "should update the attribute without invoking validations" do
|
764
812
|
document.key :name, String, :required => true
|
765
813
|
|
766
|
-
|
814
|
+
@doc.should_receive(:valid?).never
|
767
815
|
@doc.update_attribute('name', '').should be_truthy
|
768
816
|
|
769
817
|
@doc.reload.name.should == ''
|
@@ -868,7 +916,7 @@ describe "Querying" do
|
|
868
916
|
|
869
917
|
it "should insert invalid document" do
|
870
918
|
doc = document.new
|
871
|
-
|
919
|
+
doc.should_receive(:valid?).never
|
872
920
|
doc.save(:validate => false)
|
873
921
|
document.count.should == 1
|
874
922
|
end
|
@@ -885,15 +933,15 @@ describe "Querying" do
|
|
885
933
|
|
886
934
|
it "should allow passing safe" do
|
887
935
|
@document.create(:name => 'John')
|
888
|
-
|
936
|
+
lambda {
|
889
937
|
@document.new(:name => 'John').save(:safe => true)
|
890
|
-
}.
|
938
|
+
}.should raise_error(Mongo::Error::OperationFailure)
|
891
939
|
end
|
892
940
|
|
893
941
|
it "should raise argument error if options has unsupported key" do
|
894
|
-
|
942
|
+
lambda {
|
895
943
|
@document.new.save(:foo => true)
|
896
|
-
}.
|
944
|
+
}.should raise_error(ArgumentError)
|
897
945
|
end
|
898
946
|
end
|
899
947
|
|
@@ -906,21 +954,21 @@ describe "Querying" do
|
|
906
954
|
|
907
955
|
it "should allow passing safe" do
|
908
956
|
@document.create(:name => 'John')
|
909
|
-
|
957
|
+
lambda {
|
910
958
|
@document.new(:name => 'John').save!(:safe => true)
|
911
|
-
}.
|
959
|
+
}.should raise_error(Mongo::Error::OperationFailure)
|
912
960
|
end
|
913
961
|
|
914
962
|
it "should raise argument error if options has unsupported key" do
|
915
|
-
|
963
|
+
lambda {
|
916
964
|
@document.new.save!(:foo => true)
|
917
|
-
}.
|
965
|
+
}.should raise_error(ArgumentError)
|
918
966
|
end
|
919
967
|
|
920
968
|
it "should raise argument error if using validate as that would be pointless with save!" do
|
921
|
-
|
969
|
+
lambda {
|
922
970
|
@document.new.save!(:validate => false)
|
923
|
-
}.
|
971
|
+
}.should raise_error(ArgumentError)
|
924
972
|
end
|
925
973
|
end
|
926
974
|
|