summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/usage/service_ping/instrumented_payload_spec.rb
blob: 76548483cfabe904573ee75bf5b4113ceea22905 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Usage::ServicePing::InstrumentedPayload do
  let(:uuid) { "0000-0000-0000" }

  before do
    allow(ApplicationRecord.connection).to receive(:transaction_open?).and_return(false)
    allow(Gitlab::CurrentSettings).to receive(:uuid).and_return(uuid)
  end

  context 'when building service ping with values' do
    let(:metrics_key_paths) { %w(counts.boards uuid redis_hll_counters.search.i_search_total_monthly) }
    let(:expected_payload) do
      {
        counts: { boards: 0 },
        redis_hll_counters: { search: { i_search_total_monthly: 0 } },
        uuid: uuid
      }
    end

    it 'builds the service ping payload for the metrics key_paths' do
      expect(described_class.new(metrics_key_paths, :with_value).build).to eq(expected_payload)
    end
  end

  context 'when building service ping with instrumentations' do
    let(:metrics_key_paths) { %w(counts.boards uuid redis_hll_counters.search.i_search_total_monthly) }
    let(:expected_payload) do
      {
        counts: { boards: "SELECT COUNT(\"boards\".\"id\") FROM \"boards\"" },
        redis_hll_counters: { search: { i_search_total_monthly: 0 } },
        uuid: uuid
      }
    end

    it 'builds the service ping payload for the metrics key_paths' do
      expect(described_class.new(metrics_key_paths, :with_instrumentation).build).to eq(expected_payload)
    end
  end

  context 'when missing instrumentation class' do
    it 'returns empty hash' do
      expect(described_class.new(['counts.ci_builds'], :with_instrumentation).build).to eq({})
      expect(described_class.new(['counts.ci_builds'], :with_value).build).to eq({})
    end
  end
end