mongo_mapper 0.14.0 → 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.
Files changed (76) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE +1 -1
  3. data/{README.rdoc → README.md} +28 -20
  4. data/examples/keys.rb +1 -1
  5. data/examples/modifiers/set.rb +1 -1
  6. data/examples/querying.rb +1 -1
  7. data/examples/safe.rb +2 -2
  8. data/examples/scopes.rb +1 -1
  9. data/lib/mongo_mapper.rb +1 -0
  10. data/lib/mongo_mapper/connection.rb +16 -38
  11. data/lib/mongo_mapper/extensions/object_id.rb +5 -1
  12. data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +1 -1
  13. data/lib/mongo_mapper/plugins/dirty.rb +29 -37
  14. data/lib/mongo_mapper/plugins/document.rb +1 -1
  15. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +1 -1
  16. data/lib/mongo_mapper/plugins/embedded_callbacks.rb +1 -0
  17. data/lib/mongo_mapper/plugins/embedded_document.rb +1 -1
  18. data/lib/mongo_mapper/plugins/indexes.rb +13 -6
  19. data/lib/mongo_mapper/plugins/keys.rb +1 -2
  20. data/lib/mongo_mapper/plugins/modifiers.rb +27 -10
  21. data/lib/mongo_mapper/plugins/persistence.rb +6 -2
  22. data/lib/mongo_mapper/plugins/querying.rb +9 -3
  23. data/lib/mongo_mapper/plugins/querying/decorated_plucky_query.rb +3 -4
  24. data/lib/mongo_mapper/plugins/safe.rb +10 -4
  25. data/lib/mongo_mapper/plugins/stats.rb +1 -3
  26. data/lib/mongo_mapper/utils.rb +2 -2
  27. data/lib/mongo_mapper/version.rb +1 -1
  28. data/spec/examples.txt +1643 -0
  29. data/spec/functional/accessible_spec.rb +1 -1
  30. data/spec/functional/associations/belongs_to_polymorphic_proxy_spec.rb +2 -2
  31. data/spec/functional/associations/belongs_to_proxy_spec.rb +4 -4
  32. data/spec/functional/associations/in_array_proxy_spec.rb +14 -14
  33. data/spec/functional/associations/many_documents_as_proxy_spec.rb +6 -6
  34. data/spec/functional/associations/many_documents_proxy_spec.rb +22 -22
  35. data/spec/functional/associations/many_embedded_polymorphic_proxy_spec.rb +2 -2
  36. data/spec/functional/associations/many_polymorphic_proxy_spec.rb +4 -4
  37. data/spec/functional/associations/one_as_proxy_spec.rb +8 -8
  38. data/spec/functional/associations/one_proxy_spec.rb +8 -8
  39. data/spec/functional/associations_spec.rb +3 -3
  40. data/spec/functional/binary_spec.rb +2 -2
  41. data/spec/functional/caching_spec.rb +15 -22
  42. data/spec/functional/callbacks_spec.rb +2 -2
  43. data/spec/functional/counter_cache_spec.rb +10 -10
  44. data/spec/functional/dirty_spec.rb +27 -10
  45. data/spec/functional/document_spec.rb +5 -5
  46. data/spec/functional/dumpable_spec.rb +1 -1
  47. data/spec/functional/embedded_document_spec.rb +5 -5
  48. data/spec/functional/identity_map_spec.rb +6 -6
  49. data/spec/functional/indexes_spec.rb +19 -18
  50. data/spec/functional/keys_spec.rb +22 -22
  51. data/spec/functional/logger_spec.rb +2 -2
  52. data/spec/functional/modifiers_spec.rb +67 -19
  53. data/spec/functional/partial_updates_spec.rb +8 -8
  54. data/spec/functional/protected_spec.rb +1 -1
  55. data/spec/functional/querying_spec.rb +48 -22
  56. data/spec/functional/safe_spec.rb +23 -27
  57. data/spec/functional/sci_spec.rb +7 -7
  58. data/spec/functional/scopes_spec.rb +1 -1
  59. data/spec/functional/static_keys_spec.rb +2 -2
  60. data/spec/functional/stats_spec.rb +28 -12
  61. data/spec/functional/validations_spec.rb +8 -16
  62. data/spec/quality_spec.rb +1 -1
  63. data/spec/spec_helper.rb +32 -8
  64. data/spec/support/matchers.rb +1 -1
  65. data/spec/unit/associations/proxy_spec.rb +1 -1
  66. data/spec/unit/clone_spec.rb +1 -1
  67. data/spec/unit/document_spec.rb +3 -3
  68. data/spec/unit/embedded_document_spec.rb +4 -5
  69. data/spec/unit/extensions_spec.rb +3 -4
  70. data/spec/unit/identity_map_middleware_spec.rb +65 -96
  71. data/spec/unit/key_spec.rb +16 -17
  72. data/spec/unit/keys_spec.rb +7 -7
  73. data/spec/unit/mongo_mapper_spec.rb +41 -88
  74. data/spec/unit/rails_spec.rb +2 -2
  75. metadata +37 -24
  76. data/lib/mongo_mapper/extensions/ordered_hash.rb +0 -23
