|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../../app/lib/mavis_cli" |
| 4 | + |
| 5 | +describe "mavis vaccination-records generate-fhir" do |
| 6 | + context "when the vaccination record exists and is administered" do |
| 7 | + it "generates and displays the FHIR record" do |
| 8 | + given_a_vaccination_record_exists |
| 9 | + when_i_run_the_generate_fhir_command |
| 10 | + then_the_fhir_record_is_displayed |
| 11 | + end |
| 12 | + end |
| 13 | + |
| 14 | + context "when the vaccination record does not exist" do |
| 15 | + it "displays an error message" do |
| 16 | + when_i_run_the_generate_fhir_command_with_an_invalid_id |
| 17 | + then_an_error_message_is_displayed |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + context "when the vaccination record was not administered" do |
| 22 | + it "displays an error message with the reason" do |
| 23 | + given_a_not_administered_vaccination_record_exists |
| 24 | + when_i_run_the_generate_fhir_command_for_not_administered_record |
| 25 | + then_the_not_administered_error_message_is_displayed |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + private |
| 30 | + |
| 31 | + def given_a_vaccination_record_exists |
| 32 | + organisation = create(:organisation) |
| 33 | + programme = create(:programme, type: "hpv") |
| 34 | + patient = create(:patient, organisation:) |
| 35 | + vaccine = create(:vaccine, :gardasil, programme:) |
| 36 | + batch = create(:batch, vaccine:, expiry: "2023-03-20", name: "X8U375AL") |
| 37 | + |
| 38 | + @vaccination_record = |
| 39 | + create( |
| 40 | + :vaccination_record, |
| 41 | + patient:, |
| 42 | + programme:, |
| 43 | + vaccine:, |
| 44 | + batch:, |
| 45 | + outcome: "administered" |
| 46 | + ) |
| 47 | + end |
| 48 | + |
| 49 | + def given_a_not_administered_vaccination_record_exists |
| 50 | + organisation = create(:organisation) |
| 51 | + programme = create(:programme, type: "hpv") |
| 52 | + patient = create(:patient, organisation:) |
| 53 | + |
| 54 | + @not_administered_vaccination_record = |
| 55 | + create(:vaccination_record, patient:, programme:, outcome: "refused") |
| 56 | + end |
| 57 | + |
| 58 | + def when_i_run_the_generate_fhir_command |
| 59 | + @output = |
| 60 | + capture_output do |
| 61 | + Dry::CLI.new(MavisCLI).call( |
| 62 | + arguments: [ |
| 63 | + "vaccination-records", |
| 64 | + "generate-fhir", |
| 65 | + @vaccination_record.id |
| 66 | + ] |
| 67 | + ) |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + def when_i_run_the_generate_fhir_command_with_an_invalid_id |
| 72 | + @output = |
| 73 | + capture_output do |
| 74 | + Dry::CLI.new(MavisCLI).call( |
| 75 | + arguments: %w[vaccination-records generate-fhir 999999] |
| 76 | + ) |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + def when_i_run_the_generate_fhir_command_for_not_administered_record |
| 81 | + @output = |
| 82 | + capture_output do |
| 83 | + Dry::CLI.new(MavisCLI).call( |
| 84 | + arguments: [ |
| 85 | + "vaccination-records", |
| 86 | + "generate-fhir", |
| 87 | + @not_administered_vaccination_record.id |
| 88 | + ] |
| 89 | + ) |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + def then_the_fhir_record_is_displayed |
| 94 | + expect(@output).to include("resourceType") |
| 95 | + expect(@output).to include("Immunization") |
| 96 | + # We expect the JSON output to be pretty-printed |
| 97 | + expect(@output).to include("{\n") |
| 98 | + end |
| 99 | + |
| 100 | + def then_an_error_message_is_displayed |
| 101 | + expect(@output).to include( |
| 102 | + "Error: Vaccination record with ID 999999 not found" |
| 103 | + ) |
| 104 | + end |
| 105 | + |
| 106 | + def then_the_not_administered_error_message_is_displayed |
| 107 | + outcome = @not_administered_vaccination_record.outcome.humanize |
| 108 | + expect(@output).to include( |
| 109 | + "Error: Vaccination record with ID #{@not_administered_vaccination_record.id} was not " \ |
| 110 | + "administered (Outcome: #{outcome})" |
| 111 | + ) |
| 112 | + end |
| 113 | +end |
0 commit comments