|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +describe "schools:move_patients" do |
| 4 | + subject(:invoke) do |
| 5 | + Rake::Task["schools:move_patients"].invoke(source_urn, target_urn) |
| 6 | + end |
| 7 | + |
| 8 | + let(:organisation) { create(:organisation) } |
| 9 | + let(:team) { create(:team, organisation:) } |
| 10 | + let(:other_team) { create(:team, organisation:) } |
| 11 | + let(:source_school) { create(:school, organisation: organisation, team:) } |
| 12 | + let(:target_school) { create(:school, organisation: organisation) } |
| 13 | + let(:programmes) { [create(:programme, :hpv)] } |
| 14 | + let!(:patient) { create(:patient, school: source_school) } |
| 15 | + let!(:session) { create(:session, location: source_school, programmes:) } |
| 16 | + let!(:school_move) do |
| 17 | + create(:school_move, patient: patient, school: source_school) |
| 18 | + end |
| 19 | + let!(:consent_form) do |
| 20 | + create( |
| 21 | + :consent_form, |
| 22 | + school: source_school, |
| 23 | + location: source_school, |
| 24 | + session: |
| 25 | + ) |
| 26 | + end |
| 27 | + let(:other_org_school) { create(:school, team: other_team) } |
| 28 | + |
| 29 | + let(:source_urn) { source_school.urn.to_s } |
| 30 | + let(:target_urn) { target_school.urn.to_s } |
| 31 | + |
| 32 | + after { Rake.application["schools:move_patients"].reenable } |
| 33 | + |
| 34 | + it "transfers associated records from source to target school" do |
| 35 | + expect { invoke }.to change { patient.reload.school }.from( |
| 36 | + source_school |
| 37 | + ).to(target_school).and change { consent_form.reload.school }.from( |
| 38 | + source_school |
| 39 | + ).to(target_school).and change { consent_form.reload.location }.from( |
| 40 | + source_school |
| 41 | + ).to(target_school).and change { session.reload.location }.from( |
| 42 | + source_school |
| 43 | + ).to(target_school).and change { |
| 44 | + school_move.reload.school |
| 45 | + }.from(source_school).to(target_school) |
| 46 | + |
| 47 | + expect(patient.school).to eq(target_school) |
| 48 | + expect(consent_form.school).to eq(target_school) |
| 49 | + expect(consent_form.location).to eq(target_school) |
| 50 | + expect(session.location).to eq(target_school) |
| 51 | + expect(school_move.school).to eq(target_school) |
| 52 | + end |
| 53 | + |
| 54 | + context "when source school ID is invalid" do |
| 55 | + let(:source_urn) { "999999" } |
| 56 | + |
| 57 | + it "raises an error" do |
| 58 | + expect { invoke }.to raise_error( |
| 59 | + RuntimeError, |
| 60 | + /Could not find one or both schools./ |
| 61 | + ) |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + context "when target school ID is invalid" do |
| 66 | + let(:target_urn) { "999999" } |
| 67 | + |
| 68 | + it "raises an error" do |
| 69 | + expect { invoke }.to raise_error( |
| 70 | + RuntimeError, |
| 71 | + /Could not find one or both schools./ |
| 72 | + ) |
| 73 | + end |
| 74 | + end |
| 75 | +end |
0 commit comments