Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Improve full-stack testing without mocking whole modules #615

Merged
merged 3 commits into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,10 @@
# Packages needed for running the tests.
extras["test"] = [
"pytest<8",
"pytest-cov<4",
"pytest-cov<5",
"pytest-mock<4",
"pytest-mqtt<1",
"tox<4",
"surrogate==0.1",
'dataclasses; python_version<"3.7"',
"requests-toolbelt>=0.9.1,<1",
"responses>=0.13.3,<1",
Expand Down
4 changes: 0 additions & 4 deletions tests/services/test_amqp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
# (c) 2021-2022 The mqttwarn developers
from unittest.mock import ANY, Mock, call

from surrogate import surrogate

from mqttwarn.model import ProcessorItem as Item
from mqttwarn.util import load_module_by_name


@surrogate("puka")
def test_amqp_success(srv, mocker, caplog):
module = load_module_by_name("mqttwarn.services.amqp")

Expand Down Expand Up @@ -47,7 +44,6 @@ def test_amqp_success(srv, mocker, caplog):
assert "Successfully published AMQP notification" in caplog.messages


@surrogate("puka")
def test_amqp_failure(srv, mocker, caplog):
module = load_module_by_name("mqttwarn.services.amqp")

Expand Down
9 changes: 3 additions & 6 deletions tests/services/test_apns.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
from unittest import mock
from unittest.mock import call

from surrogate import surrogate
import pytest

from mqttwarn.model import ProcessorItem as Item
from mqttwarn.util import load_module_from_file

pytest.skip(reason="The `apns` package is not ready for Python3", allow_module_level=True)


@surrogate("apns")
@mock.patch("apns.APNs", create=True)
@mock.patch("apns.Payload", create=True)
def test_apns_success(mock_apns_payload, mock_apns, srv, caplog):
Expand Down Expand Up @@ -43,7 +44,6 @@ def test_apns_success(mock_apns_payload, mock_apns, srv, caplog):
assert "Successfully published APNS notification to foobar" in caplog.messages


@surrogate("apns")
@mock.patch("apns.APNs", create=True)
@mock.patch("apns.Payload", create=True)
def test_apns_success_no_payload(mock_apns_payload, mock_apns, srv, caplog):
Expand All @@ -67,7 +67,6 @@ def test_apns_success_no_payload(mock_apns_payload, mock_apns, srv, caplog):
assert "Successfully published APNS notification to foobar" in caplog.messages


@surrogate("apns")
@mock.patch("apns.APNs", create=True)
@mock.patch("apns.Payload", create=True)
def test_apns_success_custom_payload(mock_apns_payload, mock_apns, srv, caplog):
Expand Down Expand Up @@ -101,7 +100,6 @@ def test_apns_success_custom_payload(mock_apns_payload, mock_apns, srv, caplog):
assert "Successfully published APNS notification to foobar" in caplog.messages


@surrogate("apns")
@mock.patch("apns.APNs", create=True)
@mock.patch("apns.Payload", create=True)
def test_apns_failure_invalid_config(mock_apns_payload, mock_apns, srv, caplog):
Expand All @@ -121,7 +119,6 @@ def test_apns_failure_invalid_config(mock_apns_payload, mock_apns, srv, caplog):
assert "Incorrect service configuration" in caplog.messages


@surrogate("apns")
@mock.patch("apns.APNs", create=True)
@mock.patch("apns.Payload", create=True)
def test_apns_failure_apns_token_missing(mock_apns_payload, mock_apns, srv, caplog):
Expand Down
7 changes: 0 additions & 7 deletions tests/services/test_asterisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
from unittest import mock
from unittest.mock import Mock, call

from surrogate import surrogate

from mqttwarn.model import ProcessorItem as Item
from mqttwarn.util import load_module_from_file


@surrogate("asterisk.manager")
@mock.patch("asterisk.manager.Manager", create=True)
def test_asterisk_success(asterisk_mock, srv, caplog):

Expand Down Expand Up @@ -66,7 +63,6 @@ class ManagerException(Exception):
pass


@surrogate("asterisk.manager")
@mock.patch("asterisk.manager.Manager", create=True)
@mock.patch("asterisk.manager.ManagerSocketException", ManagerSocketException, create=True)
def test_asterisk_failure_no_connection(asterisk_mock, srv, caplog):
Expand Down Expand Up @@ -101,7 +97,6 @@ def test_asterisk_failure_no_connection(asterisk_mock, srv, caplog):
assert "Error connecting to the manager: something failed" in caplog.messages


@surrogate("asterisk.manager")
@mock.patch("asterisk.manager.Manager", create=True)
@mock.patch("asterisk.manager.ManagerSocketException", ManagerSocketException, create=True)
@mock.patch("asterisk.manager.ManagerAuthException", ManagerAuthException, create=True)
Expand Down Expand Up @@ -138,7 +133,6 @@ def test_asterisk_failure_login_invalid(asterisk_mock, srv, caplog):
assert "Error logging in to the manager: something failed" in caplog.messages


@surrogate("asterisk.manager")
@mock.patch("asterisk.manager.Manager", create=True)
@mock.patch("asterisk.manager.ManagerSocketException", ManagerSocketException, create=True)
@mock.patch("asterisk.manager.ManagerAuthException", ManagerAuthException, create=True)
Expand Down Expand Up @@ -188,7 +182,6 @@ def test_asterisk_failure_originate_croaks(asterisk_mock, srv, caplog):
assert "Error: something failed" in caplog.messages


@surrogate("asterisk.manager")
@mock.patch("asterisk.manager.Manager", create=True)
@mock.patch("asterisk.manager.ManagerSocketException", ManagerSocketException, create=True)
@mock.patch("asterisk.manager.ManagerAuthException", ManagerAuthException, create=True)
Expand Down
12 changes: 5 additions & 7 deletions tests/services/test_desktopnotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from unittest.mock import Mock, call

import pytest
from surrogate import surrogate

from mqttwarn.model import ProcessorItem as Item
from mqttwarn.model import Struct
Expand All @@ -13,12 +12,11 @@

@pytest.fixture
def desktop_notifier_mock(mocker):
with surrogate("desktop_notifier"):
notifier = mocker.patch("desktop_notifier.DesktopNotifier", create=True)
mocker.patch("desktop_notifier.Urgency", create=True)
mocker.patch("desktop_notifier.Button", create=True)
mocker.patch("desktop_notifier.ReplyField", create=True)
yield notifier
notifier = mocker.patch("desktop_notifier.DesktopNotifier", create=True)
mocker.patch("desktop_notifier.Urgency", create=True)
mocker.patch("desktop_notifier.Button", create=True)
mocker.patch("desktop_notifier.ReplyField", create=True)
yield notifier


def test_desktopnotify_vanilla_success(desktop_notifier_mock, srv, caplog):
Expand Down
23 changes: 10 additions & 13 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import docopt
import pytest
from surrogate import surrogate

import mqttwarn.commands
from mqttwarn.configuration import Config
Expand Down Expand Up @@ -162,12 +161,11 @@ def test_setup_logging_default(mocker):
"""
config = Config()

with surrogate("logging"):
logging_mock: Mock = mocker.patch("logging.basicConfig")
mqttwarn.commands.setup_logging(config)
logging_mock.assert_called_with(
format="%(asctime)-15s %(levelname)-8s [%(name)-26s] %(message)s", level=10, stream=mock.ANY
)
logging_mock: Mock = mocker.patch("logging.basicConfig")
mqttwarn.commands.setup_logging(config)
logging_mock.assert_called_with(
format="%(asctime)-15s %(levelname)-8s [%(name)-26s] %(message)s", level=10, stream=mock.ANY
)


def test_setup_logging_no_logfile():
Expand All @@ -186,9 +184,8 @@ def test_setup_logging_logfile_without_protocol(mocker):
config = Config()
config.logfile = "sys.stderr"

with surrogate("logging"):
logging_mock: Mock = mocker.patch("logging.basicConfig")
mqttwarn.commands.setup_logging(config)
logging_mock.assert_called_with(
filename="sys.stderr", format="%(asctime)-15s %(levelname)-8s [%(name)-26s] %(message)s", level=10
)
logging_mock: Mock = mocker.patch("logging.basicConfig")
mqttwarn.commands.setup_logging(config)
logging_mock.assert_called_with(
filename="sys.stderr", format="%(asctime)-15s %(levelname)-8s [%(name)-26s] %(message)s", level=10
)