summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/usage_data_counters/note_counter_spec.rb
blob: b385d1b07c7cbf7fe4b9eae0629c734170b6dcbf (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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::UsageDataCounters::NoteCounter, :clean_gitlab_redis_shared_state do
  shared_examples 'a note usage counter' do |event, noteable_type|
    describe ".count(#{event})" do
      it "increments the Note #{event} counter by 1" do
        expect do
          described_class.count(event, noteable_type)
        end.to change { described_class.read(event, noteable_type) }.by 1
      end
    end

    describe ".read(#{event})" do
      event_count = 5

      it "returns the total number of #{event} events" do
        event_count.times do
          described_class.count(event, noteable_type)
        end

        expect(described_class.read(event, noteable_type)).to eq(event_count)
      end
    end
  end

  it_behaves_like 'a note usage counter', :create, 'Snippet'
  it_behaves_like 'a note usage counter', :create, 'MergeRequest'
  it_behaves_like 'a note usage counter', :create, 'Commit'

  describe '.totals' do
    let(:combinations) do
      [
        [:create, 'Snippet', 3],
        [:create, 'MergeRequest', 4],
        [:create, 'Commit', 5]
      ]
    end

    let(:expected_totals) do
      { snippet_comment: 3,
        merge_request_comment: 4,
        commit_comment:  5 }
    end

    before do
      combinations.each do |event, noteable_type, n|
        n.times do
          described_class.count(event, noteable_type)
        end
      end
    end

    it 'can report all totals' do
      expect(described_class.totals).to include(expected_totals)
    end
  end

  describe 'unknown events or noteable_type' do
    using RSpec::Parameterized::TableSyntax

    let(:unknown_event_error) { Gitlab::UsageDataCounters::BaseCounter::UnknownEvent }

    where(:event, :noteable_type, :expected_count, :should_raise) do
      :create | 'Snippet'      | 1 | false
      :wibble | 'Snippet'      | 0 | true
      :create | 'MergeRequest' | 1 | false
      :wibble | 'MergeRequest' | 0 | true
      :create | 'Commit'       | 1 | false
      :wibble | 'Commit'       | 0 | true
      :create | 'Issue'        | 0 | false
      :wibble | 'Issue'        | 0 | false
    end

    with_them do
      it 'handles event' do
        if should_raise
          expect { described_class.count(event, noteable_type) }.to raise_error(unknown_event_error)
        else
          described_class.count(event, noteable_type)

          expect(described_class.read(event, noteable_type)).to eq(expected_count)
        end
      end
    end
  end
end