Commit 8341b2ea authored by alinamihaila's avatar alinamihaila

Improve tests using let

parent 45182200
...@@ -3,66 +3,104 @@ ...@@ -3,66 +3,104 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::Redis::HLL, :clean_gitlab_redis_shared_state do RSpec.describe Gitlab::Redis::HLL, :clean_gitlab_redis_shared_state do
using RSpec::Parameterized::TableSyntax
let(:expiry) { 1.day }
describe '.add' do describe '.add' do
context 'when checking key format' do context 'when checking key format' do
it 'raise an error when using an invalid key format' do context 'for invalid keys' do
[ where(:metric_key, :value) do
'test', 'test' | 1
'test-{metric', 'test-{metric' | 1
'test-{metric}}' 'test-{metric}}' | 1
].each do |key| end
expect { described_class.add(key: key, value: 1, expiry: 1.day) }.to raise_error(Gitlab::Redis::HLL::KeyFormatError)
with_them do
it 'raise an error when using an invalid key format' do
expect { described_class.add(key: metric_key, value: value, expiry: expiry) }.to raise_error(Gitlab::Redis::HLL::KeyFormatError)
end
end end
end end
it "doesn't raise error when having correct format" do context 'for valid keys' do
[ where(:metric_key, :value) do
'test-{metric}', 'test-{metric}' | 1
'test-{metric}-1', 'test-{metric}-1' | 1
'test:{metric}-1', 'test:{metric}-1' | 1
'2020-216-{project_action}', '2020-216-{project_action}' | 1
'i_{analytics}_dev_ops_score-2020-32' 'i_{analytics}_dev_ops_score-2020-32' | 1
].each do |key| end
expect { described_class.add(key: key, value: 1, expiry: 1.day) }.not_to raise_error
with_them do
it "doesn't raise error when having correct format" do
expect { described_class.add(key: metric_key, value: value, expiry: expiry) }.not_to raise_error
end
end end
end end
end end
end end
describe '.count' do describe '.count' do
let(:event_2020_32) { '2020-32-{expand_vulnerabilities}' }
let(:event_2020_33) { '2020-33-{expand_vulnerabilities}' }
let(:event_2020_34) { '2020-34-{expand_vulnerabilities}' }
let(:entity1) { 'user_id_1'}
let(:entity2) { 'user_id_2'}
let(:entity3) { 'user_id_3'}
let(:entity4) { 'user_id_4'}
before do before do
described_class.add(key: '2020-32-{expand_vulnerabilities}', value: "user_id_1", expiry: 1.day) track_event(event_2020_32, entity1)
described_class.add(key: '2020-32-{expand_vulnerabilities}', value: "user_id_1", expiry: 1.day) track_event(event_2020_32, entity1)
described_class.add(key: '2020-32-{expand_vulnerabilities}', value: "user_id_2", expiry: 1.day) track_event(event_2020_32, entity2)
described_class.add(key: '2020-32-{expand_vulnerabilities}', value: "user_id_3", expiry: 1.day) track_event(event_2020_32, entity3)
described_class.add(key: '2020-33-{expand_vulnerabilities}', value: "user_id_3", expiry: 1.day) track_event(event_2020_33, entity3)
described_class.add(key: '2020-33-{expand_vulnerabilities}', value: "user_id_3", expiry: 1.day) track_event(event_2020_33, entity3)
described_class.add(key: '2020-34-{expand_vulnerabilities}', value: "user_id_3", expiry: 1.day) track_event(event_2020_34, entity3)
described_class.add(key: '2020-34-{expand_vulnerabilities}', value: "user_id_2", expiry: 1.day) track_event(event_2020_34, entity2)
end end
it 'has 3 distinct users for weeks 32, 33, 34' do it 'has 3 distinct users for weeks 32, 33, 34' do
expect(described_class.count(keys: ['2020-32-{expand_vulnerabilities}', '2020-33-{expand_vulnerabilities}', '2020-34-{expand_vulnerabilities}'])).to eq(3) unique_counts = count_unique_events([event_2020_32, event_2020_33, event_2020_34])
expect(unique_counts).to eq(3)
end end
it 'has 3 distinct users for weeks 32, 33' do it 'has 3 distinct users for weeks 32, 33' do
expect(described_class.count(keys: ['2020-32-{expand_vulnerabilities}', '2020-33-{expand_vulnerabilities}'])).to eq(3) unique_counts = count_unique_events([event_2020_32, event_2020_33])
expect(unique_counts).to eq(3)
end end
it 'has 2 distinct users for weeks 33, 34' do it 'has 2 distinct users for weeks 33, 34' do
expect(described_class.count(keys: ['2020-33-{expand_vulnerabilities}', '2020-34-{expand_vulnerabilities}'])).to eq(2) unique_counts = count_unique_events([event_2020_33, event_2020_34])
expect(unique_counts).to eq(2)
end end
it 'has one distinct user for week 33' do it 'has one distinct user for week 33' do
expect(described_class.count(keys: ['2020-33-{expand_vulnerabilities}'])).to eq(1) unique_counts = count_unique_events([event_2020_33])
expect(unique_counts).to eq(1)
end end
it 'has 4 distinct users when one different user has an action on week 34' do it 'has 4 distinct users when one different user has an action on week 34' do
described_class.add(key: '2020-34-{expand_vulnerabilities}', value: "user_id_4", expiry: 29.days) track_event(event_2020_34, entity4, 29.days)
unique_counts = count_unique_events([event_2020_32, event_2020_33, event_2020_34])
expect(unique_counts).to eq(4)
end
def track_event(key, value, expiry = 1.day)
described_class.add(key: key, value: value, expiry: expiry)
end
expect(described_class.count(keys: ['2020-32-{expand_vulnerabilities}', '2020-33-{expand_vulnerabilities}', '2020-34-{expand_vulnerabilities}'])).to eq(4) def count_unique_events(keys)
described_class.count(keys: keys)
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment