Skip to content

Commit bd63fb1

Browse files
committed
tests
1 parent 471fa1c commit bd63fb1

File tree

3 files changed

+270
-6
lines changed

3 files changed

+270
-6
lines changed

manage_breast_screening/mammograms/forms/mastectomy_or_lumpectomy_history_form.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import datetime
2+
13
from django.forms import widgets
24
from django.forms.widgets import RadioSelect, Textarea
35

@@ -59,8 +61,8 @@ class MastectomyOrLumpectomyHistoryForm(FormWithConditionalFields):
5961
label="Year of surgery (optional)",
6062
label_classes="nhsuk-label--m",
6163
classes="nhsuk-input--width-4",
62-
min_value=2000,
63-
max_value=2001,
64+
min_value=datetime.date.today().year - 80,
65+
max_value=datetime.date.today().year,
6466
error_messages={
6567
"min_value": "year_outside_range_error_message",
6668
"max_value": "year_outside_range_error_message",

manage_breast_screening/mammograms/tests/forms/test_mastectomy_or_lumpectomy_history_form.py

Lines changed: 256 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
from urllib.parse import urlencode
23

34
import pytest
@@ -17,7 +18,7 @@
1718

1819
@pytest.mark.django_db
1920
class TestMastectomyOrLumpectomyHistoryItemForm:
20-
def test_no_data(self, clinical_user):
21+
def test_missing_required_fields(self, clinical_user):
2122
appointment = AppointmentFactory()
2223
request = RequestFactory().get("/test-form")
2324
request.user = clinical_user
@@ -43,9 +44,259 @@ def test_no_data(self, clinical_user):
4344
"surgery_reason": ["Select the reason for surgery"],
4445
}
4546

