From d6be955fa75946bb0fcf084fe10bc09cb737b8a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Wed, 11 Dec 2019 15:35:23 +0200 Subject: [PATCH] Silence stdout output from management commands during tests --- hc/accounts/tests/test_pruneusers.py | 6 ++- hc/accounts/tests/test_senddeletionnotices.py | 38 ++++--------------- hc/api/tests/test_prunepingsslow.py | 3 +- hc/api/tests/test_sendreports.py | 6 +-- 4 files changed, 16 insertions(+), 37 deletions(-) diff --git a/hc/accounts/tests/test_pruneusers.py b/hc/accounts/tests/test_pruneusers.py index e51bc68c..b18746ec 100644 --- a/hc/accounts/tests/test_pruneusers.py +++ b/hc/accounts/tests/test_pruneusers.py @@ -6,6 +6,7 @@ from hc.accounts.management.commands.pruneusers import Command from hc.accounts.models import Project from hc.api.models import Check from hc.test import BaseTestCase +from mock import Mock class PruneUsersTestCase(BaseTestCase): @@ -19,7 +20,7 @@ class PruneUsersTestCase(BaseTestCase): charlies_project = Project.objects.create(owner=self.charlie) Check(project=charlies_project).save() - Command().handle() + Command(stdout=Mock()).handle() self.assertEqual(User.objects.filter(username="charlie").count(), 0) self.assertEqual(Check.objects.count(), 0) @@ -29,6 +30,7 @@ class PruneUsersTestCase(BaseTestCase): self.bob.last_login = self.year_ago self.bob.save() - Command().handle() + Command(stdout=Mock()).handle() + # Bob belongs to a team so should not get removed self.assertEqual(User.objects.filter(username="bob").count(), 1) diff --git a/hc/accounts/tests/test_senddeletionnotices.py b/hc/accounts/tests/test_senddeletionnotices.py index a9257dcf..0187622a 100644 --- a/hc/accounts/tests/test_senddeletionnotices.py +++ b/hc/accounts/tests/test_senddeletionnotices.py @@ -24,8 +24,7 @@ class SendDeletionNoticesTestCase(BaseTestCase): self.project.member_set.all().delete() def test_it_sends_notice(self): - cmd = Command() - cmd.stdout = Mock() # silence output to stdout + cmd = Command(stdout=Mock()) cmd.pause = Mock() # don't pause for 1s result = cmd.handle() @@ -42,10 +41,7 @@ class SendDeletionNoticesTestCase(BaseTestCase): self.alice.last_login = now() - td(days=15) self.alice.save() - cmd = Command() - cmd.stdout = Mock() # silence output to stdout - - result = cmd.handle() + result = Command(stdout=Mock()).handle() self.assertEqual(result, "Done! Sent 0 notices") self.profile.refresh_from_db() @@ -56,10 +52,7 @@ class SendDeletionNoticesTestCase(BaseTestCase): self.alice.date_joined = now() - td(days=15) self.alice.save() - cmd = Command() - cmd.stdout = Mock() # silence output to stdout - - result = cmd.handle() + result = Command(stdout=Mock()).handle() self.assertEqual(result, "Done! Sent 0 notices") self.profile.refresh_from_db() @@ -70,10 +63,7 @@ class SendDeletionNoticesTestCase(BaseTestCase): self.profile.deletion_notice_date = now() - td(days=15) self.profile.save() - cmd = Command() - cmd.stdout = Mock() # silence output to stdout - - result = cmd.handle() + result = Command(stdout=Mock()).handle() self.assertEqual(result, "Done! Sent 0 notices") def test_it_checks_sms_limit(self): @@ -81,10 +71,7 @@ class SendDeletionNoticesTestCase(BaseTestCase): self.profile.sms_limit = 50 self.profile.save() - cmd = Command() - cmd.stdout = Mock() # silence output to stdout - - result = cmd.handle() + result = Command(stdout=Mock()).handle() self.assertEqual(result, "Done! Sent 0 notices") self.profile.refresh_from_db() @@ -94,10 +81,7 @@ class SendDeletionNoticesTestCase(BaseTestCase): # bob has access to alice's project Member.objects.create(user=self.bob, project=self.project) - cmd = Command() - cmd.stdout = Mock() # silence output to stdout - - result = cmd.handle() + result = Command(stdout=Mock()).handle() self.assertEqual(result, "Done! Sent 0 notices") self.profile.refresh_from_db() @@ -107,10 +91,7 @@ class SendDeletionNoticesTestCase(BaseTestCase): check = Check.objects.create(project=self.project) Ping.objects.create(owner=check) - cmd = Command() - cmd.stdout = Mock() # silence output to stdout - - result = cmd.handle() + result = Command(stdout=Mock()).handle() self.assertEqual(result, "Done! Sent 0 notices") self.profile.refresh_from_db() @@ -121,8 +102,5 @@ class SendDeletionNoticesTestCase(BaseTestCase): self.profile.last_active_date = now() - td(days=15) self.profile.save() - cmd = Command() - cmd.stdout = Mock() # silence output to stdout - - result = cmd.handle() + result = Command(stdout=Mock()).handle() self.assertEqual(result, "Done! Sent 0 notices") diff --git a/hc/api/tests/test_prunepingsslow.py b/hc/api/tests/test_prunepingsslow.py index 0db46549..fca88663 100644 --- a/hc/api/tests/test_prunepingsslow.py +++ b/hc/api/tests/test_prunepingsslow.py @@ -4,6 +4,7 @@ from django.utils import timezone from hc.api.management.commands.prunepingsslow import Command from hc.api.models import Check, Ping from hc.test import BaseTestCase +from mock import Mock class PrunePingsSlowTestCase(BaseTestCase): @@ -19,6 +20,6 @@ class PrunePingsSlowTestCase(BaseTestCase): Ping.objects.create(owner=c, n=1) Ping.objects.create(owner=c, n=2) - Command().handle() + Command(stdout=Mock()).handle() self.assertEqual(Ping.objects.count(), 1) diff --git a/hc/api/tests/test_sendreports.py b/hc/api/tests/test_sendreports.py index 0ddc4e49..94590d1b 100644 --- a/hc/api/tests/test_sendreports.py +++ b/hc/api/tests/test_sendreports.py @@ -32,8 +32,7 @@ class SendReportsTestCase(BaseTestCase): self.check.save() def test_it_sends_report(self): - cmd = Command() - cmd.stdout = Mock() # silence output to stdout + cmd = Command(stdout=Mock()) cmd.pause = Mock() # don't pause for 1s found = cmd.handle_one_monthly_report() @@ -84,8 +83,7 @@ class SendReportsTestCase(BaseTestCase): self.assertEqual(len(mail.outbox), 0) def test_it_sends_nag(self): - cmd = Command() - cmd.stdout = Mock() # silence output to stdout + cmd = Command(stdout=Mock()) cmd.pause = Mock() # don't pause for 1s found = cmd.handle_one_nag()