|
4 | 4 | let(:programme) { Programme.sample } |
5 | 5 |
|
6 | 6 | describe "#save" do |
7 | | - subject do |
8 | | - described_class.new( |
9 | | - apply_changes: "apply", |
10 | | - object: vaccination_record, |
11 | | - current_team: team |
12 | | - ).save |
13 | | - end |
| 7 | + context "resolving a vaccination record" do |
| 8 | + subject do |
| 9 | + described_class.new( |
| 10 | + apply_changes: "apply", |
| 11 | + object: vaccination_record, |
| 12 | + current_team: team |
| 13 | + ).save |
| 14 | + end |
14 | 15 |
|
15 | | - let(:team) { create(:team, programmes: [programme]) } |
16 | | - let(:vaccination_record) { create(:vaccination_record, programme:, team:) } |
| 16 | + let(:team) { create(:team, programmes: [programme]) } |
| 17 | + let(:vaccination_record) do |
| 18 | + create(:vaccination_record, programme:, team:) |
| 19 | + end |
17 | 20 |
|
18 | | - it_behaves_like "a method that updates team cached counts" |
| 21 | + it_behaves_like "a method that updates team cached counts" |
| 22 | + end |
| 23 | + |
| 24 | + context "resolving a patient record" do |
| 25 | + context "when a patient import issue includes parent relationships" do |
| 26 | + let(:team) { create(:team, programmes: [programme]) } |
| 27 | + let(:session) { create(:session, team:, programmes: [programme]) } |
| 28 | + let(:class_import) { create(:class_import, session:) } |
| 29 | + let(:existing_patient) { create(:patient) } |
| 30 | + |
| 31 | + let(:import_parent) { create(:parent) } |
| 32 | + let(:existing_parent) { create(:parent) } |
| 33 | + |
| 34 | + let(:import_relationship) do |
| 35 | + create( |
| 36 | + :parent_relationship, |
| 37 | + patient: existing_patient, |
| 38 | + parent: import_parent, |
| 39 | + type: "father" |
| 40 | + ) |
| 41 | + end |
| 42 | + |
| 43 | + let!(:existing_relationship) do |
| 44 | + create( |
| 45 | + :parent_relationship, |
| 46 | + patient: existing_patient, |
| 47 | + parent: existing_parent, |
| 48 | + type: "mother" |
| 49 | + ) |
| 50 | + end |
| 51 | + |
| 52 | + let!(:changeset) do |
| 53 | + create( |
| 54 | + :patient_changeset, |
| 55 | + :class_import, |
| 56 | + :import_issue, |
| 57 | + import: class_import, |
| 58 | + patient: existing_patient |
| 59 | + ) |
| 60 | + end |
| 61 | + |
| 62 | + before do |
| 63 | + existing_patient.update!(pending_changes: { "given_name" => "Twin" }) |
| 64 | + class_import.parent_relationships << import_relationship |
| 65 | + end |
| 66 | + |
| 67 | + it "moves imported parent relationships to the new patient when keeping both" do |
| 68 | + form = |
| 69 | + described_class.new( |
| 70 | + apply_changes: "keep_both", |
| 71 | + object: existing_patient, |
| 72 | + current_team: team |
| 73 | + ) |
| 74 | + |
| 75 | + expect(form.save).to be(true) |
| 76 | + |
| 77 | + new_patient = changeset.reload.patient |
| 78 | + |
| 79 | + expect(new_patient).not_to eq(existing_patient) |
| 80 | + expect(import_relationship.reload.patient).to eq(new_patient) |
| 81 | + expect(existing_relationship.reload.patient).to eq(existing_patient) |
| 82 | + end |
| 83 | + |
| 84 | + it "removes imported parent relationships when discarding changes" do |
| 85 | + form = |
| 86 | + described_class.new( |
| 87 | + apply_changes: "discard", |
| 88 | + object: existing_patient, |
| 89 | + current_team: team |
| 90 | + ) |
| 91 | + |
| 92 | + expect(form.save).to be(true) |
| 93 | + |
| 94 | + expect { import_relationship.reload }.to raise_error( |
| 95 | + ActiveRecord::RecordNotFound |
| 96 | + ) |
| 97 | + expect(existing_relationship.reload.patient).to eq(existing_patient) |
| 98 | + expect(changeset.reload.patient).to eq(existing_patient) |
| 99 | + end |
| 100 | + end |
| 101 | + end |
19 | 102 | end |
20 | 103 |
|
21 | 104 | describe "#can_apply?" do |
|
0 commit comments