1
0

counters_spec.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Account::Counters do
  4. let!(:account) { Fabricate(:account) }
  5. describe '#increment_count!' do
  6. let(:increment_by) { 15 }
  7. it 'increments the count' do
  8. expect(account.followers_count).to eq 0
  9. account.increment_count!(:followers_count)
  10. expect(account.followers_count).to eq 1
  11. end
  12. it 'increments the count in multi-threaded an environment' do
  13. multi_threaded_execution(increment_by) do
  14. account.increment_count!(:statuses_count)
  15. end
  16. expect(account.statuses_count).to eq increment_by
  17. end
  18. end
  19. describe '#decrement_count!' do
  20. let(:decrement_by) { 10 }
  21. it 'decrements the count' do
  22. account.followers_count = 15
  23. account.save!
  24. expect(account.followers_count).to eq 15
  25. account.decrement_count!(:followers_count)
  26. expect(account.followers_count).to eq 14
  27. end
  28. it 'decrements the count in multi-threaded an environment' do
  29. account.statuses_count = 15
  30. account.save!
  31. multi_threaded_execution(decrement_by) do
  32. account.decrement_count!(:statuses_count)
  33. end
  34. expect(account.statuses_count).to eq 5
  35. end
  36. it 'preserves last_status_at when decrementing statuses_count' do
  37. account_stat = Fabricate(
  38. :account_stat,
  39. account: account,
  40. last_status_at: 3.days.ago,
  41. statuses_count: 10
  42. )
  43. expect { account.decrement_count!(:statuses_count) }
  44. .to change(account_stat.reload, :statuses_count).by(-1)
  45. .and not_change(account_stat.reload, :last_status_at)
  46. end
  47. end
  48. end