Add specs for #cached_attr_reader and cached_attr_time_reader

This commit is contained in:
Matija Čupić 2018-05-11 18:21:17 +02:00
parent 8d49ec681f
commit 4c2b56897e
No known key found for this signature in database
GPG Key ID: 4BAF84FFACD2E5DE
1 changed files with 70 additions and 11 deletions

View File

@ -1,21 +1,28 @@
require 'spec_helper'
describe RedisCacheable do
let(:model) { double }
let(:model) do
Struct.new(:id, :attributes) do
def read_attribute(attribute)
attributes[attribute]
end
end
end
let(:payload) { { name: 'value' } }
let(:instance) { model.new(1, payload) }
let(:cache_key) { instance.__send__(:cache_attribute_key) }
before do
model.extend(described_class)
allow(model).to receive(:cache_attribute_key).and_return('key')
model.include(described_class)
end
describe '#cached_attribute' do
let(:payload) { { attribute: 'value' } }
subject { model.cached_attribute(payload.keys.first) }
subject { instance.cached_attribute(payload.keys.first) }
it 'gets the cache attribute' do
Gitlab::Redis::SharedState.with do |redis|
expect(redis).to receive(:get).with('key')
expect(redis).to receive(:get).with(cache_key)
.and_return(payload.to_json)
end
@ -24,16 +31,68 @@ describe RedisCacheable do
end
describe '#cache_attributes' do
let(:values) { { name: 'new_name' } }
subject { model.cache_attributes(values) }
subject { instance.cache_attributes(payload) }
it 'sets the cache attributes' do
Gitlab::Redis::SharedState.with do |redis|
expect(redis).to receive(:set).with('key', values.to_json, anything)
expect(redis).to receive(:set).with(cache_key, payload.to_json, anything)
end
subject
end
end
describe '#cached_attr_reader' do
subject { instance.name }
before do
model.cached_attr_reader(:name)
end
context 'when there is no cached value' do
it 'checks the cached value first then reads the attribute' do
expect(instance).to receive(:cached_attribute).and_return(nil)
expect(instance).to receive(:read_attribute).and_return(payload[:name])
expect(subject).to eq(payload[:name])
end
end
context 'when there is a cached value' do
it 'reads the cached value' do
expect(instance).to receive(:cached_attribute).and_return(payload[:name])
expect(instance).not_to receive(:read_attribute)
expect(subject).to eq(payload[:name])
end
end
end
describe '#cached_attr_time_reader' do
subject { instance.time }
before do
model.cached_attr_time_reader(:time)
end
context 'when there is no cached value' do
it 'checks the cached value first then reads the attribute' do
expect(instance).to receive(:cached_attribute).and_return(nil)
expect(instance).to receive(:read_attribute).and_return(Time.zone.now)
expect(subject).to be_instance_of(ActiveSupport::TimeWithZone)
expect(subject).to be_within(1.minute).of(Time.zone.now)
end
end
context 'when there is a cached value' do
it 'reads the cached value' do
expect(instance).to receive(:cached_attribute).and_return(Time.zone.now.to_s)
expect(instance).not_to receive(:read_attribute)
expect(subject).to be_instance_of(ActiveSupport::TimeWithZone)
expect(subject).to be_within(1.minute).of(Time.zone.now)
end
end
end
end