From fa308f05661d0dfe08cc0a1f08529eae52dd4eaa Mon Sep 17 00:00:00 2001 From: starswan Date: Thu, 28 Nov 2024 10:31:08 +0000 Subject: [PATCH] tidy test and move ordering check to different context --- app/jobs/alert_email/base.rb | 2 +- spec/jobs/send_daily_alert_email_job_spec.rb | 52 +++++++++++--------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/app/jobs/alert_email/base.rb b/app/jobs/alert_email/base.rb index c3c307f510..c7ce3acb74 100644 --- a/app/jobs/alert_email/base.rb +++ b/app/jobs/alert_email/base.rb @@ -17,7 +17,7 @@ def perform # rubocop:disable Metrics/AbcSize # The intent here is that if we don't have keyword or location searches, then this operation can all be done in memory # really fast (1 week's worth of vacancies is around 2000, so not worth leaving on disk for each of 100k daily subscriptions - default_scope = Vacancy.includes(:organisations).live.search_by_filter(from_date: from_date, to_date: Date.current) + default_scope = Vacancy.includes(:organisations).live.order(publish_on: :desc).search_by_filter(from_date: from_date, to_date: Date.current) already_run_ids = AlertRun.for_today.map(&:subscription_id) diff --git a/spec/jobs/send_daily_alert_email_job_spec.rb b/spec/jobs/send_daily_alert_email_job_spec.rb index 43eb411fc3..0512b945d1 100644 --- a/spec/jobs/send_daily_alert_email_job_spec.rb +++ b/spec/jobs/send_daily_alert_email_job_spec.rb @@ -8,15 +8,15 @@ describe "#perform" do context "with vacancies" do before do - create(:vacancy, :published_slugged, contact_number: "1", ect_status: :ect_unsuitable, job_roles: %w[teacher], subjects: %w[English], phases: %w[secondary], working_patterns: %w[full_time], created_at: 1.day.ago + 1.minute) - create(:vacancy, :published_slugged, contact_number: "2", job_roles: %w[it_support], subjects: %w[English], phases: %w[secondary], working_patterns: %w[full_time], created_at: 1.day.ago + 2.minutes) - create(:vacancy, :published_slugged, contact_number: "3", visa_sponsorship_available: true, job_roles: %w[headteacher], subjects: %w[English], phases: %w[secondary], working_patterns: %w[full_time], created_at: 1.day.ago + 3.minutes) - create(:vacancy, :published_slugged, contact_number: "4", ect_status: :ect_suitable, job_roles: %w[headteacher teacher], subjects: %w[English], phases: %w[secondary], working_patterns: %w[full_time], created_at: 1.day.ago + 4.minutes) - create(:vacancy, :published_slugged, contact_number: "5", job_roles: %w[headteacher], subjects: %w[French], phases: %w[secondary], working_patterns: %w[full_time], created_at: 1.day.ago + 5.minutes) - create(:vacancy, :published_slugged, contact_number: "6", job_roles: %w[headteacher], phases: %w[primary], working_patterns: %w[full_time], created_at: 1.day.ago + 6.minutes) - create(:vacancy, :published_slugged, contact_number: "7", job_roles: %w[headteacher], phases: %w[secondary], working_patterns: %w[part_time], created_at: 1.day.ago + 7.minutes) - create(:vacancy, :published_slugged, contact_number: "8", organisations: [new_org], job_roles: %w[headteacher], phases: %w[secondary], working_patterns: %w[full_time], created_at: 1.day.ago + 8.minutes) - create(:vacancy, :published_slugged, contact_number: "9", job_title: "This is a nice job", job_roles: %w[headteacher], phases: %w[secondary], working_patterns: %w[full_time], created_at: 1.day.ago + 9.minutes) + create(:vacancy, :published_slugged, contact_number: "1", ect_status: :ect_unsuitable, job_roles: %w[teacher], subjects: %w[English], phases: %w[secondary], working_patterns: %w[full_time]) + create(:vacancy, :published_slugged, contact_number: "2", job_roles: %w[it_support], subjects: %w[English], phases: %w[secondary], working_patterns: %w[full_time]) + create(:vacancy, :published_slugged, contact_number: "3", visa_sponsorship_available: true, job_roles: %w[headteacher], subjects: %w[English], phases: %w[secondary], working_patterns: %w[full_time]) + create(:vacancy, :published_slugged, contact_number: "4", ect_status: :ect_suitable, job_roles: %w[headteacher teacher], subjects: %w[English], phases: %w[secondary], working_patterns: %w[full_time], publish_on: 1.days.ago) + create(:vacancy, :published_slugged, contact_number: "5", job_roles: %w[headteacher], subjects: %w[French], phases: %w[secondary], working_patterns: %w[full_time]) + create(:vacancy, :published_slugged, contact_number: "6", job_roles: %w[headteacher], phases: %w[primary], working_patterns: %w[full_time]) + create(:vacancy, :published_slugged, contact_number: "7", job_roles: %w[headteacher], phases: %w[secondary], working_patterns: %w[part_time]) + create(:vacancy, :published_slugged, contact_number: "8", organisations: [new_org], job_roles: %w[headteacher], phases: %w[secondary], working_patterns: %w[full_time]) + create(:vacancy, :published_slugged, contact_number: "9", job_title: "This is a nice job", job_roles: %w[headteacher], phases: %w[secondary], working_patterns: %w[full_time]) end let(:new_org) { create(:school) } @@ -30,8 +30,6 @@ let(:new_org_job) { Vacancy.find_by!(contact_number: "8") } let(:nice_job) { Vacancy.find_by!(contact_number: "9") } - let(:vacancies) { [teacher_vacancy, support_vacancy, visa_job, ect_job, french_job, primary_job, part_time_job, new_org_job, nice_job] } - context "with keyword" do let(:subscription) { create(:subscription, keyword: "nice", frequency: :daily) } @@ -122,23 +120,33 @@ end end - context "with no subscription criteria" do + context "when a run exists" do let(:subscription) { create(:subscription, frequency: :daily) } - it "sends an email" do - expect(Jobseekers::AlertMailer).to receive(:alert).with(subscription.id, vacancies.pluck(:id)) { mail } - expect(mail).to receive(:deliver_later) { ActionMailer::MailDeliveryJob.new } + before do + subscription.alert_runs.create(run_on: Date.current) + end + + it "does not send another email" do + expect(Jobseekers::AlertMailer).to_not receive(:alert) perform_enqueued_jobs { job } end + end + end - context "when a run exists" do - let!(:run) { subscription.alert_runs.create(run_on: Date.current) } + context "with old and new vacancies" do + before do + create(:vacancy, :published_slugged, contact_number: "2", publish_on: Date.current) + create(:vacancy, :published_slugged, contact_number: "1", publish_on: 1.day.ago) + end - it "does not send another email" do - expect(Jobseekers::AlertMailer).to_not receive(:alert) - perform_enqueued_jobs { job } - end - end + let(:expected_vacancies) { [Vacancy.find_by!(contact_number: "2"), Vacancy.find_by!(contact_number: "1")]} + let(:subscription) { create(:subscription, frequency: :daily) } + + it "sends the vacancies in publish order descending" do + expect(Jobseekers::AlertMailer).to receive(:alert).with(subscription.id, expected_vacancies.pluck(:id)) { mail } + expect(mail).to receive(:deliver_later) { ActionMailer::MailDeliveryJob.new } + perform_enqueued_jobs { job } end end