Skip to content

Commit

Permalink
subscriptions factory and test rework
Browse files Browse the repository at this point in the history
  • Loading branch information
starswan committed Nov 28, 2024
1 parent d75fc56 commit e96fe7f
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 170 deletions.
10 changes: 2 additions & 8 deletions spec/controllers/publishers/vacancies/base_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
let!(:vacancy) { create(:vacancy) }

context "when DisableExpensiveJobs is not enabled" do
before { allow(DisableExpensiveJobs).to receive(:enabled?).and_return(false) }

it "does perform the task" do
expect(UpdateGoogleIndexQueueJob).to receive(:perform_later)
controller.send(:update_google_index, vacancy)
end
end

context "when DisableExpensiveJobs is enabled" do
before { allow(DisableExpensiveJobs).to receive(:enabled?).and_return(true) }

context "when DisableExpensiveJobs is enabled", :disable_expensive_jobs do
it "does NOT perform the task" do
expect(UpdateGoogleIndexQueueJob).not_to receive(:perform_later)
controller.send(:update_google_index, vacancy)
Expand All @@ -35,9 +31,7 @@
end
end

context "when DisableExpensiveJobs is enabled" do
before { allow(DisableExpensiveJobs).to receive(:enabled?).and_return(true) }

context "when DisableExpensiveJobs is enabled", :disable_expensive_jobs do
it "does NOT perform the task" do
expect(RemoveGoogleIndexQueueJob).not_to receive(:perform_later)
controller.send(:remove_google_index, vacancy)
Expand Down
54 changes: 46 additions & 8 deletions spec/factories/subscriptions.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,57 @@
FactoryBot.define do
factory :subscription do
transient do
keyword { nil }
location { nil }
radius { nil }
working_patterns { nil }
teaching_job_roles { nil }
support_job_roles { nil }
ect_statuses { nil }
phases { nil }
visa_sponsorship_availability { nil }
subjects { nil }
organisation_slug { nil }
end

email { Faker::Internet.email(domain: "contoso.com") }
frequency { factory_sample(%i[daily weekly]) }
search_criteria do
{ keyword: Faker::Lorem.word,
location: Faker::Address.postcode,
radius: "10",
working_patterns: %w[full_time part_time],
teaching_job_roles: %w[teacher],
support_job_roles: %w[teaching_assistant it_support],
ect_statuses: %w[ect_suitable],
phases: %w[primary] }
{
keyword: keyword,
location: location,
radius: radius,
working_patterns: working_patterns,
teaching_job_roles: teaching_job_roles,
support_job_roles: support_job_roles,
ect_statuses: ect_statuses,
phases: phases,
visa_sponsorship_availability: visa_sponsorship_availability,
subjects: subjects,
organisation_slug: organisation_slug,
}.delete_if { |_k, v| v.nil? }
end
active { true }

trait :with_some_criteria do
keyword { Faker::Lorem.word }
location { Faker::Address.postcode }
radius { "10" }
working_patterns { %w[full_time part_time] }
teaching_job_roles { %w[teacher] }
support_job_roles { %w[teaching_assistant it_support] }
ect_statuses { %w[ect_suitable] }
phases { %w[primary] }
end

trait :visa_sponsorship_required do
visa_sponsorship_availability { ["true"] }
end

trait :ect_suitable do
ect_statuses { ["ect_suitable"] }
end

factory :daily_subscription do
frequency { :daily }
end
Expand Down
8 changes: 1 addition & 7 deletions spec/jobs/export_dsi_users_to_big_query_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
RSpec.describe ExportDSIUsersToBigQueryJob do
subject(:job) { described_class.perform_later }

before { allow(DisableExpensiveJobs).to receive(:enabled?).and_return(disable_expensive_jobs_enabled?) }

context "when DisableExpensiveJobs is not enabled" do
let(:disable_expensive_jobs_enabled?) { false }

it "invokes the libs to export users and approvers to big query" do
export_dsi_users_to_big_query = instance_double(Publishers::DfeSignIn::BigQueryExport::Users)
expect(Publishers::DfeSignIn::BigQueryExport::Users).to receive(:new) { export_dsi_users_to_big_query }
Expand All @@ -21,9 +17,7 @@
end
end

context "when DisableExpensiveJobs is enabled" do
let(:disable_expensive_jobs_enabled?) { true }

context "when DisableExpensiveJobs is enabled", :disable_expensive_jobs do
it "does not perform the job" do
expect(Publishers::DfeSignIn::BigQueryExport::Users).not_to receive(:new)
expect(Publishers::DfeSignIn::BigQueryExport::Approvers).not_to receive(:new)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
allow(Vacancies::Export::DwpFindAJob::ClosedEarly).to receive(:new).and_return(service)
end

context "when DisableExpensiveJobs is enabled" do
before { allow(DisableExpensiveJobs).to receive(:enabled?).and_return(true) }

