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

require 'spec_helper'

describe Gitlab::UsageDataCounters::WikiPageCounter, :clean_gitlab_redis_shared_state do
  shared_examples :wiki_page_event do |event|
    describe ".count(#{event})" do
      it "increments the wiki page #{event} counter by 1" do
        expect do
          described_class.count(event)
        end.to change { described_class.read(event) }.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)
        end

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

  include_examples :wiki_page_event, :create
  include_examples :wiki_page_event, :update
  include_examples :wiki_page_event, :delete

  describe 'totals' do
    creations = 5
    edits = 3
    deletions = 2

    before do
      creations.times do
        described_class.count(:create)
      end
      edits.times do
        described_class.count(:update)
      end
      deletions.times do
        described_class.count(:delete)
      end
    end

    it 'can report all totals' do
      expect(described_class.totals).to include(
        wiki_pages_update: edits,
        wiki_pages_create: creations,
        wiki_pages_delete: deletions
      )
    end
  end

  describe 'unknown events' do
    error = described_class::UnknownEvent

    it 'cannot increment' do
      expect { described_class.count(:wibble) }.to raise_error error
    end

    it 'cannot read' do
      expect { described_class.read(:wibble) }.to raise_error error
    end
  end
end