From fbe77c9e0adfd9ea0d0b5eedc9e26a02d6b118a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Mon, 30 Apr 2018 20:02:36 +0300 Subject: [PATCH] Silence output from management commands when running tests. --- hc/api/management/commands/sendreports.py | 7 +++++-- hc/api/tests/test_sendalerts.py | 4 +++- hc/api/tests/test_sendreports.py | 13 +++++++++++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/hc/api/management/commands/sendreports.py b/hc/api/management/commands/sendreports.py index 2558be3a..bcb421cc 100644 --- a/hc/api/management/commands/sendreports.py +++ b/hc/api/management/commands/sendreports.py @@ -18,6 +18,9 @@ class Command(BaseCommand): help = 'Send due monthly reports and nags' tmpl = "Sent monthly report to %s" + def pause(self): + time.sleep(1) + def add_arguments(self, parser): parser.add_argument( '--loop', @@ -56,7 +59,7 @@ class Command(BaseCommand): if profile.send_report(): self.stdout.write(self.tmpl % profile.user.email) # Pause before next report to avoid hitting sending quota - time.sleep(1) + self.pause() return True @@ -80,7 +83,7 @@ class Command(BaseCommand): if profile.send_report(nag=True): self.stdout.write("Sent nag to %s" % profile.user.email) # Pause before next report to avoid hitting sending quota - time.sleep(1) + self.pause() else: profile.next_nag_date = None profile.save() diff --git a/hc/api/tests/test_sendalerts.py b/hc/api/tests/test_sendalerts.py index 2275adfa..6ddb0712 100644 --- a/hc/api/tests/test_sendalerts.py +++ b/hc/api/tests/test_sendalerts.py @@ -1,4 +1,5 @@ from datetime import timedelta +from io import StringIO from mock import Mock, patch from django.core.management import call_command @@ -89,7 +90,8 @@ class SendAlertsTestCase(BaseTestCase): check.alert_after = check.get_alert_after() check.save() - call_command("sendalerts", loop=False, use_threads=False) + call_command("sendalerts", loop=False, use_threads=False, + stdout=StringIO()) # It should call `notify` instead of `notify_on_thread` self.assertTrue(mock_notify.called) diff --git a/hc/api/tests/test_sendreports.py b/hc/api/tests/test_sendreports.py index 44ca4b39..ae5a9cf9 100644 --- a/hc/api/tests/test_sendreports.py +++ b/hc/api/tests/test_sendreports.py @@ -5,6 +5,7 @@ from django.utils.timezone import now from hc.api.management.commands.sendreports import Command from hc.api.models import Check from hc.test import BaseTestCase +from mock import Mock, patch class SendAlertsTestCase(BaseTestCase): @@ -28,7 +29,11 @@ class SendAlertsTestCase(BaseTestCase): self.check.save() def test_it_sends_report(self): - found = Command().handle_one_monthly_report() + cmd = Command() + cmd.stdout = Mock() # silence output to stdout + cmd.pause = Mock() # don't pause for 1s + + found = cmd.handle_one_monthly_report() self.assertTrue(found) self.profile.refresh_from_db() @@ -59,7 +64,11 @@ class SendAlertsTestCase(BaseTestCase): self.assertEqual(len(mail.outbox), 0) def test_it_sends_nag(self): - found = Command().handle_one_nag() + cmd = Command() + cmd.stdout = Mock() # silence output to stdout + cmd.pause = Mock() # don't pause for 1s + + found = cmd.handle_one_nag() self.assertTrue(found) self.profile.refresh_from_db()