Skip to content

Commit c25dce7

Browse files
committed
added tests
1 parent 0724813 commit c25dce7

File tree

6 files changed

+236
-23
lines changed

6 files changed

+236
-23
lines changed

care/facility/api/serializers/patient_consultation.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
SuggestionChoices,
5454
)
5555
from care.facility.models.patient_consultation import (
56+
ConsentType,
5657
PatientConsent,
5758
PatientConsultation,
5859
)
@@ -898,6 +899,30 @@ class Meta:
898899
"archived_date",
899900
)
900901

902+
def validate(self, attrs):
903+
if attrs.get("type") == ConsentType.PATIENT_CODE_STATUS and not attrs.get(
904+
"patient_code_status"
905+
):
906+
raise ValidationError(
907+
{
908+
"patient_code_status": [
909+
"This field is required for Patient Code Status Consent"
910+
]
911+
}
912+
)
913+
914+
if attrs.get("type") != ConsentType.PATIENT_CODE_STATUS and attrs.get(
915+
"patient_code_status"
916+
):
917+
raise ValidationError(
918+
{
919+
"patient_code_status": [
920+
"This field is not required for this type of Consent"
921+
]
922+
}
923+
)
924+
return attrs
925+
901926
def clear_existing_records(self, consultation, type, self_id=None):
902927
consents = PatientConsent.objects.filter(
903928
consultation=consultation, type=type

care/facility/migrations/0441_remove_patientconsultation_consent_records_and_more.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,26 @@ def reverse_migrate(apps, schema_editor):
182182
name="unique_consultation_consent",
183183
),
184184
),
185+
migrations.AddConstraint(
186+
model_name="patientconsent",
187+
constraint=models.CheckConstraint(
188+
check=models.Q(
189+
models.Q(("type", 2), _negated=True),
190+
("patient_code_status__isnull", False),
191+
_connector="OR",
192+
),
193+
name="patient_code_status_required",
194+
),
195+
),
196+
migrations.AddConstraint(
197+
model_name="patientconsent",
198+
constraint=models.CheckConstraint(
199+
check=models.Q(
200+
("type", 2), ("patient_code_status__isnull", True), _connector="OR"
201+
),
202+
name="patient_code_status_not_required",
203+
),
204+
),
185205
migrations.RunPython(migrate_consents, reverse_code=reverse_migrate),
186206
migrations.RemoveField(
187207
model_name="patientconsultation",

care/facility/models/patient_consultation.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,17 @@ class Meta:
415415
fields=["consultation", "type"],
416416
name="unique_consultation_consent",
417417
condition=models.Q(archived=False),
418-
)
418+
),
419+
models.CheckConstraint(
420+
name="patient_code_status_required",
421+
check=~models.Q(type=ConsentType.PATIENT_CODE_STATUS)
422+
| models.Q(patient_code_status__isnull=False),
423+
),
424+
models.CheckConstraint(
425+
name="patient_code_status_not_required",
426+
check=models.Q(type=ConsentType.PATIENT_CODE_STATUS)
427+
| models.Q(patient_code_status__isnull=True),
428+
),
419429
]
420430

