Skip to content

Commit

Permalink
Add age validation in PatientDetailSerializer (#1929)
Browse files Browse the repository at this point in the history
* Add age validation in PatientDetailSerializer

* Change error msg

* Add test for age validation

* remove try error check

* Add validation for is_antenatal

* Add api test

* fix lint errors

* Apply suggestions from code review

* add test to verify age and dob

---------

Co-authored-by: Aakash Singh <mail@singhaakash.dev>
Co-authored-by: Vignesh Hari <vichuhari100@gmail.com>
  • Loading branch information
3 people committed Apr 22, 2024
1 parent 164351d commit fe0423e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
13 changes: 13 additions & 0 deletions care/facility/api/serializers/patient.py
Expand Up @@ -218,6 +218,9 @@ class Meta:
)
abha_number_object = AbhaNumberSerializer(source="abha_number", read_only=True)

date_of_birth = serializers.DateField(required=False, allow_null=True)
year_of_birth = serializers.IntegerField(default=0)

class Meta:
model = PatientRegistration
exclude = (
Expand Down Expand Up @@ -251,6 +254,16 @@ def validate_countries_travelled(self, value):
value = [value]
return value

def validate_date_of_birth(self, value):
if value and value > now().date():
raise serializers.ValidationError("Enter a valid DOB such that age > 0")
return value

def validate_year_of_birth(self, value):
if value and value > now().year:
raise serializers.ValidationError("Enter a valid year of birth")
return value

def validate(self, attrs):
validated = super().validate(attrs)
if not self.partial and not (
Expand Down
40 changes: 40 additions & 0 deletions care/facility/models/tests/test_patient.py
@@ -1,3 +1,7 @@
from datetime import timedelta

from django.utils.timezone import now
from rest_framework import status
from rest_framework.test import APITestCase

from care.facility.models import DiseaseStatusEnum
Expand All @@ -23,3 +27,39 @@ def test_disease_state_recovery_is_aliased_to_recovered(self):
patient.refresh_from_db()

self.assertEqual(patient.disease_status, DiseaseStatusEnum.RECOVERED.value)

def test_date_of_birth_validation(self):
dist_admin = self.create_user("dist_admin", self.district, user_type=30)
sample_data = {
"facility": self.facility.external_id,
"blood_group": "AB+",
"gender": 1,
"date_of_birth": now().date() + timedelta(days=365),
"disease_status": "NEGATIVE",
"emergency_phone_number": "+919000000666",
"is_vaccinated": "false",
"number_of_doses": 0,
"phone_number": "+919000044343",
}
self.client.force_authenticate(user=dist_admin)
response = self.client.post("/api/v1/patient/", sample_data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn("date_of_birth", response.data)

def test_year_of_birth_validation(self):
dist_admin = self.create_user("dist_admin", self.district, user_type=30)
sample_data = {
"facility": self.facility.external_id,
"blood_group": "AB+",
"gender": 1,
"year_of_birth": now().year + 1,
"disease_status": "NEGATIVE",
"emergency_phone_number": "+919000000666",
"is_vaccinated": "false",
"number_of_doses": 0,
"phone_number": "+919000044343",
}
self.client.force_authenticate(user=dist_admin)
response = self.client.post("/api/v1/patient/", sample_data, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn("year_of_birth", response.data)

0 comments on commit fe0423e

Please sign in to comment.