debian-mirror-gitlab/spec/models/concerns/counter_attribute_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

78 lines
2.5 KiB
Ruby
Raw Normal View History

2020-10-24 23:57:45 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe CounterAttribute, :counter_attribute, :clean_gitlab_redis_shared_state do
using RSpec::Parameterized::TableSyntax
let(:project_statistics) { create(:project_statistics) }
let(:model) { CounterAttributeModel.find(project_statistics.id) }
2023-03-04 22:38:38 +05:30
it_behaves_like CounterAttribute, [:build_artifacts_size, :commit_count, :packages_size] do
2020-10-24 23:57:45 +05:30
let(:model) { CounterAttributeModel.find(project_statistics.id) }
end
2023-03-04 22:38:38 +05:30
describe '#counter_attribute_enabled?' do
it 'is true when counter attribute is defined' do
expect(project_statistics.counter_attribute_enabled?(:build_artifacts_size))
.to be_truthy
end
2021-01-03 14:25:43 +05:30
2023-03-04 22:38:38 +05:30
it 'is false when counter attribute is not defined' do
expect(model.counter_attribute_enabled?(:nope)).to be_falsey
2021-01-03 14:25:43 +05:30
end
2023-03-04 22:38:38 +05:30
context 'with a conditional counter attribute' do
[true, false].each do |enabled|
context "where the condition evaluates to #{enabled}" do
subject { model.counter_attribute_enabled?(:packages_size) }
2021-01-03 14:25:43 +05:30
2023-03-04 22:38:38 +05:30
before do
model.allow_package_size_counter = enabled
end
2021-01-03 14:25:43 +05:30
2023-03-04 22:38:38 +05:30
it { is_expected.to eq(enabled) }
end
2021-01-03 14:25:43 +05:30
end
end
2023-03-04 22:38:38 +05:30
end
2021-01-03 14:25:43 +05:30
2023-03-04 22:38:38 +05:30
describe '#counter' do
using RSpec::Parameterized::TableSyntax
2021-01-03 14:25:43 +05:30
2023-03-04 22:38:38 +05:30
it 'returns the counter for the respective attribute' do
expect(model.counter(:build_artifacts_size).send(:attribute)).to eq(:build_artifacts_size)
expect(model.counter(:commit_count).send(:attribute)).to eq(:commit_count)
2021-01-03 14:25:43 +05:30
end
2023-03-04 22:38:38 +05:30
it 'returns the appropriate counter for the attribute' do
expect(model.counter(:build_artifacts_size).class).to eq(Gitlab::Counters::BufferedCounter)
expect(model.counter(:packages_size).class).to eq(Gitlab::Counters::BufferedCounter)
expect(model.counter(:wiki_size).class).to eq(Gitlab::Counters::LegacyCounter)
2020-10-24 23:57:45 +05:30
end
2023-03-04 22:38:38 +05:30
context 'with a conditional counter attribute' do
where(:enabled, :expected_counter_class) do
[
[true, Gitlab::Counters::BufferedCounter],
[false, Gitlab::Counters::LegacyCounter]
]
2020-10-24 23:57:45 +05:30
end
2023-03-04 22:38:38 +05:30
with_them do
before do
model.allow_package_size_counter = enabled
end
2020-10-24 23:57:45 +05:30
2023-03-04 22:38:38 +05:30
it 'returns the appropriate counter based on the condition' do
expect(model.counter(:packages_size).class).to eq(expected_counter_class)
2020-10-24 23:57:45 +05:30
end
end
end
2022-10-11 01:57:18 +05:30
2023-03-04 22:38:38 +05:30
it 'raises error for unknown attribute' do
expect { model.counter(:unknown) }.to raise_error(ArgumentError, 'attribute "unknown" does not exist')
2022-10-11 01:57:18 +05:30
end
end
2020-10-24 23:57:45 +05:30
end