|
| 1 | +from datetime import date |
| 2 | + |
| 3 | +import pytest |
| 4 | +from dateutil.relativedelta import relativedelta |
| 5 | +from django.urls import reverse |
| 6 | +from pytest_django.asserts import assertInHTML, assertRedirects |
| 7 | + |
| 8 | +from manage_breast_screening.participants.forms import ParticipantReportedMammogramForm |
| 9 | +from manage_breast_screening.participants.models.appointment import AppointmentStatus |
| 10 | +from manage_breast_screening.participants.tests.factories import AppointmentFactory |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.django_db |
| 14 | +class TestShowAppointment: |
| 15 | + def test_get(self, clinical_user_client): |
| 16 | + appointment = AppointmentFactory.create( |
| 17 | + clinic_slot__clinic__setting__provider=clinical_user_client.current_provider |
| 18 | + ) |
| 19 | + response = clinical_user_client.http.get( |
| 20 | + reverse( |
| 21 | + "mammograms:add_previous_mammogram", |
| 22 | + kwargs={"appointment_pk": appointment.pk}, |
| 23 | + ) |
| 24 | + ) |
| 25 | + assert response.status_code == 200 |
| 26 | + |
| 27 | + def test_post_with_missing_parameters(self, clinical_user_client): |
| 28 | + appointment = AppointmentFactory.create( |
| 29 | + clinic_slot__clinic__setting__provider=clinical_user_client.current_provider |
| 30 | + ) |
| 31 | + response = clinical_user_client.http.post( |
| 32 | + reverse( |
| 33 | + "mammograms:add_previous_mammogram", |
| 34 | + kwargs={"appointment_pk": appointment.pk}, |
| 35 | + ) |
| 36 | + ) |
| 37 | + assert response.status_code == 200 |
| 38 | + assertInHTML( |
| 39 | + """ |
| 40 | + <span id="id_where_taken-error" class="nhsuk-error-message"> |
| 41 | + <span class="nhsuk-u-visually-hidden">Error:</span> This field is required. |
| 42 | + </span> |
| 43 | + """, |
| 44 | + response.text, |
| 45 | + ) |
| 46 | + assertInHTML( |
| 47 | + """ |
| 48 | + <span id="id_when_taken-error" class="nhsuk-error-message"> |
| 49 | + <span class="nhsuk-u-visually-hidden">Error:</span> This field is required. |
| 50 | + </span> |
| 51 | + """, |
| 52 | + response.text, |
| 53 | + ) |
| 54 | + assertInHTML( |
| 55 | + """ |
| 56 | + <span id="id_name_is_the_same-error" class="nhsuk-error-message"> |
| 57 | + <span class="nhsuk-u-visually-hidden">Error:</span> This field is required. |
| 58 | + </span> |
| 59 | + """, |
| 60 | + response.text, |
| 61 | + ) |
| 62 | + |
| 63 | + def test_post_date_unsure(self, clinical_user_client): |
| 64 | + appointment = AppointmentFactory.create( |
| 65 | + clinic_slot__clinic__setting__provider=clinical_user_client.current_provider |
| 66 | + ) |
| 67 | + return_url = reverse( |
| 68 | + "mammograms:record_medical_information", |
| 69 | + kwargs={"pk": appointment.pk}, |
| 70 | + ) |
| 71 | + |
| 72 | + response = clinical_user_client.http.post( |
| 73 | + reverse( |
| 74 | + "mammograms:add_previous_mammogram", |
| 75 | + kwargs={"appointment_pk": appointment.pk}, |
| 76 | + ), |
| 77 | + { |
| 78 | + "return_url": return_url, |
| 79 | + "where_taken": ParticipantReportedMammogramForm.WhereTaken.SAME_UNIT, |
| 80 | + "when_taken": ParticipantReportedMammogramForm.WhenTaken.NOT_SURE, |
| 81 | + "name_is_the_same": ParticipantReportedMammogramForm.NameIsTheSame.YES, |
| 82 | + }, |
| 83 | + ) |
| 84 | + |
| 85 | + assertRedirects(response, return_url) |
| 86 | + |
| 87 | + def test_post_approx_date(self, clinical_user_client): |
| 88 | + appointment = AppointmentFactory.create( |
| 89 | + clinic_slot__clinic__setting__provider=clinical_user_client.current_provider |
| 90 | + ) |
| 91 | + return_url = reverse( |
| 92 | + "mammograms:record_medical_information", |
| 93 | + kwargs={"pk": appointment.pk}, |
| 94 | + ) |
| 95 | + |
| 96 | + response = clinical_user_client.http.post( |
| 97 | + reverse( |
| 98 | + "mammograms:add_previous_mammogram", |
| 99 | + kwargs={"appointment_pk": appointment.pk}, |
| 100 | + ), |
| 101 | + { |
| 102 | + "return_url": return_url, |
| 103 | + "where_taken": ParticipantReportedMammogramForm.WhereTaken.SAME_UNIT, |
| 104 | + "when_taken": ParticipantReportedMammogramForm.WhenTaken.APPROX, |
| 105 | + "approx_date": "last month", |
| 106 | + "name_is_the_same": ParticipantReportedMammogramForm.NameIsTheSame.YES, |
| 107 | + }, |
| 108 | + ) |
| 109 | + |
| 110 | + assertRedirects(response, return_url) |
| 111 | + |
| 112 | + @pytest.mark.parametrize( |
| 113 | + "exact_date", |
| 114 | + [ |
| 115 | + date.today() - relativedelta(months=6), |
| 116 | + date.today() - relativedelta(months=6) - relativedelta(days=1), |
| 117 | + date.today() - relativedelta(years=50), |
| 118 | + ], |
| 119 | + ) |
| 120 | + def test_post_exact_date_six_months_ago_or_later( |
| 121 | + self, clinical_user_client, exact_date |
| 122 | + ): |
| 123 | + appointment = AppointmentFactory.create( |
| 124 | + clinic_slot__clinic__setting__provider=clinical_user_client.current_provider |
| 125 | + ) |
| 126 | + return_url = reverse( |
| 127 | + "mammograms:record_medical_information", |
| 128 | + kwargs={"pk": appointment.pk}, |
| 129 | + ) |
| 130 | + |
| 131 | + response = clinical_user_client.http.post( |
| 132 | + reverse( |
| 133 | + "mammograms:add_previous_mammogram", |
| 134 | + kwargs={"appointment_pk": appointment.pk}, |
| 135 | + ), |
| 136 | + { |
| 137 | + "return_url": return_url, |
| 138 | + "where_taken": ParticipantReportedMammogramForm.WhereTaken.SAME_UNIT, |
| 139 | + "when_taken": ParticipantReportedMammogramForm.WhenTaken.EXACT, |
| 140 | + "exact_date_0": exact_date.day, |
| 141 | + "exact_date_1": exact_date.month, |
| 142 | + "exact_date_2": exact_date.year, |
| 143 | + "name_is_the_same": ParticipantReportedMammogramForm.NameIsTheSame.YES, |
| 144 | + }, |
| 145 | + ) |
| 146 | + |
| 147 | + assertRedirects(response, return_url) |
| 148 | + |
| 149 | + @pytest.mark.parametrize( |
| 150 | + "exact_date", |
| 151 | + [ |
| 152 | + date.today(), |
| 153 | + date.today() - relativedelta(months=6) + relativedelta(days=1), |
| 154 | + date.today() - relativedelta(months=6) + relativedelta(days=2), |
| 155 | + ], |
| 156 | + ) |
| 157 | + def test_post_exact_date_within_last_six_months( |
| 158 | + self, clinical_user_client, exact_date |
| 159 | + ): |
| 160 | + appointment = AppointmentFactory.create( |
| 161 | + clinic_slot__clinic__setting__provider=clinical_user_client.current_provider |
| 162 | + ) |
| 163 | + return_url = reverse( |
| 164 | + "mammograms:record_medical_information", |
| 165 | + kwargs={"pk": appointment.pk}, |
| 166 | + ) |
| 167 | + |
| 168 | + response = clinical_user_client.http.post( |
| 169 | + reverse( |
| 170 | + "mammograms:add_previous_mammogram", |
| 171 | + kwargs={"appointment_pk": appointment.pk}, |
| 172 | + ), |
| 173 | + { |
| 174 | + "return_url": return_url, |
| 175 | + "where_taken": ParticipantReportedMammogramForm.WhereTaken.SAME_UNIT, |
| 176 | + "when_taken": ParticipantReportedMammogramForm.WhenTaken.EXACT, |
| 177 | + "exact_date_0": exact_date.day, |
| 178 | + "exact_date_1": exact_date.month, |
| 179 | + "exact_date_2": exact_date.year, |
| 180 | + "name_is_the_same": ParticipantReportedMammogramForm.NameIsTheSame.YES, |
| 181 | + }, |
| 182 | + ) |
| 183 | + assertRedirects( |
| 184 | + response, |
| 185 | + reverse( |
| 186 | + "mammograms:appointment_should_not_proceed", |
| 187 | + kwargs={"appointment_pk": appointment.pk}, |
| 188 | + ) |
| 189 | + + f"?exact_date={exact_date.isoformat()}", |
| 190 | + ) |
| 191 | + assert appointment.current_status.state == AppointmentStatus.CONFIRMED |
| 192 | + |
| 193 | + response = clinical_user_client.http.post( |
| 194 | + reverse( |
| 195 | + "mammograms:attended_not_screened", |
| 196 | + kwargs={"appointment_pk": appointment.pk}, |
| 197 | + ), |
| 198 | + ) |
| 199 | + assertRedirects( |
| 200 | + response, |
| 201 | + reverse( |
| 202 | + "clinics:show", |
| 203 | + kwargs={"pk": appointment.clinic_slot.clinic.pk}, |
| 204 | + ), |
| 205 | + ) |
| 206 | + assert ( |
| 207 | + appointment.current_status.state == AppointmentStatus.ATTENDED_NOT_SCREENED |
| 208 | + ) |
0 commit comments