From 45f6b73993094690fa50835392aa21e600658ede Mon Sep 17 00:00:00 2001 From: Paul Traylor Date: Wed, 11 Dec 2019 18:53:19 +0900 Subject: [PATCH] [BUGFIX] Fixes to unit tests to avoid signal errors - Fix mocking our signal handlers - Run isort and minor cleanup on unittests --- promgen/tests/{test_rules.py => test_alert_rules.py} | 0 promgen/tests/test_api.py | 4 ---- promgen/tests/test_cli.py | 12 ++++++++---- promgen/tests/test_email.py | 1 - promgen/tests/test_routes.py | 9 +++++---- promgen/tests/test_silence.py | 3 ++- promgen/tests/test_webhook.py | 6 +++--- 7 files changed, 18 insertions(+), 17 deletions(-) rename promgen/tests/{test_rules.py => test_alert_rules.py} (100%) diff --git a/promgen/tests/test_rules.py b/promgen/tests/test_alert_rules.py similarity index 100% rename from promgen/tests/test_rules.py rename to promgen/tests/test_alert_rules.py diff --git a/promgen/tests/test_api.py b/promgen/tests/test_api.py index 7f618a207..0468a1320 100644 --- a/promgen/tests/test_api.py +++ b/promgen/tests/test_api.py @@ -2,14 +2,10 @@ # These sources are released under the terms of the MIT license: see LICENSE -from promgen import models, views -from promgen.tests import PromgenTest - from django.test import override_settings from django.urls import reverse from promgen import models, views - from promgen.tests import PromgenTest TEST_SETTINGS = PromgenTest.data_yaml('examples', 'promgen.yml') diff --git a/promgen/tests/test_cli.py b/promgen/tests/test_cli.py index 9dfec8c97..ddbe1dad3 100644 --- a/promgen/tests/test_cli.py +++ b/promgen/tests/test_cli.py @@ -1,15 +1,18 @@ # Copyright (c) 2019 LINE Corporation # These sources are released under the terms of the MIT license: see LICENSE -from promgen import models -from promgen.tests import PromgenTest +from unittest import mock from django.core import management from django.core.management.base import CommandError +from promgen import models +from promgen.tests import PromgenTest + class CLITests(PromgenTest): - def test_register_job(self): + @mock.patch("django.dispatch.dispatcher.Signal.send") + def test_register_job(self, mock_signal): # Assert when project doesn't exist with self.assertRaises(CommandError): management.call_command("register-job", "missing-project", "example", 1234) @@ -31,7 +34,8 @@ def test_register_job(self): management.call_command("register-job", "TestProject", "example", 4321) self.assertCount(models.Exporter, 2) - def test_register_host(self): + @mock.patch("django.dispatch.dispatcher.Signal.send") + def test_register_host(self, mock_signal): # Assert when project doesn't exist with self.assertRaises(CommandError): management.call_command("register-host", "missing-project", "example.com") diff --git a/promgen/tests/test_email.py b/promgen/tests/test_email.py index 764c93bcc..06c1b7a32 100644 --- a/promgen/tests/test_email.py +++ b/promgen/tests/test_email.py @@ -10,7 +10,6 @@ from promgen.notification.email import NotificationEmail from promgen.tests import PromgenTest - TEST_SETTINGS = PromgenTest.data_yaml('examples', 'promgen.yml') TEST_ALERT = PromgenTest.data('examples', 'alertmanager.json') diff --git a/promgen/tests/test_routes.py b/promgen/tests/test_routes.py index dc43d1d12..346bffc4c 100644 --- a/promgen/tests/test_routes.py +++ b/promgen/tests/test_routes.py @@ -4,12 +4,12 @@ import requests -from promgen import models, views -from promgen.tests import PromgenTest - from django.test import override_settings from django.urls import reverse +from promgen import models, views +from promgen.tests import PromgenTest + TEST_SETTINGS = PromgenTest.data_yaml('examples', 'promgen.yml') TEST_ALERT = PromgenTest.data('examples', 'alertmanager.json') TEST_IMPORT = PromgenTest.data('examples', 'import.json') @@ -19,7 +19,8 @@ class RouteTests(PromgenTest): longMessage = True - def setUp(self): + @mock.patch('django.dispatch.dispatcher.Signal.send') + def setUp(self, mock_signal): self.user = self.add_force_login(id=999, username="Foo") @override_settings(PROMGEN=TEST_SETTINGS) diff --git a/promgen/tests/test_silence.py b/promgen/tests/test_silence.py index 76f19f2f1..dd73fe7bd 100644 --- a/promgen/tests/test_silence.py +++ b/promgen/tests/test_silence.py @@ -18,7 +18,8 @@ class SilenceTest(PromgenTest): - def setUp(self): + @mock.patch("django.dispatch.dispatcher.Signal.send") + def setUp(self, mock_signal): self.user = self.add_force_login(id=999, username="Foo") @override_settings(PROMGEN=TEST_SETTINGS) diff --git a/promgen/tests/test_webhook.py b/promgen/tests/test_webhook.py index f15b7696a..4d9461fbe 100644 --- a/promgen/tests/test_webhook.py +++ b/promgen/tests/test_webhook.py @@ -3,13 +3,13 @@ from unittest import mock +from django.test import override_settings +from django.urls import reverse + from promgen import models, views from promgen.notification.webhook import NotificationWebhook from promgen.tests import PromgenTest -from django.test import override_settings -from django.urls import reverse - TEST_SETTINGS = PromgenTest.data_yaml("examples", "promgen.yml") TEST_ALERT = PromgenTest.data("examples", "alertmanager.json")