debian-mirror-gitlab/spec/lib/gitlab/cache/helpers_spec.rb

76 lines
2.1 KiB
Ruby
Raw Normal View History

2021-09-30 23:02:18 +05:30
# 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
2022-10-11 01:57:18 +05:30
let(:method) { :render_cached }
2021-09-30 23:02:18 +05:30
let(:kwargs) do
{
with: presenter,
project: project
}
end
2022-10-11 01:57:18 +05:30
subject do
instance.public_send(method, presentable, **kwargs)
end
2021-09-30 23:02:18 +05:30
context 'single object' do
let_it_be(:presentable) { create(:merge_request, source_project: project, source_branch: 'wip') }
2023-04-23 21:23:45 +05:30
context 'when presenter is a serializer' do
let(:expected_cache_key_prefix) { 'MergeRequestSerializer' }
it_behaves_like 'object cache helper'
end
context 'when presenter is a Grape::Entity' do
let(:presenter) { API::Entities::MergeRequest }
let(:expected_cache_key_prefix) { 'API::Entities::MergeRequest' }
it_behaves_like 'object cache helper'
end
2021-09-30 23:02:18 +05:30
end
context 'collection of objects' do
2023-04-23 21:23:45 +05:30
let(:expected_cache_key_prefix) { 'MergeRequestSerializer' }
2021-09-30 23:02:18 +05:30
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
2023-04-23 21:23:45 +05:30
context 'when passed presenter is not a serializer or an entity' do
let(:presenter) { User }
let_it_be(:presentable) do
create(:merge_request, source_project: project, source_branch: 'master')
end
it 'throws an exception' do
expect { subject }.to raise_exception(ArgumentError, "presenter User is not supported")
end
end
2021-09-30 23:02:18 +05:30
end
end