Skip to content

Commit

Permalink
Added: Datadog::Contrib specs related to integration configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Jun 26, 2018
1 parent ea75490 commit 2635bf2
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/ddtrace/contrib/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def configurations
end
end

def configure(name = :default, options = {}, &block)
name = resolver.resolve(name)
def configure(name, options = {}, &block)
name = resolver.resolve(name || :default)

configurations[name].tap do |settings|
settings.configure(options, &block)
Expand Down
103 changes: 103 additions & 0 deletions spec/ddtrace/contrib/configurable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
require 'spec_helper'

require 'ddtrace'

RSpec.describe Datadog::Contrib::Configurable do
describe 'implemented' do
subject(:configurable_class) do
Class.new.tap do |klass|
klass.send(:include, described_class)
end
end

describe 'instance behavior' do
subject(:configurable_object) { configurable_class.new }

describe '#default_configuration' do
subject(:configuration) { configurable_object.default_configuration }
it { is_expected.to be_a_kind_of(Datadog::Contrib::Configuration::Settings) }
end

describe '#reset_configuration!' do
subject(:reset) { configurable_object.reset_configuration! }

context 'when a configuration has been added' do
before(:each) { configurable_object.configure(:foo, service_name: 'bar') }

it do
expect { reset }.to change { configurable_object.configurations.keys }
.from([:default, :foo])
.to([:default])
end
end
end

describe '#configuration' do
context 'when no name is provided' do
subject(:configuration) { configurable_object.configuration }
it { is_expected.to be_a_kind_of(Datadog::Contrib::Configuration::Settings) }
it { is_expected.to be(configurable_object.configurations[:default]) }
end

context 'when a name is provided' do
subject(:configuration) { configurable_object.configuration(name) }
let(:name) { :foo }

context 'and the configuration exists' do
before(:each) { configurable_object.configure(:foo, service_name: 'bar') }
it { is_expected.to be_a_kind_of(Datadog::Contrib::Configuration::Settings) }
it { is_expected.to be(configurable_object.configurations[:foo]) }
end

context 'but the configuration doesn\'t exist' do
it { is_expected.to be nil }
end
end
end

describe '#configurations' do
subject(:configurations) { configurable_object.configurations }

context 'when nothing has been explicitly configured' do
it { is_expected.to include(default: a_kind_of(Datadog::Contrib::Configuration::Settings)) }
end

context 'when a configuration has been added' do
before(:each) { configurable_object.configure(:foo, service_name: 'bar') }

it do
is_expected.to include(
default: a_kind_of(Datadog::Contrib::Configuration::Settings),
foo: a_kind_of(Datadog::Contrib::Configuration::Settings)
)
end
end
end

describe '#configure' do
context 'when provided a name' do
subject(:configure) { configurable_object.configure(name, service_name: 'bar') }
let(:name) { :foo }

context 'that matches an existing configuration' do
before(:each) { configurable_object.configure(name, service_name: 'baz') }

it do
expect { configure }.to change { configurable_object.configuration(name).service_name }
.from('baz')
.to('bar')
end
end

context 'that does not match any configuration' do
it do
expect { configure }.to change { configurable_object.configuration(name) }
.from(nil)
.to(a_kind_of(Datadog::Contrib::Configuration::Settings))
end
end
end
end
end
end
end
22 changes: 22 additions & 0 deletions spec/ddtrace/contrib/integration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'spec_helper'

require 'ddtrace'

RSpec.describe Datadog::Contrib::Integration do
describe 'implemented' do
subject(:integration_class) do
Class.new.tap do |klass|
klass.send(:include, described_class)
end
end

describe 'instance behavior' do
subject(:integration_object) { integration_class.new(name) }
let(:name) { :foo }

it { is_expected.to be_a_kind_of(Datadog::Contrib::Configurable) }
it { is_expected.to be_a_kind_of(Datadog::Contrib::Patchable) }
it { is_expected.to be_a_kind_of(Datadog::Contrib::Registerable) }
end
end
end
62 changes: 62 additions & 0 deletions spec/ddtrace/contrib/patchable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'spec_helper'