47+
def test_right_breast_other_surgery_no_other_surgery_and_others(
48+
self, clinical_user
49+
):
50+
appointment = AppointmentFactory()
51+
request = RequestFactory().get("/test-form")
52+
request.user = clinical_user
53+
54+
form = MastectomyOrLumpectomyHistoryForm(
55+
QueryDict(
56+
urlencode(
57+
{
58+
"right_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.LUMPECTOMY,
59+
"left_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.LUMPECTOMY,
60+
"right_breast_other_surgery": [
61+
MastectomyOrLumpectomyHistoryItem.Surgery.RECONSTRUCTION,
62+
MastectomyOrLumpectomyHistoryItem.Surgery.SYMMETRISATION,
63+
MastectomyOrLumpectomyHistoryItem.Surgery.NO_OTHER_SURGERY,
64+
],
65+
"left_breast_other_surgery": [
66+
MastectomyOrLumpectomyHistoryItem.Surgery.RECONSTRUCTION
67+
],
68+
"surgery_reason": MastectomyOrLumpectomyHistoryItem.SurgeryReason.GENDER_AFFIRMATION,
69+
},
70+
doseq=True,
71+
),
72+
),
73+
participant=appointment.participant,
74+
)
75+
76+
assert not form.is_valid()
77+
assert form.errors == {
78+
"right_breast_other_surgery": [
79+
'Unselect "No other surgery" in order to select other options'
80+
],
81+
}
82+
83+
def test_left_breast_other_surgery_no_other_surgery_and_others(self, clinical_user):
84+
appointment = AppointmentFactory()
85+
request = RequestFactory().get("/test-form")
86+
request.user = clinical_user
87+
88+
form = MastectomyOrLumpectomyHistoryForm(
89+
QueryDict(
90+
urlencode(
91+
{
92+
"right_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.LUMPECTOMY,
93+
"left_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.LUMPECTOMY,
94+
"right_breast_other_surgery": [
95+
MastectomyOrLumpectomyHistoryItem.Surgery.RECONSTRUCTION,
96+
],
97+
"left_breast_other_surgery": [
98+
MastectomyOrLumpectomyHistoryItem.Surgery.RECONSTRUCTION,
99+
MastectomyOrLumpectomyHistoryItem.Surgery.NO_OTHER_SURGERY,
100+
],
101+
"surgery_reason": MastectomyOrLumpectomyHistoryItem.SurgeryReason.GENDER_AFFIRMATION,
102+
},
103+
doseq=True,
104+
),
105+
),
106+
participant=appointment.participant,
107+
)
108+
109+
assert not form.is_valid()
110+
assert form.errors == {
111+
"left_breast_other_surgery": [
112+
'Unselect "No other surgery" in order to select other options'
113+
],
114+
}
115+
116+
@pytest.mark.parametrize(
117+
"year_of_surgery",
118+
[
119+
-1,
120+
1900,
121+
datetime.date.today().year - 81,
122+
datetime.date.today().year + 1,
123+
3000,
124+
],
125+
)
126+
def test_year_of_surgery_outside_range(self, clinical_user, year_of_surgery):
127+
appointment = AppointmentFactory()
128+
request = RequestFactory().get("/test-form")
129+
request.user = clinical_user
130+
131+
form = MastectomyOrLumpectomyHistoryForm(
132+
QueryDict(
133+
urlencode(
134+
{
135+
"right_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.LUMPECTOMY,
136+
"left_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.LUMPECTOMY,
137+
"right_breast_other_surgery": [
138+
MastectomyOrLumpectomyHistoryItem.Surgery.RECONSTRUCTION
139+
],
140+
"left_breast_other_surgery": [
141+
MastectomyOrLumpectomyHistoryItem.Surgery.RECONSTRUCTION
142+
],
143+
"year_of_surgery": year_of_surgery,
144+
"surgery_reason": MastectomyOrLumpectomyHistoryItem.SurgeryReason.GENDER_AFFIRMATION,
145+
},
146+
doseq=True,
147+
),
148+
),
149+
participant=appointment.participant,
150+
)
151+
152+
assert not form.is_valid()
153+
assert form.errors == {
154+
"year_of_surgery": ["year_outside_range_error_message"],
155+
}
156+
157+
def test_year_of_surgery_invalid(self, clinical_user):
158+
appointment = AppointmentFactory()
159+
request = RequestFactory().get("/test-form")
160+
request.user = clinical_user
161+
162+
form = MastectomyOrLumpectomyHistoryForm(
163+
QueryDict(
164+
urlencode(
165+
{
166+
"right_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.LUMPECTOMY,
167+
"left_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.LUMPECTOMY,
168+
"right_breast_other_surgery": [
169+
MastectomyOrLumpectomyHistoryItem.Surgery.RECONSTRUCTION
170+
],
171+
"left_breast_other_surgery": [
172+
MastectomyOrLumpectomyHistoryItem.Surgery.RECONSTRUCTION
173+
],
174+
"year_of_surgery": "invalid value for year_of_surgery",
175+
"surgery_reason": MastectomyOrLumpectomyHistoryItem.SurgeryReason.GENDER_AFFIRMATION,
176+
},
177+
doseq=True,
178+
),
179+
),
180+
participant=appointment.participant,
181+
)
182+
183+
assert not form.is_valid()
184+
assert form.errors == {
185+
"year_of_surgery": ["year_invalid_format_error_message"],
186+
}
187+
188+
def test_other_reason_without_details(self, clinical_user):
189+
appointment = AppointmentFactory()
190+
request = RequestFactory().get("/test-form")
191+
request.user = clinical_user
192+
193+
form = MastectomyOrLumpectomyHistoryForm(
194+
QueryDict(
195+
urlencode(
196+
{
197+
"right_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.LUMPECTOMY,
198+
"left_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.LUMPECTOMY,
199+
"right_breast_other_surgery": [
200+
MastectomyOrLumpectomyHistoryItem.Surgery.RECONSTRUCTION
201+
],
202+
"left_breast_other_surgery": [
203+
MastectomyOrLumpectomyHistoryItem.Surgery.RECONSTRUCTION
204+
],
205+
"surgery_reason": MastectomyOrLumpectomyHistoryItem.SurgeryReason.OTHER_REASON,
206+
},
207+
doseq=True,
208+
),
209+
),
210+
participant=appointment.participant,
211+
)
212+
213+
assert not form.is_valid()
214+
assert form.errors == {
215+
"surgery_other_reason_details": ["Provide details of the surgery"],
216+
}
217+
218+
def test_other_reason_details_when_not_other_reason(self, clinical_user):
219+
appointment = AppointmentFactory()
220+
request = RequestFactory().get("/test-form")
221+
request.user = clinical_user
222+
223+
form = MastectomyOrLumpectomyHistoryForm(
224+
QueryDict(
225+
urlencode(
226+
{
227+
"right_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.LUMPECTOMY,
228+
"left_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.MASTECTOMY_TISSUE_REMAINING,
229+
"right_breast_other_surgery": [
230+
MastectomyOrLumpectomyHistoryItem.Surgery.RECONSTRUCTION,
231+
],
232+
"left_breast_other_surgery": [
233+
MastectomyOrLumpectomyHistoryItem.Surgery.SYMMETRISATION,
234+
],
235+
"surgery_reason": MastectomyOrLumpectomyHistoryItem.SurgeryReason.GENDER_AFFIRMATION,
236+
"surgery_other_reason_details": "surgery other details",
237+
},
238+
doseq=True,
239+
),
240+
),
241+
participant=appointment.participant,
242+
)
243+
244+
assert form.is_valid()
245+
246+
obj = form.create(appointment=appointment, request=request)
247+
obj.refresh_from_db()
248+
249+
assert obj.appointment == appointment
250+
assert (
251+
obj.right_breast_procedure
252+
== MastectomyOrLumpectomyHistoryItem.Procedure.LUMPECTOMY
253+
)
254+
assert (
255+
obj.left_breast_procedure
256+
== MastectomyOrLumpectomyHistoryItem.Procedure.MASTECTOMY_TISSUE_REMAINING
257+
)
258+
assert obj.right_breast_other_surgery == [
259+
MastectomyOrLumpectomyHistoryItem.Surgery.RECONSTRUCTION,
260+
]
261+
assert obj.left_breast_other_surgery == [
262+
MastectomyOrLumpectomyHistoryItem.Surgery.SYMMETRISATION,
263+
]
264+
assert obj.year_of_surgery is None
265+
assert (
266+
obj.surgery_reason
267+
== MastectomyOrLumpectomyHistoryItem.SurgeryReason.GENDER_AFFIRMATION
268+
)
269+
assert obj.surgery_other_reason_details == ""
270+
assert obj.additional_details == ""
271+
46272
@pytest.mark.parametrize(
47273
"data",
48274
[
275+
{
276+
"right_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.MASTECTOMY_TISSUE_REMAINING,
277+
"left_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.MASTECTOMY_NO_TISSUE_REMAINING,
278+
"right_breast_other_surgery": [
279+
MastectomyOrLumpectomyHistoryItem.Surgery.RECONSTRUCTION,
280+
MastectomyOrLumpectomyHistoryItem.Surgery.SYMMETRISATION,
281+
],
282+
"left_breast_other_surgery": [
283+
MastectomyOrLumpectomyHistoryItem.Surgery.NO_OTHER_SURGERY,
284+
],
285+
"surgery_reason": MastectomyOrLumpectomyHistoryItem.SurgeryReason.RISK_REDUCTION,
286+
},
287+
{
288+
"right_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.NO_PROCEDURE,
289+
"left_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.LUMPECTOMY,
290+
"right_breast_other_surgery": [
291+
MastectomyOrLumpectomyHistoryItem.Surgery.NO_OTHER_SURGERY
292+
],
293+
"left_breast_other_surgery": [
294+
MastectomyOrLumpectomyHistoryItem.Surgery.RECONSTRUCTION,
295+
MastectomyOrLumpectomyHistoryItem.Surgery.SYMMETRISATION,
296+
],
297+
"year_of_surgery": datetime.date.today().year - 80,
298+
"surgery_reason": MastectomyOrLumpectomyHistoryItem.SurgeryReason.GENDER_AFFIRMATION,
299+
},
49300
{
50301
"right_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.LUMPECTOMY,
51302
"left_breast_procedure": MastectomyOrLumpectomyHistoryItem.Procedure.LUMPECTOMY,
@@ -55,7 +306,10 @@ def test_no_data(self, clinical_user):
55306
"left_breast_other_surgery": [
56307
MastectomyOrLumpectomyHistoryItem.Surgery.RECONSTRUCTION
57308
],
58-
"surgery_reason": MastectomyOrLumpectomyHistoryItem.SurgeryReason.RISK_REDUCTION,
309+
"year_of_surgery": datetime.date.today().year,
310+
"surgery_reason": MastectomyOrLumpectomyHistoryItem.SurgeryReason.OTHER_REASON,
311+
"surgery_other_reason_details": "surgery other details",
312+
"additional_details": "additional details",
59313
},
60314
],
61315
)

