From 97948e5866a7cfb996128d35192cbf607f8b3818 Mon Sep 17 00:00:00 2001 From: Iain McNulty Date: Wed, 15 May 2024 17:15:15 +0100 Subject: [PATCH] RecruitmentReportScheduler accepts explicit cycle_week This will allow us to run the scheduler for arbitrary weeks from the console. --- ...ecruitment_performance_report_scheduler.rb | 10 +++--- ...tment_performance_report_scheduler_spec.rb | 32 +++++++++++++++---- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/app/models/publications/recruitment_performance_report_scheduler.rb b/app/models/publications/recruitment_performance_report_scheduler.rb index 34ff11f9f65..d4aed3683ab 100644 --- a/app/models/publications/recruitment_performance_report_scheduler.rb +++ b/app/models/publications/recruitment_performance_report_scheduler.rb @@ -1,5 +1,9 @@ module Publications class RecruitmentPerformanceReportScheduler + def initialize(cycle_week: CycleTimetable.current_cycle_week.pred) + @cycle_week = cycle_week + end + def call schedule_national_report schedule_provider_report @@ -7,6 +11,8 @@ def call private + attr_accessor :cycle_week + def schedule_national_report return if Publications::NationalRecruitmentPerformanceReport.exists?(cycle_week:) @@ -25,9 +31,5 @@ def schedule_provider_report ) end end - - def cycle_week - CycleTimetable.current_cycle_week.pred - end end end diff --git a/spec/models/publications/recruitment_performance_report_scheduler_spec.rb b/spec/models/publications/recruitment_performance_report_scheduler_spec.rb index c7f0a1553bc..18750cfc813 100644 --- a/spec/models/publications/recruitment_performance_report_scheduler_spec.rb +++ b/spec/models/publications/recruitment_performance_report_scheduler_spec.rb @@ -4,19 +4,29 @@ let(:provider_worker) { Publications::ProviderRecruitmentPerformanceReportWorker } let(:national_worker) { Publications::NationalRecruitmentPerformanceReportWorker } let(:provider) { create(:provider) } - let(:previous_cycle_week) { CycleTimetable.current_cycle_week.pred } + let(:cycle_week) { CycleTimetable.current_cycle_week.pred } context 'provider report is generated for appropriate providers' do before do allow(provider_worker).to receive(:perform_async) provider - allow(ProvidersForRecruitmentPerformanceReportQuery).to receive(:call).with(cycle_week: previous_cycle_week).and_return(Provider) + allow(ProvidersForRecruitmentPerformanceReportQuery).to receive(:call).with(cycle_week:).and_return(Provider) end it 'creates a report for a provider who received an application last week' do described_class.new.call - expect(provider_worker).to have_received(:perform_async).with(provider.id, previous_cycle_week) + expect(provider_worker).to have_received(:perform_async).with(provider.id, cycle_week) + end + + context 'explicit cycle_week is passed' do + let(:cycle_week) { 2 } + + it 'creates a report for a provider who received an application before cycle_week' do + described_class.new(cycle_week:).call + + expect(provider_worker).to have_received(:perform_async).with(provider.id, cycle_week) + end end end @@ -28,19 +38,29 @@ it 'creates a National report' do described_class.new.call - expect(national_worker).to have_received(:perform_async).with(previous_cycle_week) + expect(national_worker).to have_received(:perform_async).with(cycle_week) end it 'does not create a National report worker when a report already exists' do Publications::NationalRecruitmentPerformanceReport.create!( statistics: {}, publication_date: Time.zone.today, - cycle_week: previous_cycle_week, + cycle_week:, ) described_class.new.call - expect(national_worker).not_to have_received(:perform_async).with(previous_cycle_week) + expect(national_worker).not_to have_received(:perform_async).with(cycle_week) + end + + context 'explicit cycle_week is passed' do + let(:cycle_week) { 2 } + + it 'creates a national report for the cycle_week value' do + described_class.new(cycle_week:).call + + expect(national_worker).to have_received(:perform_async).with(cycle_week) + end end end end