spec/mongo/collection/view/readable_spec.rb in mongo-2.4.0 vs spec/mongo/collection/view/readable_spec.rb in mongo-2.4.1

- old
+ new

@@ -307,10 +307,44 @@ it 'takes a read preference option' do expect(view.count(read: { mode: :secondary })).to eq(10) end + context 'when a read preference is set on the view', unless: sharded? do + + let(:client) do + # Set a timeout otherwise, the test will hang for 30 seconds. + authorized_client.with(server_selection_timeout: 1) + end + + let(:collection) do + client[authorized_collection.name] + end + + before do + allow(client.cluster).to receive(:single?).and_return(false) + end + + let(:view) do + Mongo::Collection::View.new(collection, selector, options) + end + + let(:view_with_read_pref) do + view.read(:mode => :secondary, :tag_sets => [{ 'non' => 'existent' }]) + end + + let(:result) do + view_with_read_pref.count + end + + it 'uses the read preference setting on the view' do + expect { + result + }.to raise_exception(Mongo::Error::NoServerAvailable) + end + end + context 'when the collection has a read preference set' do after do client.close end @@ -342,10 +376,25 @@ it 'uses the read preference passed to the method' do expect(result).to eq(10) end end + context 'when a read preference is set on the view' do + + let(:view_with_read_pref) do + view.read(mode: :primary) + end + + let(:result) do + view_with_read_pref.count + end + + it 'uses the read preference of the view' do + expect(result).to eq(10) + end + end + context 'when no read preference argument is provided', unless: sharded? do before do allow(view.collection.client.cluster).to receive(:single?).and_return(false) end @@ -392,10 +441,25 @@ expect { result }.to raise_exception(Mongo::Error::NoServerAvailable) end end + + context 'when a read preference is set on the view' do + + let(:view_with_read_pref) do + view.read(:mode => :primary) + end + + let(:result) do + view_with_read_pref.count + end + + it 'uses the read preference passed to the method' do + expect(result).to eq(10) + end + end end it 'takes a max_time_ms option', if: write_command_enabled? do expect { view.count(max_time_ms: 0.1) @@ -411,10 +475,58 @@ let(:selector) do { name: 'BANG' } end let(:result) do + view.count + end + + before do + authorized_collection.insert_one(name: 'bang') + end + + let(:options) do + { collation: { locale: 'en_US', strength: 2 } } + end + + context 'when the server selected supports collations', if: collation_enabled? do + + it 'applies the collation to the count' do + expect(result).to eq(1) + end + end + + context 'when the server selected does not support collations', unless: collation_enabled? do + + it 'raises an exception' do + expect { + result + }.to raise_exception(Mongo::Error::UnsupportedCollation) + end + + context 'when a String key is used' do + + let(:options) do + { 'collation' => { locale: 'en_US', strength: 2 } } + end + + it 'raises an exception' do + expect { + result + }.to raise_exception(Mongo::Error::UnsupportedCollation) + end + end + end + end + + context 'when a collation is specified in the method options' do + + let(:selector) do + { name: 'BANG' } + end + + let(:result) do view.count(count_options) end before do authorized_collection.insert_one(name: 'bang') @@ -567,10 +679,44 @@ expect(distinct).to be_empty end end end + context 'when a read preference is set on the view', unless: sharded? do + + let(:client) do + # Set a timeout otherwise, the test will hang for 30 seconds. + authorized_client.with(server_selection_timeout: 1) + end + + let(:collection) do + client[authorized_collection.name] + end + + before do + allow(client.cluster).to receive(:single?).and_return(false) + end + + let(:view) do + Mongo::Collection::View.new(collection, selector, options) + end + + let(:view_with_read_pref) do + view.read(:mode => :secondary, :tag_sets => [{ 'non' => 'existent' }]) + end + + let(:result) do + view_with_read_pref.distinct(:field) + end + + it 'uses the read preference setting on the view' do + expect { + result + }.to raise_exception(Mongo::Error::NoServerAvailable) + end + end + context 'when the collection has a read preference set' do let(:documents) do (1..3).map{ |i| { field: "test#{i}" }} end @@ -665,10 +811,25 @@ expect { distinct }.to raise_exception(Mongo::Error::NoServerAvailable) end end + + context 'when a read preference is set on the view' do + + let(:view_with_read_pref) do + view.read(:mode => :secondary, :tag_sets => [{ 'non' => 'existent' }]) + end + + let(:distinct) do + view_with_read_pref.distinct(:field, read: { mode: :primary }) + end + + it 'uses the read preference passed to the method' do + expect(distinct.sort).to eq([ 'test1', 'test2', 'test3' ]) + end + end end context 'when a max_time_ms is specified', if: write_command_enabled? do let(:documents) do @@ -695,10 +856,55 @@ it 'returns an empty array' do expect(view.distinct(:nofieldexists)).to be_empty end end - context 'when a collation is specified' do + context 'when a collation is specified on the view' do + + let(:result) do + view.distinct(:name) + end + + before do + authorized_collection.insert_one(name: 'bang') + authorized_collection.insert_one(name: 'BANG') + end + + let(:options) do + { collation: { locale: 'en_US', strength: 2 } } + end + + context 'when the server selected supports collations', if: collation_enabled? do + + it 'applies the collation to the distinct' do + expect(result).to eq(['bang']) + end + end + + context 'when the server selected does not support collations', unless: collation_enabled? do + + it 'raises an exception' do + expect { + result + }.to raise_exception(Mongo::Error::UnsupportedCollation) + end + + context 'when a String key is used' do + + let(:options) do + { 'collation' => { locale: 'en_US', strength: 2 } } + end + + it 'raises an exception' do + expect { + result + }.to raise_exception(Mongo::Error::UnsupportedCollation) + end + end + end + end + + context 'when a collation is specified in the method options' do let(:result) do view.distinct(:name, distinct_options) end