From c65729511ada269359e6363383a305d5d7bafca3 Mon Sep 17 00:00:00 2001 From: Mohit Gupta Date: Wed, 22 Jul 2020 02:18:16 +0530 Subject: [PATCH] tests: Mock print() for management command tests. This avoids spam in test-backend output. --- zerver/tests/test_management_commands.py | 28 +++++++++-- zerver/tests/test_mattermost_importer.py | 60 ++++++++++++++++-------- 2 files changed, 64 insertions(+), 24 deletions(-) diff --git a/zerver/tests/test_management_commands.py b/zerver/tests/test_management_commands.py index c5799783ca..f3d3619aea 100644 --- a/zerver/tests/test_management_commands.py +++ b/zerver/tests/test_management_commands.py @@ -439,22 +439,42 @@ class TestExport(ZulipTestCase): do_add_reaction(self.example_user("iago"), message, "outbox", "1f4e4", Reaction.UNICODE_EMOJI) do_add_reaction(self.example_user("hamlet"), message, "outbox", "1f4e4", Reaction.UNICODE_EMOJI) - with patch("zerver.management.commands.export.export_realm_wrapper") as m: + with patch("zerver.management.commands.export.export_realm_wrapper") as m, \ + patch('builtins.print') as mock_print: call_command(self.COMMAND_NAME, "-r=zulip", f"--consent-message-id={message.id}") m.assert_called_once_with(realm=realm, public_only=False, consent_message_id=message.id, delete_after_upload=False, threads=mock.ANY, output_dir=mock.ANY, upload=False) - with self.assertRaisesRegex(CommandError, "Message with given ID does not"): + self.assertEqual(mock_print.mock_calls, [ + call('\033[94mExporting realm\033[0m: zulip'), + call('\n\033[94mMessage content:\033[0m\nThumbs up for export\n'), + call('\033[94mNumber of users that reacted outbox:\033[0m 2\n') + ]) + + with self.assertRaisesRegex(CommandError, "Message with given ID does not"), \ + patch('builtins.print') as mock_print: call_command(self.COMMAND_NAME, "-r=zulip", "--consent-message-id=123456") + self.assertEqual(mock_print.mock_calls, [ + call('\033[94mExporting realm\033[0m: zulip'), + ]) message.last_edit_time = timezone_now() message.save() - with self.assertRaisesRegex(CommandError, "Message was edited. Aborting..."): + with self.assertRaisesRegex(CommandError, "Message was edited. Aborting..."), \ + patch('builtins.print') as mock_print: call_command(self.COMMAND_NAME, "-r=zulip", f"--consent-message-id={message.id}") + self.assertEqual(mock_print.mock_calls, [ + call('\033[94mExporting realm\033[0m: zulip'), + ]) message.last_edit_time = None message.save() do_add_reaction(self.mit_user("sipbtest"), message, "outbox", "1f4e4", Reaction.UNICODE_EMOJI) - with self.assertRaisesRegex(CommandError, "Users from a different realm reacted to message. Aborting..."): + with self.assertRaisesRegex(CommandError, "Users from a different realm reacted to message. Aborting..."), \ + patch('builtins.print') as mock_print: call_command(self.COMMAND_NAME, "-r=zulip", f"--consent-message-id={message.id}") + + self.assertEqual(mock_print.mock_calls, [ + call('\033[94mExporting realm\033[0m: zulip'), + ]) diff --git a/zerver/tests/test_mattermost_importer.py b/zerver/tests/test_mattermost_importer.py index f98784b4d0..725b18ecc8 100644 --- a/zerver/tests/test_mattermost_importer.py +++ b/zerver/tests/test_mattermost_importer.py @@ -2,6 +2,7 @@ import filecmp import logging import os from typing import Any, Dict, List +from unittest.mock import call, patch import ujson @@ -502,11 +503,16 @@ class MatterMostImporter(ZulipTestCase): mattermost_data_dir = self.fixture_file_name("", "mattermost_fixtures") output_dir = self.make_import_output_dir("mattermost") - do_convert_data( - mattermost_data_dir=mattermost_data_dir, - output_dir=output_dir, - masking_content=False, - ) + with patch('builtins.print') as mock_print: + do_convert_data( + mattermost_data_dir=mattermost_data_dir, + output_dir=output_dir, + masking_content=False, + ) + self.assertEqual(mock_print.mock_calls, [ + call('Generating data for', 'gryffindor'), + call('Generating data for', 'slytherin') + ]) harry_team_output_dir = self.team_output_dir(output_dir, "gryffindor") self.assertEqual(os.path.exists(os.path.join(harry_team_output_dir, 'avatars')), True) @@ -575,11 +581,15 @@ class MatterMostImporter(ZulipTestCase): mattermost_data_dir = self.fixture_file_name("direct_channel", "mattermost_fixtures") output_dir = self.make_import_output_dir("mattermost") - do_convert_data( - mattermost_data_dir=mattermost_data_dir, - output_dir=output_dir, - masking_content=False, - ) + with patch('builtins.print') as mock_print: + do_convert_data( + mattermost_data_dir=mattermost_data_dir, + output_dir=output_dir, + masking_content=False, + ) + self.assertEqual(mock_print.mock_calls, [ + call('Generating data for', 'gryffindor'), + ]) harry_team_output_dir = self.team_output_dir(output_dir, "gryffindor") self.assertEqual(os.path.exists(os.path.join(harry_team_output_dir, 'avatars')), True) @@ -666,11 +676,16 @@ class MatterMostImporter(ZulipTestCase): mattermost_data_dir = self.fixture_file_name("", "mattermost_fixtures") output_dir = self.make_import_output_dir("mattermost") - do_convert_data( - mattermost_data_dir=mattermost_data_dir, - output_dir=output_dir, - masking_content=True, - ) + with patch('builtins.print') as mock_print: + do_convert_data( + mattermost_data_dir=mattermost_data_dir, + output_dir=output_dir, + masking_content=True, + ) + self.assertEqual(mock_print.mock_calls, [ + call('Generating data for', 'gryffindor'), + call('Generating data for', 'slytherin') + ]) harry_team_output_dir = self.team_output_dir(output_dir, "gryffindor") messages = self.read_file(harry_team_output_dir, 'messages-000001.json') @@ -681,11 +696,16 @@ class MatterMostImporter(ZulipTestCase): mattermost_data_dir = self.fixture_file_name("", "mattermost_fixtures") output_dir = self.make_import_output_dir("mattermost") - do_convert_data( - mattermost_data_dir=mattermost_data_dir, - output_dir=output_dir, - masking_content=True, - ) + with patch('builtins.print') as mock_print: + do_convert_data( + mattermost_data_dir=mattermost_data_dir, + output_dir=output_dir, + masking_content=True, + ) + self.assertEqual(mock_print.mock_calls, [ + call('Generating data for', 'gryffindor'), + call('Generating data for', 'slytherin') + ]) harry_team_output_dir = self.team_output_dir(output_dir, "gryffindor")