-
Notifications
You must be signed in to change notification settings - Fork 331
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
add tests and validation for filterset #2588
Merged
vigneshhari
merged 8 commits into
ohcnetwork:develop
from
DraKen0009:additing-validaton-to-patient-filtereset
Nov 25, 2024
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
73e6a80
add tests and validation for filterset
DraKen0009 1ae5aee
Merge branch 'develop' into additing-validaton-to-patient-filtereset
DraKen0009 99e69c2
improved test case sample data
DraKen0009 26c4c6a
improved test cases
DraKen0009 f0f35eb
adding code rabbit sugestions
DraKen0009 a6a6ce9
remove validation from phone number fields
DraKen0009 f4c5d31
Merge branch 'develop' into additing-validaton-to-patient-filtereset
DraKen0009 603643d
Merge branch 'develop' into additing-validaton-to-patient-filtereset
DraKen0009 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from enum import Enum | ||
from urllib.parse import quote | ||
|
||
from django.utils.timezone import now, timedelta | ||
from rest_framework import status | ||
|
@@ -728,6 +729,173 @@ def test_filter_by_has_consents(self): | |
self.assertEqual(res.status_code, status.HTTP_200_OK) | ||
self.assertEqual(res.json()["count"], 3) | ||
|
||
def test_filter_by_invalid_params(self): | ||
self.client.force_authenticate(user=self.user) | ||
|
||
# name length > 200 words | ||
invalid_name_param = "a" * 201 | ||
res = self.client.get(self.get_base_url() + f"?name={invalid_name_param}") | ||
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn( | ||
"Ensure this value has at most 200 characters (it has 201).", | ||
res.json()["name"], | ||
) | ||
|
||
# invalid gender choice | ||
invalid_gender = 4 | ||
res = self.client.get(self.get_base_url() + f"?gender={invalid_gender}") | ||
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn( | ||
"Select a valid choice. 4 is not one of the available choices.", | ||
res.json()["gender"], | ||
) | ||
|
||
# invalid value for age, age max , age min filter (i.e <0) | ||
invalid_age = -2 | ||
res = self.client.get(self.get_base_url() + f"?age={invalid_age}") | ||
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn( | ||
"Ensure this value is greater than or equal to 0.", res.json()["age"] | ||
) | ||
|
||
invalid_min_age = -2 | ||
res = self.client.get(self.get_base_url() + f"?age_min={invalid_min_age}") | ||
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn( | ||
"Ensure this value is greater than or equal to 0.", res.json()["age_min"] | ||
) | ||
|
||
invalid_max_age = -2 | ||
res = self.client.get(self.get_base_url() + f"?age_max={invalid_max_age}") | ||
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn( | ||
"Ensure this value is greater than or equal to 0.", res.json()["age_max"] | ||
) | ||
|
||
# invalid number_of_doses param >3 or <0 | ||
invalid_number_of_doses = -2 | ||
res = self.client.get( | ||
self.get_base_url() + f"?number_of_doses={invalid_number_of_doses}" | ||
) | ||
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn( | ||
"Ensure this value is greater than or equal to 0.", | ||
res.json()["number_of_doses"], | ||
) | ||
|
||
invalid_number_of_doses = 4 | ||
res = self.client.get( | ||
self.get_base_url() + f"?number_of_doses={invalid_number_of_doses}" | ||
) | ||
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn( | ||
"Ensure this value is less than or equal to 3.", | ||
res.json()["number_of_doses"], | ||
) | ||
|
||
# invalid srf id length > 200 words | ||
invalid_srf_param = "a" * 201 | ||
res = self.client.get(self.get_base_url() + f"?srf_id={invalid_srf_param}") | ||
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn( | ||
"Ensure this value has at most 200 characters (it has 201).", | ||
res.json()["srf_id"], | ||
) | ||
|
||
# invalid district_name length > 255 words | ||
invalid_district_name_param = "a" * 256 | ||
res = self.client.get( | ||
self.get_base_url() + f"?district_name={invalid_district_name_param}" | ||
) | ||
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn( | ||
"Ensure this value has at most 255 characters (it has 256).", | ||
res.json()["district_name"], | ||
) | ||
|
||
# invalid local_body_name length > 255 words | ||
invalid_local_body_name_param = "a" * 256 | ||
res = self.client.get( | ||
self.get_base_url() + f"?local_body_name={invalid_local_body_name_param}" | ||
) | ||
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn( | ||
"Ensure this value has at most 255 characters (it has 256).", | ||
res.json()["local_body_name"], | ||
) | ||
|
||
# invalid state_name length > 255 words | ||
invalid_state_name_param = "a" * 256 | ||
res = self.client.get( | ||
self.get_base_url() + f"?state_name={invalid_state_name_param}" | ||
) | ||
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn( | ||
"Ensure this value has at most 255 characters (it has 256).", | ||
res.json()["state_name"], | ||
) | ||
|
||
# invalid patient no value > 100 | ||
invalid_patient_no_param = "A" * 101 | ||
res = self.client.get( | ||
self.get_base_url() + f"?patient_no={invalid_patient_no_param}" | ||
) | ||
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn( | ||
"Ensure this value has at most 100 characters (it has 101).", | ||
res.json()["patient_no"], | ||
) | ||
|
||
def test_invalid_phone_params(self): | ||
self.client.force_authenticate(user=self.user) | ||
|
||
# invalid phone number (non Indian or non International) | ||
invalid_phone_numbers = [ | ||
"9876543210", | ||
"+9123456789", | ||
"+915123456789", | ||
"+9191234", | ||
"+91765432abcd", | ||
"00441234567890", | ||
"+12345", | ||
"+911234567890", | ||
"+151234", | ||
"+44-123-4567-890", | ||
"+1-800-555-1212", | ||
"+91 98765 43210", | ||
"+91987654321000", | ||
"+44 1234 567890", | ||
"+123-456-7890", | ||
"1234567890", | ||
"+91-9876543210", | ||
"+123456", | ||
"+000000000000", | ||
"+9876543210123456", | ||
] | ||
|
||
for phone_number in invalid_phone_numbers: | ||
encoded_phone_number = quote(phone_number) | ||
res = self.client.get( | ||
self.get_base_url() + f"?phone_number={encoded_phone_number}" | ||
) | ||
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn( | ||
f"Invalid phone number. Must be one of the following types: mobile. Received: {phone_number}", | ||
res.json()["phone_number"], | ||
) | ||
|
||
for emergency_phone_number in invalid_phone_numbers: | ||
encoded_emergency_phone_number = quote(emergency_phone_number) | ||
res = self.client.get( | ||
self.get_base_url() | ||
+ f"?emergency_phone_number={encoded_emergency_phone_number}" | ||
) | ||
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) | ||
self.assertIn( | ||
f"Invalid phone number. Must be one of the following types: mobile. Received: {emergency_phone_number}", | ||
res.json()["emergency_phone_number"], | ||
) | ||
|
||
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 Reduce code duplication in phone number validation tests. The same validation logic is repeated for both def _test_invalid_phone_field(self, field_name):
invalid_phone_numbers = [
"9876543210",
"+9123456789",
# ... rest of the numbers
]
for phone_number in invalid_phone_numbers:
encoded_number = quote(phone_number)
res = self.client.get(
self.get_base_url() + f"?{field_name}={encoded_number}"
)
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn(
f"Invalid phone number. Must be one of the following types: mobile. Received: {phone_number}",
res.json()[field_name],
) But you know, it's your code, do what you want... |
||
|
||
class DischargePatientFilterTestCase(TestUtils, APITestCase): | ||
@classmethod | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Add validation test for covin_id field.
The PR objectives mention validating the covin_id field with a maximum length of 15 characters, but this test is missing.
Add the following test case: