Skip to content

Commit

Permalink
Fixed: Reordering of Mongo operation attributes breaking expectations.
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Jun 21, 2019
1 parent 2e0a3b2 commit 85643ea
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions spec/ddtrace/contrib/mongodb/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,22 @@ def discard_spans!
# Clear data between tests
let(:drop_database?) { true }

def suppress_warnings
original_verbosity = $VERBOSE
$VERBOSE = nil
yield
ensure
$VERBOSE = original_verbosity
end

around do |example|
# Reset before and after each example; don't allow global state to linger.
Datadog.registry[:mongo].reset_configuration!
example.run
Datadog.registry[:mongo].reset_configuration!
client.database.drop if drop_database?
suppress_warnings do
# Reset before and after each example; don't allow global state to linger.
Datadog.registry[:mongo].reset_configuration!
example.run
Datadog.registry[:mongo].reset_configuration!
client.database.drop if drop_database?
end
end

it 'evaluates the block given to the constructor' do
Expand Down Expand Up @@ -89,6 +99,14 @@ def discard_spans!
end
end

RSpec::Matchers.define :eq_serialized_operation do |expected|
match do |actual|
actual_obj = actual.is_a?(String) ? JSON.parse(actual.gsub(/=>/, ':')) : actual
expected_obj = expected.is_a?(String) ? JSON.parse(expected.gsub(/=>/, ':')) : expected
expect(expected_obj).to eq(actual_obj)
end
end

describe '#insert_one operation' do
before(:each) { client[collection].insert_one(params) }

