diff options
Diffstat (limited to 'spec/lib/api/helpers/caching_spec.rb')
-rw-r--r-- | spec/lib/api/helpers/caching_spec.rb | 160 |
1 files changed, 142 insertions, 18 deletions
diff --git a/spec/lib/api/helpers/caching_spec.rb b/spec/lib/api/helpers/caching_spec.rb index a8cd061e123..f94c44c7382 100644 --- a/spec/lib/api/helpers/caching_spec.rb +++ b/spec/lib/api/helpers/caching_spec.rb @@ -2,34 +2,46 @@ require "spec_helper" -RSpec.describe API::Helpers::Caching do +RSpec.describe API::Helpers::Caching, :use_clean_rails_redis_caching do subject(:instance) { Class.new.include(described_class).new } - describe "#present_cached" do - let_it_be(:project) { create(:project) } - let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project) } + let_it_be(:user) { create(:user) } - let(:presenter) { API::Entities::Todo } + let(:presenter) { API::Entities::Todo } - let(:kwargs) do - { - with: presenter, - project: project - } + let(:return_value) do + { + foo: "bar" + } + end + + let(:kwargs) do + { + expires_in: 1.minute + } + end + + before do + # We have to stub #body as it's a Grape method + # unavailable in the module by itself + allow(instance).to receive(:body) do |data| + data end + allow(instance).to receive(:current_user) { user } + end + + describe "#present_cached" do subject do instance.present_cached(presentable, **kwargs) end - before do - # We have to stub #body as it's a Grape method - # unavailable in the module by itself - expect(instance).to receive(:body) do |data| - data - end - - allow(instance).to receive(:current_user) { user } + let(:kwargs) do + { + with: presenter, + project: project + } end context "single object" do @@ -136,4 +148,116 @@ RSpec.describe API::Helpers::Caching do end end end + + describe "#cache_action" do + def perform + instance.cache_action(cache_key, **kwargs) do + expensive_thing.do_very_expensive_action + end + end + + subject { perform } + + let(:expensive_thing) { double(do_very_expensive_action: return_value) } + let(:cache_key) do + [user, :foo] + end + + it { is_expected.to be_a(Gitlab::Json::PrecompiledJson) } + + it "represents the correct data" do + expect(subject.to_s).to eq(Gitlab::Json.dump(return_value).to_s) + end + + it "only calls the expensive action once" do + expected_kwargs = described_class::DEFAULT_CACHE_OPTIONS.merge(kwargs) + + expect(expensive_thing).to receive(:do_very_expensive_action).once + expect(instance.cache).to receive(:fetch).with(cache_key, **expected_kwargs).exactly(5).times.and_call_original + + 5.times { perform } + end + + it "handles nested cache calls" do + nested_call = instance.cache_action(cache_key, **kwargs) do + instance.cache_action([:nested], **kwargs) do + expensive_thing.do_very_expensive_action + end + end + + expect(nested_call.to_s).to eq(subject.to_s) + end + end + + describe "#cache_action_if" do + subject do + instance.cache_action_if(conditional, cache_key, **kwargs) do + return_value + end + end + + let(:cache_key) do + [user, :conditional_if] + end + + context "conditional is truthy" do + let(:conditional) { "truthy thing" } + + it { is_expected.to be_a(Gitlab::Json::PrecompiledJson) } + + it "caches the block" do + expect(instance).to receive(:cache_action).with(cache_key, **kwargs) + + subject + end + end + + context "conditional is falsey" do + let(:conditional) { false } + + it { is_expected.to eq(return_value) } + + it "doesn't cache the block" do + expect(instance).not_to receive(:cache_action).with(cache_key, **kwargs) + + subject + end + end + end + + describe "#cache_action_unless" do + subject do + instance.cache_action_unless(conditional, cache_key, **kwargs) do + return_value + end + end + + let(:cache_key) do + [user, :conditional_unless] + end + + context "conditional is truthy" do + let(:conditional) { "truthy thing" } + + it { is_expected.to eq(return_value) } + + it "doesn't cache the block" do + expect(instance).not_to receive(:cache_action).with(cache_key, **kwargs) + + subject + end + end + + context "conditional is falsey" do + let(:conditional) { false } + + it { is_expected.to be_a(Gitlab::Json::PrecompiledJson) } + + it "caches the block" do + expect(instance).to receive(:cache_action).with(cache_key, **kwargs) + + subject + end + end + end end |