|
3 | 3 | describe SyncVaccinationRecordToNHSJob, type: :job do |
4 | 4 | subject(:perform_now) { described_class.perform_now(vaccination_record) } |
5 | 5 |
|
6 | | - before { allow(NHS::ImmunisationsAPI).to receive(:record_immunisation) } |
| 6 | + before do |
| 7 | + allow(NHS::ImmunisationsAPI).to receive(:record_immunisation) |
| 8 | + allow(NHS::ImmunisationsAPI).to receive(:update_immunisation) |
| 9 | + end |
7 | 10 |
|
| 11 | + let(:nhs_immunisations_api_synced_at) { nil } |
8 | 12 | let(:vaccination_record) do |
9 | | - instance_double( |
10 | | - VaccinationRecord, |
11 | | - id: "123", |
12 | | - nhs_immunisations_api_synced_at: nil |
13 | | - ) |
| 13 | + create(:vaccination_record, id: 123, nhs_immunisations_api_synced_at:) |
14 | 14 | end |
15 | 15 |
|
16 | 16 | it "sends the vaccination record to the NHS Immunisations API" do |
|
21 | 21 | ) |
22 | 22 | end |
23 | 23 |
|
24 | | - context "when the vaccination record has already been synced" do |
25 | | - let(:vaccination_record) do |
26 | | - instance_double( |
27 | | - VaccinationRecord, |
28 | | - id: "123", |
29 | | - nhs_immunisations_api_synced_at: Time.current |
| 24 | + context "when the vaccination record has been synced before" do |
| 25 | + let(:nhs_immunisations_api_synced_at) { 1.second.ago } |
| 26 | + |
| 27 | + it "updates the vaccination record with the NHS Immunisations API" do |
| 28 | + perform_now |
| 29 | + |
| 30 | + expect(NHS::ImmunisationsAPI).to have_received(:update_immunisation).with( |
| 31 | + vaccination_record |
30 | 32 | ) |
31 | 33 | end |
| 34 | + end |
| 35 | + |
| 36 | + context "when the vaccination record is already in-sync" do |
| 37 | + let(:nhs_immunisations_api_synced_at) { 1.second.from_now } |
32 | 38 |
|
33 | 39 | it "does not send the vaccination record to the NHS Immunisations API" do |
34 | 40 | perform_now |
|
46 | 52 | ) |
47 | 53 | end |
48 | 54 | end |
| 55 | + |
| 56 | + context "when the vaccination record has been discarded" do |
| 57 | + let(:vaccination_record) do |
| 58 | + create(:vaccination_record, :discarded, id: 123) |
| 59 | + end |
| 60 | + |
| 61 | + it "does not send the vaccination record to the NHS Immunisations API" do |
| 62 | + begin |
| 63 | + perform_now |
| 64 | + rescue StandardError |
| 65 | + nil |
| 66 | + end |
| 67 | + |
| 68 | + expect(NHS::ImmunisationsAPI).not_to have_received(:record_immunisation) |
| 69 | + end |
| 70 | + |
| 71 | + it "raises an error" do |
| 72 | + expect { perform_now }.to raise_error( |
| 73 | + "Vaccination record is discarded: #{vaccination_record.id}" |
| 74 | + ) |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + VaccinationRecord.defined_enums["outcome"].each_key do |outcome| |
| 79 | + next if outcome == "administered" |
| 80 | + |
| 81 | + context "when the vaccination record outcome is #{outcome}" do |
| 82 | + let(:vaccination_record) do |
| 83 | + create(:vaccination_record, id: 123, outcome:) |
| 84 | + end |
| 85 | + |
| 86 | + it "does not send the vaccination record to the NHS Immunisations API" do |
| 87 | + begin |
| 88 | + perform_now |
| 89 | + rescue StandardError |
| 90 | + nil |
| 91 | + end |
| 92 | + |
| 93 | + expect(NHS::ImmunisationsAPI).not_to have_received(:record_immunisation) |
| 94 | + end |
| 95 | + |
| 96 | + it "raises an error" do |
| 97 | + expect { perform_now }.to raise_error( |
| 98 | + "Vaccination record is not administered: #{vaccination_record.id}" |
| 99 | + ) |
| 100 | + end |
| 101 | + end |
| 102 | + end |
49 | 103 | end |
0 commit comments