Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added validation for max dosage and corresponding test cases #2383 #2531

Open
wants to merge 24 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
38ae0c9
feat: Add validation for max dosage and corresponding test cases
himanshu-sharmav Oct 12, 2024
ed1e461
Merge branch 'develop' into validate_prn
himanshu-sharmav Oct 12, 2024
c4138c1
Merge branch 'develop' of https://github.com/himanshu-sharmav/care in…
himanshu-sharmav Oct 12, 2024
57999f2
removed redundant validations.
himanshu-sharmav Oct 13, 2024
59c9439
Merge branch 'validate_prn' of https://github.com/himanshu-sharmav/ca…
himanshu-sharmav Oct 13, 2024
b7010df
Update Pipfile
himanshu-sharmav Oct 13, 2024
2bdff7f
Update admin.py
himanshu-sharmav Oct 13, 2024
d87342b
Update admin.py
himanshu-sharmav Oct 13, 2024
cab7c8c
Update admin.py
himanshu-sharmav Oct 13, 2024
8537afa
Update admin.py
himanshu-sharmav Oct 13, 2024
4210e98
Update admin.py
himanshu-sharmav Oct 13, 2024
55d23bf
Update prescription.py
himanshu-sharmav Oct 13, 2024
fdf75a0
added return statement
himanshu-sharmav Nov 5, 2024
639aab7
changed indendation for linter test
himanshu-sharmav Nov 5, 2024
2f4769f
removed regex and unrequired comments and functions.
himanshu-sharmav Nov 5, 2024
6291b72
indentation change
himanshu-sharmav Nov 5, 2024
0ae7922
Merge branch 'develop' into validate_prn
himanshu-sharmav Nov 5, 2024
fa839cc
Merge branch 'develop' into validate_prn
himanshu-sharmav Nov 6, 2024
25cf4b4
Update care/facility/api/serializers/prescription.py
himanshu-sharmav Nov 6, 2024
b5f08ed
pre-commit changes
himanshu-sharmav Nov 6, 2024
9a452a1
added multiple validate functions to split the branches.
himanshu-sharmav Nov 6, 2024
8cea1fb
applied changes suggested by coderabbitai
himanshu-sharmav Nov 6, 2024
a090f44
again changed suggested by coderabbitai
himanshu-sharmav Nov 6, 2024
6cba5a5
changes for pr test
himanshu-sharmav Nov 7, 2024
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
31 changes: 31 additions & 0 deletions care/facility/api/serializers/prescription.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import contextlib

from django.shortcuts import get_object_or_404
from django.utils import timezone
from rest_framework import serializers
Expand Down Expand Up @@ -127,6 +129,35 @@ def validate(self, attrs):
{"base_dosage": "Base dosage is required."}
)

base_dosage = attrs.get("base_dosage")
max_dosage = attrs.get("max_dosage")

if max_dosage:
if not base_dosage:
raise serializers.ValidationError(
{"max_dosage": "Max dosage cannot be set without base dosage."}
)
try:
base_dosage_value = float(base_dosage.split(" ", maxsplit=1)[0])
max_dosage_value = float(max_dosage.split(" ", maxsplit=1)[0])
base_unit = base_dosage.split(" ", maxsplit=1)[1]
max_unit = max_dosage.split(" ", maxsplit=1)[1]

if base_unit != max_unit:
raise serializers.ValidationError(
{"max_dosage": f"Max dosage units ({max_unit}) must match base dosage units ({base_unit})."}
)

if max_dosage_value < base_dosage_value:
raise serializers.ValidationError(
{
"max_dosage": "Max dosage in 24 hours should be greater than or equal to base dosage."
}
)
except (ValueError, IndexError) as e:
raise serializers.ValidationError(
{"max_dosage": "Invalid dosage format. Expected format: 'number unit' (e.g., '500 mg')"}
) from e
if attrs.get("dosage_type") == PrescriptionDosageType.PRN:
if not attrs.get("indicator"):
raise serializers.ValidationError(
Expand Down
29 changes: 29 additions & 0 deletions care/facility/tests/test_prescriptions_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,32 @@ def test_medicine_filter_for_prescription(self):
self.assertEqual(
prescription["medicine_object"]["name"], self.medicine.name
)

def test_max_dosage_greater_than_base_dosage(self):
data = self.prescription_data(base_dosage="500 mg", max_dosage="1000 mg")
response = self.client.post(
f"/api/v1/consultation/{self.consultation.external_id}/prescriptions/",
data,
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

def test_max_dosage_equal_to_base_dosage(self):
data = self.prescription_data(base_dosage="500 mg", max_dosage="500 mg")
response = self.client.post(
f"/api/v1/consultation/{self.consultation.external_id}/prescriptions/",
data,
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

def test_max_dosage_less_than_base_dosage(self):
data = self.prescription_data(base_dosage="500 mg", max_dosage="400 mg")
response = self.client.post(
f"/api/v1/consultation/{self.consultation.external_id}/prescriptions/",
data,
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn("max_dosage", response.data)
self.assertEqual(
response.data["max_dosage"][0],
"Max dosage in 24 hours should be greater than or equal to base dosage.",
)
Loading