Skip to content

Commit 3a34775

Browse files
fix(patient): adjusted field types (#99)
* adjusted field types * pre commit fixes
1 parent 95645d9 commit 3a34775

File tree

5 files changed

+112
-11
lines changed

5 files changed

+112
-11
lines changed

tmh_registry/registry/api/serializers.py

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ class Meta:
5858
model = PatientHospitalMapping
5959
fields = ["patient_hospital_id", "hospital_id"]
6060

61+
def to_representation(self, instance):
62+
data = super(
63+
PatientHospitalMappingPatientSerializer, self
64+
).to_representation(instance)
65+
66+
data["patient_hospital_id"] = (
67+
int(data["patient_hospital_id"])
68+
if data["patient_hospital_id"]
69+
else None
70+
)
71+
72+
return data
73+
6174

6275
class ReadPatientSerializer(ModelSerializer):
6376
age = IntegerField(allow_null=True)
@@ -135,6 +148,35 @@ def to_representation(self, instance):
135148
return serializer.data
136149

137150
def create(self, validated_data):
151+
try:
152+
input_national_id = validated_data.get("national_id")
153+
validated_data["national_id"] = (
154+
int(input_national_id) if input_national_id else None
155+
)
156+
except ValueError:
157+
raise ValidationError(
158+
{"error": "The 'national_id' field should be an integer."}
159+
)
160+
161+
try:
162+
input_phone_1 = validated_data.get("phone_1")
163+
validated_data["phone_1"] = (
164+
int(input_phone_1) if input_phone_1 else None
165+
)
166+
except ValueError:
167+
raise ValidationError(
168+
{"error": "The 'phone_1' field should be an integer."}
169+
)
170+
171+
try:
172+
input_phone_2 = validated_data.get("phone_2")
173+
validated_data["phone_2"] = (
174+
int(input_phone_2) if input_phone_2 else None
175+
)
176+
except ValueError:
177+
raise ValidationError(
178+
{"error": "The 'phone_2' field should be an integer."}
179+
)
138180

139181
if not validated_data.get("year_of_birth", None):
140182
if validated_data["age"]:
@@ -165,7 +207,18 @@ def create(self, validated_data):
165207
{"error": "The patient needs to be registered to a hospital."}
166208
)
167209

168-
patient_hospital_id = validated_data.pop("patient_hospital_id", None)
210+
try:
211+
patient_hospital_id = validated_data.pop("patient_hospital_id")
212+
patient_hospital_id = (
213+
int(patient_hospital_id) if patient_hospital_id else None
214+
)
215+
except ValueError:
216+
raise ValidationError(
217+
{
218+
"error": "The 'patient_hospital_id' field should be an integer."
219+
}
220+
)
221+
169222
if patient_hospital_id:
170223
if PatientHospitalMapping.objects.filter(
171224
hospital_id=hospital.id,
@@ -179,7 +232,7 @@ def create(self, validated_data):
179232
)
180233
else:
181234
raise ValidationError(
182-
{"error": "The patient needs to be registered to a hospital."}
235+
{"error": "The 'patient_hospital_id' should be provided."}
183236
)
184237

