Ticket #5416: assert_number_of_queries.diff

File assert_number_of_queries.diff, 1.1 KB (added by Manoj Govindan <egmanoj@…>, 15 years ago)

Patch adding assertNumberOfQueries method to TransactionTestCase.

  • test/testcases.py

     
    427427            (u"Template '%s' was used unexpectedly in rendering the"
    428428             u" response") % template_name)
    429429
     430    def assertNumberofQueries(self, num, callable, *args, **kwargs):
     431        """
     432        Asserts that exactly num queries were used when callable was invoked.
     433        """
     434        DEBUG = settings.DEBUG
     435        settings.DEBUG = True
     436
     437        num_queries_before = len(connection.queries)
     438        callable(*args, **kwargs)
     439        num_queries_after = len(connection.queries)
     440        actual = (num_queries_after - num_queries_before)
     441
     442        settings.DEBUG = DEBUG
     443        self.failUnlessEqual(num, actual,
     444                             (u"Callable did not execute %s queries as expected."
     445                              u" Actual number of queries: %s.") % (num, actual))
     446
    430447class TestCase(TransactionTestCase):
    431448    """
    432449    Does basically the same as TransactionTestCase, but surrounds every test
Back to Top