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
77from mqttwarn .model import ProcessorItem as Item
88from 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