@@ -70,7 +70,7 @@ describe "Keys" do
70
70
  end
71
71
  end
72
72
 
73
- expect { doc.new }.to_not raise_error
73
+ lambda { doc.new }.should_not raise_error
74
74
  end
75
75
 
76
76
  it "should not bomb if a key is read before Keys#initialize gets to get called" do
@@ -84,7 +84,7 @@ describe "Keys" do
84
84
  end
85
85
  end
86
86
 
87
- expect { doc.new }.to_not raise_error
87
+ lambda { doc.new }.should_not raise_error
88
88
  end
89
89
 
90
90
  it "should permit for key overrides" do
@@ -93,7 +93,7 @@ describe "Keys" do
93
93
  key :class, String, :accessors => :skip
94
94
  end
95
95
 
96
- doc.collection.insert('class' => 'String')
96
+ doc.collection.insert_one('class' => 'String')
97
97
  doc.all.first.tap do |d|
98
98
  d.class.should == doc
99
99
  d["class"].should == "String"
@@ -109,7 +109,7 @@ describe "Keys" do
109
109
  }
110
110
 
111
111
  before do
112
- doc.collection.insert(:dynamic => "foo")
112
+ doc.collection.insert_one(:dynamic => "foo")
113
113
  doc.first
114
114
  end
115
115
 
@@ -129,7 +129,7 @@ describe "Keys" do
129
129
  describe "with invalid names" do
130
130
  it "should warn when key names start with an uppercase letter" do
131
131
  doc = Doc {}
132
- expect(Kernel).to receive(:warn).once.with(/may not start with uppercase letters/)
132
+ Kernel.should_receive(:warn).once.with(/may not start with uppercase letters/)
133
133
  doc.class_eval do
134
134
  key :NotConstant
135
135
  end
@@ -137,30 +137,30 @@ describe "Keys" do
137
137
 
138
138
  it "should handle keys that start with uppercase letters by translating their first letter to lowercase" do
139
139
  doc = Doc {}
140
- allow(Kernel).to receive(:warn)
140
+ Kernel.stub(:warn)
141
141
  doc.class_eval do
142
142
  key :NotConstant
143
143
  end
144
- doc.collection.insert("NotConstant" => "Just data!")
144
+ doc.collection.insert_one("NotConstant" => "Just data!")
145
145
  doc.first.notConstant.should == "Just data!"
146
146
  end
147
147
 
148
148
  it "should not create accessors for bad keys" do
149
149
  doc = Doc {}
150
- expect(doc).to_not receive(:create_accessors_for)
150
+ doc.should_not_receive(:create_accessors_for)
151
151
  doc.class_eval do
152
152
  key :"bad-name", :__dynamic => true
153
153
  end
154
- expect { doc.new.method(:"bad-name") }.to raise_error(NameError)
154
+ lambda { doc.new.method(:"bad-name") }.should raise_error(NameError)
155
155
  end
156
156
 
157
157
  it "should not create accessors for reserved keys" do
158
158
  doc = Doc {}
159
- expect(doc).to_not receive(:create_accessors_for)
159
+ doc.should_not_receive(:create_accessors_for)
160
160
  doc.class_eval do
161
161
  key :"class", :__dynamic => true
162
162
  end
163
- expect(doc.new.class).to eq doc
163
+ doc.new.class.should == doc
164
164
  end
165
165
 
166
166
  it "should create accessors for good keys" do
@@ -168,13 +168,13 @@ describe "Keys" do
168
168
  key :good_name
169
169
  }
170
170
  doc.new.good_name.should be_nil
171
- expect { doc.new.method("good_name") }.to_not raise_error
171
+ lambda { doc.new.method("good_name") }.should_not raise_error
172
172
  end
173
173
  end
174
174
 
175
175
  it "should handle loading dynamic fields from the database that have bad names" do
176
176
  doc = Doc {}
177
- doc.collection.insert("foo-bar" => "baz-bin")
177
+ doc.collection.insert_one("foo-bar" => "baz-bin")
178
178
 
179
179
  doc.first["foo-bar"].should == "baz-bin"
180
180
  end
@@ -195,7 +195,7 @@ describe "Keys" do
195
195
  end
196
196
 
197
197
  it "should serialize with aliased keys" do
198
- AliasedKeyModel.collection.find_one.keys.should =~ %w(_id f bar)
198
+ AliasedKeyModel.collection.find.first.keys.should =~ %w(_id f bar)
199
199
 
