Do not retry when refreshing cache on deleted OC

This commit is contained in:
Pau Perez
2019-03-06 15:57:18 +01:00
parent 13b2115a29
commit bc34d04c31
2 changed files with 28 additions and 4 deletions

View File

@@ -3,6 +3,8 @@ require 'open_food_network/products_renderer'
RefreshProductsCacheJob = Struct.new(:distributor_id, :order_cycle_id) do
def perform
Rails.cache.write(key, products_json)
rescue ActiveRecord::RecordNotFound
true
end
private

View File

@@ -5,12 +5,34 @@ describe RefreshProductsCacheJob do
let(:distributor) { create(:distributor_enterprise) }
let(:order_cycle) { create(:simple_order_cycle) }
it "renders products and writes them to cache" do
RefreshProductsCacheJob.any_instance.stub(:products_json) { 'products' }
context 'when the enterprise and the order cycle exist' do
it "renders products and writes them to cache" do
RefreshProductsCacheJob.any_instance.stub(:products_json) { 'products' }
run_job RefreshProductsCacheJob.new distributor.id, order_cycle.id
run_job RefreshProductsCacheJob.new distributor.id, order_cycle.id
expect(Rails.cache.read("products-json-#{distributor.id}-#{order_cycle.id}")).to eq 'products'
expect(Rails.cache.read("products-json-#{distributor.id}-#{order_cycle.id}")).to eq 'products'
end
end
context 'when the order cycle does not exist' do
before do
allow(OrderCycle)
.to receive(:find)
.with(order_cycle.id)
.and_raise(ActiveRecord::RecordNotFound)
end
it 'does not raise' do
expect {
run_job RefreshProductsCacheJob.new(distributor.id, order_cycle.id)
}.not_to raise_error(/ActiveRecord::RecordNotFound/)
end
it 'returns true' do
refresh_products_cache_job = RefreshProductsCacheJob.new(distributor.id, order_cycle.id)
expect(refresh_products_cache_job.perform).to eq(true)
end
end
describe "fetching products JSON" do