Skip to content

Commit

Permalink
test for various endpoints in the external_result module (#2034)
Browse files Browse the repository at this point in the history
* added test for external result api

* updated external test result tests

* added filtering test in list endpoint

* updated upsert-endpoint tests

* removed print statement

* fixing lint issue

---------

Co-authored-by: Aakash Singh <[email protected]>
  • Loading branch information
DraKen0009 and sainak authored Apr 12, 2024
1 parent fc1f375 commit e31c084
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 91 deletions.
146 changes: 146 additions & 0 deletions care/facility/tests/test_external_result_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
from rest_framework import status
from rest_framework.test import APITestCase

from care.facility.models import PatientExternalTest
from care.utils.tests.test_utils import TestUtils


class PatientExternalTestViewSetTestCase(TestUtils, APITestCase):
@classmethod
def setUpTestData(cls) -> None:
cls.state = cls.create_state()
cls.district = cls.create_district(cls.state)
cls.local_body = cls.create_local_body(cls.district)
cls.ward = cls.create_ward(cls.local_body)
cls.user = cls.create_super_user("su", cls.district)
cls.external_result = cls.create_patient_external_test(
cls.district, cls.local_body, cls.ward, name="TEST_1"
)

def test_list_external_result(self):
state2 = self.create_state()
district2 = self.create_district(state2)
local_body2 = self.create_local_body(district2)
ward2 = self.create_ward(local_body2)

self.create_patient_external_test(
district2, local_body2, ward2, name="TEST_2", mobile_number="9999988888"
)
self.create_patient_external_test(
self.district, self.local_body, self.ward, srf_id="ID001"
)

response = self.client.get("/api/v1/external_result/")
patient_external_test = PatientExternalTest.objects.all()
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["count"], len(patient_external_test))

response = self.client.get("/api/v1/external_result/?name=TEST_2")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["results"][0]["name"], "TEST_2")

response = self.client.get("/api/v1/external_result/?srf_id=ID001")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["results"][0]["srf_id"], "ID001")

response = self.client.get(f"/api/v1/external_result/?ward__id={self.ward.id}")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.data["results"][0]["ward_object"]["name"], self.ward.name
)

response = self.client.get(
f"/api/v1/external_result/?district__id={self.district.id}"
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.data["results"][0]["district_object"]["name"], self.district.name
)

response = self.client.get(
f"/api/v1/external_result/?local_body={self.local_body.id}"
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.data["results"][0]["local_body_object"]["name"],
self.local_body.name,
)

response = self.client.get("/api/v1/external_result/?mobile_number=9999988888")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data["results"][0]["mobile_number"], "9999988888")
self.assertEqual(response.data["results"][0]["name"], "TEST_2")

def test_retrieve_external_result(self):
response = self.client.get(
f"/api/v1/external_result/{self.external_result.id}/"
)
patient_external_test = PatientExternalTest.objects.get(
id=self.external_result.id
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data.get("id"), patient_external_test.id)
self.assertEqual(response.data.get("name"), patient_external_test.name)

def test_update_patient_external_result(self):
sample_data = {
"address": "Upload test address Updated",
}
response = self.client.put(
f"/api/v1/external_result/{self.external_result.id}/", sample_data
)
self.external_result.refresh_from_db()
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(self.external_result.address, sample_data["address"])

def test_update_patch_patient_external_result(self):
sample_data = {
"address": "Upload test address Updated patch",
}
response = self.client.patch(
f"/api/v1/external_result/{self.external_result.id}/", sample_data
)
self.external_result.refresh_from_db()
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(self.external_result.address, sample_data["address"])

def test_delete_patient_external_result(self):
response = self.client.delete(
f"/api/v1/external_result/{self.external_result.id}/"
)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertFalse(
PatientExternalTest.objects.filter(id=self.external_result.id).exists()
)

def test_no_data_upload(self):
response = self.client.post(
"/api/v1/external_result/bulk_upsert/", sample_data={}
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data["sample_tests"], "No Data was provided")

def test_different_district_upload(self):
state2 = self.create_state()
district2 = self.create_district(state2)
external_test_data = self.get_patient_external_test_data(
str(district2), str(self.local_body.name), self.ward.number
).copy()
sample_data = {"sample_tests": [external_test_data]}
response = self.client.post(
"/api/v1/external_result/bulk_upsert/", sample_data, format="json"
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data["Error"], "User must belong to same district")

def test_same_district_upload(self):
external_test_data = self.get_patient_external_test_data(
str(self.district), str(self.local_body.name), self.ward.number
).copy()
external_test_data.update({"local_body_type": "municipality"})
sample_data = {"sample_tests": [external_test_data]}
response = self.client.post(
"/api/v1/external_result/bulk_upsert/", sample_data, format="json"
)
self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
91 changes: 0 additions & 91 deletions care/facility/tests/test_upload_external_result.py

This file was deleted.

34 changes: 34 additions & 0 deletions care/utils/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Facility,
LocalBody,
PatientConsultation,
PatientExternalTest,
PatientRegistration,
User,
Ward,
Expand Down Expand Up @@ -345,6 +346,39 @@ def create_consultation(
patient.save()
return consultation

@classmethod
def get_patient_external_test_data(cls, district, local_body, ward) -> dict:
return {
"district": district,
"srf_id": "00/EKM/0000",
"name": now().timestamp(),
"age": 24,
"age_in": "years",
"gender": "m",
"mobile_number": 8888888888,
"address": "Upload test address",
"ward": ward,
"local_body": local_body,
"source": "Secondary contact aparna",
"sample_collection_date": "2020-10-14",
"result_date": "2020-10-14",
"test_type": "Antigen",
"lab_name": "Karothukuzhi Laboratory",
"sample_type": "Ag-SD_Biosensor_Standard_Q_COVID-19_Ag_detection_kit",
"patient_status": "Asymptomatic",
"is_repeat": True,
"patient_category": "Cat 17: All individuals who wish to get themselves tested",
"result": "Negative",
}

@classmethod
def create_patient_external_test(
cls, district: District, local_body: LocalBody, ward: Ward, **kwargs
) -> PatientExternalTest:
data = cls.get_patient_external_test_data(district, local_body, ward).copy()
data.update(kwargs)
return PatientExternalTest.objects.create(**data)

@classmethod
def create_asset_location(cls, facility: Facility, **kwargs) -> AssetLocation:
data = {
Expand Down

0 comments on commit e31c084

Please sign in to comment.