Test Utils
Contains utilities helpful when testing peewee projects.
class count_queries
([only_select=False])
Context manager that will count the number of queries executed within the context.
Parameters: | only_select (bool) – Only count SELECT queries. |
---|
with count_queries() as counter:
huey = User.get(User.username == 'huey')
huey_tweets = [tweet.message for tweet in huey.tweets]
assert counter.count == 2
count
The number of queries executed.
get_queries
()Return a list of 2-tuples consisting of the SQL query and a list of parameters.
assert_query_count
(expected[, only_select=False])
Function or method decorator that will raise an AssertionError
if the number of queries executed in the decorated function does not equal the expected number.
class TestMyApp(unittest.TestCase):
@assert_query_count(1)
def test_get_popular_blogs(self):
popular_blogs = Blog.get_popular()
self.assertEqual(
[blog.title for blog in popular_blogs],
["Peewee's Playhouse!", "All About Huey", "Mickey's Adventures"])
This function can also be used as a context manager:
class TestMyApp(unittest.TestCase):
def test_expensive_operation(self):
with assert_query_count(1):
perform_expensive_operation()