Silence stdout output from management commands during tests

This commit is contained in:
Pēteris Caune 2019-12-11 15:35:23 +02:00
parent 15ba415298
commit d6be955fa7
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2
4 changed files with 16 additions and 37 deletions

View File

@ -6,6 +6,7 @@ from hc.accounts.management.commands.pruneusers import Command
from hc.accounts.models import Project from hc.accounts.models import Project
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
class PruneUsersTestCase(BaseTestCase): class PruneUsersTestCase(BaseTestCase):
@ -19,7 +20,7 @@ class PruneUsersTestCase(BaseTestCase):
charlies_project = Project.objects.create(owner=self.charlie) charlies_project = Project.objects.create(owner=self.charlie)
Check(project=charlies_project).save() Check(project=charlies_project).save()
Command().handle() Command(stdout=Mock()).handle()
self.assertEqual(User.objects.filter(username="charlie").count(), 0) self.assertEqual(User.objects.filter(username="charlie").count(), 0)
self.assertEqual(Check.objects.count(), 0) self.assertEqual(Check.objects.count(), 0)
@ -29,6 +30,7 @@ class PruneUsersTestCase(BaseTestCase):
self.bob.last_login = self.year_ago self.bob.last_login = self.year_ago
self.bob.save() self.bob.save()
Command().handle() Command(stdout=Mock()).handle()
# Bob belongs to a team so should not get removed # Bob belongs to a team so should not get removed
self.assertEqual(User.objects.filter(username="bob").count(), 1) self.assertEqual(User.objects.filter(username="bob").count(), 1)

View File

@ -24,8 +24,7 @@ class SendDeletionNoticesTestCase(BaseTestCase):
self.project.member_set.all().delete() self.project.member_set.all().delete()
def test_it_sends_notice(self): def test_it_sends_notice(self):
cmd = Command() cmd = Command(stdout=Mock())
cmd.stdout = Mock() # silence output to stdout
cmd.pause = Mock() # don't pause for 1s cmd.pause = Mock() # don't pause for 1s
result = cmd.handle() result = cmd.handle()
@ -42,10 +41,7 @@ class SendDeletionNoticesTestCase(BaseTestCase):
self.alice.last_login = now() - td(days=15) self.alice.last_login = now() - td(days=15)
self.alice.save() self.alice.save()
cmd = Command() result = Command(stdout=Mock()).handle()
cmd.stdout = Mock() # silence output to stdout
result = cmd.handle()
self.assertEqual(result, "Done! Sent 0 notices") self.assertEqual(result, "Done! Sent 0 notices")
self.profile.refresh_from_db() self.profile.refresh_from_db()
@ -56,10 +52,7 @@ class SendDeletionNoticesTestCase(BaseTestCase):
self.alice.date_joined = now() - td(days=15) self.alice.date_joined = now() - td(days=15)
self.alice.save() self.alice.save()
cmd = Command() result = Command(stdout=Mock()).handle()
cmd.stdout = Mock() # silence output to stdout
result = cmd.handle()
self.assertEqual(result, "Done! Sent 0 notices") self.assertEqual(result, "Done! Sent 0 notices")
self.profile.refresh_from_db() self.profile.refresh_from_db()
@ -70,10 +63,7 @@ class SendDeletionNoticesTestCase(BaseTestCase):
self.profile.deletion_notice_date = now() - td(days=15) self.profile.deletion_notice_date = now() - td(days=15)
self.profile.save() self.profile.save()
cmd = Command() result = Command(stdout=Mock()).handle()
cmd.stdout = Mock() # silence output to stdout
result = cmd.handle()
self.assertEqual(result, "Done! Sent 0 notices") self.assertEqual(result, "Done! Sent 0 notices")
def test_it_checks_sms_limit(self): def test_it_checks_sms_limit(self):
@ -81,10 +71,7 @@ class SendDeletionNoticesTestCase(BaseTestCase):
self.profile.sms_limit = 50 self.profile.sms_limit = 50
self.profile.save() self.profile.save()
cmd = Command() result = Command(stdout=Mock()).handle()
cmd.stdout = Mock() # silence output to stdout
result = cmd.handle()
self.assertEqual(result, "Done! Sent 0 notices") self.assertEqual(result, "Done! Sent 0 notices")
self.profile.refresh_from_db() self.profile.refresh_from_db()
@ -94,10 +81,7 @@ class SendDeletionNoticesTestCase(BaseTestCase):
# bob has access to alice's project # bob has access to alice's project
Member.objects.create(user=self.bob, project=self.project) Member.objects.create(user=self.bob, project=self.project)
cmd = Command() result = Command(stdout=Mock()).handle()
cmd.stdout = Mock() # silence output to stdout
result = cmd.handle()
self.assertEqual(result, "Done! Sent 0 notices") self.assertEqual(result, "Done! Sent 0 notices")
self.profile.refresh_from_db() self.profile.refresh_from_db()
@ -107,10 +91,7 @@ class SendDeletionNoticesTestCase(BaseTestCase):
check = Check.objects.create(project=self.project) check = Check.objects.create(project=self.project)
Ping.objects.create(owner=check) Ping.objects.create(owner=check)
cmd = Command() result = Command(stdout=Mock()).handle()
cmd.stdout = Mock() # silence output to stdout
result = cmd.handle()
self.assertEqual(result, "Done! Sent 0 notices") self.assertEqual(result, "Done! Sent 0 notices")
self.profile.refresh_from_db() self.profile.refresh_from_db()
@ -121,8 +102,5 @@ class SendDeletionNoticesTestCase(BaseTestCase):
self.profile.last_active_date = now() - td(days=15) self.profile.last_active_date = now() - td(days=15)
self.profile.save() self.profile.save()
cmd = Command() result = Command(stdout=Mock()).handle()
cmd.stdout = Mock() # silence output to stdout
result = cmd.handle()
self.assertEqual(result, "Done! Sent 0 notices") self.assertEqual(result, "Done! Sent 0 notices")

View File

@ -4,6 +4,7 @@ from django.utils import timezone
from hc.api.management.commands.prunepingsslow import Command from hc.api.management.commands.prunepingsslow import Command
from hc.api.models import Check, Ping from hc.api.models import Check, Ping
from hc.test import BaseTestCase from hc.test import BaseTestCase
from mock import Mock
class PrunePingsSlowTestCase(BaseTestCase): class PrunePingsSlowTestCase(BaseTestCase):
@ -19,6 +20,6 @@ class PrunePingsSlowTestCase(BaseTestCase):
Ping.objects.create(owner=c, n=1) Ping.objects.create(owner=c, n=1)
Ping.objects.create(owner=c, n=2) Ping.objects.create(owner=c, n=2)
Command().handle() Command(stdout=Mock()).handle()
self.assertEqual(Ping.objects.count(), 1) self.assertEqual(Ping.objects.count(), 1)

View File

@ -32,8 +32,7 @@ class SendReportsTestCase(BaseTestCase):
self.check.save() self.check.save()
def test_it_sends_report(self): def test_it_sends_report(self):
cmd = Command() cmd = Command(stdout=Mock())
cmd.stdout = Mock() # silence output to stdout
cmd.pause = Mock() # don't pause for 1s cmd.pause = Mock() # don't pause for 1s
found = cmd.handle_one_monthly_report() found = cmd.handle_one_monthly_report()
@ -84,8 +83,7 @@ class SendReportsTestCase(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):
cmd = Command() cmd = Command(stdout=Mock())
cmd.stdout = Mock() # silence output to stdout
cmd.pause = Mock() # don't pause for 1s cmd.pause = Mock() # don't pause for 1s
found = cmd.handle_one_nag() found = cmd.handle_one_nag()