Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion care/emr/resources/patient/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ def perform_extra_deserialization(self, is_update, obj):
# override dob if user chooses to update age
obj.date_of_birth = None
obj.year_of_birth = timezone.now().date().year - self.age
else:
elif self.date_of_birth:
obj.date_of_birth = self.date_of_birth
obj.year_of_birth = self.date_of_birth.year
obj._identifiers = self.identifiers # noqa: SLF001
obj._tags = self.tags # noqa: SLF001
Expand Down Expand Up @@ -186,6 +187,7 @@ def perform_extra_deserialization(self, is_update, obj):
obj.date_of_birth = None
obj.year_of_birth = timezone.now().year - self.age
elif self.date_of_birth:
obj.date_of_birth = self.date_of_birth
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor Author

@nandkishorr nandkishorr Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while creating and updating , on de serialization the date_of_birth is converted to str (in-memory) So on serialize the get_age() will crash since using data from the same in-memory obj !

obj.year_of_birth = self.date_of_birth.year
if not self.pincode:
obj.pincode = None
Expand Down Expand Up @@ -248,6 +250,7 @@ class PatientIdentifierResponse(BaseModel):


class PatientRetrieveSpec(PatientListSpec, PatientPermissionsMixin):
age: str
geo_organization: dict = {}

created_by: dict | None = None
Expand Down Expand Up @@ -290,3 +293,4 @@ def perform_extra_serialization(cls, mapping, obj, *args, **kwargs):
}
for x in obj.facility_identifiers.get(str(facility.id), [])
]
mapping["age"] = obj.get_age()
15 changes: 15 additions & 0 deletions care/utils/tests/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import secrets
import sys
from secrets import choice

Expand All @@ -6,11 +7,18 @@
from model_bakery import baker
from rest_framework.test import APITestCase

from care.emr.management.commands.load_fixtures import (
generate_unique_indian_phone_number,
)
from care.emr.models.organization import FacilityOrganizationUser, OrganizationUser
from care.emr.resources.encounter.constants import (
ClassChoices,
EncounterPriorityChoices,
)
from care.emr.resources.patient.spec import (
BloodGroupChoices,
GenderChoices,
)

# Global mocking, since the types are loaded when specs load, mocking using patch was not working as the validations were already loaded.
# TODO: figure out a more customizeable approach to mock this
Expand Down Expand Up @@ -83,6 +91,13 @@ def create_role_with_permissions(self, permissions, role_name=None):
def create_patient(self, **kwargs):
from care.emr.models import Patient

kwargs.setdefault("name", self.fake.name())
kwargs.setdefault("gender", secrets.choice(list(GenderChoices)).value)
kwargs.setdefault("phone_number", generate_unique_indian_phone_number())
kwargs.setdefault("address", self.fake.address())
kwargs.setdefault("pincode", self.fake.random_int(min=100000, max=999999))
kwargs.setdefault("blood_group", secrets.choice(list(BloodGroupChoices)).value)
kwargs.setdefault("date_of_birth", self.fake.date_of_birth())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Use a deterministic date_of_birth default to reduce flaky age-based tests.

Line 100 currently randomizes DOB, which can make age assertions drift over time. A fixed default is more reliable, and specific tests can still override it.

Proposed change
+from datetime import date
 import secrets
 import sys
 from secrets import choice
@@
-        kwargs.setdefault("date_of_birth", self.fake.date_of_birth())
+        kwargs.setdefault("date_of_birth", date(1990, 1, 1))

As per coding guidelines, **/tests/**/*.py: Use Django’s built-in tools for testing (unittest and pytest-django) to ensure code quality and reliability.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
kwargs.setdefault("date_of_birth", self.fake.date_of_birth())
from datetime import date
import secrets
import sys
from secrets import choice
kwargs.setdefault("date_of_birth", date(1990, 1, 1))
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@care/utils/tests/base.py` at line 100, Replace the non-deterministic default
in the test factory call kwargs.setdefault("date_of_birth",
self.fake.date_of_birth()) with a stable deterministic date (e.g., a fixed
datetime.date like 1990-01-01) so age-based assertions don't drift; keep using
kwargs.setdefault so individual tests can still override date_of_birth when
needed and update any docstring/comment to note the fixed default.

return baker.make(Patient, **kwargs)

def create_facility(self, user, **kwargs):
Expand Down