require 'ddtrace'

RSpec.describe Datadog::Contrib::Patchable do
describe 'implemented' do
subject(:patchable_class) do
Class.new.tap do |klass|
klass.send(:include, described_class)
end
end

describe 'class behavior' do
describe '#compatible?' do
subject(:compatible) { patchable_class.compatible? }
let(:expected_compatibility) { RUBY_VERSION >= '1.9.3' ? true : false }
it { is_expected.to be expected_compatibility }
end
end

describe 'instance behavior' do
subject(:patchable_object) { patchable_class.new }

describe '#patcher' do
subject(:patcher) { patchable_object.patcher }
it { is_expected.to be nil }
end

describe '#patch' do
subject(:patch) { patchable_object.patch }

context 'when the patchable object' do
context 'is compatible' do
before(:each) { allow(patchable_class).to receive(:compatible?).and_return(true) }

context 'and the patcher is defined' do
let(:patcher) { double('patcher') }
before(:each) { allow(patchable_object).to receive(:patcher).and_return(patcher) }

it 'applies the patch' do
expect(patcher).to receive(:patch)
patch
end
end

context 'and the patcher is nil' do
it 'does not applies the patch' do
is_expected.to be nil
end
end
end

context 'is not compatible' do
it 'does not applies the patch' do
is_expected.to be nil
end
end
end
end
end
end
end
32 changes: 32 additions & 0 deletions spec/ddtrace/contrib/patcher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'

require 'ddtrace'
require 'ddtrace/contrib/patcher'

RSpec.describe Datadog::Contrib::Patcher do
describe 'implemented' do
subject(:patcher_class) do
Class.new.tap do |klass|
klass.send(:include, described_class)
end
end

describe 'class behavior' do
describe '#patch' do
subject(:patch) { patcher_class.patch }
it { expect { patch }.to raise_error(NotImplementedError) }
end
end

describe 'instance behavior' do
subject(:patcher_object) { patcher_class.new }

it { is_expected.to be_a_kind_of(Datadog::Patcher) }

describe '#patch' do
subject(:patch) { patcher_object.patch }
it { expect { patch }.to raise_error(NotImplementedError) }
end
end
end
end
70 changes: 70 additions & 0 deletions spec/ddtrace/contrib/registerable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require 'spec_helper'

require 'ddtrace'

RSpec.describe Datadog::Contrib::Registerable do
describe 'implemented' do
subject(:registerable_class) do
Class.new.tap do |klass|
klass.send(:include, described_class)
end
end

describe 'class behavior' do
describe '#register_as' do
subject(:register_as) { registerable_class.register_as(name, options) }
let(:name) { :foo }
let(:options) { {} }

context 'when a registry' do
context 'is provided' do
let(:options) { { registry: registry } }
let(:registry) { instance_double(Datadog::Registry) }

it do
expect(registry).to receive(:add)
.with(name, a_kind_of(registerable_class), false)
register_as
end
end

context 'is not provided' do
it do
expect(Datadog.registry).to receive(:add)
.with(name, a_kind_of(registerable_class), false)
register_as
end
end
end

context 'when auto_patch' do
context 'is provided' do
let(:options) { { auto_patch: true } }

it do
expect(Datadog.registry).to receive(:add)
.with(name, a_kind_of(registerable_class), true)
register_as
end
end

context 'is not provided' do
it do
expect(Datadog.registry).to receive(:add)
.with(name, a_kind_of(registerable_class), false)
register_as
end
end
end
end
end

describe 'instance behavior' do
subject(:registerable_object) { registerable_class.new(name, options) }
let(:name) { :foo }
let(:options) { {} }

it { is_expected.to have_attributes(name: name) }
end
end
end

0 comments on commit 2635bf2

Please sign in to comment.