spec/mongo/protocol/msg_spec.rb in mongo-2.11.6 vs spec/mongo/protocol/msg_spec.rb in mongo-2.12.0.rc0

- old
+ new

@@ -4,25 +4,25 @@ describe Mongo::Protocol::Msg do let(:opcode) { 2013 } let(:flags) { [] } let(:options) { {} } - let(:global_args) { { '$db' => SpecConfig.instance.test_db, ping: 1 } } - let(:sections) { [ ] } + let(:main_document) { { '$db' => SpecConfig.instance.test_db, ping: 1 } } + let(:sequences) { [ ] } let(:message) do - described_class.new(flags, options, global_args, *sections) + described_class.new(flags, options, main_document, *sequences) end let(:deserialized) do Mongo::Protocol::Message.deserialize(StringIO.new(message.serialize.to_s)) end describe '#initialize' do - it 'adds the global_args to the sections' do - expect(message.sections[0]).to eq(type: 0, payload: global_args) + it 'adds the main_document to the sections' do + expect(message.sections[0]).to eq(type: 0, payload: main_document) end context 'when flag bits are provided' do context 'when valid flags are provided' do @@ -57,17 +57,17 @@ expect(flag_bytes).to be_int32(1) end end end - context 'with user-provided and driver-generated keys in global_args' do - let(:global_args) do + context 'with user-provided and driver-generated keys in main_document' do + let(:main_document) do { 'ping' => 1, 'lsid' => '__lsid__', 'a' => 'b', '$clusterTime' => '__ct__', 'signature' => '__signature__', 'd' => 'f'} end - it 'reorders global_args for better logging' do + it 'reorders main_document for better logging' do expect(message.payload[:command].keys).to eq(%w(ping a d lsid $clusterTime signature)) end end end @@ -76,33 +76,37 @@ context 'when the other is a msg' do context 'when the fields are equal' do let(:other) do - described_class.new(flags, options, global_args) + described_class.new(flags, options, main_document) end it 'returns true' do expect(message).to eq(other) end end context 'when the flags are not equal' do let(:other) do - described_class.new([:more_to_come], options, global_args) + described_class.new([:more_to_come], options, main_document) end it 'returns false' do expect(message).not_to eq(other) end end - context 'when the global_args are not equal' do + context 'when the main_document are not equal' do + let(:other_main_document) do + { '$db'=> SpecConfig.instance.test_db, ismaster: 1 } + end + let(:other) do - described_class.new(flags, nil, { '$db'=> SpecConfig.instance.test_db, ismaster: 1 }) + described_class.new(flags, nil, other_main_document) end it 'returns false' do expect(message).not_to eq(other) end @@ -156,11 +160,11 @@ end let(:flag_bytes) { bytes.to_s[16..19] } let(:payload_type) { bytes.to_s[20] } let(:payload_bytes) { bytes.to_s[21..-1] } - let(:global_args) { { ping: 1 } } + let(:main_document) { { ping: 1 } } include_examples 'message with a header' context 'when flags are provided' do @@ -203,17 +207,17 @@ it 'sets the payload type' do expect(payload_type).to eq(0.chr) end it 'serializes the global arguments' do - expect(payload_bytes).to be_bson(global_args) + expect(payload_bytes).to be_bson(main_document) end end - context 'when additional sections are provided' do + context 'when sequences are provided' do - let(:sections) do + let(:sequences) do [ section ] end context 'when an invalid payload type is specified' do @@ -222,16 +226,17 @@ payload: { identifier: 'documents', sequence: [ { a: 1 } ] } } end it 'raises an exception' do - expect { - message.serialize - }.to raise_exception(Mongo::Error::UnknownPayloadType) + expect do + message + end.to raise_exception(ArgumentError, /All sequences must be Section1 instances/) end end +=begin no longer supported context 'when a 0 payload type is specified' do let(:section) do { type: 0, payload: { ismaster: 1 } } end @@ -263,17 +268,16 @@ it 'serializes the section' do expect(section_bytes).to be_bson(section[:payload]) end end +=end - context 'when a 1 payload type is specified' do + context 'when a payload of type 1 is specified' do let(:section) do - { type: 1, - payload: { identifier: 'documents', - sequence: [ { a: 1 } ] } } + Mongo::Protocol::Msg::Section1.new('documents', [ { a: 1 } ]) end let(:section_payload_type) { bytes.to_s[36] } let(:section_size) { bytes.to_s[37..40] } let(:section_identifier) { bytes.to_s[41..50] } @@ -295,27 +299,27 @@ expect(section_bytes).to be_bson({ a: 1 }) end context 'when two sections are specified' do - let(:sections) do + let(:sequences) do [ section1, section2 ] end let(:section1) do - { type: 1, - payload: { identifier: 'documents', - sequence: [ { a: 1 } ] } } + Mongo::Protocol::Msg::Section1.new('documents', [ { a: 1 } ]) end let(:section2) do - { type: 1, - payload: { identifier: 'updates', - sequence: [ {:q => { :bar => 1 }, - :u => { :$set => { :bar => 2 } }, - :multi => true, - :upsert => false } ] } } + Mongo::Protocol::Msg::Section1.new('updates', [ + { + :q => { :bar => 1 }, + :u => { :$set => { :bar => 2 } }, + :multi => true, + :upsert => false, + } + ]) end let(:section1_payload_type) { bytes.to_s[36] } let(:section1_size) { bytes.to_s[37..40] } let(:section1_identifier) { bytes.to_s[41..50] } @@ -353,20 +357,20 @@ it 'serializes the second section identifier' do expect(section2_identifier).to eq("updates#{BSON::NULL_BYTE}") end it 'serializes the second section bytes' do - expect(section2_bytes).to be_bson(section2[:payload][:sequence][0]) + expect(section2_bytes).to be_bson(section2.documents[0]) end end end +=begin no longer supported context 'when the sections are mixed types and payload type 1 comes before type 0' do let(:section1) do - { type: 1, - payload: { identifier: 'documents', sequence: [ { 'a' => 1 }]}} + Mongo::Protocol::Msg::Section1.new('documents', [ { a: 1 } ]) end let(:section2) do { type: 0, payload: { 'b' => 2 } } end @@ -374,23 +378,24 @@ let(:sections) do [ section1, section2 ] end it 'serializes all sections' do - expect(deserialized.documents).to eq([ BSON::Document.new(global_args), { 'a' => 1 }, { 'b' => 2 }]) + expect(deserialized.documents).to eq([ BSON::Document.new(main_document), { 'a' => 1 }, { 'b' => 2 }]) end end +=end end context 'when the validating_keys option is true with payload 1' do - let(:sections) do + let(:sequences) do [ section ] end let(:section) do - { type: 1, payload: { identifier: 'documents', sequence: [ { '$b' => 2 } ] } } + Mongo::Protocol::Msg::Section1.new('documents', [ { '$b' => 2 } ]) end let(:options) do { validating_keys: true } end @@ -402,16 +407,16 @@ end end context 'when the validating_keys option is false with payload 1' do - let(:sections) do + let(:sequences) do [ section ] end let(:section) do - { type: 1, payload: { identifier: 'documents', sequence: [ { '$b' => 2 } ] } } + Mongo::Protocol::Msg::Section1.new('documents', [ { '$b' => 2 } ]) end let(:options) do { validating_keys: false } end @@ -425,11 +430,11 @@ describe '#deserialize' do context 'when the payload type is valid' do it 'deserializes the message' do - expect(deserialized.documents).to eq([ BSON::Document.new(global_args) ]) + expect(deserialized.documents).to eq([ BSON::Document.new(main_document) ]) end end context 'when the payload type is not valid' do @@ -438,13 +443,13 @@ s[20] = 5.chr end end it 'raises an exception' do - expect { + expect do Mongo::Protocol::Message.deserialize(StringIO.new(invalid_payload_message)) - }.to raise_exception(Mongo::Error::UnknownPayloadType) + end.to raise_exception(Mongo::Error::UnknownPayloadType) end end end describe '#payload' do @@ -452,38 +457,37 @@ context 'when the msg only contains a payload type 0' do it 'creates a payload with the command' do expect(message.payload[:command_name]).to eq('ping') expect(message.payload[:database_name]).to eq(SpecConfig.instance.test_db) - expect(message.payload[:command]).to eq('ping' => 1) + expect(message.payload[:command]).to eq('ping' => 1, '$db' => SpecConfig.instance.test_db) expect(message.payload[:request_id]).to eq(message.request_id) end end context 'when the contains a payload type 1' do let(:section) do - { type: 1, - payload: { identifier: 'documents', - sequence: [ { a: 1 } ] } } + Mongo::Protocol::Msg::Section1.new('documents', [ { a: 1 } ]) end - let(:global_args) do + let(:main_document) do { '$db' => SpecConfig.instance.test_db, 'insert' => 'foo', 'ordered' => true } end - let(:sections) do + let(:sequences) do [ section ] end let(:expected_command_doc) do { - 'insert' => 'foo', - 'documents' => [{ 'a' => 1 }], - 'ordered' => true + 'insert' => 'foo', + 'documents' => [{ 'a' => 1 }], + 'ordered' => true, + '$db' => SpecConfig.instance.test_db, } end it 'creates a payload with the command' do expect(message.payload[:command_name]).to eq('insert')