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

database_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: 5e36820df5e885d735d17f96b13a8fed0986c3e1 (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::DatabaseMetric do
  subject do
    described_class.tap do |m|
      m.relation { Issue }
      m.operation :count
      m.start { m.relation.minimum(:id) }
      m.finish { m.relation.maximum(:id) }
    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 }

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

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

    it 'does not cache the result of start and finish', :request_store, :use_clean_rails_redis_caching do
      expect(Gitlab::Cache).not_to receive(:fetch_once)
      expect(subject).to receive(:count).with(any_args, hash_including(start: issues.min_by(&:id).id, finish: issues.max_by(&:id).id)).and_call_original

      subject.value

      expect(Rails.cache.read('metric_instrumentation/special_issue_count_minimum_id')).to eq(nil)
      expect(Rails.cache.read('metric_instrumentation/special_issue_count_maximum_id')).to eq(nil)
    end

    context 'with start and finish not called' do
      subject do
        described_class.tap do |m|
          m.relation { Issue }
          m.operation :count
        end.new(time_frame: 'all')
      end

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

    context 'with cache_start_and_finish_as called' do
      subject do
        described_class.tap do |m|
          m.relation { Issue }
          m.operation :count
          m.start { m.relation.minimum(:id) }
          m.finish { m.relation.maximum(:id) }
          m.cache_start_and_finish_as :special_issue_count
        end.new(time_frame: 'all')
      end

      it 'caches using the key name passed', :request_store, :use_clean_rails_redis_caching do
        expect(Gitlab::Cache).to receive(:fetch_once).with('metric_instrumentation/special_issue_count_minimum_id', any_args).and_call_original
        expect(Gitlab::Cache).to receive(:fetch_once).with('metric_instrumentation/special_issue_count_maximum_id', any_args).and_call_original
        expect(subject).to receive(:count).with(any_args, hash_including(start: issues.min_by(&:id).id, finish: issues.max_by(&:id).id)).and_call_original

        subject.value

        expect(Rails.cache.read('metric_instrumentation/special_issue_count_minimum_id')).to eq(issues.min_by(&:id).id)
        expect(Rails.cache.read('metric_instrumentation/special_issue_count_maximum_id')).to eq(issues.max_by(&:id).id)
      end
    end
  end
end