-
Notifications
You must be signed in to change notification settings - Fork 14
✨ [maykinmedia/commonground-api-common#142] Use exception handler registry from commonground-api-common #723
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
Changes from all commits
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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |||||||
| from furl import furl | ||||||||
| from rest_framework import status | ||||||||
| from rest_framework.test import APITestCase | ||||||||
| from vng_api_common.tests import get_validation_errors | ||||||||
|
|
||||||||
| from objects.core.tests.factories import ( | ||||||||
| ObjectFactory, | ||||||||
|
|
@@ -66,7 +67,11 @@ def test_filter_invalid_objecttype(self): | |||||||
| response = self.client.get(self.url, {"type": "invalid-objecttype-url"}) | ||||||||
|
|
||||||||
| self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||||||||
| self.assertEqual(response.json()["type"], ["Invalid value."]) | ||||||||
|
|
||||||||
| error = get_validation_errors(response, "type") | ||||||||
|
|
||||||||
| self.assertEqual(error["reason"], "Invalid value.") | ||||||||
| self.assertEqual(error["code"], "invalid") | ||||||||
|
|
||||||||
| def test_filter_unknown_objecttype(self): | ||||||||
| objecttype_url = ( | ||||||||
|
|
@@ -75,19 +80,30 @@ def test_filter_unknown_objecttype(self): | |||||||
| response = self.client.get(self.url, {"type": objecttype_url}) | ||||||||
|
|
||||||||
| self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||||||||
|
|
||||||||
|
Contributor
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. Now, since the response is consistent with other projects, with You can apply this for all tests as well. |
||||||||
| error = get_validation_errors(response, "type") | ||||||||
|
|
||||||||
| self.assertEqual( | ||||||||
| response.json()["type"], | ||||||||
| [ | ||||||||
| f"Select a valid object type. {objecttype_url} is not one of the available choices." | ||||||||
| ], | ||||||||
| error, | ||||||||
| { | ||||||||
| "name": "type", | ||||||||
| "code": "invalid_choice", | ||||||||
| "reason": ( | ||||||||
| f"Select a valid object type. {objecttype_url} is not one of the available choices." | ||||||||
| ), | ||||||||
| }, | ||||||||
| ) | ||||||||
|
|
||||||||
| def test_filter_too_long_object_type(self): | ||||||||
| object_type_long = f"{OBJECT_TYPES_API}{'a' * 1000}/{self.object_type.uuid}" | ||||||||
| response = self.client.get(self.url, {"type": object_type_long}) | ||||||||
|
|
||||||||
| self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||||||||
| self.assertEqual(response.json()["type"], ["The value has too many characters"]) | ||||||||
|
|
||||||||
| error = get_validation_errors(response, "type") | ||||||||
|
|
||||||||
| self.assertEqual(error["code"], "max_length") | ||||||||
| self.assertEqual(error["reason"], "The value has too many characters") | ||||||||
|
|
||||||||
|
|
||||||||
| class FilterDataAttrsTests(TokenAuthMixin, APITestCase): | ||||||||
|
|
@@ -218,8 +234,12 @@ def test_filter_lte_not_numerical(self): | |||||||
| response = self.client.get(self.url, {"data_attrs": "diameter__lt__value"}) | ||||||||
|
|
||||||||
| self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||||||||
|
|
||||||||
| error = get_validation_errors(response, "") | ||||||||
|
|
||||||||
| self.assertEqual( | ||||||||
| response.json(), ["Operator `lt` supports only dates and/or numeric values"] | ||||||||
| error["reason"], | ||||||||
| "Operator `lt` supports only dates and/or numeric values", | ||||||||
| ) | ||||||||
|
|
||||||||
| def test_filter_lte_date(self): | ||||||||
|
|
@@ -255,20 +275,27 @@ def test_filter_lte_date(self): | |||||||
|
|
||||||||
| def test_filter_invalid_operator(self): | ||||||||
| response = self.client.get(self.url, {"data_attrs": "diameter__not__value"}) | ||||||||
|
|
||||||||
| self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||||||||
| self.assertEqual(response.json(), ["Comparison operator `not` is unknown"]) | ||||||||
|
|
||||||||
| error = get_validation_errors(response, "") | ||||||||
|
|
||||||||
| self.assertEqual( | ||||||||
| error["reason"], | ||||||||
| "Comparison operator `not` is unknown", | ||||||||
| ) | ||||||||
| self.assertEqual(error["code"], "invalid-data-attrs-query") | ||||||||
|
|
||||||||
| def test_filter_invalid_param(self): | ||||||||
| response = self.client.get(self.url, {"data_attrs": "diameter__exact"}) | ||||||||
|
|
||||||||
| self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||||||||
|
|
||||||||
| error = get_validation_errors(response, "") | ||||||||
|
|
||||||||
| self.assertEqual( | ||||||||
| response.json(), | ||||||||
| [ | ||||||||
| "Filter expression 'diameter__exact' doesn't have the shape 'key__operator__value'" | ||||||||
| ], | ||||||||
| error["reason"], | ||||||||
| "Filter expression 'diameter__exact' doesn't have the shape 'key__operator__value'", | ||||||||
| ) | ||||||||
| self.assertEqual(error["code"], "invalid-data-attrs-query") | ||||||||
|
|
||||||||
| def test_filter_nested_attr(self): | ||||||||
| record = ObjectRecordFactory.create( | ||||||||
|
|
@@ -570,8 +597,11 @@ def test_filter_lte_not_numerical(self): | |||||||
| response = self.client.get(self.url, {"data_attr": "diameter__lt__value"}) | ||||||||
|
|
||||||||
| self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||||||||
|
|
||||||||
| error = get_validation_errors(response, "") | ||||||||
|
|
||||||||
| self.assertEqual( | ||||||||
| response.json(), ["Operator `lt` supports only dates and/or numeric values"] | ||||||||
| "Operator `lt` supports only dates and/or numeric values", error["reason"] | ||||||||
| ) | ||||||||
|
|
||||||||
| def test_filter_lte_date(self): | ||||||||
|
|
@@ -609,17 +639,24 @@ def test_filter_invalid_operator(self): | |||||||
| response = self.client.get(self.url, {"data_attr": "diameter__not__value"}) | ||||||||
|
|
||||||||
| self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||||||||
| self.assertEqual(response.json(), ["Comparison operator `not` is unknown"]) | ||||||||
|
|
||||||||
| error = get_validation_errors(response, "") | ||||||||
|
|
||||||||
| self.assertEqual( | ||||||||
|
Contributor
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.
Suggested change
|
||||||||
| error["reason"], | ||||||||
| "Comparison operator `not` is unknown", | ||||||||
| ) | ||||||||
|
|
||||||||
| def test_filter_invalid_param(self): | ||||||||
| response = self.client.get(self.url, {"data_attr": "diameter__exact"}) | ||||||||
|
|
||||||||
| self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||||||||
|
|
||||||||
| error = get_validation_errors(response, "") | ||||||||
|
|
||||||||
| self.assertEqual( | ||||||||
| response.json(), | ||||||||
| [ | ||||||||
| "Filter expression 'diameter__exact' doesn't have the shape 'key__operator__value'" | ||||||||
| ], | ||||||||
| error["reason"], | ||||||||
| "Filter expression 'diameter__exact' doesn't have the shape 'key__operator__value'", | ||||||||
| ) | ||||||||
|
|
||||||||
| def test_filter_nested_attr(self): | ||||||||
|
|
@@ -819,18 +856,21 @@ def test_filter_two_icontains_with_comma(self): | |||||||
|
|
||||||||
| def test_filter_comma_separated_invalid(self): | ||||||||
| response = self.client.get( | ||||||||
| self.url, {"data_attr": "dimensions__diameter__exact__4,name__exact__demo"} | ||||||||
| self.url, | ||||||||
| {"data_attr": "dimensions__diameter__exact__4,name__exact__demo"}, | ||||||||
| ) | ||||||||
|
|
||||||||
| self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||||||||
|
|
||||||||
| error = get_validation_errors(response, "") | ||||||||
|
|
||||||||
| self.assertEqual( | ||||||||
| response.json(), | ||||||||
| [ | ||||||||
| "Filter expression 'dimensions__diameter__exact__4,name__exact__demo' " | ||||||||
| "must have the shape 'key__operator__value', commas can only be present in " | ||||||||
| "the 'value'" | ||||||||
| ], | ||||||||
| error["reason"], | ||||||||
| "Filter expression 'dimensions__diameter__exact__4,name__exact__demo' " | ||||||||
| "must have the shape 'key__operator__value', commas can only be present in " | ||||||||
| "the 'value'", | ||||||||
| ) | ||||||||
| self.assertEqual(error["code"], "invalid-data-attr-query") | ||||||||
|
|
||||||||
|
|
||||||||
| class FilterDateTests(TokenAuthMixin, APITestCase): | ||||||||
|
|
@@ -941,6 +981,8 @@ def test_filter_registration_date_list(self): | |||||||
|
|
||||||||
| response = self.client.get(url, {"registrationDate": "2020-07-01"}) | ||||||||
|
|
||||||||
| self.assertEqual(response.status_code, status.HTTP_200_OK) | ||||||||
|
|
||||||||
| data = response.json()["results"] | ||||||||
|
|
||||||||
| self.assertEqual(len(data), 1) | ||||||||
|
|
@@ -952,17 +994,18 @@ def test_filter_registration_date_list(self): | |||||||
|
|
||||||||
| def test_filter_on_both_date_and_registration_date(self): | ||||||||
| url = reverse_lazy("object-list") | ||||||||
|
|
||||||||
| response = self.client.get( | ||||||||
| url, {"date": "2020-07-01", "registrationDate": "2020-08-01"} | ||||||||
| url, | ||||||||
| {"date": "2020-07-01", "registrationDate": "2020-08-01"}, | ||||||||
| ) | ||||||||
|
|
||||||||
| self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||||||||
|
|
||||||||
| data = response.json() | ||||||||
|
|
||||||||
| self.assertEqual( | ||||||||
| response.json(), | ||||||||
| [ | ||||||||
| "'date' and 'registrationDate' parameters can't be used in the same request" | ||||||||
| ], | ||||||||
| data["invalid_params"][0]["reason"], | ||||||||
|
Contributor
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.
Suggested change
Contributor
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. And also here |
||||||||
| "'date' and 'registrationDate' parameters can't be used in the same request", | ||||||||
| ) | ||||||||
|
|
||||||||
|
|
||||||||
|
|
@@ -1033,13 +1076,19 @@ def test_filter_db_error(self, mock_query): | |||||||
| response = self.client.get(self.url, {"data_icontains": "some"}) | ||||||||
|
|
||||||||
| self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) | ||||||||
|
|
||||||||
| data = response.json() | ||||||||
|
|
||||||||
| data.pop("instance", None) | ||||||||
|
|
||||||||
| self.assertEqual( | ||||||||
| response.json(), | ||||||||
| data, | ||||||||
| { | ||||||||
| "code": "error", | ||||||||
| "code": "search-not-supported", | ||||||||
| "title": "Internal Server Error", | ||||||||
| "status": 500, | ||||||||
| "detail": "This search operation is not supported by the underlying data store.", | ||||||||
| "type": "http://testserver/ref/fouten/ProgrammingError/", | ||||||||
| }, | ||||||||
| ) | ||||||||
|
|
||||||||
|
|
||||||||
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.
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.
I think you forget this one