2023-02-22 01:55:31 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-03-19 13:14:57 +01:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-12-01 12:00:41 +01:00
|
|
|
describe Account::Counters do
|
2021-03-19 13:14:57 +01:00
|
|
|
let!(:account) { Fabricate(:account) }
|
|
|
|
|
|
|
|
describe '#increment_count!' do
|
2024-02-07 15:53:29 +01:00
|
|
|
let(:increment_by) { 15 }
|
|
|
|
|
2021-03-19 13:14:57 +01:00
|
|
|
it 'increments the count' do
|
|
|
|
expect(account.followers_count).to eq 0
|
|
|
|
account.increment_count!(:followers_count)
|
|
|
|
expect(account.followers_count).to eq 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'increments the count in multi-threaded an environment' do
|
2024-02-07 15:53:29 +01:00
|
|
|
multi_threaded_execution(increment_by) do
|
|
|
|
account.increment_count!(:statuses_count)
|
2021-03-19 13:14:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(account.statuses_count).to eq increment_by
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#decrement_count!' do
|
2024-02-07 15:53:29 +01:00
|
|
|
let(:decrement_by) { 10 }
|
|
|
|
|
2021-03-19 13:14:57 +01:00
|
|
|
it 'decrements the count' do
|
|
|
|
account.followers_count = 15
|
|
|
|
account.save!
|
|
|
|
expect(account.followers_count).to eq 15
|
|
|
|
account.decrement_count!(:followers_count)
|
|
|
|
expect(account.followers_count).to eq 14
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'decrements the count in multi-threaded an environment' do
|
|
|
|
account.statuses_count = 15
|
|
|
|
account.save!
|
|
|
|
|
2024-02-07 15:53:29 +01:00
|
|
|
multi_threaded_execution(decrement_by) do
|
|
|
|
account.decrement_count!(:statuses_count)
|
2021-03-19 13:14:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(account.statuses_count).to eq 5
|
|
|
|
end
|
2024-04-17 10:16:51 +02:00
|
|
|
|
|
|
|
it 'preserves last_status_at when decrementing statuses_count' do
|
|
|
|
account_stat = Fabricate(
|
|
|
|
:account_stat,
|
|
|
|
account: account,
|
|
|
|
last_status_at: 3.days.ago,
|
|
|
|
statuses_count: 10
|
|
|
|
)
|
|
|
|
|
|
|
|
expect { account.decrement_count!(:statuses_count) }
|
|
|
|
.to change(account_stat.reload, :statuses_count).by(-1)
|
|
|
|
.and not_change(account_stat.reload, :last_status_at)
|
|
|
|
end
|
2021-03-19 13:14:57 +01:00
|
|
|
end
|
|
|
|
end
|