spec/mongo/collection_spec.rb in mongo-2.13.0.beta1 vs spec/mongo/collection_spec.rb in mongo-2.13.0.rc1

- old
+ new

@@ -939,10 +939,34 @@ it_behaves_like 'an operation using a session' it_behaves_like 'a failed operation using a session' end end + + context 'when collation has a strength' do + min_server_fcv '3.4' + + let(:band_collection) do + described_class.new(database, :bands) + end + + before do + band_collection.delete_many + band_collection.insert_many([{ name: "Depeche Mode" }, { name: "New Order" }]) + end + + let(:options) do + { collation: { locale: 'en_US', strength: 2 } } + end + let(:band_result) do + band_collection.find({ name: 'DEPECHE MODE' }, options) + end + + it 'finds Capitalize from UPPER CASE' do + expect(band_result.count_documents).to eq(1) + end + end end describe '#drop' do let(:database) do @@ -1599,10 +1623,44 @@ it 'does not raise an exception' do expect(result.inserted_count).to be(0) end end + + context 'when various options passed in' do + # w: 2 requires a replica set + require_topology :replica_set + + # https://jira.mongodb.org/browse/RUBY-2306 + min_server_fcv '3.6' + + let(:session) do + authorized_client.start_session + end + + let(:events) do + subscriber.command_started_events('insert') + end + + let(:collection) do + authorized_collection.with(write_concern: {w: 2}) + end + + let!(:command) do + Utils.get_command_event(authorized_client, 'insert') do |client| + collection.insert_many([{ name: 'test1' }, { name: 'test2' }], session: session, + write_concern: {w: 1}, bypass_document_validation: true) + end.command + end + + it 'inserts many successfully with correct options sent to server' do + expect(events.length).to eq(1) + expect(command[:writeConcern]).to_not be_nil + expect(command[:writeConcern][:w]).to eq(1) + expect(command[:bypassDocumentValidation]).to be(true) + end + end end describe '#insert_one' do describe 'updating cluster time' do @@ -1681,10 +1739,41 @@ end it_behaves_like 'an implicit session with an unacknowledged write' end + context 'when various options passed in' do + # https://jira.mongodb.org/browse/RUBY-2306 + min_server_fcv '3.6' + + let(:session) do + authorized_client.start_session + end + + let(:events) do + subscriber.command_started_events('insert') + end + + let(:collection) do + authorized_collection.with(write_concern: {w: 3}) + end + + let!(:command) do + Utils.get_command_event(authorized_client, 'insert') do |client| + collection.insert_one({name: 'test1'}, session: session, write_concern: {w: 1}, + bypass_document_validation: true) + end.command + end + + it 'inserts one successfully with correct options sent to server' do + expect(events.length).to eq(1) + expect(command[:writeConcern]).to_not be_nil + expect(command[:writeConcern][:w]).to eq(1) + expect(command[:bypassDocumentValidation]).to be(true) + end + end + context 'when the document contains invalid keys' do let(:doc) do { 'testing.test' => 'value' } end @@ -1789,10 +1878,56 @@ end end end end + describe '#bulk_write' do + + context 'when various options passed in' do + min_server_fcv '3.2' + require_topology :replica_set + + # https://jira.mongodb.org/browse/RUBY-2306 + min_server_fcv '3.6' + + let(:requests) do + [ + { insert_one: { name: "anne" }}, + { insert_one: { name: "bob" }}, + { insert_one: { name: "charlie" }} + ] + end + + let(:session) do + authorized_client.start_session + end + + let!(:command) do + Utils.get_command_event(authorized_client, 'insert') do |client| + collection.bulk_write(requests, session: session, write_concern: {w: 1}, + bypass_document_validation: true) + end.command + end + + let(:events) do + subscriber.command_started_events('insert') + end + + let(:collection) do + authorized_collection.with(write_concern: {w: 2}) + end + + it 'inserts successfully with correct options sent to server' do + expect(collection.count).to eq(3) + expect(events.length).to eq(1) + expect(command[:writeConcern]).to_not be_nil + expect(command[:writeConcern][:w]).to eq(1) + expect(command[:bypassDocumentValidation]).to eq(true) + end + end + end + describe '#inspect' do it 'includes the object id' do expect(authorized_collection.inspect).to include(authorized_collection.object_id.to_s) end @@ -1998,10 +2133,43 @@ end end end describe '#count_documents' do + + before do + authorized_collection.delete_many + end + + context 'no argument provided' do + + context 'when collection is empty' do + it 'returns 0 matching documents' do + expect(authorized_collection.count_documents).to eq(0) + end + end + + context 'when collection is not empty' do + + let(:documents) do + documents = [] + 1.upto(10) do |index| + documents << { key: 'a', _id: "in#{index}" } + end + documents + end + + before do + authorized_collection.insert_many(documents) + end + + it 'returns 10 matching documents' do + expect(authorized_collection.count_documents).to eq(10) + end + end + end + context 'when transactions are enabled' do require_wired_tiger require_transaction_support before do @@ -2476,10 +2644,52 @@ it 'does not apply the collation' do expect(result.written_count).to eq(0) expect(authorized_collection.find(name: 'bang').count).to eq(1) end end + + context 'when various options passed in' do + # w: 2 requires a replica set + require_topology :replica_set + + # https://jira.mongodb.org/browse/RUBY-2306 + min_server_fcv '3.6' + + before do + authorized_collection.insert_many([{ name: 'test1' }, { name: 'test2' }]) + end + + let(:selector) do + {name: 'test2'} + end + + let(:session) do + authorized_client.start_session + end + + let(:events) do + subscriber.command_started_events('delete') + end + + let(:collection) do + authorized_collection.with(write_concern: {w: 2}) + end + + let!(:command) do + Utils.get_command_event(authorized_client, 'delete') do |client| + collection.delete_one(selector, session: session, write_concern: {w: 1}, + bypass_document_validation: true) + end.command + end + + it 'deletes one successfully with correct options sent to server' do + expect(events.length).to eq(1) + expect(command[:writeConcern]).to_not be_nil + expect(command[:writeConcern][:w]).to eq(1) + expect(command[:bypassDocumentValidation]).to eq(true) + end + end end describe '#delete_many' do before do @@ -2666,10 +2876,52 @@ it 'does not apply the collation' do expect(result.written_count).to eq(0) expect(authorized_collection.find(name: 'bang').count).to eq(2) end end + + context 'when various options passed in' do + # w: 2 requires a replica set + require_topology :replica_set + + # https://jira.mongodb.org/browse/RUBY-2306 + min_server_fcv '3.6' + + before do + collection.insert_many([{ name: 'test1' }, { name: 'test2' }, { name: 'test3'}]) + end + + let(:selector) do + {name: 'test1'} + end + + let(:session) do + authorized_client.start_session + end + + let(:events) do + subscriber.command_started_events('delete') + end + + let(:collection) do + authorized_collection.with(write_concern: {w: 1}) + end + + let!(:command) do + Utils.get_command_event(authorized_client, 'delete') do |client| + collection.delete_many(selector, session: session, write_concern: {w: 2}, + bypass_document_validation: true) + end.command + end + + it 'deletes many successfully with correct options sent to server' do + expect(events.length).to eq(1) + expect(command[:writeConcern]).to_not be_nil + expect(command[:writeConcern][:w]).to eq(2) + expect(command[:bypassDocumentValidation]).to be(true) + end + end end describe '#parallel_scan' do max_server_version '4.0' require_topology :single, :replica_set @@ -3172,10 +3424,55 @@ collection_with_unacknowledged_write_concern.replace_one({ a: 1 }, { x: 5 }) end it_behaves_like 'an implicit session with an unacknowledged write' end + + context 'when various options passed in' do + # w: 2 requires a replica set + require_topology :replica_set + + # https://jira.mongodb.org/browse/RUBY-2306 + min_server_fcv '3.6' + + before do + authorized_collection.insert_one({field: 'test1'}) + end + + let(:session) do + authorized_client.start_session + end + + let(:events) do + subscriber.command_started_events('update') + end + + let(:collection) do + authorized_collection.with(write_concern: {w: 3}) + end + + let(:updated) do + collection.find(field: 'test4').first + end + + let!(:command) do + Utils.get_command_event(authorized_client, 'update') do |client| + collection.replace_one(selector, { field: 'test4'}, + session: session, :return_document => :after, write_concern: {w: 2}, + upsert: true, bypass_document_validation: true) + end.command + end + + it 'replaced one successfully with correct options sent to server' do + expect(updated[:field]).to eq('test4') + expect(events.length).to eq(1) + expect(command[:writeConcern]).to_not be_nil + expect(command[:writeConcern][:w]).to eq(2) + expect(command[:bypassDocumentValidation]).to be(true) + expect(command[:updates][0][:upsert]).to be(true) + end + end end describe '#update_many' do let(:selector) do @@ -3594,10 +3891,49 @@ collection_with_unacknowledged_write_concern.update_many({a: 1}, {'$set' => {x: 1}}) end it_behaves_like 'an implicit session with an unacknowledged write' end + + context 'when various options passed in' do + # w: 2 requires a replica set + require_topology :replica_set + + # https://jira.mongodb.org/browse/RUBY-2306 + min_server_fcv '3.6' + + before do + collection.insert_many([{ field: 'test' }, { field: 'test2' }], session: session) + end + + let(:session) do + authorized_client.start_session + end + + let(:collection) do + authorized_collection.with(write_concern: {w: 1}) + end + + let(:events) do + subscriber.command_started_events('update') + end + + let!(:command) do + Utils.get_command_event(authorized_client, 'update') do |client| + collection.update_many(selector, {'$set'=> { field: 'testing' }}, session: session, + write_concern: {w: 2}, bypass_document_validation: true, upsert: true) + end.command + end + + it 'updates many successfully with correct options sent to server' do + expect(events.length).to eq(1) + expect(collection.options[:write_concern]).to eq(w: 1) + expect(command[:writeConcern][:w]).to eq(2) + expect(command[:bypassDocumentValidation]).to be(true) + expect(command[:updates][0][:upsert]).to be(true) + end + end end describe '#update_one' do let(:selector) do @@ -4012,10 +4348,51 @@ collection_with_unacknowledged_write_concern.update_one({ a: 1 }, { '$set' => { x: 1 }}) end it_behaves_like 'an implicit session with an unacknowledged write' end + + context 'when various options passed in' do + # w: 2 requires a replica set + require_topology :replica_set + + # https://jira.mongodb.org/browse/RUBY-2306 + min_server_fcv '3.6' + + before do + collection.insert_many([{ field: 'test1' }, { field: 'test2' }], session: session) + end + + let(:session) do + authorized_client.start_session + end + + let(:collection) do + authorized_collection.with(write_concern: {w: 1}) + end + + let(:events) do + subscriber.command_started_events('update') + end + + let!(:command) do + Utils.get_command_event(authorized_client, 'update') do |client| + collection.update_one(selector, { '$set'=> { field: 'testing' } }, session: session, + write_concern: {w: 2}, bypass_document_validation: true, :return_document => :after, + upsert: true) + end.command + end + + it 'updates one successfully with correct options sent to server' do + expect(events.length).to eq(1) + expect(command[:writeConcern]).to_not be_nil + expect(command[:writeConcern][:w]).to eq(2) + expect(collection.options[:write_concern]).to eq(w:1) + expect(command[:bypassDocumentValidation]).to be(true) + expect(command[:updates][0][:upsert]).to be(true) + end + end end describe '#find_one_and_delete' do before do @@ -4229,10 +4606,50 @@ it 'does not apply the collation' do expect(result).to be_nil end end + + context 'when various options passed in' do + # w: 2 requires a replica set + require_topology :replica_set + + # https://jira.mongodb.org/browse/RUBY-2306 + min_server_fcv '3.6' + + before do + authorized_collection.delete_many + authorized_collection.insert_many([{ name: 'test1' }, { name: 'test2' }]) + end + + let(:collection) do + authorized_collection.with(write_concern: {w: 2}) + end + + let(:session) do + authorized_client.start_session + end + + let!(:command) do + Utils.get_command_event(authorized_client, 'findAndModify') do |client| + collection.find_one_and_delete(selector, session: session, write_concern: {w: 2}, + bypass_document_validation: true, max_time_ms: 300) + end.command + end + + let(:events) do + subscriber.command_started_events('findAndModify') + end + + it 'finds and deletes successfully with correct options sent to server' do + expect(events.length).to eq(1) + expect(command[:writeConcern]).to_not be_nil + expect(command[:writeConcern][:w]).to eq(2) + expect(command[:bypassDocumentValidation]).to eq(true) + expect(command[:maxTimeMS]).to eq(300) + end + end end describe '#find_one_and_update' do let(:selector) do @@ -4650,10 +5067,55 @@ }.to raise_exception(Mongo::Error::UnsupportedArrayFilters) end end end end + + context 'when various options passed in' do + # w: 2 requires a replica set + require_topology :replica_set + + # https://jira.mongodb.org/browse/RUBY-2306 + min_server_fcv '3.6' + + let(:session) do + authorized_client.start_session + end + + let(:events) do + subscriber.command_started_events('findAndModify') + end + + let(:collection) do + authorized_collection.with(write_concern: {w: 2}) + end + + let(:selector) do + {field: 'test1'} + end + + before do + collection.insert_one({field: 'test1'}, session: session) + end + + let!(:command) do + Utils.get_command_event(authorized_client, 'findAndModify') do |client| + collection.find_one_and_update(selector, { '$set' => {field: 'testing'}}, + :return_document => :after, write_concern: {w: 1}, upsert: true, + bypass_document_validation: true, max_time_ms: 100, session: session) + end.command + end + + it 'find and updates successfully with correct options sent to server' do + expect(events.length).to eq(1) + expect(command[:writeConcern]).to_not be_nil + expect(command[:writeConcern][:w]).to eq(1) + expect(command[:upsert]).to eq(true) + expect(command[:bypassDocumentValidation]).to be(true) + expect(command[:maxTimeMS]).to eq(100) + end + end end describe '#find_one_and_replace' do before do @@ -4961,9 +5423,47 @@ authorized_collection.insert_one(name: 'bang') end it 'does not apply the collation' do expect(result).to be_nil + end + end + + context 'when various options passed in' do + # https://jira.mongodb.org/browse/RUBY-2306 + min_server_fcv '3.6' + + before do + authorized_collection.insert_one({field: 'test1'}) + end + + let(:session) do + authorized_client.start_session + end + + let(:events) do + subscriber.command_started_events('findAndModify') + end + + let(:collection) do + authorized_collection.with(write_concern: { w: 2 }) + end + + let!(:command) do + Utils.get_command_event(authorized_client, 'findAndModify') do |client| + collection.find_one_and_replace(selector, { '$set' => {field: 'test5'}}, + :return_document => :after, write_concern: {w: 1}, session: session, + upsert: true, bypass_document_validation: false, max_time_ms: 200) + end.command + end + + it 'find and replaces successfully with correct options sent to server' do + expect(events.length).to eq(1) + expect(command[:writeConcern]).to_not be_nil + expect(command[:writeConcern][:w]).to eq(1) + expect(command[:upsert]).to be(true) + expect(command[:bypassDocumentValidation]).to be_nil + expect(command[:maxTimeMS]).to eq(200) end end end describe '#watch' do