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

note_counter_spec.rb « usage_data_counters « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1669a22879f535bb1e2d2070b0e5a4cb42eb213b (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
# 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'

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

    let(:expected_totals) do
      { snippet_comment: 3 }
    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 | '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