Commit 420d8aa9 authored by alinamihaila's avatar alinamihaila

Added more tests

parent 9210e9da
...@@ -15,22 +15,23 @@ module Gitlab ...@@ -15,22 +15,23 @@ module Gitlab
FALLBACK = -1 FALLBACK = -1
class << self class << self
attr_reader :metric_operation attr_reader :metric_operation, :metric_value
@metric_operation = :alt @metric_operation = :alt
def fallback(custom_fallback) def fallback(custom_fallback = FALLBACK)
return @metric_fallback if defined?(@metric_fallback)
@metric_fallback = custom_fallback @metric_fallback = custom_fallback
end end
def value(&block) def value(&block)
@metric_value = block @metric_value = block
end end
attr_reader :metric_value, :metric_fallback
end end
def value def value
alt_usage_data(fallback: fallback) do alt_usage_data(fallback: self.class.fallback) do
self.class.metric_value.call self.class.metric_value.call
end end
end end
...@@ -40,10 +41,6 @@ module Gitlab ...@@ -40,10 +41,6 @@ module Gitlab
self.class.metric_operation self.class.metric_operation
) )
end end
def fallback
self.class.metric_fallback || FALLBACK
end
end end
end end
end end
......
...@@ -3,23 +3,23 @@ ...@@ -3,23 +3,23 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::Usage::Metrics::Instrumentations::GenericMetric do RSpec.describe Gitlab::Usage::Metrics::Instrumentations::GenericMetric do
context 'with custom fallback' do shared_examples 'custom fallback' do |custom_fallback|
subject do subject do
Class.new(described_class) do Class.new(described_class) do
fallback(-2) fallback(custom_fallback)
value { Gitlab::Database.version } value { Gitlab::Database.version }
end.new(time_frame: 'none') end.new(time_frame: 'none')
end end
describe '#value' do describe '#value' do
it 'gives the correct value' do it 'gives the correct value' do
expect(subject.value).to eq(Gitlab::Database.version ) expect(subject.value).to eq(Gitlab::Database.version)
end end
context 'when raising an exception' do context 'when raising an exception' do
it 'return the custom fallback' do it 'return the custom fallback' do
expect(Gitlab::Database).to receive(:version).and_raise('Error') expect(Gitlab::Database).to receive(:version).and_raise('Error')
expect(subject.value).to eq(-2) expect(subject.value).to eq(custom_fallback)
end end
end end
end end
...@@ -38,11 +38,35 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::GenericMetric do ...@@ -38,11 +38,35 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::GenericMetric do
end end
context 'when raising an exception' do context 'when raising an exception' do
it 'return the custom fallback' do it 'return the default fallback' do
expect(Gitlab::Database).to receive(:version).and_raise('Error') expect(Gitlab::Database).to receive(:version).and_raise('Error')
expect(subject.value).to eq(-1) expect(subject.value).to eq(described_class::FALLBACK)
end end
end end
end end
end end
context 'with custom fallback -2' do
it_behaves_like 'custom fallback', -2
end
context 'with custom fallback nil' do
it_behaves_like 'custom fallback', nil
end
context 'with custom fallback false' do
it_behaves_like 'custom fallback', false
end
context 'with custom fallback true' do
it_behaves_like 'custom fallback', true
end
context 'with custom fallback []' do
it_behaves_like 'custom fallback', []
end
context 'with custom fallback { major: -1 }' do
it_behaves_like 'custom fallback', { major: -1 }
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment