django-query-capture icon indicating copy to clipboard operation
django-query-capture copied to clipboard

Add assert_inefficient_query to test_utils module

Open LeeHanYeong opened this issue 3 years ago • 0 comments

  • 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.

LeeHanYeong avatar Feb 21 '22 13:02 LeeHanYeong