spec/mongo/collection_spec.rb in mongo-2.6.4 vs spec/mongo/collection_spec.rb in mongo-2.7.0.rc0

- old
+ new

@@ -1,11 +1,11 @@ require 'spec_helper' describe Mongo::Collection do - after do - authorized_collection.delete_many + before do + authorized_collection.drop end let(:collection_invalid_write_concern) do authorized_collection.client.with(write: INVALID_WRITE_CONCERN)[authorized_collection.name] end @@ -99,11 +99,11 @@ end describe '#with' do let(:client) do - Mongo::Client.new(SpecConfig.instance.addresses, TEST_OPTIONS) + new_local_client(SpecConfig.instance.addresses, SpecConfig.instance.test_options) end let(:database) do Mongo::Database.new(client, :test) end @@ -131,22 +131,22 @@ end context 'when the client has a server selection timeout setting' do let(:client) do - Mongo::Client.new(SpecConfig.instance.addresses, TEST_OPTIONS.merge(server_selection_timeout: 2)) + new_local_client(SpecConfig.instance.addresses, SpecConfig.instance.test_options.merge(server_selection_timeout: 2)) end it 'passes the the server_selection_timeout to the cluster' do expect(client.cluster.options[:server_selection_timeout]).to eq(client.options[:server_selection_timeout]) end end context 'when the client has a read preference set' do let(:client) do - Mongo::Client.new(SpecConfig.instance.addresses, TEST_OPTIONS.merge(read: { mode: :primary_preferred })) + new_local_client(SpecConfig.instance.addresses, SpecConfig.instance.test_options.merge(read: { mode: :primary_preferred })) end it 'sets the new read options on the new collection' do expect(new_collection.read_preference).to eq(new_options[:read]) expect(new_collection.read_preference).not_to eq(client.read_preference) @@ -154,11 +154,11 @@ end context 'when the client has a read preference and server selection timeout set' do let(:client) do - Mongo::Client.new(SpecConfig.instance.addresses, TEST_OPTIONS.merge(read: { mode: :primary_preferred }, server_selection_timeout: 2)) + new_local_client(SpecConfig.instance.addresses, SpecConfig.instance.test_options.merge(read: { mode: :primary_preferred }, server_selection_timeout: 2)) end it 'sets the new read options on the new collection' do expect(new_collection.read_preference).to eq(new_options[:read]) end @@ -184,11 +184,11 @@ end context 'when the client has a write concern set' do let(:client) do - Mongo::Client.new(SpecConfig.instance.addresses, TEST_OPTIONS.merge(write: INVALID_WRITE_CONCERN)) + new_local_client(SpecConfig.instance.addresses, SpecConfig.instance.test_options.merge(write: INVALID_WRITE_CONCERN)) end it 'sets the new write options on the new collection' do expect(new_collection.write_concern.options).to eq(Mongo::WriteConcern.get(new_options[:write]).options) end @@ -217,22 +217,22 @@ end context 'when the client has a server selection timeout setting' do let(:client) do - Mongo::Client.new(SpecConfig.instance.addresses, TEST_OPTIONS.merge(server_selection_timeout: 2)) + new_local_client(SpecConfig.instance.addresses, SpecConfig.instance.test_options.merge(server_selection_timeout: 2)) end it 'passes the server_selection_timeout setting to the cluster' do expect(client.cluster.options[:server_selection_timeout]).to eq(client.options[:server_selection_timeout]) end end context 'when the client has a read preference set' do let(:client) do - Mongo::Client.new(SpecConfig.instance.addresses, TEST_OPTIONS.merge(read: { mode: :primary_preferred })) + new_local_client(SpecConfig.instance.addresses, SpecConfig.instance.test_options.merge(read: { mode: :primary_preferred })) end it 'sets the new read options on the new collection' do expect(new_collection.read_preference).to eq(new_options[:read]) expect(new_collection.read_preference).not_to be(client.read_preference) @@ -355,17 +355,14 @@ let(:collection) do described_class.new(database, :specs, :capped => true, :size => 1024) end before do + authorized_client[:specs].drop collection.create end - after do - collection.drop - end - it 'returns true' do expect(collection).to be_capped end end @@ -374,24 +371,24 @@ let(:collection) do described_class.new(database, :specs) end before do + authorized_client[:specs].drop collection.create end - after do - collection.drop - end - it 'returns false' do expect(collection).to_not be_capped end end end describe '#create' do + before do + authorized_client[:specs].drop + end let(:database) do authorized_client.database end @@ -403,14 +400,10 @@ let!(:response) do collection.create end - after do - collection.drop - end - it 'executes the command' do expect(response).to be_successful end it 'creates the collection in the database' do @@ -430,14 +423,10 @@ let(:options) do { :capped => true, :size => 1024 } end - after do - collection.drop - end - it 'executes the command' do expect(response).to be_successful end it 'sets the collection as capped' do @@ -462,14 +451,10 @@ let(:collection_info) do database.list_collections.find { |i| i['name'] == 'specs' } end - after do - collection.drop - end - it 'executes the command' do expect(response).to be_successful end it 'sets the collection with validators' do @@ -510,11 +495,11 @@ end end context 'when the collection has a write concern' do - after do + before do database[:specs].drop end let(:options) do { @@ -557,11 +542,11 @@ let(:collection_info) do database.list_collections.find { |i| i['name'] == 'specs' } end - after do + before do collection.drop end context 'when the server supports collations', if: collation_enabled? do @@ -642,11 +627,11 @@ let(:failed_operation) do authorized_client[:specs, invalid: true].create(session: session) end - after do + before do collection.drop end it_behaves_like 'an operation using a session' it_behaves_like 'a failed operation using a session' @@ -665,11 +650,14 @@ end context 'when the collection exists' do before do + authorized_client[:specs].drop collection.create + # wait for the collection to be created + sleep 0.4 end context 'when a session is provided' do let(:operation) do @@ -686,14 +674,10 @@ let(:client) do authorized_client end - after do - collection.drop - end - it_behaves_like 'an operation using a session' # Due to how rspec interprets nested `if: condition` guards, we can't use one to skip this # test in the case that the server will override the write concern (preventing the expected # failure), so we're forced to use a traditional conditional to avoid defining the test. @@ -732,14 +716,10 @@ let(:collection_with_write_options) do collection.with(write_options) end - after do - collection.drop - end - context 'when the server supports write concern on the drop command', if: collation_enabled? && can_set_write_concern? do it 'applies the write concern' do expect{ collection_with_write_options.drop @@ -755,10 +735,16 @@ end end end context 'when the collection does not exist', if: can_set_write_concern? do + before do + begin + collection.drop + rescue Mongo::Error::OperationFailure + end + end it 'returns false' do expect(collection.drop).to be(false) end end @@ -897,11 +883,13 @@ it_behaves_like 'an operation using a session' it_behaves_like 'a failed operation using a session' end - context 'session id', if: test_sessions? do + context 'session id' do + min_server_version '3.6' + require_topology :replica_set, :sharded let(:options) do { session: session } end @@ -1171,22 +1159,25 @@ let(:custom_client) do authorized_client.with(id_generator: generator) end let(:custom_collection) do - custom_client[TEST_COLL] + custom_client['custom_id_generator_test_collection'] end before do + custom_collection.delete_many custom_collection.insert_many([{ name: 'testing' }]) + expect(custom_collection.count).to eq(1) end after do custom_client.close end it 'inserts with the custom id' do + expect(custom_collection.count).to eq(1) expect(custom_collection.find.first[:_id]).to eq(1) end end context 'when the inserts fail' do @@ -1213,11 +1204,12 @@ authorized_collection.insert_many(documents) }.to raise_error(Mongo::Error::MaxBSONSize) end end - context 'when the documents are sent with OP_MSG', if: op_msg_enabled? do + context 'when the documents are sent with OP_MSG' do + min_server_version '3.6' let(:client) do subscribed_client end @@ -1245,10 +1237,11 @@ context 'when collection has a validator' do min_server_version '3.2' around(:each) do |spec| + authorized_client[:validating].drop authorized_client[:validating, :validator => { :a => { '$exists' => true } }].tap do |c| c.create end spec.run @@ -1435,10 +1428,11 @@ let(:custom_collection) do custom_client[TEST_COLL] end before do + custom_collection.delete_many custom_collection.insert_one({ name: 'testing' }) end after do custom_client.close @@ -1527,14 +1521,10 @@ before do authorized_collection.indexes.create_one(index_spec, unique: true) end - after do - authorized_collection.indexes.drop_one('name_1') - end - it 'returns a list of indexes' do expect(index_names).to include(*'name_1', '_id_') end context 'when a session is provided' do @@ -3589,11 +3579,12 @@ end end end end - context 'when the documents are sent with OP_MSG', if: op_msg_enabled? do + context 'when the documents are sent with OP_MSG' do + min_server_version '3.6' let(:client) do subscribed_client end @@ -4062,10 +4053,11 @@ context 'when collection has a validator' do min_server_version '3.2' around(:each) do |spec| + authorized_client[:validating].drop authorized_client[:validating, :validator => { :a => { '$exists' => true } }].tap do |c| c.create end spec.run @@ -4450,9 +4442,10 @@ context 'when collection has a validator' do min_server_version '3.2' around(:each) do |spec| + authorized_client[:validating].drop authorized_client[:validating, :validator => { :a => { '$exists' => true } }].tap do |c| c.create end spec.run