421431
def __str__(self) -> str:
@@ -449,7 +459,7 @@ def has_object_read_permission(self, request):
449459
return (
450460
request.user.is_superuser
451461
or (
452-
self.patient.facility
462+
self.consultation.patient.facility
453463
and request.user in self.consultation.patient.facility.users.all()
454464
)
455465
or (

care/facility/tests/test_file_upload.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import json
2-
31
from rest_framework import status
42
from rest_framework.test import APITestCase
53

@@ -64,31 +62,16 @@ def setUpTestData(cls) -> None:
6462
cls.district, cls.facility, local_body=cls.local_body
6563
)
6664
cls.consultation = cls.create_consultation(cls.patient, cls.facility)
65+
cls.consent = cls.create_patient_consent(cls.consultation)
6766

6867
def test_consent_file_upload(self):
69-
response = self.client.patch(
70-
f"/api/v1/consultation/{self.consultation.external_id}/",
71-
{
72-
"consent_records": json.dumps(
73-
[
74-
{
75-
"id": "consent-12345",
76-
"type": 2,
77-
"patient_code_status": 1,
78-
}
79-
]
80-
)
81-
},
82-
)
83-
self.assertEqual(response.status_code, status.HTTP_200_OK)
84-
8568
upload_response = self.client.post(
8669
"/api/v1/files/",
8770
{
8871
"original_name": "test.pdf",
8972
"file_type": "CONSENT_RECORD",
9073
"name": "Test File",
91-
"associating_id": "consent-12345",
74+
"associating_id": self.consent.external_id,
9275
"file_category": "UNSPECIFIED",
9376
"mime_type": "application/pdf",
9477
},
@@ -97,11 +80,12 @@ def test_consent_file_upload(self):
9780
self.assertEqual(upload_response.status_code, status.HTTP_201_CREATED)
9881

9982
self.assertEqual(
100-
FileUpload.objects.filter(associating_id="consent-12345").count(), 1
83+
FileUpload.objects.filter(associating_id=self.consent.external_id).count(),
84+
1,
10185
)
10286

10387
all_files = self.client.get(
104-
"/api/v1/files/?associating_id=consent-12345&file_type=CONSENT_RECORD&is_archived=false"
88+
f"/api/v1/files/?associating_id={self.consent.external_id}&file_type=CONSENT_RECORD&is_archived=false"
10589
)
10690

10791
self.assertEqual(all_files.status_code, status.HTTP_200_OK)
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
from rest_framework.test import APITestCase
2+
3+
from care.facility.models.patient_consultation import ConsentType, PatientCodeStatusType
4+
from care.utils.tests.test_utils import TestUtils
5+
6+
7+
class TestPatientConsent(TestUtils, APITestCase):
8+
@classmethod
9+
def setUpTestData(cls) -> None:
10+
cls.state = cls.create_state()
11+
cls.district = cls.create_district(cls.state)
12+
cls.local_body = cls.create_local_body(cls.district)
13+
cls.super_user = cls.create_super_user("su", cls.district)
14+
cls.facility = cls.create_facility(cls.super_user, cls.district, cls.local_body)
15+
cls.location = cls.create_asset_location(cls.facility)
16+
cls.user = cls.create_user("staff1", cls.district, home_facility=cls.facility)
17+
cls.doctor = cls.create_user(
18+
"doctor", cls.district, home_facility=cls.facility, user_type=15
19+
)
20+
cls.patient1 = cls.create_patient(cls.district, cls.facility)
21+
cls.consultation = cls.create_consultation(
22+
cls.patient1, cls.facility, cls.doctor
23+
)
24+
cls.patient2 = cls.create_patient(cls.district, cls.facility)
25+
cls.consultation2 = cls.create_consultation(
26+
cls.patient2, cls.facility, cls.doctor
27+
)
28+
29+
def test_create_consent(self):
30+
response = self.client.post(
31+
f"/api/v1/consultation/{self.consultation.external_id}/consents/",
32+
{
33+
"type": ConsentType.CONSENT_FOR_ADMISSION,
34+
},
35+
)
36+
self.assertEqual(response.status_code, 201)
37+
self.assertEqual(response.data["type"], ConsentType.CONSENT_FOR_ADMISSION)
38+
39+
def test_list_consent(self):
40+
response = self.client.get(
41+
f"/api/v1/consultation/{self.consultation.external_id}/consents/"
42+
)
43+
self.assertEqual(response.status_code, 200)
44+
self.assertEqual(len(response.data.get("results")), 0)
45+
46+
self.client.post(
47+
f"/api/v1/consultation/{self.consultation.external_id}/consents/",
48+
{
49+
"type": ConsentType.CONSENT_FOR_ADMISSION,
50+
},
51+
)
52+
response = self.client.get(
53+
f"/api/v1/consultation/{self.consultation.external_id}/consents/"
54+
)
55+
self.assertEqual(response.status_code, 200)
56+
self.assertEqual(len(response.data.get("results")), 1)
57+
58+
def test_retrieve_consent(self):
59+
response = self.client.post(
60+
f"/api/v1/consultation/{self.consultation.external_id}/consents/",
61+
{
62+
"type": ConsentType.CONSENT_FOR_ADMISSION,
63+
},
64+
)
65+
self.assertEqual(response.status_code, 201)
66+
response = self.client.get(
67+
f"/api/v1/consultation/{self.consultation.external_id}/consents/{response.data['id']}/"
68+
)
69+
self.assertEqual(response.status_code, 200)
70+
self.assertEqual(response.data["type"], ConsentType.CONSENT_FOR_ADMISSION)
71+
72+
def test_update_consent(self):
73+
response = self.client.post(
74+
f"/api/v1/consultation/{self.consultation.external_id}/consents/",
75+
{
76+
"type": ConsentType.CONSENT_FOR_ADMISSION,
77+
},
78+
)
79+
self.assertEqual(response.status_code, 201)
80+
response = self.client.patch(
81+
f"/api/v1/consultation/{self.consultation.external_id}/consents/{response.data['id']}/",
82+
{
83+
"type": ConsentType.CONSENT_FOR_PROCEDURE,
84+
},
85+
)
86+
self.assertEqual(response.status_code, 200)
87+
self.assertEqual(response.data["type"], ConsentType.CONSENT_FOR_PROCEDURE)
88+
89+
def test_auto_archive_consents(self):
90+
response_1 = self.client.post(
91+
f"/api/v1/consultation/{self.consultation.external_id}/consents/",
92+
{
93+
"type": ConsentType.PATIENT_CODE_STATUS,
94+
"patient_code_status": PatientCodeStatusType.ACTIVE_TREATMENT,
95+
},
96+
)
97+
self.assertEqual(response_1.status_code, 201)
98+
99+
upload_response = self.client.post(
100+
"/api/v1/files/",
101+
{
102+
"original_name": "test.pdf",
103+
"file_type": "CONSENT_RECORD",
104+
"name": "Test File",
105+
"associating_id": response_1.data["id"],
106+
"file_category": "UNSPECIFIED",
107+
"mime_type": "application/pdf",
108+
},
109+
)
110+
111+
self.assertEqual(upload_response.status_code, 201)
112+
113+
response_2 = self.client.post(
114+
f"/api/v1/consultation/{self.consultation.external_id}/consents/",
115+
{
116+
"type": ConsentType.PATIENT_CODE_STATUS,
117+
"patient_code_status": PatientCodeStatusType.COMFORT_CARE,
118+
},
119+
)
120+
121+
self.assertEqual(response_2.status_code, 201)
122+
123+
response = self.client.get(
124+
f"/api/v1/consultation/{self.consultation.external_id}/consents/{response_1.data['id']}/"
125+
)
126+
127+
self.assertEqual(response.status_code, 200)
128+
self.assertEqual(response.data["archived"], True)
129+
130+
files = self.client.get(
131+
f"/api/v1/files/?associating_id={response_1.data['id']}&file_type=CONSENT_RECORD&is_archived=false"
132+
)
133+
134+
self.assertEqual(files.status_code, 200)
135+
self.assertEqual(files.data["count"], 0)
136+
137+
def test_patient_code_status_constraint(self):
138+
response = self.client.post(
139+
f"/api/v1/consultation/{self.consultation.external_id}/consents/",
140+
{
141+
"type": ConsentType.PATIENT_CODE_STATUS,
142+
},
143+
)
144+
145+
self.assertEqual(response.status_code, 400)
146+
147+
response = self.client.post(
148+
f"/api/v1/consultation/{self.consultation.external_id}/consents/",
149+
{
150+
"type": ConsentType.CONSENT_FOR_ADMISSION,
151+
"patient_code_status": PatientCodeStatusType.ACTIVE_TREATMENT,
152+
},
153+
)
154+
155+
self.assertEqual(response.status_code, 400)

care/utils/tests/test_utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
ICD11Diagnosis,
3333
)
3434
from care.facility.models.patient import RationCardCategory
35+
from care.facility.models.patient_consultation import (
36+
ConsentType,
37+
PatientCodeStatusType,
38+
PatientConsent,
39+
)
3540
from care.users.models import District, State
3641

3742

@@ -448,6 +453,20 @@ def create_consultation_diagnosis(
448453
data.update(kwargs)
449454
return ConsultationDiagnosis.objects.create(**data)
450455

456+
@classmethod
457+
def create_patient_consent(
458+
cls,
459+
consultation: PatientConsultation,
460+
**kwargs,
461+
):
462+
data = {
463+
"consultation": consultation,
464+
"type": ConsentType.PATIENT_CODE_STATUS,
465+
"patient_code_status": PatientCodeStatusType.COMFORT_CARE,
466+
}
467+
data.update(kwargs)
468+
return PatientConsent.objects.create(**data)
469+
451470
@classmethod
452471
def clone_object(cls, obj, save=True):
453472
new_obj = obj._meta.model.objects.get(pk=obj.id)

0 commit comments

Comments
 (0)