185238
validated_data["gender"] = (
@@ -211,6 +264,19 @@ class Meta:
211264
model = PatientHospitalMapping
212265
fields = ["patient", "hospital", "patient_hospital_id"]
213266

267+
def to_representation(self, instance):
268+
data = super(
269+
PatientHospitalMappingReadSerializer, self
270+
).to_representation(instance)
271+
272+
data["patient_hospital_id"] = (
273+
int(data["patient_hospital_id"])
274+
if data["patient_hospital_id"]
275+
else None
276+
)
277+
278+
return data
279+
214280

215281
class PatientHospitalMappingWriteSerializer(ModelSerializer):
216282
patient_id = PrimaryKeyRelatedField(queryset=Patient.objects.all())
@@ -225,6 +291,19 @@ def to_representation(self, instance):
225291
return serializer.data
226292

227293
def create(self, validated_data):
294+
try:
295+
validated_data["patient_hospital_id"] = int(
296+
validated_data.get("patient_hospital_id")
297+
)
298+
except ValueError:
299+
raise ValidationError(
300+
{
301+
"error": "The 'patient_hospital_id' field should be an integer."
302+
}
303+
)
304+
validated_data["patient_hospital_id"] = str(
305+
validated_data["patient_hospital_id"]
306+
)
228307
validated_data["patient_id"] = validated_data["patient_id"].id
229308
validated_data["hospital_id"] = validated_data["hospital_id"].id
230309

tmh_registry/registry/factories.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ class Meta:
5252

5353
patient = SubFactory(PatientFactory)
5454
hospital = SubFactory(HospitalFactory)
55-
patient_hospital_id = LazyAttribute(lambda _: faker.ssn())
55+
patient_hospital_id = LazyAttribute(
56+
lambda _: faker.numerify(text="#########")
57+
)
5658

5759

5860
class EpisodeFactory(DjangoModelFactory):

tmh_registry/registry/tests/api/viewsets/episodes/test_post.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def test_create_episode_successful(self):
100100

101101
self.assertEqual(
102102
response.data["patient_hospital_mapping"]["patient_hospital_id"],
103-
self.patient_hospital_mapping.patient_hospital_id,
103+
int(self.patient_hospital_mapping.patient_hospital_id),
104104
)
105105
self.assertEqual(response.data["surgery_date"], data["surgery_date"])
106106
self.assertEqual(response.data["episode_type"], data["episode_type"])

tmh_registry/registry/tests/api/viewsets/test_patient_hospital_mappings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_create_successful(self):
2424
data = {
2525
"patient_id": patient.id,
2626
"hospital_id": hospital.id,
27-
"patient_hospital_id": "blabla",
27+
"patient_hospital_id": 1234,
2828
}
2929

3030
response = self.client.post(

tmh_registry/registry/tests/api/viewsets/test_patients.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22

33
from django.test import TestCase
4+
from parameterized import parameterized
45
from pytest import mark
56
from rest_framework.authtoken.models import Token
67
from rest_framework.status import (
@@ -37,7 +38,7 @@ def setUpClass(cls) -> None:
3738
cls.patient.created_at = datetime.date(year=2021, month=4, day=11)
3839
cls.patient.save()
3940

40-
cls.patient_hospital_mapping = PatientHospitalMapping.objects.create(
41+
cls.patient_hospital_mapping = PatientHospitalMappingFactory(
4142
patient=cls.patient, hospital=cls.hospital
4243
)
4344
cls.episode = EpisodeFactory(
@@ -63,7 +64,7 @@ def get_patient_test_data(self):
6364
"phone_2": 324362141,
6465
"address": "16 Test Street, Test City, Test Country",
6566
"hospital_id": self.hospital.id,
66-
"patient_hospital_id": "1111",
67+
"patient_hospital_id": 1111,
6768
}
6869

6970
######################
@@ -82,7 +83,7 @@ def test_get_patients_list_successful(self):
8283
1, len(response.data["results"][0]["hospital_mappings"])
8384
)
8485
self.assertEqual(
85-
self.patient_hospital_mapping.patient_hospital_id,
86+
int(self.patient_hospital_mapping.patient_hospital_id),
8687
response.data["results"][0]["hospital_mappings"][0][
8788
"patient_hospital_id"
8889
],
@@ -224,7 +225,7 @@ def test_get_patients_detail_successful(self):
224225
response.data["hospital_mappings"][0]["hospital_id"],
225226
)
226227
self.assertEqual(
227-
patient_hospital_id,
228+
int(patient_hospital_id),
228229
response.data["hospital_mappings"][0]["patient_hospital_id"],
229230
)
230231

@@ -286,7 +287,8 @@ def test_get_patients_detail_with_multiple_hospitals(self):
286287
patient_id=self.patient.id,
287288
).patient_hospital_id
288289
self.assertEqual(
289-
patient_hospital_id, hospital_mapping["patient_hospital_id"]
290+
int(patient_hospital_id),
291+
hospital_mapping["patient_hospital_id"],
290292
)
291293

292294
########################
@@ -343,7 +345,7 @@ def test_create_patients_only_with_mandatory_fields(self):
343345
data["full_name"] = "John Doe"
344346
data["year_of_birth"] = 1989
345347
data["hospital_id"] = self.hospital.id
346-
data["patient_hospital_id"] = "1111"
348+
data["patient_hospital_id"] = 1111
347349

348350
response = self.client.post(
349351
"/api/v1/patients/", data=data, format="json"
@@ -434,3 +436,21 @@ def test_create_patient_without_hospital_id(self):
434436
)
435437

436438
self.assertEqual(HTTP_400_BAD_REQUEST, response.status_code)
439+
440+
@parameterized.expand(
441+
[
442+
("national_id", "ABC12345"),
443+
("phone_1", "GR30697111111"),
444+
("phone_2", "GR30697122222"),
445+
("patient_hospital_id", "HOSP1234"),
446+
]
447+
)
448+
def test_create_patient_with_invalid_values(self, field, value):
449+
data = self.get_patient_test_data()
450+
data[field] = value
451+
452+
response = self.client.post(
453+
"/api/v1/patients/", data=data, format="json"
454+
)
455+
456+
self.assertEqual(HTTP_400_BAD_REQUEST, response.status_code)

0 commit comments

Comments
 (0)