Skip to content

Commit

Permalink
feat: add test helper to assert 400 error
Browse files Browse the repository at this point in the history
  • Loading branch information
joaodaher committed May 1, 2024
1 parent d346d61 commit 864e79a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions drf_kit/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ def assertResponseDeleted(self, response: Response):
logger.info("Please use assertResponseDelete instead")
self.assertResponseDelete(response=response)

def assertResponseBadRequest(self, response: Response, expected: dict = ANY):
self.assertResponse(
expected_status=status.HTTP_400_BAD_REQUEST,
expected_body=expected,
response=response,
response_key=None,
)

def assertResponseNotAllowed(self, response: Response):
method = response.request["REQUEST_METHOD"]
expected = {"detail": f'Method "{method}" not allowed.'}
Expand Down
2 changes: 1 addition & 1 deletion test_app/tests/tests_views/tests_crud_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_post_endpoint_with_constraint_error(self):
expected = {
"errors": "This Beast violates the check `minimum-beast-age` which states `(AND: ('age__gte', 0))`",
}
self.assertResponse(expected_status=status.HTTP_400_BAD_REQUEST, expected_body=expected, response=response)
self.assertResponseBadRequest(response=response, expected=expected)

beasts = models.Beast.objects.all()
self.assertEqual(0, beasts.count())

0 comments on commit 864e79a

Please sign in to comment.