Skip to content

Commit 6f74e31

Browse files
committed
Tests: Trim and improve test cases for service plugins
By removing the unneeded `caplog` context, and using `mocker` more often, this patch reduces indentation levels and makes reading the test cases more pleasant to the eye.
1 parent 8e0ccc4 commit 6f74e31

28 files changed

+1355
-1543
lines changed

mqttwarn/services/alexa-notify-me.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def plugin(srv, item):
2323
srv.logging.debug("*** MODULE=%s: service=%s, target=%s", __file__, item.service, item.target)
2424

2525
try:
26-
srv.logging.debug("Sending to NotifyMe service.")
26+
srv.logging.debug("Sending to NotifyMe service")
2727
body = json.dumps({
2828
"notification": item.message,
2929
"accessCode": item.addrs[0]
@@ -32,7 +32,7 @@ def plugin(srv, item):
3232
response = requests.post(url="https://api.notifymyecho.com/v1/NotifyMe", data=body)
3333
response.raise_for_status()
3434

35-
srv.logging.debug("Successfully sent to NotifyMe service.")
35+
srv.logging.debug("Successfully sent to NotifyMe service")
3636

3737
except Exception as e:
3838
srv.logging.warning("Failed to send message to NotifyMe service: %s" % e)

mqttwarn/services/amqp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def plugin(srv, item):
1616
exchange, routing_key = item.addrs
1717

1818
try:
19-
srv.logging.debug("AMQP publish to %s [%s/%s]..." % (item.target, exchange, routing_key))
19+
srv.logging.debug("AMQP publish to %s [%s/%s]" % (item.target, exchange, routing_key))
2020

2121
client = puka.Client(uri)
2222
promise = client.connect()

mqttwarn/services/azure_iot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def plugin(srv, item):
2121
iothubname = item.config['iothubname']
2222
qos = int(item.config.get('qos', 0))
2323
if qos < 0 or qos > 1:
24-
srv.logging.error("Only QoS 0 or 1 allowed for Azure IoT Hub, not '%s'." % str(qos))
24+
srv.logging.error("Only QoS 0 or 1 allowed for Azure IoT Hub, not '%s'" % str(qos))
2525
return False
2626

2727
# connection info...

mqttwarn/services/pushover.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def plugin(srv, item):
132132
image = base64.decodebytes(imagebase64)
133133

134134
try:
135-
srv.logging.debug("Sending pushover notification to %s [%s]...." % (item.target, params))
135+
srv.logging.debug("Sending pushover notification to %s [%s]" % (item.target, params))
136136
pushover(image=image, user=userkey, token=token, **params)
137137
srv.logging.debug("Successfully sent pushover notification")
138138
except Exception as e:

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
[tool.isort]
66
profile = "black"
7-
#src_paths = ["mqttwarn", "examples", "tests"]
8-
src_paths = ["test"]
97

108
[tool.black]
119
line-length = 120

tests/services/test_alexa.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
# -*- coding: utf-8 -*-
2-
# (c) 2021 The mqttwarn developers
3-
import logging
4-
from unittest import mock
5-
2+
# (c) 2021-2022 The mqttwarn developers
63
from mqttwarn.model import ProcessorItem as Item
74
from mqttwarn.util import load_module_from_file
85

96

10-
def test_alexa_notify_me_success(srv, caplog):
7+
def test_alexa_notify_me_success(srv, mocker, caplog):
118

129
module = load_module_from_file("mqttwarn/services/alexa-notify-me.py")
1310

1411
accessCode = "myToken"
1512
item = Item(addrs=[accessCode], message="⚽ Notification message ⚽")
1613

17-
with caplog.at_level(logging.DEBUG):
18-
with mock.patch("requests.post") as requests_mock:
19-
outcome = module.plugin(srv, item)
20-
requests_mock.assert_called_once_with(
21-
url="https://api.notifymyecho.com/v1/NotifyMe",
22-
data='{"notification": "\\u26bd Notification message \\u26bd", "accessCode": "myToken"}',
23-
)
14+
requests_mock = mocker.patch("requests.post")
15+
outcome = module.plugin(srv, item)
16+
requests_mock.assert_called_once_with(
17+
url="https://api.notifymyecho.com/v1/NotifyMe",
18+
data='{"notification": "\\u26bd Notification message \\u26bd", "accessCode": "myToken"}',
19+
)
2420

25-
assert outcome is True
26-
assert "Sending to NotifyMe service" in caplog.text
27-
assert "Successfully sent to NotifyMe service" in caplog.text
21+
assert outcome is True
22+
assert "Sending to NotifyMe service" in caplog.messages
23+
assert "Successfully sent to NotifyMe service" in caplog.messages
2824

2925

3026
def test_alexa_notify_me_real_auth_failure(srv, caplog):
@@ -33,9 +29,8 @@ def test_alexa_notify_me_real_auth_failure(srv, caplog):
3329
accessCode = "myToken"
3430
item = Item(addrs=[accessCode], message="⚽ Notification message ⚽")
3531

36-
with caplog.at_level(logging.DEBUG):
37-
outcome = module.plugin(srv, item)
32+
outcome = module.plugin(srv, item)
3833

39-
assert outcome is False
40-
assert "Sending to NotifyMe service" in caplog.text
41-
assert "Failed to send message to NotifyMe service" in caplog.text
34+
assert outcome is False
35+
assert "Sending to NotifyMe service" in caplog.messages
36+
assert "Failed to send message to NotifyMe service" in caplog.text

tests/services/test_amqp.py

Lines changed: 53 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
# -*- coding: utf-8 -*-
2-
# (c) 2021 The mqttwarn developers
3-
import logging
4-
from unittest import mock
5-
from unittest.mock import call
2+
# (c) 2021-2022 The mqttwarn developers
3+
from unittest.mock import ANY, Mock, call
4+
5+
from surrogate import surrogate
66

77
from mqttwarn.model import ProcessorItem as Item
88
from mqttwarn.util import load_module_by_name
9-
from surrogate import surrogate
109

1110

1211
@surrogate("puka")
13-
@mock.patch("puka.Client", create=True)
14-
def test_amqp_success(mock_puka_client, srv, caplog):
12+
def test_amqp_success(srv, mocker, caplog):
1513
module = load_module_by_name("mqttwarn.services.amqp")
1614

1715
exchange, routing_key = ["name_of_exchange", "my_routing_key"]
@@ -22,35 +20,35 @@ def test_amqp_success(mock_puka_client, srv, caplog):
2220
message="⚽ Notification message ⚽",
2321
)
2422

25-
with caplog.at_level(logging.DEBUG):
26-
27-
outcome = module.plugin(srv, item)
28-
29-
assert mock_puka_client.mock_calls == [
30-
mock.call("amqp://user:password@localhost:5672/"),
31-
call().connect(),
32-
call().wait(mock.ANY),
33-
call().basic_publish(
34-
exchange="name_of_exchange",
35-
routing_key="my_routing_key",
36-
headers={
37-
"content_type": "text/plain",
38-
"x-agent": "mqttwarn",
39-
"delivery_mode": 1,
40-
},
41-
body="⚽ Notification message ⚽",
42-
),
43-
call().wait(mock.ANY),
44-
call().close(),
45-
]
46-
47-
assert outcome is True
48-
assert "AMQP publish to test [name_of_exchange/my_routing_key]" in caplog.text
49-
assert "Successfully published AMQP notification" in caplog.text
23+
mock_puka_client = mocker.patch("puka.Client", create=True)
24+
25+
outcome = module.plugin(srv, item)
26+
27+
assert mock_puka_client.mock_calls == [
28+
call("amqp://user:password@localhost:5672/"),
29+
call().connect(),
30+
call().wait(ANY),
31+
call().basic_publish(
32+
exchange="name_of_exchange",
33+
routing_key="my_routing_key",
34+
headers={
35+
"content_type": "text/plain",
36+
"x-agent": "mqttwarn",
37+
"delivery_mode": 1,
38+
},
39+
body="⚽ Notification message ⚽",
40+
),
41+
call().wait(ANY),
42+
call().close(),
43+
]
44+
45+
assert outcome is True
46+
assert "AMQP publish to test [name_of_exchange/my_routing_key]" in caplog.messages
47+
assert "Successfully published AMQP notification" in caplog.messages
5048

5149

5250
@surrogate("puka")
53-
def test_amqp_failure(srv, caplog):
51+
def test_amqp_failure(srv, mocker, caplog):
5452
module = load_module_by_name("mqttwarn.services.amqp")
5553

5654
exchange, routing_key = ["name_of_exchange", "my_routing_key"]
@@ -61,28 +59,25 @@ def test_amqp_failure(srv, caplog):
6159
message="⚽ Notification message ⚽",
6260
)
6361

64-
with caplog.at_level(logging.DEBUG):
65-
66-
mock_connection = mock.MagicMock()
67-
68-
# Make the call to `basic_publish` raise an exception.
69-
def error(*args, **kwargs):
70-
raise Exception("something failed")
71-
72-
mock_connection.basic_publish = error
73-
74-
with mock.patch("puka.Client", side_effect=[mock_connection], create=True) as mock_client:
75-
76-
outcome = module.plugin(srv, item)
77-
78-
assert mock_client.mock_calls == [
79-
mock.call("amqp://user:password@localhost:5672/"),
80-
]
81-
assert mock_connection.mock_calls == [
82-
call.connect(),
83-
call.wait(mock.ANY),
84-
]
85-
86-
assert outcome is False
87-
assert "AMQP publish to test [name_of_exchange/my_routing_key]" in caplog.text
88-
assert "Error on AMQP publish to test [name_of_exchange/my_routing_key]: something failed" in caplog.text
62+
mock_connection = Mock(**{"basic_publish.side_effect": Exception("something failed")})
63+
mock_client = mocker.patch("puka.Client", side_effect=[mock_connection], create=True)
64+
65+
outcome = module.plugin(srv, item)
66+
67+
assert mock_client.mock_calls == [
68+
call("amqp://user:password@localhost:5672/"),
69+
]
70+
assert mock_connection.mock_calls == [
71+
call.connect(),
72+
call.wait(ANY),
73+
call.basic_publish(
74+
exchange="name_of_exchange",
75+
routing_key="my_routing_key",
76+
headers={"content_type": "text/plain", "x-agent": "mqttwarn", "delivery_mode": 1},
77+
body="⚽ Notification message ⚽",
78+
),
79+
]
80+
81+
assert outcome is False
82+
assert "AMQP publish to test [name_of_exchange/my_routing_key]" in caplog.messages
83+
assert "Error on AMQP publish to test [name_of_exchange/my_routing_key]: something failed" in caplog.messages

0 commit comments

Comments
 (0)