Welcome to mirror list, hosted at ThFree Co, Russian Federation.

generic_metric_spec.rb « instrumentations « metrics « usage « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 158be34d39c47a3a547d0dbce2d51c6b609894f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Usage::Metrics::Instrumentations::GenericMetric do
  shared_examples 'custom fallback' do |custom_fallback|
    subject do
      Class.new(described_class) do
        fallback(custom_fallback)
        value { Gitlab::Database.main.version }
      end.new(time_frame: 'none')
    end

    describe '#value' do
      it 'gives the correct value' do
        expect(subject.value).to eq(Gitlab::Database.main.version)
      end

      context 'when raising an exception' do
        it 'return the custom fallback' do
          expect(Gitlab::Database.main).to receive(:version).and_raise('Error')
          expect(subject.value).to eq(custom_fallback)
        end
      end
    end
  end

  context 'with default fallback' do
    subject do
      Class.new(described_class) do
        value { Gitlab::Database.main.version }
      end.new(time_frame: 'none')
    end

    describe '#value' do
      it 'gives the correct value' do
        expect(subject.value).to eq(Gitlab::Database.main.version )
      end

      context 'when raising an exception' do
        it 'return the default fallback' do
          expect(Gitlab::Database.main).to receive(:version).and_raise('Error')
          expect(subject.value).to eq(described_class::FALLBACK)
        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