-
Notifications
You must be signed in to change notification settings - Fork 336
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
fix: Add validation to prevent numeric values in patient names #2605
Changes from 8 commits
46c9e39
e0c8351
dd57b7f
fae9153
dc5278c
176a68d
0f02ac3
9972c9d
e455511
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,3 +65,44 @@ def test_year_of_birth_validation(self): | |
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) | ||
|
||
def test_valid_patient_name(self): | ||
dist_admin = self.create_user("dist_admin", self.district, user_type=30) | ||
sample_data = { | ||
"name": "Rithvik", | ||
"gender": 1, | ||
"facility": self.facility.external_id, | ||
"blood_group": "AB+", | ||
"date_of_birth": "2000-01-01", | ||
"year_of_birth": 2000, | ||
"disease_status": "NEGATIVE", | ||
"emergency_phone_number": "+919000000666", | ||
"is_vaccinated": "false", | ||
"number_of_doses": 0, | ||
"phone_number": "+919000044343", | ||
"is_antenatal": False, | ||
} | ||
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_201_CREATED) | ||
|
||
def test_invalid_patient_name(self): | ||
dist_admin = self.create_user("dist_admin", self.district, user_type=30) | ||
sample_data = { | ||
"name": "Rithvik123", | ||
"gender": 1, | ||
"facility": self.facility.external_id, | ||
"blood_group": "AB+", | ||
"date_of_birth": "2000-01-01", | ||
"year_of_birth": 2000, | ||
"disease_status": "NEGATIVE", | ||
"emergency_phone_number": "+919000000666", | ||
"is_vaccinated": "false", | ||
"number_of_doses": 0, | ||
"phone_number": "+919000044343", | ||
"is_antenatal": False, | ||
} | ||
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("name", response.data) | ||
Comment on lines
+89
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance invalid name test coverage. The test could be more thorough by checking these delightful invalid scenarios:
Also, it might be helpful to assert the specific error message instead of just checking if "name" exists in response.data. Something like: - self.assertIn("name", response.data)
+ self.assertEqual(response.data["name"][0], "Name should not contain numeric characters")
|
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.
🛠️ Refactor suggestion
The validation could be a bit more... accommodating.
While the current implementation prevents numeric values, it might be a bit too restrictive for real-world names. Also, the error handling could use some polish.
Consider this enhanced implementation:
This implementation:
📝 Committable suggestion
🧰 Tools
🪛 Ruff
215-215: Single quotes found but double quotes preferred
Replace single quotes with double quotes
(Q000)
216-216: Exception must not use a string literal, assign to variable first
Assign to variable; remove string literal
(EM101)