test_classes: Create a dedicate helper for query count check.

This adds a helper based on testing patterns of using the "queries_captured"
context manager with "assert_length" to check the number of queries
executed for preventing performance regression.

It explains the rationale of checking the query count through an
"AssertionError" and prints the queries captured as assert_length does,
but with a format optimized for displaying the queries in a more
readable manner.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Zixuan James Li
2022-10-15 16:47:40 -04:00
committed by Tim Abbott
parent 9117db8887
commit 46329a2710
21 changed files with 122 additions and 200 deletions

View File

@@ -16,7 +16,7 @@ from zerver.lib.bot_config import ConfigError, get_bot_config
from zerver.lib.bot_lib import get_bot_handler
from zerver.lib.integrations import EMBEDDED_BOTS, WebhookIntegration
from zerver.lib.test_classes import UploadSerializeMixin, ZulipTestCase
from zerver.lib.test_helpers import avatar_disk_path, get_test_image_file, queries_captured
from zerver.lib.test_helpers import avatar_disk_path, get_test_image_file
from zerver.models import (
Realm,
Service,
@@ -153,13 +153,11 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):
self.assert_num_bots_equal(num_bots)
with queries_captured() as queries:
with self.assert_database_query_count(3):
users_result = self.client_get("/json/users")
self.assert_json_success(users_result)
self.assert_length(queries, 3)
def test_add_bot(self) -> None:
hamlet = self.example_user("hamlet")
self.login("hamlet")