spec/mongo/collection/view/readable_spec.rb in mongo-2.1.2 vs spec/mongo/collection/view/readable_spec.rb in mongo-2.2.0.rc0
- old
+ new
@@ -16,18 +16,52 @@
after do
authorized_collection.delete_many
end
+ shared_examples_for 'a read concern aware operation' do
+
+ context 'when a read concern is provided', if: find_command_enabled? do
+
+ let(:new_view) do
+ Mongo::Collection::View.new(new_collection, selector, options)
+ end
+
+ context 'when the read concern is valid' do
+
+ let(:new_collection) do
+ authorized_collection.with(read_concern: { level: 'local' })
+ end
+
+ it 'sends the read concern' do
+ expect { result }.to_not raise_error
+ end
+ end
+
+ context 'when the read concern is not valid' do
+
+ let(:new_collection) do
+ authorized_collection.with(read_concern: { level: 'na' })
+ end
+
+ it 'raises an exception' do
+ expect {
+ result
+ }.to raise_error(Mongo::Error::OperationFailure)
+ end
+ end
+ end
+ end
+
describe '#allow_partial_results' do
let(:new_view) do
view.allow_partial_results
end
it 'sets the flag' do
- expect(new_view.send(:flags)).to include(:partial)
+ expect(new_view.options[:allow_partial_results]).to be true
end
it 'returns a new View' do
expect(new_view).not_to be(view)
end
@@ -58,10 +92,19 @@
let(:aggregation) do
view.aggregate(pipeline)
end
+ context 'when incorporating read concern' do
+
+ let(:result) do
+ new_view.aggregate(pipeline, options).to_a
+ end
+
+ it_behaves_like 'a read concern aware operation'
+ end
+
context 'when not iterating the aggregation' do
it 'returns the aggregation object' do
expect(aggregation).to be_a(Mongo::Collection::View::Aggregation)
end
@@ -125,10 +168,19 @@
let(:map_reduce) do
view.map_reduce(map, reduce)
end
+ context 'when incorporating read concern' do
+
+ let(:result) do
+ new_view.map_reduce(map, reduce, options).to_a
+ end
+
+ it_behaves_like 'a read concern aware operation'
+ end
+
context 'when not iterating the map/reduce' do
it 'returns the map/reduce object' do
expect(map_reduce).to be_a(Mongo::Collection::View::MapReduce)
end
@@ -216,10 +268,23 @@
after do
authorized_collection.delete_many
end
+ let(:result) do
+ view.count(options)
+ end
+
+ context 'when incorporating read concern' do
+
+ let(:result) do
+ new_view.count(options)
+ end
+
+ it_behaves_like 'a read concern aware operation'
+ end
+
context 'when a selector is provided' do
let(:selector) do
{ field: 'test1' }
end
@@ -255,10 +320,19 @@
end
end
describe '#distinct' do
+ context 'when incorporating read concern' do
+
+ let(:result) do
+ new_view.distinct(:field, options)
+ end
+
+ it_behaves_like 'a read concern aware operation'
+ end
+
context 'when a selector is provided' do
let(:selector) do
{ field: 'test' }
end
@@ -496,11 +570,11 @@
let(:new_view) do
view.no_cursor_timeout
end
it 'sets the flag' do
- expect(new_view.send(:flags)).to include(:no_cursor_timeout)
+ expect(new_view.options[:no_cursor_timeout]).to be true
end
it 'returns a new View' do
expect(new_view).not_to be(view)
end
@@ -516,18 +590,26 @@
let(:new_projection) do
{ 'y' => 1 }
end
+ before do
+ authorized_collection.insert_one(y: 'value', a: 'other_value')
+ end
+
it 'sets the projection' do
new_view = view.projection(new_projection)
expect(new_view.projection).to eq(new_projection)
end
it 'returns a new View' do
expect(view.projection(new_projection)).not_to be(view)
end
+
+ it 'returns only that field on the collection' do
+ expect(view.projection(new_projection).first.keys).to match_array(['_id', 'y'])
+ end
end
context 'when projection is not specified' do
it 'returns the projection' do
@@ -634,17 +716,17 @@
end
describe '#modifiers' do
let(:options) do
- { :modifiers => { :$orderby => Mongo::Index::ASCENDING } }
+ { :modifiers => { '$orderby' => 1 } }
end
context 'when a modifiers document is specified' do
let(:new_modifiers) do
- { :modifiers => { :$orderby => Mongo::Index::DESCENDING } }
+ { '$orderby' => -1 }
end
it 'sets the new_modifiers document' do
new_view = view.modifiers(new_modifiers)
expect(new_view.modifiers).to eq(new_modifiers)
@@ -766,85 +848,9 @@
{ :sort => { 'x' => Mongo::Index::ASCENDING }}
end
it 'returns the sort' do
expect(view.sort).to eq(options[:sort])
- end
- end
-
- context 'when an option is a cursor flag' do
-
- let(:query_spec_options) do
- view.send(:query_spec)[:options]
- end
-
- context 'when allow_partial_results is set as an option' do
-
- let(:options) do
- { :allow_partial_results => true }
- end
-
- it 'sets the cursor flag' do
- expect(query_spec_options[:flags]).to eq([:partial])
- end
-
- context 'when allow_partial_results is also called as a method' do
-
- before do
- view.allow_partial_results
- end
-
- it 'sets only one cursor flag' do
- expect(query_spec_options[:flags]).to eq([:partial])
- end
- end
- end
-
- context 'when oplog_replay is set as an option' do
-
- let(:options) do
- { :oplog_replay => true }
- end
-
- it 'sets the cursor flag' do
- expect(query_spec_options[:flags]).to eq([:oplog_replay])
- end
- end
-
- context 'when no_cursor_timeout is set as an option' do
-
- let(:options) do
- { :no_cursor_timeout => true }
- end
-
- it 'sets the cursor flag' do
- expect(query_spec_options[:flags]).to eq([:no_cursor_timeout])
- end
- end
-
- context 'when cursor_type is set as an option' do
-
- context 'when :tailable is the cursor type' do
-
- let(:options) do
- { :cursor_type => :tailable }
- end
-
- it 'sets the cursor flag' do
- expect(query_spec_options[:flags]).to eq([:tailable_cursor])
- end
- end
-
- context 'when :tailable_await is the cursor type' do
-
- let(:options) do
- { :cursor_type => :tailable_await }
- end
-
- it 'sets the cursor flags' do
- expect(query_spec_options[:flags]).to eq([:await_data, :tailable_cursor])
- end
- end
end
end
end
end