File: metrics_instrumentation_shared_examples.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (66 lines) | stat: -rw-r--r-- 2,123 bytes parent folder | download
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
# frozen_string_literal: true

RSpec.shared_examples 'a correct instrumented metric value' do |params|
  let(:time_frame) { params[:time_frame] }
  let(:options) { params[:options] }
  let(:events) { params.slice(:events) }
  let(:metric) { described_class.new({ time_frame: time_frame, options: options }.merge(events)) }

  around do |example|
    freeze_time { example.run }
  end

  before do
    if metric.respond_to?(:relation, true) && metric.send(:relation).respond_to?(:connection)
      allow(metric.send(:relation).connection).to receive(:transaction_open?).and_return(false)
    end
  end

  it 'has correct value' do
    expect(metric.value).to eq(expected_value)
  end
end

RSpec.shared_examples 'a correct instrumented metric query' do |params|
  let(:time_frame) { params[:time_frame] }
  let(:options) { params[:options] }
  let(:metric) { described_class.new(time_frame: time_frame, options: options) }

  around do |example|
    freeze_time { example.run }
  end

  before do
    if metric.respond_to?(:relation, true) && metric.send(:relation).respond_to?(:connection)
      allow(metric.send(:relation).connection).to receive(:transaction_open?).and_return(false)
    end
  end

  it 'has correct generate query' do
    expect(metric.instrumentation).to eq(expected_query)
  end
end

RSpec.shared_examples 'a correct instrumented database query execution value' do |params|
  let(:time_frame) { params[:time_frame] }
  let(:options) { params[:options] }
  let(:metric) { described_class.new(time_frame: time_frame, options: options) }

  around do |example|
    freeze_time { example.run }
  end

  before do
    allow(metric.relation).to receive(:transaction_open?).and_return(false)
  end

  it 'returns correct value' do
    query_result = metric.relation.connection.execute(metric.instrumentation).to_a.first.each_value.first
    expect(query_result).to eq(expected_value)
  end
end

RSpec.shared_examples 'a correct instrumented metric value and query' do |params|
  it_behaves_like 'a correct instrumented metric value', params
  it_behaves_like 'a correct instrumented metric query', params
end