summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/usage/metrics/instrumentations/numbers_metric_spec.rb
blob: 180c76d56f3959f1139f8e662aad83ebf49d0ee4 (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
73
74
75
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Usage::Metrics::Instrumentations::NumbersMetric do
  subject do
    described_class.tap do |metric_class|
      metric_class.operation :add
      metric_class.data do |time_frame|
        [
          Gitlab::Usage::Metrics::Instrumentations::CountIssuesMetric.new(time_frame: time_frame).value,
          Gitlab::Usage::Metrics::Instrumentations::CountBoardsMetric.new(time_frame: time_frame).value
        ]
      end
    end.new(time_frame: 'all')
  end

  describe '#value' do
    let_it_be(:issue_1) { create(:issue) }
    let_it_be(:issue_2) { create(:issue) }
    let_it_be(:issue_3) { create(:issue) }
    let_it_be(:issues) { Issue.all }

    let_it_be(:board_1) { create(:board) }
    let_it_be(:boards) { Board.all }

    before do
      allow(Issue.connection).to receive(:transaction_open?).and_return(false)
    end

    it 'calculates a correct result' do
      expect(subject.value).to eq(4)
    end

    context 'with availability defined' do
      subject do
        described_class.tap do |metric_class|
          metric_class.operation :add
          metric_class.data { [1] }
          metric_class.available? { false }
        end.new(time_frame: 'all')
      end

      it 'responds to #available? properly' do
        expect(subject.available?).to eq(false)
      end
    end

    context 'with availability not defined' do
      subject do
        Class.new(described_class) do
          operation :add
          data { [] }
        end.new(time_frame: 'all')
      end

      it 'responds to #available? properly' do
        expect(subject.available?).to eq(true)
      end
    end
  end

  context 'with unimplemented operation method used' do
    subject do
      described_class.tap do |metric_class|
        metric_class.operation :invalid_operation
        metric_class.data { [] }
      end.new(time_frame: 'all')
    end

    it 'raises an error' do
      expect { subject }.to raise_error(described_class::UnimplementedOperationError)
    end
  end
end