Test Utils
Contains utilities helpful when testing peewee projects.
- class
countqueries
([_only_select=False]) - Context manager that will count the number of queries executed withinthe 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.
- Return a list of 2-tuples consisting of the SQL query and a list ofparameters.
assertquery_count
(_expected[, only_select=False])- Function or method decorator that will raise an
AssertionError
if thenumber of queries executed in the decorated function does not equal theexpected 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()