From 509c1f01cc9541e6eb6a0315a5cf5d76a99b61d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Mon, 5 Feb 2018 00:33:29 +0100 Subject: Add specs for AttributeCacheable --- app/models/concerns/attribute_cacheable.rb | 10 ++++--- spec/models/concerns/attribute_cacheable_spec.rb | 35 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 spec/models/concerns/attribute_cacheable_spec.rb diff --git a/app/models/concerns/attribute_cacheable.rb b/app/models/concerns/attribute_cacheable.rb index 34617489ac5..9ddef3c033b 100644 --- a/app/models/concerns/attribute_cacheable.rb +++ b/app/models/concerns/attribute_cacheable.rb @@ -11,10 +11,6 @@ module AttributeCacheable end end - def cache_attribute_key(key) - "#{self.class.name}:attributes:#{self.id}:#{key}" - end - def cached_attribute(key) Gitlab::Redis::SharedState.with do |redis| redis.get(cache_attribute_key(key)) @@ -28,4 +24,10 @@ module AttributeCacheable end end end + + private + + def cache_attribute_key(key) + "#{self.class.name}:attributes:#{self.id}:#{key}" + end end diff --git a/spec/models/concerns/attribute_cacheable_spec.rb b/spec/models/concerns/attribute_cacheable_spec.rb new file mode 100644 index 00000000000..738c3a9ca8c --- /dev/null +++ b/spec/models/concerns/attribute_cacheable_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +describe AttributeCacheable do + let(:runner) { create(:ci_runner) } + + describe '#cached_attribute' do + let(:key) { 'test_key' } + + subject { runner.cached_attribute(key) } + + it 'gets the cache attribute' do + Gitlab::Redis::SharedState.with do |redis| + expect(redis).to receive(:get).with(runner.send(:cache_attribute_key, key)) + end + + subject + end + end + + describe '#cache_attributes' do + let(:values) { { name: 'new_name' } } + + subject { runner.cache_attributes(values) } + + it 'sets the cache attributes' do + Gitlab::Redis::SharedState.with do |redis| + values.each do |key, value| + expect(redis).to receive(:set).with(runner.send(:cache_attribute_key, key), value, anything) + end + end + + subject + end + end +end -- cgit v1.2.1