django-query-capture
django-query-capture copied to clipboard
Add assert_inefficient_query to test_utils module
- Add assert_inefficient_query decorator
- Add usage of "assert_inefficient_query" decorator to README
Problem
The assert_inefficient_query
decorator and with self.assertRaises
statement do not work together.
example:
@assert_inefficient_query(199)
def test_assert_inefficient_query_with_decorator(self):
with self.assertRaises(AssertionError):
[list(Reporter.objects.all()) for i in range(200)]
[Reporter.objects.create(full_name=f"reporter-{i}") for i in range(200)]
AssertionError is not caught in the with self.assertRaises(AssertionError)
block of the above code. This is probably because the decorator's AssertInefficientQuery
is at a higher level than the self.assertRaises
.
I'm not sure how to solve this. I can't catch exceptions with self.assertRaises
at this time. However, when more than the specified number of duplicate/similar queries occur, the function that fails the test works well.