Skip to content

Commit

Permalink
tidy test and move ordering check to different context
Browse files Browse the repository at this point in the history
  • Loading branch information
starswan committed Nov 28, 2024
1 parent b5b9b28 commit fa308f0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/jobs/alert_email/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
52 changes: 30 additions & 22 deletions spec/jobs/send_daily_alert_email_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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) }

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit fa308f0

Please sign in to comment.