debian-mirror-gitlab/spec/lib/gitlab/counters/legacy_counter_spec.rb

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

55 lines
1.8 KiB
Ruby
Raw Normal View History

2023-03-04 22:38:38 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Counters::LegacyCounter do
subject(:counter) { described_class.new(counter_record, attribute) }
2023-03-17 16:20:25 +05:30
let_it_be(:counter_record, reload: true) { create(:project_statistics) }
2023-03-04 22:38:38 +05:30
let(:attribute) { :snippets_size }
2023-03-17 16:20:25 +05:30
let(:increment) { Gitlab::Counters::Increment.new(amount: 123) }
let(:other_increment) { Gitlab::Counters::Increment.new(amount: 100) }
2023-03-04 22:38:38 +05:30
describe '#increment' do
it 'increments the attribute in the counter record' do
2023-03-17 16:20:25 +05:30
expect { counter.increment(increment) }
.to change { counter_record.reload.method(attribute).call }.by(increment.amount)
2023-03-04 22:38:38 +05:30
end
it 'returns the value after the increment' do
2023-03-17 16:20:25 +05:30
counter.increment(other_increment)
2023-03-04 22:38:38 +05:30
2023-03-17 16:20:25 +05:30
expect(counter.increment(increment)).to eq(other_increment.amount + increment.amount)
2023-03-04 22:38:38 +05:30
end
it 'executes after counter_record after commit callback' do
expect(counter_record).to receive(:execute_after_commit_callbacks).and_call_original
2023-03-17 16:20:25 +05:30
counter.increment(increment)
2023-03-04 22:38:38 +05:30
end
end
2023-03-17 16:20:25 +05:30
describe '#bulk_increment' do
let(:increments) { [Gitlab::Counters::Increment.new(amount: 123), Gitlab::Counters::Increment.new(amount: 456)] }
it 'increments the attribute in the counter record' do
expect { counter.bulk_increment(increments) }
.to change { counter_record.reload.method(attribute).call }.by(increments.sum(&:amount))
end
it 'returns the value after the increment' do
counter.increment(other_increment)
expect(counter.bulk_increment(increments)).to eq(other_increment.amount + increments.sum(&:amount))
2023-03-04 22:38:38 +05:30
end
2023-03-17 16:20:25 +05:30
it 'executes after counter_record after commit callback' do
expect(counter_record).to receive(:execute_after_commit_callbacks).and_call_original
2023-03-04 22:38:38 +05:30
2023-03-17 16:20:25 +05:30
counter.bulk_increment(increments)
2023-03-04 22:38:38 +05:30
end
end
end