manage_breast_screening/mammograms/tests/presenters/test_mastectomy_or_lumpectomy_history_item_presenter.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_single(self):
1818
MastectomyOrLumpectomyHistoryItem.Surgery.RECONSTRUCTION
1919
],
2020
left_breast_other_surgery=[
21-
MastectomyOrLumpectomyHistoryItem.Surgery.NO_SURGERY
21+
MastectomyOrLumpectomyHistoryItem.Surgery.NO_OTHER_SURGERY
2222
],
2323
year_of_surgery=2018,
2424
surgery_reason=MastectomyOrLumpectomyHistoryItem.SurgeryReason.RISK_REDUCTION,
@@ -42,7 +42,7 @@ def test_single(self):
4242
"text": "Other surgery",
4343
},
4444
"value": {
45-
"html": "Right breast: Reconstruction<br>Left breast: No surgery",
45+
"html": "Right breast: Reconstruction<br>Left breast: No other surgery",
4646
},
4747
},
4848
{
@@ -61,6 +61,14 @@ def test_single(self):
6161
"html": "Risk reduction",
6262
},
6363
},
64+
{
65+
"key": {
66+
"text": "Surgery other reason details",
67+
},
68+
"value": {
69+
"text": "",
70+
},
71+
},
6472
{
6573
"key": {
6674
"text": "Additional details",

0 commit comments

Comments
 (0)