1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
# frozen_string_literal: true
require 'spec_helper'
if !mongoid? && active_record?
describe Bullet::Detector::CounterCache do
before(:each) { Bullet.start_request }
after(:each) { Bullet.end_request }
it 'should need counter cache with all cities' do
Country.all.each { |country| country.cities.size }
expect(Bullet.collected_counter_cache_notifications).not_to be_empty
end
it 'should not need counter cache if already define counter_cache' do
Person.all.each { |person| person.pets.size }
expect(Bullet.collected_counter_cache_notifications).to be_empty
end
it 'should not need counter cache with only one object' do
Country.first.cities.size
expect(Bullet.collected_counter_cache_notifications).to be_empty
end
it 'should not need counter cache without size' do
Country.includes(:cities).each { |country| country.cities.empty? }
expect(Bullet.collected_counter_cache_notifications).to be_empty
end
if ActiveRecord::VERSION::MAJOR > 4
it 'should not need counter cache for has_many through' do
Client.all.each { |client| client.firms.size }
expect(Bullet.collected_counter_cache_notifications).to be_empty
end
else
it 'should need counter cache for has_many through' do
Client.all.each { |client| client.firms.size }
expect(Bullet.collected_counter_cache_notifications).not_to be_empty
end
end
it 'should not need counter cache with part of cities' do
Country.all.each { |country| country.cities.where(name: 'first').size }
expect(Bullet.collected_counter_cache_notifications).to be_empty
end
context 'disable' do
before { Bullet.counter_cache_enable = false }
after { Bullet.counter_cache_enable = true }
it 'should not detect counter cache' do
Country.all.each { |country| country.cities.size }
expect(Bullet.collected_counter_cache_notifications).to be_empty
end
end
context 'safelist' do
before { Bullet.add_safelist type: :counter_cache, class_name: 'Country', association: :cities }
after { Bullet.clear_safelist }
it 'should not detect counter cache' do
Country.all.each { |country| country.cities.size }
expect(Bullet.collected_counter_cache_notifications).to be_empty
end
end
describe 'with count' do
it 'should need counter cache' do
Country.all.each { |country| country.cities.count }
expect(Bullet.collected_counter_cache_notifications).not_to be_empty
end
it 'should notify even with counter cache' do
Person.all.each { |person| person.pets.count }
expect(Bullet.collected_counter_cache_notifications).not_to be_empty
end
if ActiveRecord::VERSION::MAJOR > 4
it 'should not need counter cache for has_many through' do
Client.all.each { |client| client.firms.count }
expect(Bullet.collected_counter_cache_notifications).to be_empty
end
else
it 'should need counter cache for has_many through' do
Client.all.each { |client| client.firms.count }
expect(Bullet.collected_counter_cache_notifications).not_to be_empty
end
end
end
end
end
|