diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-07-20 09:55:51 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-07-20 09:55:51 +0000 |
commit | e8d2c2579383897a1dd7f9debd359abe8ae8373d (patch) | |
tree | c42be41678c2586d49a75cabce89322082698334 /spec/lib/gitlab/cache | |
parent | fc845b37ec3a90aaa719975f607740c22ba6a113 (diff) | |
download | gitlab-ce-e8d2c2579383897a1dd7f9debd359abe8ae8373d.tar.gz |
Add latest changes from gitlab-org/gitlab@14-1-stable-eev14.1.0-rc42
Diffstat (limited to 'spec/lib/gitlab/cache')
-rw-r--r-- | spec/lib/gitlab/cache/ci/project_pipeline_status_spec.rb | 41 | ||||
-rw-r--r-- | spec/lib/gitlab/cache/helpers_spec.rb | 49 | ||||
-rw-r--r-- | spec/lib/gitlab/cache/import/caching_spec.rb | 24 |
3 files changed, 78 insertions, 36 deletions
diff --git a/spec/lib/gitlab/cache/ci/project_pipeline_status_spec.rb b/spec/lib/gitlab/cache/ci/project_pipeline_status_spec.rb index 8d625cab1d8..c0e4d1b5355 100644 --- a/spec/lib/gitlab/cache/ci/project_pipeline_status_spec.rb +++ b/spec/lib/gitlab/cache/ci/project_pipeline_status_spec.rb @@ -4,6 +4,7 @@ require 'spec_helper' RSpec.describe Gitlab::Cache::Ci::ProjectPipelineStatus, :clean_gitlab_redis_cache do let_it_be(:project) { create(:project, :repository) } + let(:pipeline_status) { described_class.new(project) } let(:cache_key) { pipeline_status.cache_key } @@ -83,24 +84,8 @@ RSpec.describe Gitlab::Cache::Ci::ProjectPipelineStatus, :clean_gitlab_redis_cac expect(pipeline_status).not_to be_has_cache end - context 'ci_pipeline_status_omit_commit_sha_in_cache_key is enabled' do - before do - stub_feature_flags(ci_pipeline_status_omit_commit_sha_in_cache_key: project) - end - - it 'makes a Gitaly call' do - expect { pipeline_status.load_status }.to change { Gitlab::GitalyClient.get_request_count }.by(1) - end - end - - context 'ci_pipeline_status_omit_commit_sha_in_cache_key is disabled' do - before do - stub_feature_flags(ci_pipeline_status_omit_commit_sha_in_cache_key: false) - end - - it 'makes a Gitaly calls' do - expect { pipeline_status.load_status }.to change { Gitlab::GitalyClient.get_request_count }.by(1) - end + it 'makes a Gitaly call' do + expect { pipeline_status.load_status }.to change { Gitlab::GitalyClient.get_request_count }.by(1) end end @@ -111,24 +96,8 @@ RSpec.describe Gitlab::Cache::Ci::ProjectPipelineStatus, :clean_gitlab_redis_cac expect(pipeline_status).to be_has_cache end - context 'ci_pipeline_status_omit_commit_sha_in_cache_key is enabled' do - before do - stub_feature_flags(ci_pipeline_status_omit_commit_sha_in_cache_key: project) - end - - it 'makes no Gitaly calls' do - expect { pipeline_status.load_status }.to change { Gitlab::GitalyClient.get_request_count }.by(0) - end - end - - context 'ci_pipeline_status_omit_commit_sha_in_cache_key is disabled' do - before do - stub_feature_flags(ci_pipeline_status_omit_commit_sha_in_cache_key: false) - end - - it 'makes a Gitaly calls' do - expect { pipeline_status.load_status }.to change { Gitlab::GitalyClient.get_request_count }.by(1) - end + it 'makes no Gitaly calls' do + expect { pipeline_status.load_status }.to change { Gitlab::GitalyClient.get_request_count }.by(0) end end end diff --git a/spec/lib/gitlab/cache/helpers_spec.rb b/spec/lib/gitlab/cache/helpers_spec.rb new file mode 100644 index 00000000000..08e0d7729bd --- /dev/null +++ b/spec/lib/gitlab/cache/helpers_spec.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Cache::Helpers, :use_clean_rails_redis_caching do + subject(:instance) { Class.new.include(described_class).new } + + let_it_be(:project) { create(:project, :repository) } + let_it_be(:user) { create(:user) } + + let(:presenter) { MergeRequestSerializer.new(current_user: user, project: project) } + + before do + # We have to stub #render as it's a Rails controller method unavailable in + # the module by itself + allow(instance).to receive(:render) { |data| data } + allow(instance).to receive(:current_user) { user } + end + + describe "#render_cached" do + subject do + instance.render_cached(presentable, **kwargs) + end + + let(:kwargs) do + { + with: presenter, + project: project + } + end + + context 'single object' do + let_it_be(:presentable) { create(:merge_request, source_project: project, source_branch: 'wip') } + + it_behaves_like 'object cache helper' + end + + context 'collection of objects' do + let_it_be(:presentable) do + [ + create(:merge_request, source_project: project, source_branch: 'fix'), + create(:merge_request, source_project: project, source_branch: 'master') + ] + end + + it_behaves_like 'collection cache helper' + end + end +end diff --git a/spec/lib/gitlab/cache/import/caching_spec.rb b/spec/lib/gitlab/cache/import/caching_spec.rb index 8ce12f5d32e..f770960e27a 100644 --- a/spec/lib/gitlab/cache/import/caching_spec.rb +++ b/spec/lib/gitlab/cache/import/caching_spec.rb @@ -100,6 +100,30 @@ RSpec.describe Gitlab::Cache::Import::Caching, :clean_gitlab_redis_cache do end end + describe '.hash_add' do + it 'adds a value to a hash' do + described_class.hash_add('foo', 1, 1) + described_class.hash_add('foo', 2, 2) + + key = described_class.cache_key_for('foo') + values = Gitlab::Redis::Cache.with { |r| r.hgetall(key) } + + expect(values).to eq({ '1' => '1', '2' => '2' }) + end + end + + describe '.values_from_hash' do + it 'returns empty hash when the hash is empty' do + expect(described_class.values_from_hash('foo')).to eq({}) + end + + it 'returns the set list of values' do + described_class.hash_add('foo', 1, 1) + + expect(described_class.values_from_hash('foo')).to eq({ '1' => '1' }) + end + end + describe '.write_multiple' do it 'sets multiple keys when key_prefix not set' do mapping = { 'foo' => 10, 'bar' => 20 } |