Skip to content

Commit

Permalink
Don't create parents by default in tests (#2743)
Browse files Browse the repository at this point in the history
This simplifies the patient factory to not create parents by default to
improve test performance and match production where patients won't
necessarily always have parents attached to them.
  • Loading branch information
thomasleese authored Dec 13, 2024
2 parents 0bcb2f4 + 8b1d534 commit d8bd0d1
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 115 deletions.
22 changes: 16 additions & 6 deletions spec/controllers/concerns/vaccination_mailer_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ def initialize(current_user:)

let(:programme) { create(:programme) }
let(:session) { create(:session, programme:) }
let(:consent) { create(:consent, :given, programme:) }
let(:patient) { create(:patient, consents: [consent]) }
let(:parent) { consent.parent || patient.parents.first }
let(:parent) { create(:parent) }
let(:patient) { create(:patient, parents: [parent]) }
let(:patient_session) { create(:patient_session, session:, patient:) }
let(:vaccination_record) do
create(:vaccination_record, programme:, patient_session:)
end

context "when the vaccination has taken place" do
before { create(:consent, :given, patient:, programme:) }

it "sends an email" do
expect { send_vaccination_confirmation }.to have_enqueued_mail(
VaccinationMailer,
Expand All @@ -54,6 +55,8 @@ def initialize(current_user:)
end

context "when the vaccination hasn't taken place" do
before { create(:consent, :given, patient:, programme:) }

let(:vaccination_record) do
create(
:vaccination_record,
Expand Down Expand Up @@ -90,8 +93,15 @@ def initialize(current_user:)
end

context "when child wants parents to be notified" do
let(:consent) do
create(:consent, :given, :self_consent, :notify_parents, programme:)
before do
create(
:consent,
:given,
:self_consent,
:notify_parents,
patient:,
programme:
)
end

it "sends an email" do
Expand All @@ -116,7 +126,7 @@ def initialize(current_user:)
end

context "when child doesn't want a parent to be notified" do
let(:consent) { create(:consent, :given, :self_consent, programme:) }
before { create(:consent, :given, :self_consent, patient:, programme:) }

it "doesn't send an email" do
expect { send_vaccination_confirmation }.not_to have_enqueued_mail
Expand Down
5 changes: 4 additions & 1 deletion spec/factories/consents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@
end

patient
parent { patient.parents.first }
parent do
patient.parents.first ||
association(:parent_relationship, patient:).parent
end

response { "given" }
route { "website" }
Expand Down
12 changes: 7 additions & 5 deletions spec/factories/patients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

factory :patient do
transient do
parents { [create(:parent, family_name:)] }
parents { [] }
performed_by { association(:user) }
programme { session&.programmes&.first }
session { nil }
Expand Down Expand Up @@ -109,6 +109,12 @@
address_town { Faker::Address.city }
address_postcode { Faker::Address.uk_postcode }

parent_relationships do
parents.map do |parent|
association(:parent_relationship, patient: instance, parent:)
end
end

after(:create) do |patient, evaluator|
if evaluator.session
patient_session =
Expand All @@ -120,10 +126,6 @@
create(:session_attendance, :present, patient_session:)
end
end

evaluator.parents.each do |parent|
create(:parent_relationship, parent:, patient:)
end
end

trait :in_attendance do
Expand Down
10 changes: 2 additions & 8 deletions spec/features/invalidate_consent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,9 @@ def given_i_am_signed_in
end

def and_consent_has_been_given
@parent = @patient.parents.first
@consent =
create(
:consent,
:given,
patient: @patient,
parent: @parent,
programme: @programme
)
create(:consent, :given, patient: @patient, programme: @programme)
@parent = @consent.parent
end

def and_triaged_as_safe_to_vaccinate
Expand Down
5 changes: 3 additions & 2 deletions spec/features/parental_consent_send_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ def given_a_patient_without_consent_exists
location:,
date: Date.current + 2.days
)
@patient = create(:patient, session: @session)
@parent = @patient.parents.first

@parent = create(:parent)
@patient = create(:patient, session: @session, parents: [@parent])
end

def and_i_am_signed_in
Expand Down
8 changes: 5 additions & 3 deletions spec/features/verbal_consent_but_no_triage_for_admin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def given_i_am_signed_in_as_an_admin
organisation =
create(:organisation, :with_one_admin, programmes: [programme])
@session = create(:session, organisation:, programme:)
@patient = create(:patient, session: @session)

@parent = create(:parent)
@patient = create(:patient, session: @session, parents: [@parent])

sign_in organisation.users.first, role: :admin_staff
end
Expand All @@ -26,7 +28,7 @@ def when_i_record_that_consent_was_given_but_keep_in_triage
click_button "Get consent"

# Who are you trying to get consent from?
choose @patient.parents.first.full_name
choose @parent.full_name
click_button "Continue"

# Details for parent or guardian: leave prepopulated details
Expand Down Expand Up @@ -65,6 +67,6 @@ def and_the_patient_status_is_needing_triage
end

def then_an_email_is_sent_to_the_parent_about_triage
expect_email_to(@patient.parents.first.email, :consent_confirmation_triage)
expect_email_to(@parent.email, :consent_confirmation_triage)
end
end
11 changes: 5 additions & 6 deletions spec/features/verbal_consent_given_do_not_vaccinate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def given_i_am_signed_in
organisation =
create(:organisation, :with_one_nurse, programmes: [programme])
@session = create(:session, organisation:, programme:)
@patient = create(:patient, session: @session)

@parent = create(:parent)
@patient = create(:patient, session: @session, parents: [@parent])

sign_in organisation.users.first
end
Expand All @@ -26,7 +28,7 @@ def when_i_record_that_verbal_consent_was_given_but_that_its_not_safe_to_vaccina
click_button "Get consent"

# Who are you trying to get consent from?
choose @patient.parents.first.full_name
choose @parent.full_name
click_button "Continue"

# Details for parent or guardian: leave prepopulated details
Expand Down Expand Up @@ -65,9 +67,6 @@ def and_the_patients_status_is_do_not_vaccinate
end

def then_an_email_is_sent_to_the_parent_that_the_vaccination_wont_happen
expect_email_to(
@patient.parents.first.email,
:triage_vaccination_wont_happen
)
expect_email_to(@parent.email, :triage_vaccination_wont_happen)
end
end
8 changes: 5 additions & 3 deletions spec/features/verbal_consent_given_keep_in_triage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def given_i_am_signed_in
organisation =
create(:organisation, :with_one_nurse, programmes: [programme])
@session = create(:session, organisation:, programme:)
@patient = create(:patient, session: @session)

@parent = create(:parent)
@patient = create(:patient, session: @session, parents: [@parent])

sign_in organisation.users.first
end
Expand All @@ -26,7 +28,7 @@ def when_i_record_that_consent_was_given_but_keep_in_triage
click_button "Get consent"

# Who are you trying to get consent from?
choose @patient.parents.first.full_name
choose @parent.full_name
click_button "Continue"

# Details for parent or guardian: leave prepopulated details
Expand Down Expand Up @@ -67,6 +69,6 @@ def and_the_patient_status_is_needing_triage
end

def then_an_email_is_sent_to_the_parent_about_triage
expect_email_to(@patient.parents.first.email, :consent_confirmation_triage)
expect_email_to(@parent.email, :consent_confirmation_triage)
end
end
11 changes: 5 additions & 6 deletions spec/features/verbal_consent_given_safe_to_vaccinate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def given_i_am_signed_in
create(:organisation, :with_one_nurse, programmes: [programme])

@session = create(:session, organisation:, programme:)
@patient = create(:patient, session: @session)

@parent = create(:parent)
@patient = create(:patient, session: @session, parents: [@parent])

sign_in organisation.users.first
end
Expand All @@ -27,7 +29,7 @@ def when_i_record_that_consent_was_given_with_some_health_notes_that_dont_contra
click_button "Get consent"

# Who are you trying to get consent from?
choose @patient.parents.first.full_name
choose @parent.full_name
click_button "Continue"

# Details for parent or guardian: leave existing contact details
Expand Down Expand Up @@ -66,9 +68,6 @@ def and_the_patients_status_is_safe_to_vaccinate
end

def then_an_email_is_sent_to_the_parent_that_vaccination_will_happen
expect_email_to(
@patient.parents.first.email,
:triage_vaccination_will_happen
)
expect_email_to(@parent.email, :triage_vaccination_will_happen)
end
end
33 changes: 13 additions & 20 deletions spec/features/verbal_consent_given_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@ def given_i_am_signed_in
organisation =
create(:organisation, :with_one_nurse, programmes: [programme])
@session = create(:session, organisation:, programme:)
@patient = create(:patient, session: @session)

create(
:parent_relationship,
patient: @patient,
parent: build(:parent, full_name: nil)
)
@parent = create(:parent)
@patient = create(:patient, session: @session, parents: [@parent])

sign_in organisation.users.first
end
Expand All @@ -38,14 +34,12 @@ def when_i_record_that_verbal_consent_was_given
"Choose who you are trying to get consent from"
)

