summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/usage/metrics/instrumentations/count_bulk_imports_entities_metric_spec.rb
blob: b85d5a3ebf960e3adb4a0d9ee6e58139cdbfa50c (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Usage::Metrics::Instrumentations::CountBulkImportsEntitiesMetric do
  let_it_be(:user) { create(:user) }
  let_it_be(:bulk_import_projects) do
    create_list(:bulk_import_entity, 3, source_type: 'project_entity', created_at: 3.weeks.ago)
  end

  let_it_be(:bulk_import_groups) do
    create_list(:bulk_import_entity, 3, source_type: 'group_entity', created_at: 3.weeks.ago)
  end

  let_it_be(:old_bulk_import_project) do
    create(:bulk_import_entity, source_type: 'project_entity', created_at: 2.months.ago)
  end

  context 'with no source_type' do
    context 'with all time frame' do
      let(:expected_value) { 7 }
      let(:expected_query) do
        "SELECT COUNT(\"bulk_import_entities\".\"id\") FROM \"bulk_import_entities\""
      end

      it_behaves_like 'a correct instrumented metric value and query', time_frame: 'all', options: {}
    end

    context 'for 28d time frame' do
      let(:expected_value) { 6 }
      let(:start) { 30.days.ago.to_s(:db) }
      let(:finish) { 2.days.ago.to_s(:db) }
      let(:expected_query) do
        "SELECT COUNT(\"bulk_import_entities\".\"id\") FROM \"bulk_import_entities\""\
        " WHERE \"bulk_import_entities\".\"created_at\" BETWEEN '#{start}' AND '#{finish}'"
      end

      it_behaves_like 'a correct instrumented metric value and query', time_frame: '28d', options: {}
    end
  end

  context 'with invalid source_type' do
    it 'raises ArgumentError' do
      expect { described_class.new(time_frame: 'all', options: { source_type: 'random' }) }
        .to raise_error(ArgumentError, /source_type/)
    end
  end

  context 'with source_type project_entity' do
    context 'with all time frame' do
      let(:expected_value) { 4 }
      let(:expected_query) do
        "SELECT COUNT(\"bulk_import_entities\".\"id\") FROM \"bulk_import_entities\""\
        " WHERE \"bulk_import_entities\".\"source_type\" = 1"
      end

      it_behaves_like 'a correct instrumented metric value and query',
        time_frame: 'all',
        options: { source_type: 'project_entity' }
    end

    context 'for 28d time frame' do
      let(:expected_value) { 3 }
      let(:start) { 30.days.ago.to_s(:db) }
      let(:finish) { 2.days.ago.to_s(:db) }
      let(:expected_query) do
        "SELECT COUNT(\"bulk_import_entities\".\"id\") FROM \"bulk_import_entities\""\
        " WHERE \"bulk_import_entities\".\"created_at\" BETWEEN '#{start}' AND '#{finish}'"\
        " AND \"bulk_import_entities\".\"source_type\" = 1"
      end

      it_behaves_like 'a correct instrumented metric value and query',
        time_frame: '28d',
        options: { source_type: 'project_entity' }
    end
  end

  context 'with source_type group_entity' do
    context 'with all time frame' do
      let(:expected_value) { 3 }
      let(:expected_query) do
        "SELECT COUNT(\"bulk_import_entities\".\"id\") FROM \"bulk_import_entities\""\
        " WHERE \"bulk_import_entities\".\"source_type\" = 0"
      end

      it_behaves_like 'a correct instrumented metric value and query',
        time_frame: 'all',
        options: { source_type: 'group_entity' }
    end

    context 'for 28d time frame' do
      let(:expected_value) { 3 }
      let(:start) { 30.days.ago.to_s(:db) }
      let(:finish) { 2.days.ago.to_s(:db) }
      let(:expected_query) do
        "SELECT COUNT(\"bulk_import_entities\".\"id\") FROM \"bulk_import_entities\""\
        " WHERE \"bulk_import_entities\".\"created_at\" BETWEEN '#{start}' AND '#{finish}'"\
        " AND \"bulk_import_entities\".\"source_type\" = 0"
      end

      it_behaves_like 'a correct instrumented metric value and query',
        time_frame: '28d',
        options: { source_type: 'group_entity' }
    end
  end
end