Skip to content

RUBY-895 Check valid document in View#find and #projection #608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/mongo/collection/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def hash
#
# @since 2.0.0
def initialize(collection, selector = {}, options = {})
validate_doc!(selector)
@collection = collection
@selector = selector.dup
@options = options.dup
Expand Down
5 changes: 5 additions & 0 deletions lib/mongo/collection/view/readable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def no_cursor_timeout
#
# @since 2.0.0
def projection(document = nil)
validate_doc!(document) if document
configure(:projection, document)
end

Expand Down Expand Up @@ -357,6 +358,10 @@ def special_selector
def to_return
[ limit || batch_size, batch_size || limit ].min
end

def validate_doc!(doc)
raise Error::InvalidDocument.new unless doc.respond_to?(:keys)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/error/invalid_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class InvalidDocument < Error
# The error message.
#
# @since 2.0.0
MESSAGE = 'Invalid document provided'.freeze
MESSAGE = 'Invalid document provided.'.freeze

# Instantiate the new exception.
#
Expand Down
25 changes: 18 additions & 7 deletions spec/mongo/collection/view/readable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,11 @@

describe '#projection' do

context 'when projection are specified' do
let(:options) do
{ :projection => { 'x' => 1 } }
end

let(:options) do
{ :projection => { 'x' => 1 } }
end
context 'when projection are specified' do

let(:new_projection) do
{ 'y' => 1 }
Expand All @@ -456,14 +456,25 @@
end
end

context 'when projection are not specified' do

let(:options) { { :projection => { 'x' => 1 } } }
context 'when projection is not specified' do

it 'returns the projection' do
expect(view.projection).to eq(options[:projection])
end
end

context 'when projection is not a document' do

let(:new_projection) do
'y'
end

it 'raises an error' do
expect do
view.projection(new_projection)
end.to raise_error(Mongo::Error::InvalidDocument)
end
end
end

describe '#read' do
Expand Down
13 changes: 13 additions & 0 deletions spec/mongo/collection/view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,19 @@
it 'dups the options' do
expect(view.options).not_to be(options)
end

context 'when the selector is not a valid document' do

let(:selector) do
'y'
end

it 'raises an error' do
expect do
view
end.to raise_error(Mongo::Error::InvalidDocument)
end
end
end

describe '#inspect' do
Expand Down