context "when DisableExpensiveJobs is enabled", :disable_expensive_jobs do
it "does not call the service" do
perform_enqueued_jobs { job }
expect(Vacancies::Export::DwpFindAJob::ClosedEarly).not_to have_received(:new)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
allow(Vacancies::Export::DwpFindAJob::PublishedAndUpdated).to receive(:new).and_return(service)
end

context "when DisableExpensiveJobs is enabled" do
before { allow(DisableExpensiveJobs).to receive(:enabled?).and_return(true) }

context "when DisableExpensiveJobs is enabled", :disable_expensive_jobs do
it "does not call the service" do
perform_enqueued_jobs { job }
expect(Vacancies::Export::DwpFindAJob::PublishedAndUpdated).not_to have_received(:new)
Expand Down
4 changes: 1 addition & 3 deletions spec/jobs/import_from_vacancy_sources_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def self.source_name
end
end

context "when DisableExpensiveJobs is enabled" do
before { allow(DisableExpensiveJobs).to receive(:enabled?).and_return(true) }

context "when DisableExpensiveJobs is enabled", :disable_expensive_jobs do
it "does not enqueue any jobs" do
expect { described_class.perform_now }.not_to have_enqueued_job(ImportFromVacancySourceJob)
end
Expand Down
8 changes: 1 addition & 7 deletions spec/jobs/remove_invalid_subscriptions_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
require "rails_helper"

RSpec.describe RemoveInvalidSubscriptionsJob do
before { allow(DisableExpensiveJobs).to receive(:enabled?).and_return(disable_expensive_jobs_enabled?) }

context "when DisableExpensiveJobs is not enabled" do
let(:disable_expensive_jobs_enabled?) { false }

let(:email_address1) { Faker::Internet.email(domain: TEST_EMAIL_DOMAIN) }
let(:email_address2) { Faker::Internet.email(domain: TEST_EMAIL_DOMAIN) }
let(:email_address3) { Faker::Internet.email(domain: TEST_EMAIL_DOMAIN) }
Expand Down Expand Up @@ -38,9 +34,7 @@
end
end

context "when DisabledExpensiveJobs is enabled" do
let(:disable_expensive_jobs_enabled?) { true }

context "when DisabledExpensiveJobs is enabled", :disable_expensive_jobs do
it "does not perform the job" do
expect(Notifications::Client).not_to receive(:new)

Expand Down
174 changes: 132 additions & 42 deletions spec/jobs/send_daily_alert_email_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,150 @@
RSpec.describe SendDailyAlertEmailJob do
subject(:job) { described_class.perform_later }

let(:search_criteria) do
{
subject: "English",
working_patterns: %w[full_time],
phases: %w[primary secondary],
}
end
let(:mail) { double(:mail) }

let!(:subscription) { create(:subscription, search_criteria: search_criteria, frequency: :daily) }
let!(:vacancies) { create_list(:vacancy, 5, :published_slugged) }
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)
end

let(:mail) { double(:mail) }
let(:new_org) { create(:school) }
let(:teacher_vacancy) { Vacancy.find_by!(contact_number: "1") }
let(:support_vacancy) { Vacancy.find_by!(contact_number: "2") }
let(:visa_job) { Vacancy.find_by!(contact_number: "3") }
let(:ect_job) { Vacancy.find_by!(contact_number: "4") }
let(:french_job) { Vacancy.find_by!(contact_number: "5") }
let(:primary_job) { Vacancy.find_by!(contact_number: "6") }
let(:part_time_job) { Vacancy.find_by!(contact_number: "7") }
let(:new_org_job) { Vacancy.find_by!(contact_number: "8") }
let(:nice_job) { Vacancy.find_by!(contact_number: "9") }

context "with vacancies" do
before do
allow_any_instance_of(described_class).to receive(:vacancies_for_subscription) { vacancies }
allow(DisableExpensiveJobs).to receive(:enabled?).and_return(false)
end
let(:vacancies) { [teacher_vacancy, support_vacancy, visa_job, ect_job, french_job, primary_job, part_time_job, new_org_job, nice_job] }

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 }
perform_enqueued_jobs { job }
end
context "with keyword" do
let(:subscription) { create(:subscription, keyword: "nice", frequency: :daily) }

context "when a run exists" do
let!(:run) { subscription.alert_runs.create(run_on: Date.current) }
it "only finds the nice job" do
expect(Jobseekers::AlertMailer).to receive(:alert).with(subscription.id, [nice_job].pluck(:id)) { mail }
expect(mail).to receive(:deliver_later) { ActionMailer::MailDeliveryJob.new }
perform_enqueued_jobs { job }
end
end

