File tree Expand file tree Collapse file tree 4 files changed +44
-8
lines changed Expand file tree Collapse file tree 4 files changed +44
-8
lines changed Original file line number Diff line number Diff line change @@ -115,7 +115,8 @@ def execute
115
115
# )
116
116
#
117
117
# @param [ Mongo::Collection ] collection The collection.
118
- # @param [ Array<Hash, BSON::Document> ] requests The requests, cannot be empty.
118
+ # @param [ Enumerable<Hash, BSON::Document> ] requests The requests,
119
+ # cannot be empty.
119
120
# @param [ Hash, BSON::Document ] options The options.
120
121
#
121
122
# @since 2.1.0
@@ -330,10 +331,9 @@ def can_hint?(connection)
330
331
# ArgumentError ]
331
332
# if the document is invalid.
332
333
def validate_requests!
333
- if @requests . empty?
334
- raise ArgumentError , "Bulk write requests cannot be empty"
335
- end
334
+ requests_empty = true
336
335
@requests . each do |req |
336
+ requests_empty = false
337
337
if op = req . keys . first
338
338
if [ :update_one , :update_many ] . include? ( op )
339
339
if doc = maybe_first ( req . dig ( op , :update ) )
@@ -359,6 +359,8 @@ def validate_requests!
359
359
end
360
360
end
361
361
end
362
+ end . tap do
363
+ raise ArgumentError , "Bulk write requests cannot be empty" if requests_empty
362
364
end
363
365
end
364
366
Original file line number Diff line number Diff line change @@ -783,7 +783,7 @@ def insert_one(document, opts = {})
783
783
# @example Insert documents into the collection.
784
784
# collection.insert_many([{ name: 'test' }])
785
785
#
786
- # @param [ Array <Hash> ] documents The documents to insert.
786
+ # @param [ Enumerable <Hash> ] documents The documents to insert.
787
787
# @param [ Hash ] options The insert options.
788
788
#
789
789
# @option options [ true | false ] :bypass_document_validation Whether or
@@ -811,7 +811,7 @@ def insert_many(documents, options = {})
811
811
# @example Execute a bulk write.
812
812
# collection.bulk_write(operations, options)
813
813
#
814
- # @param [ Array <Hash> ] requests The bulk write requests.
814
+ # @param [ Enumerable <Hash> ] requests The bulk write requests.
815
815
# @param [ Hash ] options The options.
816
816
#
817
817
# @option options [ true | false ] :ordered Whether the operations
Original file line number Diff line number Diff line change @@ -98,8 +98,6 @@ def find_one_and_delete(opts = {})
98
98
end . first &.fetch ( 'value' , nil )
99
99
end
100
100
101
- # db['users'].bulk_write([{insert_one: {x: 1}}, {insert_one: {x: 2}}])
102
-
103
101
# Finds a single document and replaces it.
104
102
#
105
103
# @example Find a document and replace it, returning the original.
Original file line number Diff line number Diff line change 362
362
expect ( result . inserted_ids . size ) . to eq ( 2 )
363
363
end
364
364
365
+ context 'when an enumerable is used instead of an array' do
366
+
367
+ context 'when the enumerable is not empty' do
368
+
369
+ let ( :source_data ) do
370
+ [ { name : 'test1' } , { name : 'test2' } ]
371
+ end
372
+
373
+ let ( :result ) do
374
+ authorized_collection . insert_many ( source_data . lazy )
375
+ end
376
+
377
+ it 'should accepts them without raising an error' do
378
+ expect { result } . to_not raise_error
379
+ expect ( result . inserted_count ) . to eq ( source_data . size )
380
+ end
381
+ end
382
+
383
+ context 'when the enumerable is empty' do
384
+
385
+ let ( :source_data ) do
386
+ [ ]
387
+ end
388
+
389
+ let ( :result ) do
390
+ authorized_collection . insert_many ( source_data . lazy )
391
+ end
392
+
393
+ it 'should raise ArgumentError' do
394
+ expect do
395
+ result
396
+ end . to raise_error ( ArgumentError , /Bulk write requests cannot be empty/ )
397
+ end
398
+ end
399
+ end
400
+
365
401
context 'when a session is provided' do
366
402
367
403
let ( :session ) do
You can’t perform that action at this time.
0 commit comments