1+ import datetime
12from urllib .parse import urlencode
23
34import pytest
1718
1819@pytest .mark .django_db
1920class 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 )
0 commit comments