Expand All @@ -101,7 +119,7 @@ def discard_spans!
if mongo_gem_version < Gem::Version.new('2.5')
expect(span.resource).to eq("{\"operation\"=>:insert, \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"documents\"=>[{:name=>\"?\"}], \"ordered\"=>\"?\"}")
else
expect(span.resource).to eq("{\"operation\"=>\"insert\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"ordered\"=>\"?\", \"lsid\"=>{\"id\"=>\"?\"}, \"documents\"=>[{\"name\"=>\"?\"}]}")
expect(span.resource).to eq_serialized_operation("{\"operation\"=>\"insert\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"ordered\"=>\"?\", \"lsid\"=>{\"id\"=>\"?\"}, \"documents\"=>[{\"name\"=>\"?\"}]}")
end
expect(span.get_tag('mongodb.rows')).to eq('1')
end
Expand All @@ -117,7 +135,7 @@ def discard_spans!
if mongo_gem_version < Gem::Version.new('2.5')
expect(span.resource).to eq("{\"operation\"=>:insert, \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"documents\"=>[{:name=>\"?\", :hobbies=>[\"?\"]}], \"ordered\"=>\"?\"}")
else
expect(span.resource).to eq("{\"operation\"=>\"insert\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"ordered\"=>\"?\", \"lsid\"=>{\"id\"=>\"?\"}, \"documents\"=>[{\"name\"=>\"?\", \"hobbies\"=>[\"?\"]}]}")
expect(span.resource).to eq_serialized_operation("{\"operation\"=>\"insert\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"ordered\"=>\"?\", \"lsid\"=>{\"id\"=>\"?\"}, \"documents\"=>[{\"name\"=>\"?\", \"hobbies\"=>[\"?\"]}]}")
end
expect(span.get_tag('mongodb.rows')).to eq('1')
end
Expand All @@ -143,7 +161,7 @@ def discard_spans!
if mongo_gem_version < Gem::Version.new('2.5')
expect(span.resource).to eq("{\"operation\"=>:insert, \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"documents\"=>[{:name=>\"?\", :hobbies=>[\"?\"]}, \"?\"], \"ordered\"=>\"?\"}")
else
expect(span.resource).to eq("{\"operation\"=>\"insert\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"ordered\"=>\"?\", \"lsid\"=>{\"id\"=>\"?\"}, \"documents\"=>[{\"name\"=>\"?\", \"hobbies\"=>[\"?\"]}, \"?\"]}")
expect(span.resource).to eq_serialized_operation("{\"operation\"=>\"insert\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"ordered\"=>\"?\", \"lsid\"=>{\"id\"=>\"?\"}, \"documents\"=>[{\"name\"=>\"?\", \"hobbies\"=>[\"?\"]}, \"?\"]}")
end
expect(span.get_tag('mongodb.rows')).to eq('2')
end
Expand All @@ -170,7 +188,7 @@ def discard_spans!
if mongo_gem_version < Gem::Version.new('2.5')
expect(span.resource).to eq("{\"operation\"=>\"find\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"filter\"=>{}}")
else
expect(span.resource).to eq("{\"operation\"=>\"find\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"filter\"=>{}, \"lsid\"=>{\"id\"=>\"?\"}}")
expect(span.resource).to eq_serialized_operation("{\"operation\"=>\"find\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"filter\"=>{}, \"lsid\"=>{\"id\"=>\"?\"}}")
end
expect(span.get_tag('mongodb.rows')).to be nil
end
Expand All @@ -195,7 +213,7 @@ def discard_spans!
if mongo_gem_version < Gem::Version.new('2.5')
expect(span.resource).to eq("{\"operation\"=>\"find\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"filter\"=>{\"name\"=>\"?\"}}")
else
expect(span.resource).to eq("{\"operation\"=>\"find\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"filter\"=>{\"name\"=>\"?\"}, \"lsid\"=>{\"id\"=>\"?\"}}")
expect(span.resource).to eq_serialized_operation("{\"operation\"=>\"find\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"filter\"=>{\"name\"=>\"?\"}, \"lsid\"=>{\"id\"=>\"?\"}}")
end
expect(span.get_tag('mongodb.rows')).to be nil
end
Expand Down Expand Up @@ -224,7 +242,7 @@ def discard_spans!
if mongo_gem_version < Gem::Version.new('2.5')
expect(span.resource).to eq("{\"operation\"=>:update, \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"updates\"=>[{\"q\"=>{\"name\"=>\"?\"}, \"u\"=>{\"$set\"=>{\"phone_number\"=>\"?\"}}, \"multi\"=>\"?\", \"upsert\"=>\"?\"}], \"ordered\"=>\"?\"}")
else
expect(span.resource).to eq("{\"operation\"=>\"update\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"ordered\"=>\"?\", \"lsid\"=>{\"id\"=>\"?\"}, \"updates\"=>[{\"q\"=>{\"name\"=>\"?\"}, \"u\"=>{\"$set\"=>{\"phone_number\"=>\"?\"}}, \"multi\"=>\"?\", \"upsert\"=>\"?\"}]}")
expect(span.resource).to eq_serialized_operation("{\"operation\"=>\"update\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"ordered\"=>\"?\", \"lsid\"=>{\"id\"=>\"?\"}, \"updates\"=>[{\"q\"=>{\"name\"=>\"?\"}, \"u\"=>{\"$set\"=>{\"phone_number\"=>\"?\"}}, \"multi\"=>\"?\", \"upsert\"=>\"?\"}]}")
end
expect(span.get_tag('mongodb.rows')).to eq('1')
end
Expand Down Expand Up @@ -261,7 +279,7 @@ def discard_spans!
if mongo_gem_version < Gem::Version.new('2.5')
expect(span.resource).to eq("{\"operation\"=>:update, \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"updates\"=>[{\"q\"=>{}, \"u\"=>{\"$set\"=>{\"phone_number\"=>\"?\"}}, \"multi\"=>\"?\", \"upsert\"=>\"?\"}], \"ordered\"=>\"?\"}")
else
expect(span.resource).to eq("{\"operation\"=>\"update\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"ordered\"=>\"?\", \"lsid\"=>{\"id\"=>\"?\"}, \"updates\"=>[{\"q\"=>{}, \"u\"=>{\"$set\"=>{\"phone_number\"=>\"?\"}}, \"multi\"=>\"?\", \"upsert\"=>\"?\"}]}")
expect(span.resource).to eq_serialized_operation("{\"operation\"=>\"update\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"ordered\"=>\"?\", \"lsid\"=>{\"id\"=>\"?\"}, \"updates\"=>[{\"q\"=>{}, \"u\"=>{\"$set\"=>{\"phone_number\"=>\"?\"}}, \"multi\"=>\"?\", \"upsert\"=>\"?\"}]}")
end
expect(span.get_tag('mongodb.rows')).to eq('2')
end
Expand Down Expand Up @@ -290,7 +308,7 @@ def discard_spans!
if mongo_gem_version < Gem::Version.new('2.5')
expect(span.resource).to eq("{\"operation\"=>:delete, \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"deletes\"=>[{\"q\"=>{\"name\"=>\"?\"}, \"limit\"=>\"?\"}], \"ordered\"=>\"?\"}")
else
expect(span.resource).to eq("{\"operation\"=>\"delete\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"ordered\"=>\"?\", \"lsid\"=>{\"id\"=>\"?\"}, \"deletes\"=>[{\"q\"=>{\"name\"=>\"?\"}, \"limit\"=>\"?\"}]}")
expect(span.resource).to eq_serialized_operation("{\"operation\"=>\"delete\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"ordered\"=>\"?\", \"lsid\"=>{\"id\"=>\"?\"}, \"deletes\"=>[{\"q\"=>{\"name\"=>\"?\"}, \"limit\"=>\"?\"}]}")
end
expect(span.get_tag('mongodb.rows')).to eq('1')
end
Expand Down Expand Up @@ -327,7 +345,7 @@ def discard_spans!
if mongo_gem_version < Gem::Version.new('2.5')
expect(span.resource).to eq("{\"operation\"=>:delete, \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"deletes\"=>[{\"q\"=>{\"name\"=>\"?\"}, \"limit\"=>\"?\"}], \"ordered\"=>\"?\"}")
else
expect(span.resource).to eq("{\"operation\"=>\"delete\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"ordered\"=>\"?\", \"lsid\"=>{\"id\"=>\"?\"}, \"deletes\"=>[{\"q\"=>{\"name\"=>\"?\"}, \"limit\"=>\"?\"}]}")
expect(span.resource).to eq_serialized_operation("{\"operation\"=>\"delete\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"ordered\"=>\"?\", \"lsid\"=>{\"id\"=>\"?\"}, \"deletes\"=>[{\"q\"=>{\"name\"=>\"?\"}, \"limit\"=>\"?\"}]}")
end
expect(span.get_tag('mongodb.rows')).to eq('2')
end
Expand All @@ -344,7 +362,7 @@ def discard_spans!
if mongo_gem_version < Gem::Version.new('2.5')
expect(span.resource).to eq("{\"operation\"=>:dropDatabase, \"database\"=>\"#{database}\", \"collection\"=>1}")
else
expect(span.resource).to eq("{\"operation\"=>\"dropDatabase\", \"database\"=>\"#{database}\", \"collection\"=>1, \"lsid\"=>{\"id\"=>\"?\"}}")
expect(span.resource).to eq_serialized_operation("{\"operation\"=>\"dropDatabase\", \"database\"=>\"#{database}\", \"collection\"=>1, \"lsid\"=>{\"id\"=>\"?\"}}")
end
expect(span.get_tag('mongodb.rows')).to be nil
end
Expand All @@ -359,7 +377,7 @@ def discard_spans!
if mongo_gem_version < Gem::Version.new('2.5')
expect(span.resource).to eq("{\"operation\"=>:drop, \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\"}")
else
expect(span.resource).to eq("{\"operation\"=>\"drop\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"lsid\"=>{\"id\"=>\"?\"}}")
expect(span.resource).to eq_serialized_operation("{\"operation\"=>\"drop\", \"database\"=>\"#{database}\", \"collection\"=>\"#{collection}\", \"lsid\"=>{\"id\"=>\"?\"}}")
end
expect(span.get_tag('mongodb.rows')).to be nil
expect(span.status).to eq(1)
Expand Down

0 comments on commit 85643ea

Please sign in to comment.