-
Notifications
You must be signed in to change notification settings - Fork 278
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
base: develop
Are you sure you want to change the base?
feat: Added validation for max dosage and corresponding test cases #2383 #2531
Conversation
- Added validation to ensure max dosage is greater than or equal to base dosage in PrescriptionSerializer. - Added test cases to validate the new dosage constraints. - Updated test_prescriptions_api.py to include tests for valid, equal, and invalid dosage scenarios. Resolves issue with incorrect dosage validation.
def extract_numeric_value(dosage): | ||
|
||
match = re.match(r"(\d+(\.\d+)?)", dosage) # Matches digits and optional decimal part | ||
if match: | ||
return float(match.group(1)) | ||
raise serializers.ValidationError({"dosage": "Invalid dosage format."}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This validation is not required as it's already handled by DenominationValidator
which is used by dosage-like fields.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So should I only remove the validation error part?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep only validations mentioned in the issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine will do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done that, please review it again.Thanks
… into validate_prn
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #2531 +/- ##
===========================================
+ Coverage 65.59% 65.62% +0.02%
===========================================
Files 223 223
Lines 13383 13395 +12
Branches 1855 1858 +3
===========================================
+ Hits 8779 8790 +11
Misses 4218 4218
- Partials 386 387 +1 ☔ View full report in Codecov by Sentry. |
What is the issue here? @rithviknishad @sainak please help |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
regex is not required here as the value is already validated, so a simple split does the job
avoid writing comments for obvious code
base_dosage_value = extract_numeric_value(base_dosage) | ||
max_dosage_value = extract_numeric_value(max_dosage) | ||
|
||
# Raise error if max_dosage is less than base_dosage | ||
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."} | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
base_dosage_value = extract_numeric_value(base_dosage) | |
max_dosage_value = extract_numeric_value(max_dosage) | |
# Raise error if max_dosage is less than base_dosage | |
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."} | |
) | |
with contextlib.suppress(ValueError): | |
base_dosage_value = float(base_dosage.split(" ", maxsplit=1)[0]) | |
max_dosage_value = float(max_dosage.split(" ", maxsplit=1)[0]) | |
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."} | |
) |
max_dosage = attrs.get("max_dosage") | ||
|
||
if base_dosage and max_dosage: | ||
# Extract numeric values from dosage strings |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Extract numeric values from dosage strings |
@@ -127,6 +134,21 @@ def validate(self, attrs): | |||
{"base_dosage": "Base dosage is required."} | |||
) | |||
|
|||
# Validate max_dosage is greater than or equal to base_dosage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Validate max_dosage is greater than or equal to base_dosage |
@@ -9,6 +9,7 @@ | |||
PrescriptionDosageType, | |||
) | |||
from care.users.api.serializers.user import UserBaseMinimumSerializer | |||
import re |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import re | |
import contextlib |
def extract_numeric_value(dosage): | ||
|
||
match = re.match(r"(\d+(\.\d+)?)", dosage) # Matches digits and optional decimal part | ||
if match: | ||
return float(match.group(1)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def extract_numeric_value(dosage): | |
match = re.match(r"(\d+(\.\d+)?)", dosage) # Matches digits and optional decimal part | |
if match: | |
return float(match.group(1)) |
Fixes: #2383
Resolves issue with incorrect dosage validation.
Merge Checklist
/docs
Only PR's with test cases included and passing lint and test pipelines will be reviewed
@ohcnetwork/care-backend-maintainers @ohcnetwork/care-backend-admins