From 8b1d53470a5fdcedf1fc109289b7438f53f04e26 Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Fri, 13 Dec 2024 14:35:14 +0000 Subject: [PATCH] Don't create parents by default in tests 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. --- .../vaccination_mailer_concern_spec.rb | 22 +++++++++---- spec/factories/consents.rb | 5 ++- spec/factories/patients.rb | 12 ++++--- spec/features/invalidate_consent_spec.rb | 10 ++---- .../parental_consent_send_request_spec.rb | 5 +-- ...al_consent_but_no_triage_for_admin_spec.rb | 8 +++-- ...bal_consent_given_do_not_vaccinate_spec.rb | 11 +++---- ...erbal_consent_given_keep_in_triage_spec.rb | 8 +++-- ...al_consent_given_safe_to_vaccinate_spec.rb | 11 +++---- spec/features/verbal_consent_given_spec.rb | 33 ++++++++----------- ...al_consent_refused_personal_choice_spec.rb | 4 ++- spec/features/verbal_consent_refused_spec.rb | 27 ++++++++------- spec/features/withdraw_consent_spec.rb | 10 ++---- .../jobs/school_consent_reminders_job_spec.rb | 1 + spec/lib/patient_merger_spec.rb | 4 +-- spec/mailers/consent_mailer_spec.rb | 16 ++++----- spec/mailers/session_mailer_spec.rb | 18 ++++++---- spec/models/patient_spec.rb | 21 ++++-------- 18 files changed, 111 insertions(+), 115 deletions(-) diff --git a/spec/controllers/concerns/vaccination_mailer_concern_spec.rb b/spec/controllers/concerns/vaccination_mailer_concern_spec.rb index 76fd0060f..8a5974075 100644 --- a/spec/controllers/concerns/vaccination_mailer_concern_spec.rb +++ b/spec/controllers/concerns/vaccination_mailer_concern_spec.rb @@ -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, @@ -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, @@ -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 @@ -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 diff --git a/spec/factories/consents.rb b/spec/factories/consents.rb index e1803da99..f8a9131d1 100644 --- a/spec/factories/consents.rb +++ b/spec/factories/consents.rb @@ -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" } diff --git a/spec/factories/patients.rb b/spec/factories/patients.rb index b3656a8e8..fba336caa 100644 --- a/spec/factories/patients.rb +++ b/spec/factories/patients.rb @@ -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 } @@ -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 = @@ -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 diff --git a/spec/features/invalidate_consent_spec.rb b/spec/features/invalidate_consent_spec.rb index 0d008582c..5b79d9005 100644 --- a/spec/features/invalidate_consent_spec.rb +++ b/spec/features/invalidate_consent_spec.rb @@ -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 diff --git a/spec/features/parental_consent_send_request_spec.rb b/spec/features/parental_consent_send_request_spec.rb index e0e2f5349..fa9b46cbf 100644 --- a/spec/features/parental_consent_send_request_spec.rb +++ b/spec/features/parental_consent_send_request_spec.rb @@ -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 diff --git a/spec/features/verbal_consent_but_no_triage_for_admin_spec.rb b/spec/features/verbal_consent_but_no_triage_for_admin_spec.rb index 36be7aacf..898f32c17 100644 --- a/spec/features/verbal_consent_but_no_triage_for_admin_spec.rb +++ b/spec/features/verbal_consent_but_no_triage_for_admin_spec.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/features/verbal_consent_given_do_not_vaccinate_spec.rb b/spec/features/verbal_consent_given_do_not_vaccinate_spec.rb index 9f0ba9a37..f0c7b68ca 100644 --- a/spec/features/verbal_consent_given_do_not_vaccinate_spec.rb +++ b/spec/features/verbal_consent_given_do_not_vaccinate_spec.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/features/verbal_consent_given_keep_in_triage_spec.rb b/spec/features/verbal_consent_given_keep_in_triage_spec.rb index e1e21d0ee..ed59d5b53 100644 --- a/spec/features/verbal_consent_given_keep_in_triage_spec.rb +++ b/spec/features/verbal_consent_given_keep_in_triage_spec.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/features/verbal_consent_given_safe_to_vaccinate_spec.rb b/spec/features/verbal_consent_given_safe_to_vaccinate_spec.rb index 71a1e11eb..ef99d56ef 100644 --- a/spec/features/verbal_consent_given_safe_to_vaccinate_spec.rb +++ b/spec/features/verbal_consent_given_safe_to_vaccinate_spec.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/features/verbal_consent_given_spec.rb b/spec/features/verbal_consent_given_spec.rb index 35062eb1d..48543d6b5 100644 --- a/spec/features/verbal_consent_given_spec.rb +++ b/spec/features/verbal_consent_given_spec.rb @@ -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 @@ -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" @@ -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 ) @@ -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 diff --git a/spec/features/verbal_consent_refused_personal_choice_spec.rb b/spec/features/verbal_consent_refused_personal_choice_spec.rb index f5b3c99c8..11dba8e93 100644 --- a/spec/features/verbal_consent_refused_personal_choice_spec.rb +++ b/spec/features/verbal_consent_refused_personal_choice_spec.rb @@ -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 diff --git a/spec/features/verbal_consent_refused_spec.rb b/spec/features/verbal_consent_refused_spec.rb index 42d4aae3f..9e76fabe8 100644 --- a/spec/features/verbal_consent_refused_spec.rb +++ b/spec/features/verbal_consent_refused_spec.rb @@ -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 @@ -28,7 +30,7 @@ def when_i_record_the_consent_refusal_and_reason 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 @@ -52,9 +54,7 @@ def when_i_record_the_consent_refusal_and_reason # Confirm expect(page).to have_content(["Decision", "Consent refused"].join) - expect(page).to have_content( - ["Name", @patient.parents.first.full_name].join - ) + expect(page).to have_content(["Name", @parent.full_name].join) click_button "Confirm" expect(page).to have_content("Check consent responses") @@ -64,14 +64,13 @@ def when_i_record_the_consent_refusal_and_reason def and_the_patients_status_is_consent_refused click_link @patient.full_name - relation = @patient.parents.first.relationship_to(patient: @patient).label + relation = @parent.relationship_to(patient: @patient).label expect(page).to have_content("Consent refused") expect(page).to have_content("#{relation} refused to give consent.") 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( ["Response date", Time.zone.today.to_fs(:long)].join @@ -89,21 +88,21 @@ 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).not_to have_content("Answers to health questions") end def then_an_email_is_sent_to_the_parent_confirming_the_refusal - expect_email_to @patient.parents.first.email, :consent_confirmation_refused + expect_email_to @parent.email, :consent_confirmation_refused end def and_a_text_is_sent_to_the_parent_confirming_the_refusal - expect_text_to @patient.parents.first.phone, :consent_confirmation_refused + expect_text_to @parent.phone, :consent_confirmation_refused end end diff --git a/spec/features/withdraw_consent_spec.rb b/spec/features/withdraw_consent_spec.rb index 292254802..def251f82 100644 --- a/spec/features/withdraw_consent_spec.rb +++ b/spec/features/withdraw_consent_spec.rb @@ -64,15 +64,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 diff --git a/spec/jobs/school_consent_reminders_job_spec.rb b/spec/jobs/school_consent_reminders_job_spec.rb index c7f5aa48b..cda4f0d2a 100644 --- a/spec/jobs/school_consent_reminders_job_spec.rb +++ b/spec/jobs/school_consent_reminders_job_spec.rb @@ -12,6 +12,7 @@ :patient, :consent_request_sent, :initial_consent_reminder_sent, + parents:, programme: ) end diff --git a/spec/lib/patient_merger_spec.rb b/spec/lib/patient_merger_spec.rb index 34ef1b26b..4e11dd59f 100644 --- a/spec/lib/patient_merger_spec.rb +++ b/spec/lib/patient_merger_spec.rb @@ -4,8 +4,8 @@ describe "#call" do subject(:call) do described_class.call( - to_keep: patient_to_keep, - to_destroy: patient_to_destroy + to_keep: patient_to_keep.reload, + to_destroy: patient_to_destroy.reload ) end diff --git a/spec/mailers/consent_mailer_spec.rb b/spec/mailers/consent_mailer_spec.rb index 8e8ea1df4..5be30ab6f 100644 --- a/spec/mailers/consent_mailer_spec.rb +++ b/spec/mailers/consent_mailer_spec.rb @@ -69,8 +69,8 @@ ).school_request end - let(:patient) { create(:patient) } - let(:parent) { patient.parents.first } + let(:parent) { create(:parent) } + let(:patient) { create(:patient, parents: [parent]) } let(:programme) { create(:programme) } let(:date) { Date.current } let(:session) { create(:session, date:, patients: [patient], programme:) } @@ -107,8 +107,8 @@ ).clinic_request end - let(:patient) { create(:patient) } - let(:parent) { patient.parents.first } + let(:parent) { create(:parent) } + let(:patient) { create(:patient, parents: [parent]) } let(:programme) { create(:programme) } let(:date) { Date.current } let(:session) { create(:session, date:, patients: [patient], programme:) } @@ -141,8 +141,8 @@ ).school_initial_reminder end - let(:patient) { create(:patient) } - let(:parent) { patient.parents.first } + let(:parent) { create(:parent) } + let(:patient) { create(:patient, parents: [parent]) } let(:programme) { create(:programme) } let(:date) { Date.current } let(:session) { create(:session, date:, patients: [patient], programme:) } @@ -179,8 +179,8 @@ ).school_subsequent_reminder end - let(:patient) { create(:patient) } - let(:parent) { patient.parents.first } + let(:parent) { create(:parent) } + let(:patient) { create(:patient, parents: [parent]) } let(:programme) { create(:programme) } let(:date) { Date.current } let(:session) { create(:session, date:, patients: [patient], programme:) } diff --git a/spec/mailers/session_mailer_spec.rb b/spec/mailers/session_mailer_spec.rb index 07299361e..7d15a3175 100644 --- a/spec/mailers/session_mailer_spec.rb +++ b/spec/mailers/session_mailer_spec.rb @@ -12,11 +12,13 @@ end let(:programme) { create(:programme) } - let(:patient) { create(:patient, preferred_given_name: "Joey") } + let(:parent) { create(:parent) } + let(:patient) do + create(:patient, preferred_given_name: "Joey", parents: [parent]) + end let(:session) { create(:session, programme:, patients: [patient]) } - let(:parent) { patient.parents.first } - it { should have_attributes(to: [patient.parents.first.email]) } + it { should have_attributes(to: [parent.email]) } describe "personalisation" do subject(:personalisation) do @@ -58,16 +60,17 @@ organisation: ) end + let(:parent) { create(:parent) } let(:patient) do create( :patient, given_name: "John", family_name: "Smith", - preferred_given_name: "Joey" + preferred_given_name: "Joey", + parents: [parent] ) end let(:session) { create(:session, organisation:, programme:, team:) } - let(:parent) { patient.parents.first } let(:patient_session) { create(:patient_session, patient:, session:) } it { should have_attributes(to: [parent.email]) } @@ -108,16 +111,17 @@ organisation: ) end + let(:parent) { create(:parent) } let(:patient) do create( :patient, given_name: "John", family_name: "Smith", - preferred_given_name: "Joey" + preferred_given_name: "Joey", + parents: [parent] ) end let(:session) { create(:session, organisation:, programme:, team:) } - let(:parent) { patient.parents.first } let(:patient_session) { create(:patient_session, patient:, session:) } it { should have_attributes(to: [parent.email]) } diff --git a/spec/models/patient_spec.rb b/spec/models/patient_spec.rb index 5eb851586..08090b7ec 100644 --- a/spec/models/patient_spec.rb +++ b/spec/models/patient_spec.rb @@ -571,11 +571,9 @@ end describe "#destroy_childless_parents" do - let(:patient) { create(:patient, parents: []) } - let(:parent) { create(:parent) } - context "when parent has only one child" do - before { create(:parent_relationship, parent:, patient:) } + let(:parent) { create(:parent) } + let!(:patient) { create(:patient, parents: [parent]) } it "destroys the parent when the patient is destroyed" do expect { patient.destroy }.to change(Parent, :count).by(-1) @@ -583,12 +581,10 @@ end context "when parent has multiple children" do - let(:sibling) { create(:patient) } + let(:parent) { create(:parent) } + let!(:patient) { create(:patient, parents: [parent]) } - before do - create(:parent_relationship, parent:, patient:) - create(:parent_relationship, parent:, patient: sibling) - end + before { create(:patient, parents: [parent]) } it "does not destroy the parent when one patient is destroyed" do expect { patient.destroy }.not_to change(Parent, :count) @@ -596,12 +592,7 @@ end context "when patient has multiple parents" do - let(:other_parent) { create(:parent) } - - before do - create(:parent_relationship, parent:, patient:) - create(:parent_relationship, parent: other_parent, patient:) - end + let!(:patient) { create(:patient, parents: create_list(:parent, 2)) } it "destroys only the childless parents" do expect { patient.destroy }.to change(Parent, :count).by(-2)