forked from GithubBackups/healthchecks
Silence output from management commands when running tests.
This commit is contained in:
parent
45a8bd0df1
commit
fbe77c9e0a
@ -18,6 +18,9 @@ class Command(BaseCommand):
|
|||||||
help = 'Send due monthly reports and nags'
|
help = 'Send due monthly reports and nags'
|
||||||
tmpl = "Sent monthly report to %s"
|
tmpl = "Sent monthly report to %s"
|
||||||
|
|
||||||
|
def pause(self):
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--loop',
|
'--loop',
|
||||||
@ -56,7 +59,7 @@ class Command(BaseCommand):
|
|||||||
if profile.send_report():
|
if profile.send_report():
|
||||||
self.stdout.write(self.tmpl % profile.user.email)
|
self.stdout.write(self.tmpl % profile.user.email)
|
||||||
# Pause before next report to avoid hitting sending quota
|
# Pause before next report to avoid hitting sending quota
|
||||||
time.sleep(1)
|
self.pause()
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -80,7 +83,7 @@ class Command(BaseCommand):
|
|||||||
if profile.send_report(nag=True):
|
if profile.send_report(nag=True):
|
||||||
self.stdout.write("Sent nag to %s" % profile.user.email)
|
self.stdout.write("Sent nag to %s" % profile.user.email)
|
||||||
# Pause before next report to avoid hitting sending quota
|
# Pause before next report to avoid hitting sending quota
|
||||||
time.sleep(1)
|
self.pause()
|
||||||
else:
|
else:
|
||||||
profile.next_nag_date = None
|
profile.next_nag_date = None
|
||||||
profile.save()
|
profile.save()
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
from io import StringIO
|
||||||
from mock import Mock, patch
|
from mock import Mock, patch
|
||||||
|
|
||||||
from django.core.management import call_command
|
from django.core.management import call_command
|
||||||
@ -89,7 +90,8 @@ class SendAlertsTestCase(BaseTestCase):
|
|||||||
check.alert_after = check.get_alert_after()
|
check.alert_after = check.get_alert_after()
|
||||||
check.save()
|
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`
|
# It should call `notify` instead of `notify_on_thread`
|
||||||
self.assertTrue(mock_notify.called)
|
self.assertTrue(mock_notify.called)
|
||||||
|
@ -5,6 +5,7 @@ from django.utils.timezone import now
|
|||||||
from hc.api.management.commands.sendreports import Command
|
from hc.api.management.commands.sendreports import Command
|
||||||
from hc.api.models import Check
|
from hc.api.models import Check
|
||||||
from hc.test import BaseTestCase
|
from hc.test import BaseTestCase
|
||||||
|
from mock import Mock, patch
|
||||||
|
|
||||||
|
|
||||||
class SendAlertsTestCase(BaseTestCase):
|
class SendAlertsTestCase(BaseTestCase):
|
||||||
@ -28,7 +29,11 @@ class SendAlertsTestCase(BaseTestCase):
|
|||||||
self.check.save()
|
self.check.save()
|
||||||
|
|
||||||
def test_it_sends_report(self):
|
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.assertTrue(found)
|
||||||
|
|
||||||
self.profile.refresh_from_db()
|
self.profile.refresh_from_db()
|
||||||
@ -59,7 +64,11 @@ class SendAlertsTestCase(BaseTestCase):
|
|||||||
self.assertEqual(len(mail.outbox), 0)
|
self.assertEqual(len(mail.outbox), 0)
|
||||||
|
|
||||||
def test_it_sends_nag(self):
|
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.assertTrue(found)
|
||||||
|
|
||||||
self.profile.refresh_from_db()
|
self.profile.refresh_from_db()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user