it "does not send another email" do
expect(Jobseekers::AlertMailer).to_not receive(:alert)
perform_enqueued_jobs { job }
context "with teaching job roles" do
let(:subscription) { create(:subscription, teaching_job_roles: %w[teacher], frequency: :daily) }

it "only finds the teaching job" do
expect(Jobseekers::AlertMailer).to receive(:alert).with(subscription.id, [teacher_vacancy, ect_job].pluck(:id)) { mail }
expect(mail).to receive(:deliver_later) { ActionMailer::MailDeliveryJob.new }
perform_enqueued_jobs { job }
end
end
end

context "with no vacancies" do
before do
allow_any_instance_of(described_class).to receive(:vacancies_for_subscription) { [] }
context "with support job roles" do
let(:subscription) { create(:subscription, support_job_roles: %w[it_support], frequency: :daily) }

it "only finds the support job" do
expect(Jobseekers::AlertMailer).to receive(:alert).with(subscription.id, [support_vacancy].pluck(:id)) { mail }
expect(mail).to receive(:deliver_later) { ActionMailer::MailDeliveryJob.new }
perform_enqueued_jobs { job }
end
end

it "does not send an email" do
expect(Jobseekers::AlertMailer).to_not receive(:alert)
perform_enqueued_jobs { job }
context "with visa sponsorship" do
let(:subscription) { create(:subscription, :visa_sponsorship_required, frequency: :daily) }

it "only finds the visa job" do
expect(Jobseekers::AlertMailer).to receive(:alert).with(subscription.id, [visa_job].pluck(:id)) { mail }
expect(mail).to receive(:deliver_later) { ActionMailer::MailDeliveryJob.new }
perform_enqueued_jobs { job }
end
end

context "with ECT" do
let(:subscription) { create(:subscription, :ect_suitable, frequency: :daily) }

it "only finds the ECT job" do
expect(Jobseekers::AlertMailer).to receive(:alert).with(subscription.id, [ect_job].pluck(:id)) { mail }
expect(mail).to receive(:deliver_later) { ActionMailer::MailDeliveryJob.new }
perform_enqueued_jobs { job }
end
end

context "with subjects filter" do
let(:subscription) { create(:subscription, subjects: %w[French], frequency: :daily) }

it "only finds the Maths job" do
expect(Jobseekers::AlertMailer).to receive(:alert).with(subscription.id, [french_job].pluck(:id)) { mail }
expect(mail).to receive(:deliver_later) { ActionMailer::MailDeliveryJob.new }
perform_enqueued_jobs { job }
end
end

it "does not create a run" do
context "with phases filter" do
let(:subscription) { create(:subscription, phases: %w[primary], frequency: :daily) }

it "only finds the primary school job" do
expect(Jobseekers::AlertMailer).to receive(:alert).with(subscription.id, [primary_job].pluck(:id)) { mail }
expect(mail).to receive(:deliver_later) { ActionMailer::MailDeliveryJob.new }
perform_enqueued_jobs { job }
end
end

context "with working patterns filter" do
let(:subscription) { create(:subscription, working_patterns: %w[part_time], frequency: :daily) }

it "only finds the part_time job" do
expect(Jobseekers::AlertMailer).to receive(:alert).with(subscription.id, [part_time_job].pluck(:id)) { mail }
expect(mail).to receive(:deliver_later) { ActionMailer::MailDeliveryJob.new }
perform_enqueued_jobs { job }
end
end

context "with organisation filter" do
let(:subscription) { create(:subscription, organisation_slug: new_org.slug, frequency: :daily) }

it "only finds the new_publisher job" do
expect(Jobseekers::AlertMailer).to receive(:alert).with(subscription.id, [new_org_job].pluck(:id)) { mail }
expect(mail).to receive(:deliver_later) { ActionMailer::MailDeliveryJob.new }
perform_enqueued_jobs { job }
end
end

context "with no subscription criteria" 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 }
perform_enqueued_jobs { job }
end

context "when a run exists" do
let!(:run) { subscription.alert_runs.create(run_on: Date.current) }

it "does not send another email" do
expect(Jobseekers::AlertMailer).to_not receive(:alert)
perform_enqueued_jobs { job }
end
end
end
end

context "with no vacancies" do
let(:subscription) { create(:subscription, frequency: :daily) }

it "does not send an email or create a run" do
expect(Jobseekers::AlertMailer).to_not receive(:alert)
perform_enqueued_jobs { job }
expect(subscription.alert_runs.count).to eq(0)
end
Expand All @@ -64,13 +163,4 @@
job.subscriptions
end
end

describe "#vacancies_for_subscription" do
let(:job) { described_class.new }

it "gets vacancies in the last day" do
expect(subscription).to receive(:vacancies_for_range).with(Time.zone.yesterday, Date.current) { Vacancy.none }
job.vacancies_for_subscription(subscription)
end
end
end
Loading

0 comments on commit e96fe7f

Please sign in to comment.