debian-mirror-gitlab/spec/lib/gitlab/usage_data_counters/note_counter_spec.rb

89 lines
2.5 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::UsageDataCounters::NoteCounter, :clean_gitlab_redis_shared_state do
2019-10-12 21:52:04 +05:30
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'
2019-12-04 20:38:33 +05:30
it_behaves_like 'a note usage counter', :create, 'MergeRequest'
it_behaves_like 'a note usage counter', :create, 'Commit'
2019-10-12 21:52:04 +05:30
describe '.totals' do
let(:combinations) do
[
2019-12-04 20:38:33 +05:30
[:create, 'Snippet', 3],
[:create, 'MergeRequest', 4],
[:create, 'Commit', 5]
2019-10-12 21:52:04 +05:30
]
end
let(:expected_totals) do
2019-12-04 20:38:33 +05:30
{ snippet_comment: 3,
merge_request_comment: 4,
commit_comment: 5 }
2019-10-12 21:52:04 +05:30
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
2019-12-04 20:38:33 +05:30
: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
2019-10-12 21:52:04 +05:30
end
with_them do
2019-12-04 20:38:33 +05:30
it 'handles event' do
2019-10-12 21:52:04 +05:30
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