200
200
  AliasedKeyModel.first.tap do |d|
201
201
  d.foo.should == "whee!"
@@ -219,15 +219,15 @@ describe "Keys" do
219
219
  it "should permit dealiasing of atomic operations" do
220
220
  m = AliasedKeyModel.first
221
221
  m.set(:foo => 1)
222
- AliasedKeyModel.collection.find_one["f"].should == 1
223
- AliasedKeyModel.collection.find_one["foo"].should be_nil
222
+ AliasedKeyModel.collection.find.first["f"].should == 1
223
+ AliasedKeyModel.collection.find.first["foo"].should be_nil
224
224
  end
225
225
 
226
226
  it "should permit dealiasing of update operations" do
227
227
  m = AliasedKeyModel.first
228
228
  m.update_attributes(:foo => 1)
229
- AliasedKeyModel.collection.find_one["f"].should == 1
230
- AliasedKeyModel.collection.find_one["foo"].should be_nil
229
+ AliasedKeyModel.collection.find.first["f"].should == 1
230
+ AliasedKeyModel.collection.find.first["foo"].should be_nil
231
231
  end
232
232
 
233
233
  it "should not break when unaliasing non-keys" do
@@ -246,7 +246,7 @@ describe "Keys" do
246
246
  before { AliasedKeyModel.create(:with_underscores => "foobar") }
247
247
  it "should work" do
248
248
  AliasedKeyModel.first.with_underscores.should == "foobar"
249
- AliasedKeyModel.collection.find_one["with-hyphens"].should == "foobar"
249
+ AliasedKeyModel.collection.find.first["with-hyphens"].should == "foobar"
250
250
  end
251
251
  end
252
252
 
@@ -254,7 +254,7 @@ describe "Keys" do
254
254
  before { AliasedKeyModel.create(:field_name => "foobar") }
255
255
  it "should work" do
256
256
  AliasedKeyModel.first.field_name.should == "foobar"
257
- AliasedKeyModel.collection.find_one["alternate_field_name"].should == "foobar"
257
+ AliasedKeyModel.collection.find.first["alternate_field_name"].should == "foobar"
258
258
  end
259
259
  end
260
260
 
@@ -291,7 +291,7 @@ describe "Keys" do
291
291
  owner.reload
292
292
  owner.associated_documents.to_a.should =~ associated_documents.to_a
293
293
 
294
- AssociatedKeyWithAlias.collection.find_one.keys.should =~ %w(_id n aid)
294
+ AssociatedKeyWithAlias.collection.find.first.keys.should =~ %w(_id n aid)
295
295
  end
296
296
 
297
297
  it "should work with embedded documents" do
@@ -301,7 +301,7 @@ describe "Keys" do
301
301
 
302
302
  owner.reload
303
303
  owner.other_documents[0].embedded_name.should == "Underling"
304
- owner.collection.find_one["other_documents"][0]["en"].should == "Underling"
304
+ owner.collection.find.first["other_documents"][0]["en"].should == "Underling"
305
305
  end
306
306
  end
307
307
  end
@@ -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::MongoClient.new('127.0.0.1', 27017, :logger => @logger)
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, BSON::OrderedHash, :default => lambda { BSON::OrderedHash['n', 42, 'i', BSON::ObjectId.new] }
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.find_one({:_id => page.id})
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.find_one({:_id => page.id}).tap do |doc|
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
- expect_any_instance_of(Mongo::Collection).to receive(:update).with(
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, :multi => true}
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
- expect {
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
- }.to_not raise_error
173
+ }.should_not raise_error
174
174
  end
175
175
 
