diff --git a/spec/ddtrace/configurable_spec.rb b/spec/ddtrace/configurable_spec.rb new file mode 100644 index 00000000000..a463ce567b3 --- /dev/null +++ b/spec/ddtrace/configurable_spec.rb @@ -0,0 +1,114 @@ +require 'spec_helper' + +require 'ddtrace' + +RSpec.describe Datadog::Configurable do + shared_examples_for 'a configurable constant' do + describe '#option' do + let(:name) { :foo } + let(:options) { {} } + let(:block) { nil } + before(:each) { configurable.send(:option, name, options, &block) } + + context 'given a default option' do + let(:options) { { default: default_value } } + let(:default_value) { :bar } + it { expect(configurable.get_option(name)).to eq(default_value) } + end + + context 'given a custom setter' do + let(:name) { :shout } + before(:each) { configurable.set_option(name, 'loud') } + + context 'option' do + let(:options) { { setter: ->(v) { v.upcase } } } + it { expect(configurable.get_option(name)).to eq('LOUD') } + end + + context 'block' do + let(:block) { Proc.new { |value| "#{value.upcase}!" } } + it { expect(configurable.get_option(name)).to eq('LOUD!') } + end + end + end + + describe '#get_option' do + subject(:result) { configurable.get_option(name) } + let(:name) { :foo } + let(:options) { {} } + + it { expect(configurable).to respond_to(:get_option) } + + context 'when the option doesn\'t exist' do + it { expect { result }.to raise_error(Datadog::InvalidOptionError) } + end + end + + describe '#set_option' do + let(:name) { :foo } + let(:options) { {} } + let(:value) { :bar } + + before(:each) do + configurable.send(:option, name, options) + configurable.set_option(name, value) + end + + it { expect(configurable).to respond_to(:set_option) } + + context 'when a default has been defined' do + let(:options) { { default: default_value } } + let(:default_value) { :bar } + let(:value) { 'baz!' } + it { expect(configurable.get_option(name)).to eq(value) } + + context 'and the value set is \'false\'' do + let(:default_value) { true } + let(:value) { false } + it { expect(configurable.get_option(name)).to eq(value) } + end + end + + context 'when the option doesn\'t exist' do + subject(:result) { configurable.set_option(:bad_option, value) } + it { expect { result }.to raise_error(Datadog::InvalidOptionError) } + end + end + + describe '#to_h' do + subject(:hash) { configurable.to_h } + + before(:each) do + configurable.send(:option, :x, default: 1) + configurable.send(:option, :y, default: 2) + configurable.set_option(:y, 100) + end + + it { is_expected.to eq({ x: 1, y: 100 }) } + end + + describe '#sorted_options' do + subject(:sorted_options) { configurable.sorted_options } + + before(:each) do + configurable.send(:option, :foo, depends_on: [:bar]) + configurable.send(:option, :bar, depends_on: [:baz]) + configurable.send(:option, :baz) + end + + it { is_expected.to eq([:baz, :bar, :foo]) } + end + end + + describe 'implemented' do + describe 'class' do + subject(:configurable) { Class.new { include(Datadog::Configurable) } } + it_behaves_like 'a configurable constant' + end + + describe 'module' do + subject(:configurable) { Module.new { include(Datadog::Configurable) } } + it_behaves_like 'a configurable constant' + end + end +end diff --git a/spec/ddtrace/configuration/pin_setup_spec.rb b/spec/ddtrace/configuration/pin_setup_spec.rb new file mode 100644 index 00000000000..f33310f8fd0 --- /dev/null +++ b/spec/ddtrace/configuration/pin_setup_spec.rb @@ -0,0 +1,56 @@ +require 'spec_helper' + +require 'ddtrace' + +RSpec.describe Datadog::Configuration::PinSetup do + let(:target) { Object.new } + + before(:each) do + Datadog::Pin.new('original-service', app: 'original-app').onto(target) + end + + describe '#call' do + before(:each) { described_class.new(target, options).call } + + context 'given options' do + let(:options) do + { + service_name: 'my-service', + app: 'my-app', + app_type: :cache, + tracer: tracer, + tags: { env: :prod }, + distributed_tracing: true + } + end + + let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + + it do + expect(target.datadog_pin.service).to eq('my-service') + expect(target.datadog_pin.app).to eq('my-app') + expect(target.datadog_pin.tags).to eq({ env: :prod }) + expect(target.datadog_pin.config).to eq({ distributed_tracing: true }) + expect(target.datadog_pin.tracer).to eq(tracer) + end + end + + context 'missing options' do + let(:options) { { app: 'custom-app' } } + + it do + expect(target.datadog_pin.app).to eq('custom-app') + expect(target.datadog_pin.service).to eq('original-service') + end + end + end + + describe 'Datadog#configure' do + before(:each) { Datadog.configure(target, service_name: :foo, extra: :bar) } + + it do + expect(target.datadog_pin.service).to eq(:foo) + expect(target.datadog_pin.config).to eq({ extra: :bar }) + end + end +end diff --git a/spec/ddtrace/configuration/proxy_spec.rb b/spec/ddtrace/configuration/proxy_spec.rb new file mode 100644 index 00000000000..909f70d4287 --- /dev/null +++ b/spec/ddtrace/configuration/proxy_spec.rb @@ -0,0 +1,43 @@ +require 'spec_helper' + +require 'ddtrace' + +RSpec.describe Datadog::Configuration::Proxy do + subject(:proxy) { described_class.new(configurable_module) } + + let(:configurable_module) do + Module.new do + include Datadog::Configurable + option :x, default: :a + option :y, default: :b + end + end + + describe '#[]' do + before(:each) do + proxy[:x] = 1 + proxy[:y] = 2 + end + + it do + expect(proxy[:x]).to eq(1) + expect(proxy[:y]).to eq(2) + end + end + + describe '#to_h' do + subject(:hash) { proxy.to_h } + it { is_expected.to eq({ x: :a, y: :b }) } + end + + describe '#to_hash' do + subject(:hash) { proxy.to_hash } + it { is_expected.to eq({ x: :a, y: :b }) } + end + + describe '#merge' do + subject(:result) { proxy.merge(hash) } + let(:hash) { { z: :c } } + it { is_expected.to eq({ x: :a, y: :b, z: :c }) } + end +end diff --git a/spec/ddtrace/configuration/resolver_spec.rb b/spec/ddtrace/configuration/resolver_spec.rb new file mode 100644 index 00000000000..300a81b0ed7 --- /dev/null +++ b/spec/ddtrace/configuration/resolver_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +require 'ddtrace' + +RSpec.describe Datadog::Configuration::Resolver do + subject(:resolver) { described_class.new(graph) } + + describe '#call' do + subject(:order) { resolver.call } + + context 'given a set of dependencies' do + let(:graph) { { 1 => [2], 2 => [3, 4], 3 => [], 4 => [3], 5 => [1] } } + it { expect(order).to eq([3, 4, 2, 1, 5]) } + end + + context 'given cyclic dependencies' do + let(:graph) { { 1 => [2], 2 => [1] } } + it { expect { order }.to raise_error(TSort::Cyclic) } + end + end +end diff --git a/spec/ddtrace/configuration_spec.rb b/spec/ddtrace/configuration_spec.rb new file mode 100644 index 00000000000..2cef25e2dfc --- /dev/null +++ b/spec/ddtrace/configuration_spec.rb @@ -0,0 +1,170 @@ +require 'spec_helper' + +require 'ddtrace' + +RSpec.describe Datadog::Configuration do + let(:configuration) { described_class.new(registry: registry) } + let(:registry) { Datadog::Registry.new } + + describe '#use' do + subject(:result) { configuration.use name, options } + let(:name) { :example } + let(:integration) { double('integration') } + let(:options) { {} } + + before(:each) do + registry.add(name, integration) + end + + context 'for a generic integration' do + before(:each) do + expect(integration).to receive(:sorted_options).and_return([]) + expect(integration).to receive(:patch).and_return(true) + end + + it { expect { result }.to_not raise_error } + end + + context 'for an integration that includes Datadog::Contrib::Base' do + let(:options) { { option1: :foo!, option2: :bar! } } + let(:integration) do + Module.new do + include Datadog::Contrib::Base + option :option1 + option :option2 + end + end + + it do + expect { result }.to_not raise_error + expect(configuration[name][:option1]).to eq(:foo!) + expect(configuration[name][:option2]).to eq(:bar!) + end + + context 'and has a lazy option' do + let(:integration) do + Module.new do + include Datadog::Contrib::Base + option :option1, default: -> { 1 + 1 }, lazy: true + end + end + + it { expect(configuration[name][:option1]).to eq(2) } + end + + context 'and has dependencies' do + let(:options) { { multiply_by: 5, number: 5 } } + let(:integration) do + Module.new do + include Datadog::Contrib::Base + option :multiply_by, depends_on: [:number] do |value| + get_option(:number) * value + end + + option :number + end + end + + it do + expect { result }.to_not raise_error + expect(configuration[name][:number]).to eq(5) + expect(configuration[name][:multiply_by]).to eq(25) + end + end + + context 'and has a setter' do + let(:array) { [] } + let(:options) { { option1: :foo! } } + let(:integration) do + arr = array + Module.new do + include Datadog::Contrib::Base + option :option1 + option :option2, default: 10 do |value| + arr << value + value + end + end + end + + it 'pass through the setter' do + expect { result }.to_not raise_error + expect(configuration[name][:option1]).to eq(:foo!) + expect(configuration[name][:option2]).to eq(10) + expect(array).to include(10) + end + end + + context 'when a setting is changed' do + before(:each) { configuration[name][:option1] = :foo } + it { expect(configuration[:example][:option1]).to eq(:foo) } + end + + context 'when coerced to a hash' do + let(:integration) do + Module.new do + include Datadog::Contrib::Base + option :option1, default: :foo + option :option2, default: :bar + end + end + + it { expect(configuration[name].to_h).to eq({ option1: :foo, option2: :bar }) } + end + end + end + + describe '#tracer' do + let(:tracer) { Datadog::Tracer.new } + let(:debug_state) { tracer.class.debug_logging } + let(:custom_log) { Logger.new(STDOUT) } + + context 'given some settings' do + before(:each) do + @original_log = tracer.class.log + + configuration.tracer( + enabled: false, + debug: !debug_state, + log: custom_log, + hostname: 'tracer.host.com', + port: 1234, + env: :config_test, + tags: { foo: :bar }, + instance: tracer + ) + end + + after(:each) do + tracer.class.debug_logging = debug_state + tracer.class.log = @original_log + end + + it 'applies settings correctly' do + expect(tracer.enabled).to be false + expect(debug_state).to be false + expect(Datadog::Tracer.log).to eq(custom_log) + expect(tracer.writer.transport.hostname).to eq('tracer.host.com') + expect(tracer.writer.transport.port).to eq(1234) + expect(tracer.tags[:env]).to eq(:config_test) + expect(tracer.tags[:foo]).to eq(:bar) + end + end + + it 'acts on the default tracer' do + previous_state = Datadog.tracer.enabled + configuration.tracer(enabled: !previous_state) + expect(Datadog.tracer.enabled).to_not eq(previous_state) + configuration.tracer(enabled: previous_state) + expect(Datadog.tracer.enabled).to eq(previous_state) + end + end + + describe '#[]' do + context 'when the integration doesn\'t exist' do + it do + expect { configuration[:foobar] }.to raise_error(Datadog::Configuration::InvalidIntegrationError) + end + end + end +end diff --git a/spec/ddtrace/pin_spec.rb b/spec/ddtrace/pin_spec.rb new file mode 100644 index 00000000000..e0869f565ac --- /dev/null +++ b/spec/ddtrace/pin_spec.rb @@ -0,0 +1,123 @@ +require 'spec_helper' + +require 'ddtrace' + +RSpec.describe Datadog::Pin do + subject(:pin) { described_class.new(service_name, options) } + + let(:service_name) { 'test-service' } + let(:options) { {} } + let(:target) { Object.new } + + describe '#initialize' do + before(:each) { pin } + + context 'when given some options' do + let(:options) { { app: 'anapp' } } + + it do + expect(pin.service).to eq(service_name) + expect(pin.app).to eq(options[:app]) + end + end + + context 'when given sufficient info' do + let(:options) { { app: 'test-app', app_type: 'test-type', tracer: tracer } } + let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + + it 'sets the service info' do + expect(tracer.services.key?(service_name)).to be true + expect(tracer.services[service_name]).to eq( + { 'app' => 'test-app', 'app_type' => 'test-type' } + ) + end + end + + context 'when given insufficient info' do + let(:options) { { app_type: 'test-type', tracer: tracer } } + let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + + it 'does not sets the service info' do + expect(tracer.services).to be_empty + end + end + end + + describe '#onto' do + let(:options) { { app: 'anapp' } } + let(:returned_pin) { described_class.get_from(target) } + + before(:each) { pin.onto(target) } + + it 'attaches the pin to the target' do + expect(returned_pin.service).to eq(service_name) + expect(returned_pin.app).to eq(options[:app]) + end + end + + describe '#get_from' do + subject(:returned_pin) { described_class.get_from(target) } + + context 'called against' do + context '0' do + let(:target) { 0 } + it { is_expected.to be nil } + end + + context 'nil' do + let(:target) { nil } + it { is_expected.to be nil } + end + + context 'self' do + let(:target) { self } + it { is_expected.to be nil } + end + end + + context 'when a custom pin has already been defined' do + let(:target_class) do + Class.new do + def datadog_pin + @custom_attribute + end + + def datadog_pin=(pin) + @custom_attribute = 'The PIN is set!' + end + end + end + + let(:target) { target_class.new } + before(:each) { pin.onto(target) } + + it 'returns the custom pin' do + is_expected.to eq('The PIN is set!') + end + end + end + + describe '#to_s' do + subject(:string) { pin.to_s } + let(:service_name) { 'abc' } + let(:options) { { app: 'anapp', app_type: 'db' } } + it { is_expected.to eq('Pin(service:abc,app:anapp,app_type:db,name:)') } + end + + describe '#datadog_pin' do + let(:returned_pin) { target.datadog_pin } + before(:each) { pin.onto(target) } + it { expect(returned_pin.service).to eq(service_name) } + end + + describe '#enabled?' do + subject(:enabled) { pin.enabled? } + it { is_expected.to be true } + + context 'when the tracer is disabled' do + let(:options) { { tracer: Datadog::Tracer.new(writer: FauxWriter.new) } } + before(:each) { pin.tracer.enabled = false } + it { is_expected.to be false } + end + end +end diff --git a/test/configurable_test.rb b/test/configurable_test.rb deleted file mode 100644 index e7bb0417186..00000000000 --- a/test/configurable_test.rb +++ /dev/null @@ -1,88 +0,0 @@ -require 'ddtrace/configurable' - -module Datadog - class ConfigurableTest < Minitest::Test - def setup - @module = Module.new { include(Configurable) } - end - - def test_option_methods - assert_respond_to(@module, :set_option) - assert_respond_to(@module, :get_option) - end - - def test_option_default - @module.class_eval do - option :foo, default: :bar - end - - assert_equal(:bar, @module.get_option(:foo)) - end - - def test_setting_an_option - @module.class_eval do - option :foo, default: :bar - end - - @module.set_option(:foo, 'baz!') - assert_equal('baz!', @module.get_option(:foo)) - end - - def test_custom_setter - @module.class_eval do - option :shout, setter: ->(v) { v.upcase } - end - - @module.set_option(:shout, 'loud') - assert_equal('LOUD', @module.get_option(:shout)) - end - - def test_custom_setter_block - @module.class_eval do - option(:shout) { |value| "#{value.upcase}!" } - end - - @module.set_option(:shout, 'ouch') - assert_equal('OUCH!', @module.get_option(:shout)) - end - - def test_invalid_option - assert_raises(InvalidOptionError) do - @module.set_option(:bad_option, 'foo') - end - - assert_raises(InvalidOptionError) do - @module.get_option(:bad_option) - end - end - - def test_to_h - @module.class_eval do - option :x, default: 1 - option :y, default: 2 - end - - @module.set_option(:y, 100) - assert_equal({ x: 1, y: 100 }, @module.to_h) - end - - def test_false_options - @module.class_eval do - option :boolean, default: true - end - - @module.set_option(:boolean, false) - refute(@module.get_option(:boolean)) - end - - def test_dependency_solving - @module.class_eval do - option :foo, depends_on: [:bar] - option :bar, depends_on: [:baz] - option :baz - end - - assert_equal([:baz, :bar, :foo], @module.sorted_options) - end - end -end diff --git a/test/configuration/pin_setup_test.rb b/test/configuration/pin_setup_test.rb deleted file mode 100644 index 19a2e449b23..00000000000 --- a/test/configuration/pin_setup_test.rb +++ /dev/null @@ -1,50 +0,0 @@ -require 'ddtrace/configuration' - -module Datadog - class Configuration - class PinSetupTest < Minitest::Test - def setup - @target = Object.new - - Pin - .new('original-service', app: 'original-app') - .onto(@target) - end - - def test_setting_options - custom_tracer = get_test_tracer - - custom_options = { - service_name: 'my-service', - app: 'my-app', - app_type: :cache, - tracer: custom_tracer, - tags: { env: :prod }, - distributed_tracing: true - } - - PinSetup.new(@target, custom_options).call - - assert_equal('my-service', @target.datadog_pin.service) - assert_equal('my-app', @target.datadog_pin.app) - assert_equal({ env: :prod }, @target.datadog_pin.tags) - assert_equal({ distributed_tracing: true }, @target.datadog_pin.config) - assert_equal(custom_tracer, @target.datadog_pin.tracer) - end - - def test_missing_options_are_not_set - PinSetup.new(@target, app: 'custom-app').call - - assert_equal('custom-app', @target.datadog_pin.app) - assert_equal('original-service', @target.datadog_pin.service) - end - - def test_configure_api - Datadog.configure(@target, service_name: :foo, extra: :bar) - - assert_equal(:foo, @target.datadog_pin.service) - assert_equal({ extra: :bar }, @target.datadog_pin.config) - end - end - end -end diff --git a/test/configuration/proxy_test.rb b/test/configuration/proxy_test.rb deleted file mode 100644 index ea2903f6213..00000000000 --- a/test/configuration/proxy_test.rb +++ /dev/null @@ -1,34 +0,0 @@ -require 'ddtrace/configurable' - -module Datadog - class Configuration - class ProxyTest < Minitest::Test - def setup - @module = Module.new do - include Configurable - option :x, default: :a - option :y, default: :b - end - - @proxy = Proxy.new(@module) - end - - def test_hash_syntax - @proxy[:x] = 1 - @proxy[:y] = 2 - - assert_equal(1, @proxy[:x]) - assert_equal(2, @proxy[:y]) - end - - def test_hash_coercion - assert_equal({ x: :a, y: :b }, @proxy.to_h) - assert_equal({ x: :a, y: :b }, @proxy.to_hash) - end - - def test_merge - assert_equal({ x: :a, y: :b, z: :c }, @proxy.merge(z: :c)) - end - end - end -end diff --git a/test/configuration/resolver_test.rb b/test/configuration/resolver_test.rb deleted file mode 100644 index 2b2900edf8f..00000000000 --- a/test/configuration/resolver_test.rb +++ /dev/null @@ -1,22 +0,0 @@ -require 'ddtrace/configuration' - -module Datadog - class Configuration - class ResolverTest < Minitest::Test - def test_dependency_solving - graph = { 1 => [2], 2 => [3, 4], 3 => [], 4 => [3], 5 => [1] } - tsort = Resolver.new(graph).call - - assert_equal([3, 4, 2, 1, 5], tsort) - end - - def test_cyclic_dependecy - graph = { 1 => [2], 2 => [1] } - - assert_raises(TSort::Cyclic) do - Resolver.new(graph).call - end - end - end - end -end diff --git a/test/configuration_test.rb b/test/configuration_test.rb deleted file mode 100644 index 141e60894ea..00000000000 --- a/test/configuration_test.rb +++ /dev/null @@ -1,150 +0,0 @@ -require 'ddtrace/configuration' -require 'ddtrace/configurable' -require 'logger' - -module Datadog - class ConfigurationTest < Minitest::Test - def setup - @registry = Registry.new - @configuration = Configuration.new(registry: @registry) - end - - def test_use_method - contrib = Minitest::Mock.new - contrib.expect(:patch, true) - contrib.expect(:sorted_options, []) - - @registry.add(:example, contrib) - @configuration.use(:example) - - assert_mock(contrib) - end - - def test_module_configuration - integration = Module.new do - include Contrib::Base - option :option1 - option :option2 - end - - @registry.add(:example, integration) - - @configuration.use(:example, option1: :foo!, option2: :bar!) - - assert_equal(:foo!, @configuration[:example][:option1]) - assert_equal(:bar!, @configuration[:example][:option2]) - end - - def test_setting_a_configuration_param - integration = Module.new do - include Contrib::Base - option :option1 - end - - @registry.add(:example, integration) - @configuration[:example][:option1] = :foo - assert_equal(:foo, @configuration[:example][:option1]) - end - - def test_invalid_integration - assert_raises(Configuration::InvalidIntegrationError) do - @configuration[:foobar] - end - end - - def test_lazy_option - integration = Module.new do - include Contrib::Base - option :option1, default: -> { 1 + 1 }, lazy: true - end - - @registry.add(:example, integration) - - assert_equal(2, @configuration[:example][:option1]) - end - - def test_hash_coercion - integration = Module.new do - include Contrib::Base - option :option1, default: :foo - option :option2, default: :bar - end - - @registry.add(:example, integration) - assert_equal({ option1: :foo, option2: :bar }, @configuration[:example].to_h) - end - - def test_dependency_solving - integration = Module.new do - include Contrib::Base - option :multiply_by, depends_on: [:number] do |value| - get_option(:number) * value - end - - option :number - end - - @registry.add(:example, integration) - @configuration.use(:example, multiply_by: 5, number: 5) - assert_equal(5, @configuration[:example][:number]) - assert_equal(25, @configuration[:example][:multiply_by]) - end - - def test_default_also_passes_through_setter - array = [] - - integration = Module.new do - include Contrib::Base - option :option1 - option :option2, default: 10 do |value| - array << value - value - end - end - - @registry.add(:example, integration) - @configuration.use(:example, option1: :foo!) - - assert_equal(:foo!, @configuration[:example][:option1]) - assert_equal(10, @configuration[:example][:option2]) - assert_includes(array, 10) - end - - def test_tracer_configuration - tracer = Datadog::Tracer.new - debug_state = tracer.class.debug_logging - original_log = tracer.class.log - custom_log = Logger.new(STDOUT) - - @configuration.tracer( - enabled: false, - debug: !debug_state, - log: custom_log, - hostname: 'tracer.host.com', - port: 1234, - env: :config_test, - tags: { foo: :bar }, - instance: tracer - ) - - refute(tracer.enabled) - refute(debug_state) - assert_equal(custom_log, Datadog::Tracer.log) - assert_equal('tracer.host.com', tracer.writer.transport.hostname) - assert_equal(1234, tracer.writer.transport.port) - assert_equal(:config_test, tracer.tags[:env]) - assert_equal(:bar, tracer.tags[:foo]) - tracer.class.debug_logging = debug_state - tracer.class.log = original_log - end - - def test_configuration_acts_on_default_tracer - previous_state = Datadog.tracer.enabled - - @configuration.tracer(enabled: !previous_state) - refute_equal(previous_state, Datadog.tracer.enabled) - @configuration.tracer(enabled: previous_state) - assert_equal(previous_state, Datadog.tracer.enabled) - end - end -end diff --git a/test/pin_test.rb b/test/pin_test.rb deleted file mode 100644 index 18c74937a81..00000000000 --- a/test/pin_test.rb +++ /dev/null @@ -1,90 +0,0 @@ -require 'helper' -require 'ddtrace' -require 'ddtrace/pin' -require 'ddtrace/tracer' - -class CustomPinSetGet - def datadog_pin - @custom_attribute - end - - def datadog_pin=(pin) - @custom_attribute = 'The PIN is set!' - end -end - -class PinTest < Minitest::Test - def test_pin_onto - a = '' # using String, but really, any object should fit - - pin = Datadog::Pin.new('abc', app: 'anapp') - assert_equal('abc', pin.service) - assert_equal('anapp', pin.app) - pin.onto(a) - - got = Datadog::Pin.get_from(a) - assert_equal('abc', got.service) - assert_equal('anapp', got.app) - end - - def test_pin_get_from - a = [0, nil, self] # get_from should be callable on anything - - a.each do |x| - assert_nil(Datadog::Pin.get_from(x)) - end - end - - def test_to_s - pin = Datadog::Pin.new('abc', app: 'anapp', app_type: 'db') - assert_equal('abc', pin.service) - assert_equal('anapp', pin.app) - assert_equal('db', pin.app_type) - repr = pin.to_s - assert_equal('Pin(service:abc,app:anapp,app_type:db,name:)', repr) - end - - def test_pin_accessor - a = '' # using String, but really, any object should fit - - pin = Datadog::Pin.new('abc') - pin.onto(a) - - got = a.datadog_pin - assert_equal('abc', got.service) - end - - def test_enabled - pin = Datadog::Pin.new('abc') - assert_equal(true, pin.enabled?) - end - - def test_custom_getter_setter - # ensures that if datadog_pin is available in the class, it will - # be used instead of the default datadog_pin - obj = CustomPinSetGet.new - pin = Datadog::Pin.new('pin', app: 'app', app_type: 'db') - pin.onto(obj) - assert_equal('The PIN is set!', Datadog::Pin.get_from(obj)) - end - - def test_service_info_update - tracer = get_test_tracer - Datadog::Pin.new('test-service', app: 'test-app', app_type: 'test-type', tracer: tracer) - - assert(tracer.services.key?('test-service')) - assert_equal( - { 'app' => 'test-app', 'app_type' => 'test-type' }, - tracer.services['test-service'] - ) - end - - def test_service_info_update_with_missing_params - tracer = get_test_tracer - - # instantiating `Pin` without an `app` param (which is necessary for service info) - Datadog::Pin.new('test-service', app_type: 'test-type', tracer: tracer) - - assert(tracer.services.empty?) - end -end diff --git a/test/registry_test.rb b/test/registry_test.rb deleted file mode 100644 index 97649c9f2cd..00000000000 --- a/test/registry_test.rb +++ /dev/null @@ -1,60 +0,0 @@ -require 'minitest/autorun' -require 'ddtrace' -require 'ddtrace/registry' - -module Datadog - class RegistryTest < Minitest::Test - def test_object_retrieval - registry = Registry.new - - object1 = Object.new - object2 = Object.new - - registry.add(:object1, object1) - registry.add(:object2, object2) - - assert_same(object1, registry[:object1]) - assert_same(object2, registry[:object2]) - end - - def test_hash_coercion - registry = Registry.new - - object1 = Object.new - object2 = Object.new - - registry.add(:object1, object1, true) - registry.add(:object2, object2, false) - - assert_equal({ object1: true, object2: false }, registry.to_h) - end - - def test_enumeration - registry = Registry.new - - object1 = Object.new - object2 = Object.new - - registry.add(:object1, object1, true) - registry.add(:object2, object2, false) - - assert(registry.respond_to?(:each)) - assert_kind_of(Enumerable, registry) - - # Enumerable#map - objects = registry.map(&:klass) - assert_kind_of(Array, objects) - assert_equal(2, objects.size) - assert_includes(objects, object1) - assert_includes(objects, object2) - end - - def test_registry_entry - entry = Registry::Entry.new(:array, Array, true) - - assert_equal(:array, entry.name) - assert_equal(Array, entry.klass) - assert_equal(true, entry.auto_patch) - end - end -end