From ffd4abaa4e84b3d10a68be8542e7edc52694cff9 Mon Sep 17 00:00:00 2001 From: m-e-l-u-h-a-n Date: Tue, 9 Mar 2021 23:21:16 +0530 Subject: [PATCH] logging: migrate test_import_export to use assertLogs. This commit migrates some of the backend tests in test_import_export to use assertLogs(), instead of mock.patch() as planned in #15331. Logs for tests in this file are suppressed and are not asserted as that made changes to import/export codebase more fragile. As we already have checks for the actual functionalities, it made less sense to assert those logs. --- zerver/tests/test_import_export.py | 73 ++++++++++++++---------------- 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/zerver/tests/test_import_export.py b/zerver/tests/test_import_export.py index 32f77ebf40..2c0b055587 100644 --- a/zerver/tests/test_import_export.py +++ b/zerver/tests/test_import_export.py @@ -207,7 +207,7 @@ class ImportExportTest(ZulipTestCase): consent_message_id: Optional[int] = None, ) -> Dict[str, Any]: output_dir = self._make_output_dir() - with patch("logging.info"), patch("zerver.lib.export.create_soft_link"): + with patch("zerver.lib.export.create_soft_link"), self.assertLogs(level="INFO"): do_export_realm( realm=realm, output_dir=output_dir, @@ -729,7 +729,7 @@ class ImportExportTest(ZulipTestCase): output_dir = self._make_output_dir() cordelia = self.example_user("cordelia") - with patch("logging.info"): + with self.assertLogs(level="INFO"): do_export_user(cordelia, output_dir) def read_file(fn: str) -> Any: @@ -827,9 +827,8 @@ class ImportExportTest(ZulipTestCase): self._export_realm(original_realm) - with patch("logging.info"): - with self.settings(BILLING_ENABLED=False): - do_import_realm(os.path.join(settings.TEST_WORKER_DIR, "test-export"), "test-zulip") + with self.settings(BILLING_ENABLED=False), self.assertLogs(level="INFO"): + do_import_realm(os.path.join(settings.TEST_WORKER_DIR, "test-export"), "test-zulip") # sanity checks @@ -1130,9 +1129,8 @@ class ImportExportTest(ZulipTestCase): self._export_realm(realm) - with self.settings(BILLING_ENABLED=False): - with patch("logging.info"): - do_import_realm(os.path.join(settings.TEST_WORKER_DIR, "test-export"), "test-zulip") + with self.settings(BILLING_ENABLED=False), self.assertLogs(level="INFO"): + do_import_realm(os.path.join(settings.TEST_WORKER_DIR, "test-export"), "test-zulip") imported_realm = Realm.objects.get(string_id="test-zulip") # Test attachments @@ -1193,9 +1191,9 @@ class ImportExportTest(ZulipTestCase): self._setup_export_files(realm) self._export_realm(realm) - with self.settings(BILLING_ENABLED=False): - with patch("logging.info"): - do_import_realm(os.path.join(settings.TEST_WORKER_DIR, "test-export"), "test-zulip") + with self.settings(BILLING_ENABLED=False), self.assertLogs(level="INFO"): + do_import_realm(os.path.join(settings.TEST_WORKER_DIR, "test-export"), "test-zulip") + imported_realm = Realm.objects.get(string_id="test-zulip") with get_test_image_file("img.png") as f: test_image_data = f.read() @@ -1278,30 +1276,29 @@ class ImportExportTest(ZulipTestCase): self._setup_export_files(realm) self._export_realm(realm) - with patch("logging.info"): - with self.settings(BILLING_ENABLED=True): - realm = do_import_realm( - os.path.join(settings.TEST_WORKER_DIR, "test-export"), "test-zulip-1" - ) - self.assertEqual(realm.plan_type, Realm.LIMITED) - self.assertEqual(realm.max_invites, 100) - self.assertEqual(realm.upload_quota_gb, 5) - self.assertEqual(realm.message_visibility_limit, 10000) - self.assertTrue( - RealmAuditLog.objects.filter( - realm=realm, event_type=RealmAuditLog.REALM_PLAN_TYPE_CHANGED - ).exists() - ) - with self.settings(BILLING_ENABLED=False): - realm = do_import_realm( - os.path.join(settings.TEST_WORKER_DIR, "test-export"), "test-zulip-2" - ) - self.assertEqual(realm.plan_type, Realm.SELF_HOSTED) - self.assertEqual(realm.max_invites, 100) - self.assertEqual(realm.upload_quota_gb, None) - self.assertEqual(realm.message_visibility_limit, None) - self.assertTrue( - RealmAuditLog.objects.filter( - realm=realm, event_type=RealmAuditLog.REALM_PLAN_TYPE_CHANGED - ).exists() - ) + with self.settings(BILLING_ENABLED=True), self.assertLogs(level="INFO"): + realm = do_import_realm( + os.path.join(settings.TEST_WORKER_DIR, "test-export"), "test-zulip-1" + ) + self.assertEqual(realm.plan_type, Realm.LIMITED) + self.assertEqual(realm.max_invites, 100) + self.assertEqual(realm.upload_quota_gb, 5) + self.assertEqual(realm.message_visibility_limit, 10000) + self.assertTrue( + RealmAuditLog.objects.filter( + realm=realm, event_type=RealmAuditLog.REALM_PLAN_TYPE_CHANGED + ).exists() + ) + with self.settings(BILLING_ENABLED=False), self.assertLogs(level="INFO"): + realm = do_import_realm( + os.path.join(settings.TEST_WORKER_DIR, "test-export"), "test-zulip-2" + ) + self.assertEqual(realm.plan_type, Realm.SELF_HOSTED) + self.assertEqual(realm.max_invites, 100) + self.assertEqual(realm.upload_quota_gb, None) + self.assertEqual(realm.message_visibility_limit, None) + self.assertTrue( + RealmAuditLog.objects.filter( + realm=realm, event_type=RealmAuditLog.REALM_PLAN_TYPE_CHANGED + ).exists() + )