176
- it "should not typecast keys that are not defined in document" do
177
- expect {
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
- }.to raise_error(BSON::InvalidDocument)
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
- expect_any_instance_of(Mongo::Collection).to receive(:update).with(
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, :multi => true}
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
- expect {
397
+ lambda {
389
398
  page_class.increment({:title => "Better Be Safe than Sorry"}, {:title => 1}, {:safe => true})
390
- }.to raise_error(Mongo::OperationFailure)
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
- expect {
441
+ lambda {
405
442
  page_class_with_compound_key.create(:title => 'Foo', :tags => %w(foo))
406
- }.to change { page_class_with_compound_key.count }.by(1)
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",
@@ -467,6 +508,13 @@ module Modifiers
467
508
  page.tags.should == %w(foo bar)
468
509
  end
469
510
 
511
+ it "should issue a warning with push_all" do
512
+ Kernel.should_receive(:warn).with("push_all no longer supported. use $push with $each")
513
+
514
+ page = page_class.create
515
+ page.push_all(:tags => %w(foo bar))
516
+ end
517
+
470
518
  it "should be able to pull with criteria and modifier hashes" do
471
519
  page = page_class.create(:tags => %w(foo bar))
472
520
  page.pull(:tags => 'foo')
@@ -531,9 +579,9 @@ module Modifiers
531
579
  page = page_class.create(:title => "Safe Page")
532
580
 
533
581
  # We are trying to increment a key of type string here which should fail
534
- expect {
582
+ lambda {
535
583
  page.increment({:title => 1}, {:safe => true})
536
- }.to raise_error(Mongo::OperationFailure)
584
+ }.should raise_error(Mongo::Error::OperationFailure)
537
585
  end
538
586
 
539
587
  it "should be able to pass upsert and safe options" do
@@ -547,4 +595,4 @@ module Modifiers
547
595
  end
548
596
  end
549
597
  end
550
- end
598
+ end
@@ -85,9 +85,9 @@ describe "Partial Updates" do
85
85
  @obj.save!
86
86
 
87
87
  mock_collection = double 'collection'
88
- allow(@obj).to receive(:collection).and_return mock_collection
88
+ @obj.stub(:collection).and_return mock_collection
89
89
 
90
- expect(@obj.collection).to receive(:update).with({:_id => @obj.id}, {
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
- allow(@obj).to receive(:collection).and_return mock_collection
129
+ @obj.stub(:collection).and_return mock_collection
130
130
 
131
- expect(@obj.collection).to receive(:save).with({
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
- allow(@obj).to receive(:attributes).and_return(attrs)
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
- allow(obj).to receive(:collection).and_return(mock_collection)
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.should_not receive(:update).with(anything, update_query_expectation, anything)
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.remove
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
- expect { document.find! }.to raise_error(MongoMapper::DocumentNotFound)
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
- expect { document.find!(123) }.to raise_error(MongoMapper::DocumentNotFound)
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
- expect {
206
+ lambda {
207
207
  document.find!(@doc1._id, BSON::ObjectId.new.to_s)
208
- }.to raise_error(MongoMapper::DocumentNotFound)
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
- expect {
212
+ lambda {
213
213
  document.find!([@doc1._id, BSON::ObjectId.new.to_s])
214
- }.to raise_error(MongoMapper::DocumentNotFound)
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
- expect {
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
- }.to_not raise_error
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
- expect {
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
- }.to_not raise_error
326
+ }.should_not raise_error
327
327
  end
328
328
  end
329
329
 
@@ -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
@@ -763,7 +789,7 @@ describe "Querying" do
763
789
  it "should update the attribute without invoking validations" do
764
790
  document.key :name, String, :required => true
765
791
 
766
- expect(@doc).to receive(:valid?).never
792
+ @doc.should_receive(:valid?).never
767
793
  @doc.update_attribute('name', '').should be_truthy
768
794
 
769
795
  @doc.reload.name.should == ''
@@ -868,7 +894,7 @@ describe "Querying" do
868
894
 
869
895
  it "should insert invalid document" do
870
896
  doc = document.new
871
- expect(doc).to receive(:valid?).never
897
+ doc.should_receive(:valid?).never
872
898
  doc.save(:validate => false)
873
899
  document.count.should == 1
874
900
  end
@@ -885,15 +911,15 @@ describe "Querying" do
885
911
 
886
912
  it "should allow passing safe" do
887
913
  @document.create(:name => 'John')
888
- expect {
914
+ lambda {
889
915
  @document.new(:name => 'John').save(:safe => true)
890
- }.to raise_error(Mongo::OperationFailure)
916
+ }.should raise_error(Mongo::Error::OperationFailure)
891
917
  end
892
918
 
893
919
  it "should raise argument error if options has unsupported key" do
894
- expect {
920
+ lambda {
895
921
  @document.new.save(:foo => true)
896
- }.to raise_error(ArgumentError)
922
+ }.should raise_error(ArgumentError)
897
923
  end
898
924
  end
899
925
 
@@ -906,21 +932,21 @@ describe "Querying" do
906
932
 
907
933
  it "should allow passing safe" do
908
934
  @document.create(:name => 'John')
909
- expect {
935
+ lambda {
910
936
  @document.new(:name => 'John').save!(:safe => true)
911
- }.to raise_error(Mongo::OperationFailure)
937
+ }.should raise_error(Mongo::Error::OperationFailure)
912
938
  end
913
939
 
914
940
  it "should raise argument error if options has unsupported key" do
915
- expect {
941
+ lambda {
916
942
  @document.new.save!(:foo => true)
917
- }.to raise_error(ArgumentError)
943
+ }.should raise_error(ArgumentError)
918
944
  end
919
945
 
920
946
  it "should raise argument error if using validate as that would be pointless with save!" do
921
- expect {
947
+ lambda {
922
948
  @document.new.save!(:validate => false)
923
- }.to raise_error(ArgumentError)
949
+ }.should raise_error(ArgumentError)
924
950
  end
925
951
  end
926
952