debian-mirror-gitlab/spec/models/concerns/redis_cacheable_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

103 lines
2.5 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe RedisCacheable do
2018-11-08 19:23:39 +05:30
let(:model) do
Struct.new(:id, :attributes) do
def read_attribute(attribute)
attributes[attribute]
end
def cast_value_from_cache(attribute, cached_value)
cached_value
end
def has_attribute?(attribute)
attributes.has_key?(attribute)
end
end
end
let(:payload) { { name: 'value', time: Time.zone.now } }
let(:instance) { model.new(1, payload) }
let(:cache_key) { instance.__send__(:cache_attribute_key) }
2018-03-17 18:26:18 +05:30
before do
2018-11-08 19:23:39 +05:30
model.include(described_class)
2018-03-17 18:26:18 +05:30
end
describe '#cached_attribute' do
2020-03-13 15:44:24 +05:30
subject { instance.cached_attribute(payload.each_key.first) }
2018-03-17 18:26:18 +05:30
it 'gets the cache attribute' do
2020-05-24 23:13:21 +05:30
Gitlab::Redis::Cache.with do |redis|
2018-11-08 19:23:39 +05:30
expect(redis).to receive(:get).with(cache_key)
2018-03-17 18:26:18 +05:30
.and_return(payload.to_json)
end
2020-03-13 15:44:24 +05:30
expect(subject).to eq(payload.each_value.first)
2018-03-17 18:26:18 +05:30
end
end
describe '#cache_attributes' do
2018-11-08 19:23:39 +05:30
subject { instance.cache_attributes(payload) }
2018-03-17 18:26:18 +05:30
it 'sets the cache attributes' do
2020-05-24 23:13:21 +05:30
Gitlab::Redis::Cache.with do |redis|
2018-11-08 19:23:39 +05:30
expect(redis).to receive(:set).with(cache_key, payload.to_json, anything)
2018-03-17 18:26:18 +05:30
end
subject
end
end
2018-11-08 19:23:39 +05:30
2020-05-24 23:13:21 +05:30
describe '#cached_attr_reader', :clean_gitlab_redis_cache do
2018-11-08 19:23:39 +05:30
subject { instance.name }
before do
model.cached_attr_reader(:name)
end
context 'when there is no cached value' do
it 'reads the attribute' do
expect(instance).to receive(:read_attribute).and_call_original
expect(subject).to eq(payload[:name])
end
end
context 'when there is a cached value' do
it 'reads the cached value' do
expect(instance).not_to receive(:read_attribute)
instance.cache_attributes(payload)
expect(subject).to eq(payload[:name])
end
end
it 'always returns the latest values' do
expect(instance.name).to eq(payload[:name])
instance.cache_attributes(name: 'new_value')
expect(instance.name).to eq('new_value')
end
end
describe '#cast_value_from_cache' do
subject { instance.__send__(:cast_value_from_cache, attribute, value) }
context 'with runner contacted_at' do
let(:instance) { Ci::Runner.new }
let(:attribute) { :contacted_at }
let(:value) { '2018-05-07 13:53:08 UTC' }
it 'converts cache string to appropriate type' do
expect(subject).to be_an_instance_of(ActiveSupport::TimeWithZone)
end
end
end
2018-03-17 18:26:18 +05:30
end