Skip to content

Commit

Permalink
Avoid N+1 issues in tests
Browse files Browse the repository at this point in the history
This updates various tests to fix N+1 issues that are triggered by
strict loading being enabled globally.
  • Loading branch information
thomasleese committed Jan 8, 2025
1 parent 67ed062 commit 7ffa295
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 19 deletions.
8 changes: 5 additions & 3 deletions spec/controllers/concerns/patient_tabs_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,22 @@
end

context "some of the groups are empty" do
let(:patient_session) { create(:patient_session, :consent_refused) }
let(:patient_sessions) do
create_list(:patient_session, 1, :consent_refused)
end

it "returns an empty array for all the empty groups" do
result =
controller.group_patient_sessions_by_state(
[patient_session],
patient_sessions,
section: :triage
)

expect(result).to eq(
{
needs_triage: [],
triage_complete: [],
no_triage_needed: [patient_session]
no_triage_needed: patient_sessions
}.with_indifferent_access
)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/unscheduled_sessions_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
it "creates missing unscheduled sessions" do
expect { call }.to change(organisation.sessions, :count).by(1)

session = organisation.sessions.first
session = organisation.sessions.includes(:location, :programmes).first
expect(session.location).to eq(location)
expect(session.programmes).to eq([programme])
end
Expand All @@ -25,7 +25,7 @@
it "creates missing unscheduled sessions" do
expect { call }.to change(organisation.sessions, :count).by(1)

session = organisation.sessions.first
session = organisation.sessions.includes(:location, :programmes).first
expect(session.location).to eq(location)
expect(session.programmes).to eq([programme])
end
Expand Down
4 changes: 2 additions & 2 deletions spec/models/class_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@
expect { process! }.to change(Parent, :count).by(4)

parent_relationship = patient.reload.parent_relationships.first
expect(parent_relationship.parent).to eq(parent)
expect(parent_relationship.parent_id).to eq(parent.id)
expect(parent_relationship).to be_father
end
end
Expand Down Expand Up @@ -351,7 +351,7 @@
)

school_move = patient.school_moves.first
expect(school_move.school).to eq(session.location)
expect(school_move.school_id).to eq(session.location_id)
end

it "doesn't stage school changes" do
Expand Down
2 changes: 1 addition & 1 deletion spec/models/consent_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@
).by(1)

school_move = patient.school_moves.first
expect(school_move.school).to eq(new_school)
expect(school_move.school_id).to eq(new_school.id)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/models/immunisation_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@

expect(immunisation_import.sessions.count).to eq(5)

session = immunisation_import.sessions.first
session = immunisation_import.sessions.includes(:session_dates).first
expect(session.dates).to contain_exactly(Date.new(2024, 5, 14))
end

Expand Down
4 changes: 2 additions & 2 deletions spec/models/onboarding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
expect(organisation.careplus_venue_code).to eq("EXAMPLE")
expect(organisation.programmes).to contain_exactly(programme)

team1 = organisation.teams.find_by!(name: "Team 1")
team1 = organisation.teams.includes(:schools).find_by!(name: "Team 1")
expect(team1.email).to eq("[email protected]")
expect(team1.phone).to eq("07700 900816")

team2 = organisation.teams.find_by!(name: "Team 2")
team2 = organisation.teams.includes(:schools).find_by!(name: "Team 2")
expect(team2.email).to eq("[email protected]")
expect(team2.phone).to eq("07700 900817")

Expand Down
4 changes: 3 additions & 1 deletion spec/models/patient_session_stats_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

describe PatientSessionStats do
describe "#to_h" do
subject(:to_h) { described_class.new(session.patient_sessions).to_h }
subject(:to_h) do
described_class.new(session.patient_sessions.preload_for_status).to_h
end

let(:programme) { create(:programme) }
let(:session) { create(:session, programme:) }
Expand Down
11 changes: 4 additions & 7 deletions spec/models/triage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,11 @@
let(:status) { :delay_vaccination }

it "adds the patient to the generic clinic" do
expect { process! }.to change(
triage.patient.upcoming_sessions,
:count
).by(1)
upcoming_sessions = triage.patient.upcoming_sessions.includes(:location)

expect(
triage.patient.upcoming_sessions.last.location
).to be_generic_clinic
expect { process! }.to change(upcoming_sessions, :count).by(1)

expect(upcoming_sessions.last.location).to be_generic_clinic
end
end
end
Expand Down

0 comments on commit 7ffa295

Please sign in to comment.