tests: Mock print() for management command tests.

This avoids spam in test-backend output.
This commit is contained in:
Mohit Gupta 2020-07-22 02:18:16 +05:30 committed by Tim Abbott
parent a7db38c6c9
commit c65729511a
2 changed files with 64 additions and 24 deletions

View File

@ -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'),
])

View File

@ -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")