parent = @patient.parents.first

choose "#{parent.full_name} (#{parent.relationship_to(patient: @patient).label})"
choose "#{@parent.full_name} (#{@parent.relationship_to(patient: @patient).label})"
click_button "Continue"

# Details for parent or guardian
expect(page).to have_content(
"Details for #{parent.full_name} (#{parent.relationship_to(patient: @patient).label})"
"Details for #{@parent.full_name} (#{@parent.relationship_to(patient: @patient).label})"
)
# don't change any details
click_button "Continue"
Expand Down Expand Up @@ -84,10 +78,9 @@ def and_the_patients_status_is_safe_to_vaccinate
end

def and_i_can_see_the_consent_response_details
parent = @patient.parents.first
click_link parent.full_name
click_link @parent.full_name

expect(page).to have_content("Consent response from #{parent.full_name}")
expect(page).to have_content("Consent response from #{@parent.full_name}")
expect(page).to have_content(
["Response date", Time.zone.today.to_fs(:long)].join
)
Expand All @@ -100,25 +93,25 @@ def and_i_can_see_the_consent_response_details
)
expect(page).to have_content(["School", @patient.school.name].join)

expect(page).to have_content(["Name", parent.full_name].join)
expect(page).to have_content(["Name", @parent.full_name].join)
expect(page).to have_content(
["Relationship", parent.relationship_to(patient: @patient).label].join
["Relationship", @parent.relationship_to(patient: @patient).label].join
)
expect(page).to have_content(["Email address", parent.email].join)
expect(page).to have_content(["Phone number", parent.phone].join)
expect(page).to have_content(["Email address", @parent.email].join)
expect(page).to have_content(["Phone number", @parent.phone].join)

expect(page).to have_content("Answers to health questions")
expect(page).to have_content(
"#{parent.relationship_to(patient: @patient).label} responded: No",
"#{@parent.relationship_to(patient: @patient).label} responded: No",
count: 4
)
end

def then_an_email_is_sent_to_the_parent_confirming_their_consent
expect_email_to(@patient.parents.first.email, :consent_confirmation_given)
expect_email_to(@parent.email, :consent_confirmation_given)
end

def and_a_text_is_sent_to_the_parent_confirming_their_consent
expect_text_to(@patient.parents.first.phone, :consent_confirmation_given)
expect_text_to(@parent.phone, :consent_confirmation_given)
end
end
4 changes: 3 additions & 1 deletion spec/features/verbal_consent_refused_personal_choice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def given_i_am_signed_in
organisation =
create(:organisation, :with_one_nurse, programmes: [programme])
@session = create(:session, organisation:, programme:)
@patient = create(:patient, session: @session)

@parent = create(:parent)
@patient = create(:patient, session: @session, parents: [@parent])

sign_in organisation.users.first
end
Expand Down
Loading

0 comments on commit d8bd0d1

Please sign in to comment.