Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
tests: Clear in-memory Client caches before testing query counts.
Alex Vandiver7f46773ef1a1
diff --git a/corporate/tests/test_activity_views.py b/corporate/tests/test_activity_views.pyindex 2828f746491..89552727eab 100644--- a/corporate/tests/test_activity_views.py+++ b/corporate/tests/test_activity_views.py@@ -141,7 +141,7 @@ class ActivityTest(ZulipTestCase):user_profile.is_staff = Trueuser_profile.save(update_fields=["is_staff"])- with self.assert_database_query_count(11):+ with self.assert_database_query_count(12):result = self.client_get("/activity")self.assertEqual(result.status_code, 200)@@ -187,20 +187,20 @@ class ActivityTest(ZulipTestCase):event_time=timezone_now() - timedelta(days=1),extra_data=extra_data,)- with self.assert_database_query_count(9):+ with self.assert_database_query_count(10):result = self.client_get("/activity/remote")self.assertEqual(result.status_code, 200)- with self.assert_database_query_count(4):+ with self.assert_database_query_count(5):result = self.client_get("/activity/integrations")self.assertEqual(result.status_code, 200)- with self.assert_database_query_count(6):+ with self.assert_database_query_count(7):result = self.client_get("/realm_activity/zulip/")self.assertEqual(result.status_code, 200)iago = self.example_user("iago")- with self.assert_database_query_count(5):+ with self.assert_database_query_count(6):result = self.client_get(f"/user_activity/{iago.id}/")self.assertEqual(result.status_code, 200)diff --git a/zerver/lib/test_helpers.py b/zerver/lib/test_helpers.pyindex 45e4d77e05e..65f85532380 100644--- a/zerver/lib/test_helpers.py+++ b/zerver/lib/test_helpers.py@@ -53,7 +53,7 @@ from zerver.lib.rate_limiter import RateLimitedIPAddr, rulesfrom zerver.lib.request import RequestNotesfrom zerver.lib.upload.s3 import S3UploadBackendfrom zerver.models import Client, Message, RealmUserDefault, Subscription, UserMessage, UserProfile-from zerver.models.clients import get_client+from zerver.models.clients import clear_client_cache, get_clientfrom zerver.models.realms import get_realmfrom zerver.models.streams import get_streamfrom zerver.tornado.handlers import AsyncDjangoHandler, allocate_handler_id@@ -181,6 +181,7 @@ def queries_captured(cache = get_cache_backend(None)cache.clear()flush_per_request_caches()+ clear_client_cache()with mock.patch.multiple(TimeTrackingCursor, execute=cursor_execute, executemany=cursor_executemany):diff --git a/zerver/tests/test_bots.py b/zerver/tests/test_bots.pyindex 24692df53bd..eab39c90953 100644--- a/zerver/tests/test_bots.py+++ b/zerver/tests/test_bots.py@@ -148,7 +148,7 @@ class BotTest(ZulipTestCase, UploadSerializeMixin):self.assert_num_bots_equal(num_bots)- with self.assert_database_query_count(3):+ with self.assert_database_query_count(4):users_result = self.client_get("/json/users")self.assert_json_success(users_result)diff --git a/zerver/tests/test_custom_profile_data.py b/zerver/tests/test_custom_profile_data.pyindex 6b8268b24f7..4bc17ab7beb 100644--- a/zerver/tests/test_custom_profile_data.py+++ b/zerver/tests/test_custom_profile_data.py@@ -906,7 +906,7 @@ class ListCustomProfileFieldTest(CustomProfileFieldTestCase):test_bot = self.create_test_bot("foo-bot", iago)self.login_user(iago)- with self.assert_database_query_count(4):+ with self.assert_database_query_count(5):response = self.client_get("/json/users", {"client_gravatar": "false", "include_custom_profile_fields": "true"})diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.pyindex 3f8d7bace6c..8193bd095bc 100644--- a/zerver/tests/test_decorators.py+++ b/zerver/tests/test_decorators.py@@ -53,7 +53,6 @@ from zerver.lib.users import get_api_keyfrom zerver.lib.utils import generate_api_key, has_api_key_formatfrom zerver.middleware import LogRequests, parse_clientfrom zerver.models import Client, Realm, UserProfile-from zerver.models.clients import clear_client_cachefrom zerver.models.realms import get_realmfrom zerver.models.users import get_user@@ -1664,7 +1663,7 @@ class ClientTestCase(ZulipTestCase):return notes.client, notes.client_nameself.assertEqual(Client.objects.filter(name="ZulipThingy").count(), 0)- with queries_captured() as queries:+ with queries_captured(keep_cache_warm=True) as queries:client, client_name = request_user_agent("ZulipThingy/1.0.0")self.assertEqual(client.name, "ZulipThingy")self.assertEqual(client_name, "ZulipThingy")@@ -1672,7 +1671,7 @@ class ClientTestCase(ZulipTestCase):self.assert_length(queries, 2)# Ensure our in-memory cache prevents another database hit- with queries_captured() as queries:+ with queries_captured(keep_cache_warm=True) as queries:client, client_name = request_user_agent("ZulipThingy/1.0.0",)@@ -1681,7 +1680,7 @@ class ClientTestCase(ZulipTestCase):self.assert_length(queries, 0)# This operates on the extracted value, so different ZulipThingy versions don't cause another DB query- with queries_captured() as queries:+ with queries_captured(keep_cache_warm=True) as queries:client, client_name = request_user_agent("ZulipThingy/2.0.0",)@@ -1691,8 +1690,7 @@ class ClientTestCase(ZulipTestCase):# If we clear the memory cache we see a database query but get# the same client-id back.- clear_client_cache()- with queries_captured() as queries:+ with queries_captured(keep_cache_warm=False) as queries:fresh_client, client_name = request_user_agent("ZulipThingy/2.0.0",)@@ -1701,7 +1699,7 @@ class ClientTestCase(ZulipTestCase):self.assert_length(queries, 1)# Ensure that long parsed user-agents (longer than 30 characters) work- with queries_captured() as queries:+ with queries_captured(keep_cache_warm=True) as queries:client, client_name = request_user_agent("very-long-name-goes-here-and-somewhere-else ([email protected])")@@ -1711,7 +1709,7 @@ class ClientTestCase(ZulipTestCase):self.assert_length(queries, 2)# Longer than that uses the same in-memory cache key, so no database queries- with queries_captured() as queries:+ with queries_captured(keep_cache_warm=True) as queries:client, client_name = request_user_agent("very-long-name-goes-here-and-still-works ([email protected])")diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.pyindex 8f50253bb3a..aea9fb407b1 100644--- a/zerver/tests/test_home.py+++ b/zerver/tests/test_home.py@@ -249,7 +249,7 @@ class HomeTest(ZulipTestCase):self.client_post("/json/bots", bot_info)# Verify succeeds once logged-in- with self.assert_database_query_count(49):+ with self.assert_database_query_count(50):with patch("zerver.lib.cache.cache_set") as cache_mock:result = self._get_home_page(stream="Denmark")self.check_rendered_logged_in_app(result)@@ -257,7 +257,7 @@ class HomeTest(ZulipTestCase):set(result["Cache-Control"].split(", ")), {"must-revalidate", "no-store", "no-cache"})- self.assert_length(cache_mock.call_args_list, 5)+ self.assert_length(cache_mock.call_args_list, 6)html = result.content.decode()@@ -465,7 +465,7 @@ class HomeTest(ZulipTestCase):self._get_home_page()# Then for the second page load, measure the number of queries.- with self.assert_database_query_count(44):+ with self.assert_database_query_count(45):result = self._get_home_page()# Do a sanity check that our new streams were in the payload.diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.pyindex fbdb1a4a456..c25c3474d2f 100644--- a/zerver/tests/test_subs.py+++ b/zerver/tests/test_subs.py@@ -4621,7 +4621,7 @@ class SubscriptionAPITest(ZulipTestCase):# Now add ourselveswith self.capture_send_event_calls(expected_num_events=2) as events:- with self.assert_database_query_count(13):+ with self.assert_database_query_count(14):self.common_subscribe_to_streams(self.test_user,streams_to_sub,@@ -5530,7 +5530,7 @@ class SubscriptionAPITest(ZulipTestCase):)# Test creating private stream.- with self.assert_database_query_count(36):+ with self.assert_database_query_count(38):self.common_subscribe_to_streams(self.test_user,[new_streams[1]],@@ -5542,7 +5542,7 @@ class SubscriptionAPITest(ZulipTestCase):notifications_stream = get_stream(self.streams[0], self.test_realm)self.test_realm.notifications_stream_id = notifications_stream.idself.test_realm.save()- with self.assert_database_query_count(45):+ with self.assert_database_query_count(47):self.common_subscribe_to_streams(self.test_user,[new_streams[2]],diff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.pyindex 3803395d7d6..9a6c51e8bda 100644--- a/zerver/tests/test_upload.py+++ b/zerver/tests/test_upload.py@@ -713,7 +713,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):# Owner user should be able to view fileself.login_user(hamlet)- with self.assert_database_query_count(5):+ with self.assert_database_query_count(6):response = self.client_get(url)self.assertEqual(response.status_code, 200)self.assertEqual(response.getvalue(), b"zulip!")@@ -721,7 +721,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):# Subscribed user who received the message should be able to view fileself.login_user(cordelia)- with self.assert_database_query_count(6):+ with self.assert_database_query_count(7):response = self.client_get(url)self.assertEqual(response.status_code, 200)self.assertEqual(response.getvalue(), b"zulip!")@@ -774,7 +774,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):# Owner user should be able to view fileself.login_user(user)- with self.assert_database_query_count(5):+ with self.assert_database_query_count(6):response = self.client_get(url)self.assertEqual(response.status_code, 200)self.assertEqual(response.getvalue(), b"zulip!")@@ -782,7 +782,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):# Originally subscribed user should be able to view fileself.login_user(polonius)- with self.assert_database_query_count(6):+ with self.assert_database_query_count(7):response = self.client_get(url)self.assertEqual(response.status_code, 200)self.assertEqual(response.getvalue(), b"zulip!")@@ -790,7 +790,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):# Subscribed user who did not receive the message should also be able to view fileself.login_user(late_subscribed_user)- with self.assert_database_query_count(9):+ with self.assert_database_query_count(10):response = self.client_get(url)self.assertEqual(response.status_code, 200)self.assertEqual(response.getvalue(), b"zulip!")@@ -800,7 +800,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):def assert_cannot_access_file(user: UserProfile) -> None:self.login_user(user)# It takes a few extra queries to verify lack of access with shared history.- with self.assert_database_query_count(8):+ with self.assert_database_query_count(9):response = self.client_get(url)self.assertEqual(response.status_code, 403)self.assert_in_response("You are not authorized to view this file.", response)@@ -841,7 +841,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):user = self.example_user("aaron")self.login_user(user)- with self.assert_database_query_count(8):+ with self.assert_database_query_count(9):response = self.client_get(url)self.assertEqual(response.status_code, 403)self.assert_in_response("You are not authorized to view this file.", response)@@ -850,7 +850,7 @@ class FileUploadTest(UploadSerializeMixin, ZulipTestCase):self.subscribe(user, "test-subscribe 2")# If we were accidentally one query per message, this would be 20+- with self.assert_database_query_count(9):+ with self.assert_database_query_count(10):response = self.client_get(url)self.assertEqual(response.status_code, 200)self.assertEqual(response.getvalue(), b"zulip!")diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex 45b2293bb2e..2df83134290 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -1462,7 +1462,7 @@ class UserProfileTest(ZulipTestCase):# Subscribe to the stream.self.subscribe(iago, stream.name)- with self.assert_database_query_count(6):+ with self.assert_database_query_count(7):result = orjson.loads(self.client_get(f"/json/users/{iago.id}/subscriptions/{stream.id}").content)