gitlab-org--gitlab-foss/spec/services/base_count_service_spec.rb
Yorick Peterse 3e561736b2
Cache the number of user SSH keys
By caching the number of personal SSH keys we reduce the number of
queries necessary on pages such as ProjectsController#show (which can
end up querying this data multiple times).

The cache is refreshed/flushed whenever an SSH key is added, removed, or
when a user is removed.
2017-11-16 14:59:38 +01:00

80 lines
1.8 KiB
Ruby

require 'spec_helper'
describe BaseCountService, :use_clean_rails_memory_store_caching do
let(:service) { described_class.new }
describe '#relation_for_count' do
it 'raises NotImplementedError' do
expect { service.relation_for_count }.to raise_error(NotImplementedError)
end
end
describe '#count' do
it 'returns the number of values' do
expect(service)
.to receive(:cache_key)
.and_return('foo')
expect(service)
.to receive(:uncached_count)
.and_return(5)
expect(service.count).to eq(5)
end
end
describe '#uncached_count' do
it 'returns the uncached number of values' do
expect(service)
.to receive(:relation_for_count)
.and_return(double(:relation, count: 5))
expect(service.uncached_count).to eq(5)
end
end
describe '#refresh_cache' do
it 'refreshes the cache' do
allow(service)
.to receive(:cache_key)
.and_return('foo')
allow(service)
.to receive(:uncached_count)
.and_return(4)
service.refresh_cache
expect(Rails.cache.fetch(service.cache_key, raw: service.raw?)).to eq(4)
end
end
describe '#delete_cache' do
it 'deletes the cache' do
allow(service)
.to receive(:cache_key)
.and_return('foo')
allow(service)
.to receive(:uncached_count)
.and_return(4)
service.refresh_cache
service.delete_cache
expect(Rails.cache.fetch(service.cache_key, raw: service.raw?)).to be_nil
end
end
describe '#raw?' do
it 'returns false' do
expect(service.raw?).to eq(false)
end
end
describe '#cache_key' do
it 'raises NotImplementedError' do
expect { service.cache_key }.to raise_error(NotImplementedError)
end
end
end