diff options
author | Matija Čupić <matteeyah@gmail.com> | 2018-05-11 18:21:17 +0200 |
---|---|---|
committer | Matija Čupić <matteeyah@gmail.com> | 2018-05-11 18:21:17 +0200 |
commit | 4c2b56897e17f884f28cb8824dacb724f85f96fb (patch) | |
tree | 6b025063d258cc967299ea64e7c212f267b53ff8 | |
parent | 8d49ec681ffe4638f4db3311879448958d34c6f3 (diff) | |
download | gitlab-ce-4c2b56897e17f884f28cb8824dacb724f85f96fb.tar.gz |
Add specs for #cached_attr_reader and cached_attr_time_reader
-rw-r--r-- | spec/models/concerns/redis_cacheable_spec.rb | 81 |
1 files changed, 70 insertions, 11 deletions
diff --git a/spec/models/concerns/redis_cacheable_spec.rb b/spec/models/concerns/redis_cacheable_spec.rb index 3d7963120b6..ca09bf75216 100644 --- a/spec/models/concerns/redis_cacheable_spec.rb +++ b/spec/models/concerns/redis_cacheable_spec.rb @@ -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 |