File tree Expand file tree Collapse file tree 5 files changed +38
-8
lines changed Expand file tree Collapse file tree 5 files changed +38
-8
lines changed Original file line number Diff line number Diff line change @@ -125,6 +125,7 @@ def hash
125
125
#
126
126
# @since 2.0.0
127
127
def initialize ( collection , selector = { } , options = { } )
128
+ validate_doc! ( selector )
128
129
@collection = collection
129
130
@selector = selector . dup
130
131
@options = options . dup
Original file line number Diff line number Diff line change @@ -232,6 +232,7 @@ def no_cursor_timeout
232
232
#
233
233
# @since 2.0.0
234
234
def projection ( document = nil )
235
+ validate_doc! ( document ) if document
235
236
configure ( :projection , document )
236
237
end
237
238
@@ -357,6 +358,10 @@ def special_selector
357
358
def to_return
358
359
[ limit || batch_size , batch_size || limit ] . min
359
360
end
361
+
362
+ def validate_doc! ( doc )
363
+ raise Error ::InvalidDocument . new unless doc . respond_to? ( :keys )
364
+ end
360
365
end
361
366
end
362
367
end
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ class InvalidDocument < Error
23
23
# The error message.
24
24
#
25
25
# @since 2.0.0
26
- MESSAGE = 'Invalid document provided' . freeze
26
+ MESSAGE = 'Invalid document provided. ' . freeze
27
27
28
28
# Instantiate the new exception.
29
29
#
Original file line number Diff line number Diff line change 436
436
437
437
describe '#projection' do
438
438
439
- context 'when projection are specified' do
439
+ let ( :options ) do
440
+ { :projection => { 'x' => 1 } }
441
+ end
440
442
441
- let ( :options ) do
442
- { :projection => { 'x' => 1 } }
443
- end
443
+ context 'when projection are specified' do
444
444
445
445
let ( :new_projection ) do
446
446
{ 'y' => 1 }
456
456
end
457
457
end
458
458
459
- context 'when projection are not specified' do
460
-
461
- let ( :options ) { { :projection => { 'x' => 1 } } }
459
+ context 'when projection is not specified' do
462
460
463
461
it 'returns the projection' do
464
462
expect ( view . projection ) . to eq ( options [ :projection ] )
465
463
end
466
464
end
465
+
466
+ context 'when projection is not a document' do
467
+
468
+ let ( :new_projection ) do
469
+ 'y'
470
+ end
471
+
472
+ it 'raises an error' do
473
+ expect do
474
+ view . projection ( new_projection )
475
+ end . to raise_error ( Mongo ::Error ::InvalidDocument )
476
+ end
477
+ end
467
478
end
468
479
469
480
describe '#read' do
Original file line number Diff line number Diff line change 496
496
it 'dups the options' do
497
497
expect ( view . options ) . not_to be ( options )
498
498
end
499
+
500
+ context 'when the selector is not a valid document' do
501
+
502
+ let ( :selector ) do
503
+ 'y'
504
+ end
505
+
506
+ it 'raises an error' do
507
+ expect do
508
+ view
509
+ end . to raise_error ( Mongo ::Error ::InvalidDocument )
510
+ end
511
+ end
499
512
end
500
513
501
514
describe '#inspect' do
You can’t perform that action at this time.
0 commit comments