Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
tests: Eliminate use of get_user_profile_by_email in tests.
Mateusz Mandera20fd9b1decea
diff --git a/zerver/tests/test_cache.py b/zerver/tests/test_cache.pyindex 1b75d37a67f..7b62a059f22 100644--- a/zerver/tests/test_cache.py+++ b/zerver/tests/test_cache.py@@ -19,12 +19,12 @@ from zerver.lib.cache import (get_cache_with_key,safe_cache_get_many,safe_cache_set_many,- user_profile_by_email_cache_key,+ user_profile_by_id_cache_key,validate_cache_key,)from zerver.lib.test_classes import ZulipTestCasefrom zerver.lib.test_helpers import queries_captured-from zerver.models import UserProfile, get_system_bot, get_user_profile_by_email+from zerver.models import UserProfile, get_realm, get_system_bot, get_user, get_user_profile_by_idclass AppsTest(ZulipTestCase):@@ -258,8 +258,9 @@ class SafeCacheFunctionsTest(ZulipTestCase):class BotCacheKeyTest(ZulipTestCase):def test_bot_profile_key_deleted_on_save(self) -> None:+ realm = get_realm(settings.SYSTEM_BOT_REALM)# Get the profile cached on both cache keys:- user_profile = get_user_profile_by_email(settings.EMAIL_GATEWAY_BOT)+ user_profile = get_user(settings.EMAIL_GATEWAY_BOT, realm)bot_profile = get_system_bot(settings.EMAIL_GATEWAY_BOT)self.assertEqual(user_profile, bot_profile)@@ -273,34 +274,39 @@ class BotCacheKeyTest(ZulipTestCase):bot_profile2 = get_system_bot(settings.EMAIL_GATEWAY_BOT)self.assertEqual(bot_profile2.can_forge_sender, flipped_setting)- user_profile2 = get_user_profile_by_email(settings.EMAIL_GATEWAY_BOT)+ user_profile2 = get_user(settings.EMAIL_GATEWAY_BOT, realm)self.assertEqual(user_profile2.can_forge_sender, flipped_setting)+def get_user_id(user: UserProfile) -> int:+ return user.id # nocoverage++def get_user_email(user: UserProfile) -> str:return user.email # nocoverageclass GenericBulkCachedFetchTest(ZulipTestCase):def test_query_function_called_only_if_needed(self) -> None:+ hamlet = self.example_user("hamlet")# Get the user cached:- hamlet = get_user_profile_by_email(self.example_email("hamlet"))+ get_user_profile_by_id(hamlet.id)class CustomException(Exception):pass- def query_function(emails: List[str]) -> List[UserProfile]:+ def query_function(ids: List[int]) -> List[UserProfile]:raise CustomException("The query function was called")# query_function shouldn't be called, because the only requested object# is already cached:- result: Dict[str, UserProfile] = bulk_cached_fetch(- cache_key_function=user_profile_by_email_cache_key,+ result: Dict[int, UserProfile] = bulk_cached_fetch(+ cache_key_function=user_profile_by_id_cache_key,query_function=query_function,- object_ids=[self.example_email("hamlet")],- id_fetcher=get_user_email,+ object_ids=[hamlet.id],+ id_fetcher=get_user_id,)- self.assertEqual(result, {hamlet.delivery_email: hamlet})+ self.assertEqual(result, {hamlet.id: hamlet})with self.assertLogs(level="INFO") as info_log:flush_cache(Mock())self.assertEqual(info_log.output, ["INFO:root:Clearing memcached cache after migrations"])@@ -308,10 +314,10 @@ class GenericBulkCachedFetchTest(ZulipTestCase):# With the cache flushed, the query_function should get called:with self.assertRaises(CustomException):result = bulk_cached_fetch(- cache_key_function=user_profile_by_email_cache_key,+ cache_key_function=user_profile_by_id_cache_key,query_function=query_function,- object_ids=[self.example_email("hamlet")],- id_fetcher=get_user_email,+ object_ids=[hamlet.id],+ id_fetcher=get_user_id,)def test_empty_object_ids_list(self) -> None:diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.pyindex 0dcec14c8a6..c99af576de1 100644--- a/zerver/tests/test_realm.py+++ b/zerver/tests/test_realm.py@@ -33,7 +33,6 @@ from zerver.models import (UserProfile,get_realm,get_stream,- get_user_profile_by_email,get_user_profile_by_id,)@@ -179,10 +178,11 @@ class RealmTest(ZulipTestCase):Hamlet, and we end by checking the cache to ensure that hisrealm appears to be deactivated. You can make this test failby disabling cache.flush_realm()."""- get_user_profile_by_email("[email protected]")+ hamlet_id = self.example_user("hamlet").id+ get_user_profile_by_id(hamlet_id)realm = get_realm("zulip")do_deactivate_realm(realm)- user = get_user_profile_by_email("[email protected]")+ user = get_user_profile_by_id(hamlet_id)self.assertTrue(user.realm.deactivated)def test_do_change_realm_subdomain_clears_user_realm_cache(self) -> None:@@ -191,10 +191,11 @@ class RealmTest(ZulipTestCase):Hamlet, and we end by checking the cache to ensure that hisrealm appears to be deactivated. You can make this test failby disabling cache.flush_realm()."""- user = get_user_profile_by_email("[email protected]")+ hamlet_id = self.example_user("hamlet").id+ user = get_user_profile_by_id(hamlet_id)realm = get_realm("zulip")do_change_realm_subdomain(realm, "newzulip")- user = get_user_profile_by_email("[email protected]")+ user = get_user_profile_by_id(hamlet_id)self.assertEqual(user.realm.string_id, "newzulip")placeholder_realm = get_realm("zulip")