Skip to content

Commit 1665396

Browse files
committed
Benign lumps: Add tests for form object
1 parent 13807b6 commit 1665396

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import datetime
2+
from urllib.parse import urlencode
3+
4+
import pytest
5+
from django.http import QueryDict
6+
from django.test import RequestFactory
7+
8+
from manage_breast_screening.core.models import AuditLog
9+
from manage_breast_screening.mammograms.forms.benign_lump_history_item_form import (
10+
BenignLumpHistoryItemForm,
11+
)
12+
from manage_breast_screening.participants.models.benign_lump_history_item import (
13+
BenignLumpHistoryItem,
14+
)
15+
from manage_breast_screening.participants.tests.factories import AppointmentFactory
16+
17+
18+
def _form_data(data):
19+
return QueryDict(urlencode(data, doseq=True))
20+
21+
22+
@pytest.mark.django_db
23+
class TestBenignLumpHistoryItemForm:
24+
def test_missing_required_fields(self):
25+
form = BenignLumpHistoryItemForm(_form_data({}))
26+
27+
assert not form.is_valid()
28+
assert form.errors == {
29+
"left_breast_procedures": [
30+
"Select which procedures they have had in the left breast"
31+
],
32+
"right_breast_procedures": [
33+
"Select which procedures they have had in the right breast"
34+
],
35+
"procedure_location": ["Select where the tests and treatment were done"],
36+
}
37+
38+
@pytest.mark.parametrize(
39+
("location", "detail_field"),
40+
tuple(BenignLumpHistoryItemForm.LOCATION_DETAIL_FIELDS.items()),
41+
)
42+
def test_requires_location_details_for_selected_location(
43+
self, location, detail_field
44+
):
45+
form = BenignLumpHistoryItemForm(
46+
_form_data(
47+
[
48+
(
49+
"procedure_location",
50+
location,
51+
),
52+
]
53+
)
54+
)
55+
56+
assert not form.is_valid()
57+
assert form.errors.get(detail_field) == ["This field is required."]
58+
59+
@pytest.mark.parametrize(
60+
"procedure_year",
61+
[
62+
datetime.date.today().year - 80 - 1,
63+
datetime.date.today().year + 1,
64+
],
65+
)
66+
def test_procedure_year_must_be_within_range(self, procedure_year):
67+
max_year = datetime.date.today().year
68+
min_year = max_year - 80
69+
year_outside_range_message = f"Year must be between {min_year} and {max_year}"
70+
71+
form = BenignLumpHistoryItemForm(
72+
_form_data(
73+
[
74+
("procedure_year", procedure_year),
75+
]
76+
)
77+
)
78+
79+
assert not form.is_valid()
80+
assert form.errors.get("procedure_year") == [year_outside_range_message]
81+
82+
def test_create_persists_data_and_audits(self, clinical_user):
83+
appointment = AppointmentFactory()
84+
request = RequestFactory().post("/test-form")
85+
request.user = clinical_user
86+
87+
data = [
88+
(
89+
"left_breast_procedures",
90+
BenignLumpHistoryItem.Procedure.NEEDLE_BIOPSY,
91+
),
92+
(
93+
"right_breast_procedures",
94+
BenignLumpHistoryItem.Procedure.NO_PROCEDURES,
95+
),
96+
("procedure_year", datetime.date.today().year - 1),
97+
(
98+
"procedure_location",
99+
BenignLumpHistoryItem.ProcedureLocation.NHS_HOSPITAL,
100+
),
101+
("nhs_hospital_details", "St Thomas' Hospital"),
102+
("additional_details", "Additional details."),
103+
]
104+
form = BenignLumpHistoryItemForm(_form_data(data))
105+
assert form.is_valid()
106+
107+
existing_log_count = AuditLog.objects.count()
108+
obj = form.create(appointment=appointment, request=request)
109+
assert AuditLog.objects.count() == existing_log_count + 1
110+
audit_log = AuditLog.objects.filter(
111+
object_id=obj.pk, operation=AuditLog.Operations.CREATE
112+
).first()
113+
assert audit_log.actor == clinical_user
114+
115+
obj.refresh_from_db()
116+
assert obj.appointment == appointment
117+
assert obj.left_breast_procedures == [
118+
BenignLumpHistoryItem.Procedure.NEEDLE_BIOPSY
119+
]
120+
assert obj.right_breast_procedures == [
121+
BenignLumpHistoryItem.Procedure.NO_PROCEDURES
122+
]
123+
assert obj.procedure_year == data[2][1]
124+
assert obj.procedure_location == (
125+
BenignLumpHistoryItem.ProcedureLocation.NHS_HOSPITAL
126+
)
127+
assert obj.procedure_location_details == "St Thomas' Hospital"
128+
assert obj.additional_details == "Additional details."

0 commit comments

Comments
 (0)