summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/json_cache_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/lib/gitlab/json_cache_spec.rb')
-rw-r--r--spec/lib/gitlab/json_cache_spec.rb88
1 files changed, 42 insertions, 46 deletions
diff --git a/spec/lib/gitlab/json_cache_spec.rb b/spec/lib/gitlab/json_cache_spec.rb
index 7899d01b475..d7d28a94cfe 100644
--- a/spec/lib/gitlab/json_cache_spec.rb
+++ b/spec/lib/gitlab/json_cache_spec.rb
@@ -1,4 +1,5 @@
# frozen_string_literal: true
+# rubocop:disable Style/RedundantFetchBlock
require 'spec_helper'
@@ -8,7 +9,7 @@ RSpec.describe Gitlab::JsonCache do
let(:backend) { double('backend').as_null_object }
let(:namespace) { 'geo' }
let(:key) { 'foo' }
- let(:expanded_key) { "#{namespace}:#{key}:#{Gitlab::VERSION}:#{Rails.version}" }
+ let(:expanded_key) { "#{namespace}:#{key}:#{Gitlab.revision}" }
subject(:cache) { described_class.new(namespace: namespace, backend: backend) }
@@ -35,69 +36,63 @@ RSpec.describe Gitlab::JsonCache do
end
describe '#cache_key' do
- context 'when namespace is not defined' do
- context 'when cache_key_with_version is true' do
- it 'expands out the key with GitLab, and Rails versions' do
- cache = described_class.new(cache_key_with_version: true)
+ using RSpec::Parameterized::TableSyntax
- cache_key = cache.cache_key(key)
-
- expect(cache_key).to eq("#{key}:#{Gitlab::VERSION}:#{Rails.version}")
- end
- end
+ where(:namespace, :cache_key_strategy, :expanded_key) do
+ nil | :revision | "#{key}:#{Gitlab.revision}"
+ nil | :version | "#{key}:#{Gitlab::VERSION}:#{Rails.version}"
+ namespace | :revision | "#{namespace}:#{key}:#{Gitlab.revision}"
+ namespace | :version | "#{namespace}:#{key}:#{Gitlab::VERSION}:#{Rails.version}"
+ end
- context 'when cache_key_with_version is false' do
- it 'returns the key' do
- cache = described_class.new(namespace: nil, cache_key_with_version: false)
+ with_them do
+ let(:cache) { described_class.new(namespace: namespace, cache_key_strategy: cache_key_strategy) }
- cache_key = cache.cache_key(key)
+ subject { cache.cache_key(key) }
- expect(cache_key).to eq(key)
- end
- end
+ it { is_expected.to eq expanded_key }
end
- context 'when namespace is nil' do
- context 'when cache_key_with_version is true' do
- it 'expands out the key with GitLab, and Rails versions' do
- cache = described_class.new(cache_key_with_version: true)
-
- cache_key = cache.cache_key(key)
+ context 'when cache_key_strategy is unknown' do
+ let(:cache) { described_class.new(namespace: namespace, cache_key_strategy: 'unknown') }
- expect(cache_key).to eq("#{key}:#{Gitlab::VERSION}:#{Rails.version}")
- end
+ it 'raises KeyError' do
+ expect { cache.cache_key('key') }.to raise_error(KeyError)
end
+ end
+ end
- context 'when cache_key_with_version is false' do
- it 'returns the key' do
- cache = described_class.new(namespace: nil, cache_key_with_version: false)
+ describe '#namespace' do
+ it 'defaults to nil' do
+ cache = described_class.new
+ expect(cache.namespace).to be_nil
+ end
+ end
- cache_key = cache.cache_key(key)
+ describe '#strategy_key_component' do
+ subject { cache.strategy_key_component }
- expect(cache_key).to eq(key)
- end
- end
+ it 'defaults to Gitlab.revision' do
+ expect(described_class.new.strategy_key_component).to eq Gitlab.revision
end
- context 'when namespace is set' do
- context 'when cache_key_with_version is true' do
- it 'expands out the key with namespace and Rails version' do
- cache = described_class.new(namespace: namespace, cache_key_with_version: true)
+ context 'when cache_key_strategy is :revision' do
+ let(:cache) { described_class.new(cache_key_strategy: :revision) }
- cache_key = cache.cache_key(key)
+ it { is_expected.to eq Gitlab.revision }
+ end
- expect(cache_key).to eq("#{namespace}:#{key}:#{Gitlab::VERSION}:#{Rails.version}")
- end
- end
+ context 'when cache_key_strategy is :version' do
+ let(:cache) { described_class.new(cache_key_strategy: :version) }
- context 'when cache_key_with_version is false' do
- it 'expands out the key with namespace' do
- cache = described_class.new(namespace: namespace, cache_key_with_version: false)
+ it { is_expected.to eq [Gitlab::VERSION, Rails.version] }
+ end
- cache_key = cache.cache_key(key)
+ context 'when cache_key_strategy is invalid' do
+ let(:cache) { described_class.new(cache_key_strategy: 'unknown') }
- expect(cache_key).to eq("#{namespace}:#{key}")
- end
+ it 'raises KeyError' do
+ expect { subject }.to raise_error(KeyError)
end
end
end
@@ -553,3 +548,4 @@ RSpec.describe Gitlab::JsonCache do
end
end
end
+# rubocop:enable Style/RedundantFetchBlock