-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
201 lines (173 loc) · 8.93 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# -*- coding: utf-8 -*-
# vim: set ft=python ts=4 sw=4 expandtab:
# Note: this must be executed by supybot-test. Use 'run test' from the command line.
#
# Unfortunately, tests must live alongside the source code for supybot-test to execute them.
# So, this lives here rather than in the tests modules with all of the other unit tests.
from datetime import datetime
from unittest.mock import ANY, MagicMock, call, patch
from supybot.test import ChannelPluginTestCase
from HcoopMeetbot.plugin import _context
from hcoopmeetbotlogic.interface import Message
# These are values used by the plugin test case
ID = "id"
NICK = "test"
CHANNEL = "#test"
NETWORK = "test"
PREFIX = "@"
TIMESTAMP = datetime(2021, 3, 7, 13, 14, 0)
def _stub(context, **kwargs): # pylint: disable=unused-argument:
"""Stub handler method that returns a static reply; without this, the handler tests all time out."""
context.send_reply("Hello")
def _inbound(payload: str):
"""Generate an expected inbound message generated via doPrivmsg()."""
return Message(
id=ID,
timestamp=TIMESTAMP,
nick=NICK,
channel=CHANNEL,
network=NETWORK,
payload="%s%s" % (PREFIX, payload),
topic="",
channel_nicks=[NICK],
)
def _outbound():
"""Generate an expected outbound message returned to the caller based on the _stub() call"""
return Message(id=ID, timestamp=TIMESTAMP, nick=NICK, channel=CHANNEL, network=NETWORK, payload="%s: Hello" % NICK)
class HcoopMeetbotTestCase(ChannelPluginTestCase): # type: ignore
plugins = ("HcoopMeetbot",)
@patch("HcoopMeetbot.plugin.ircmsgs.topic")
@patch("HcoopMeetbot.plugin.ircmsgs.privmsg")
def test_context(self, privmsg, topic):
"""Test behavior of the Context object returned by the _context() function."""
topic.return_value = "generated-topic"
privmsg.return_value = "generated-message"
plugin = MagicMock()
plugin.log = MagicMock()
msg = MagicMock(args=["channel"])
channel = MagicMock(topic="topic")
channels = {"channel": channel}
state = MagicMock(channels=channels)
irc = MagicMock(state=state)
irc.sendMsg = MagicMock()
irc.reply = MagicMock()
result = _context(plugin, irc, msg)
assert result.get_topic() == "topic"
result.set_topic("provided-topic")
result.send_reply("provided-reply")
result.send_message("provided-message")
topic.assert_called_once_with("channel", "provided-topic")
privmsg.assert_called_once_with("channel", "provided-message")
irc.sendMsg.assert_has_calls([call("generated-topic"), call("generated-message")])
@patch("HcoopMeetbot.plugin.handler.meetversion")
@patch("HcoopMeetbot.plugin.handler.outbound_message")
@patch("HcoopMeetbot.plugin.handler.irc_message")
@patch("HcoopMeetbot.plugin.now")
@patch("HcoopMeetbot.plugin.uuid4")
def test_meetversion(self, uuid4, now, irc_message, outbound_message, meetversion) -> None:
"""Test the meetversion command"""
uuid4.return_value = MagicMock(hex=ID)
now.return_value = TIMESTAMP
meetversion.side_effect = _stub
self.assertNotError("meetversion")
irc_message.assert_called_once_with(context=ANY, message=_inbound("meetversion"))
outbound_message.assert_called_once_with(context=ANY, message=_outbound())
meetversion.assert_called_once_with(context=ANY)
@patch("HcoopMeetbot.plugin.handler.listmeetings")
@patch("HcoopMeetbot.plugin.handler.outbound_message")
@patch("HcoopMeetbot.plugin.handler.irc_message")
@patch("HcoopMeetbot.plugin.now")
@patch("HcoopMeetbot.plugin.uuid4")
def test_listmeetings(self, uuid4, now, irc_message, outbound_message, listmeetings) -> None:
"""Test the listmeetings command"""
uuid4.return_value = MagicMock(hex=ID)
now.return_value = TIMESTAMP
listmeetings.side_effect = _stub
self.assertNotError("listmeetings")
irc_message.assert_called_once_with(context=ANY, message=_inbound("listmeetings"))
outbound_message.assert_called_once_with(context=ANY, message=_outbound())
listmeetings.assert_called_once_with(context=ANY)
@patch("HcoopMeetbot.plugin.handler.savemeetings")
@patch("HcoopMeetbot.plugin.handler.outbound_message")
@patch("HcoopMeetbot.plugin.handler.irc_message")
@patch("HcoopMeetbot.plugin.now")
@patch("HcoopMeetbot.plugin.uuid4")
def test_savemeetings(self, uuid4, now, irc_message, outbound_message, savemeetings) -> None:
"""Test the savemeetings command"""
uuid4.return_value = MagicMock(hex=ID)
now.return_value = TIMESTAMP
savemeetings.side_effect = _stub
self.assertNotError("savemeetings")
irc_message.assert_called_once_with(context=ANY, message=_inbound("savemeetings"))
outbound_message.assert_called_once_with(context=ANY, message=_outbound())
savemeetings.assert_called_once_with(context=ANY)
@patch("HcoopMeetbot.plugin.handler.addchair")
@patch("HcoopMeetbot.plugin.handler.outbound_message")
@patch("HcoopMeetbot.plugin.handler.irc_message")
@patch("HcoopMeetbot.plugin.now")
@patch("HcoopMeetbot.plugin.uuid4")
def test_addchair(self, uuid4, now, irc_message, outbound_message, addchair) -> None:
"""Test the addchair command"""
uuid4.return_value = MagicMock(hex=ID)
now.return_value = TIMESTAMP
addchair.side_effect = _stub
self.assertNotError("addchair nick")
irc_message.assert_called_once_with(context=ANY, message=_inbound("addchair nick"))
outbound_message.assert_called_once_with(context=ANY, message=_outbound())
addchair.assert_called_once_with(context=ANY, channel=CHANNEL, network=NETWORK, nick="nick")
@patch("HcoopMeetbot.plugin.handler.deletemeeting")
@patch("HcoopMeetbot.plugin.handler.outbound_message")
@patch("HcoopMeetbot.plugin.handler.irc_message")
@patch("HcoopMeetbot.plugin.now")
@patch("HcoopMeetbot.plugin.uuid4")
def test_deletemeeting_save(self, uuid4, now, irc_message, outbound_message, deletemeeting) -> None:
"""Test the deletemeeting command,.save=True"""
uuid4.return_value = MagicMock(hex=ID)
now.return_value = TIMESTAMP
deletemeeting.side_effect = _stub
self.assertNotError("deletemeeting true")
irc_message.assert_called_once_with(context=ANY, message=_inbound("deletemeeting true"))
outbound_message.assert_called_once_with(context=ANY, message=_outbound())
deletemeeting.assert_called_once_with(context=ANY, channel=CHANNEL, network=NETWORK, save=True)
@patch("HcoopMeetbot.plugin.handler.deletemeeting")
@patch("HcoopMeetbot.plugin.handler.outbound_message")
@patch("HcoopMeetbot.plugin.handler.irc_message")
@patch("HcoopMeetbot.plugin.now")
@patch("HcoopMeetbot.plugin.uuid4")
def test_deletemeeting_nosave(self, uuid4, now, irc_message, outbound_message, deletemeeting) -> None:
"""Test the deletemeeting command,.save=False"""
uuid4.return_value = MagicMock(hex=ID)
now.return_value = TIMESTAMP
deletemeeting.side_effect = _stub
self.assertNotError("deletemeeting false")
irc_message.assert_called_once_with(context=ANY, message=_inbound("deletemeeting false"))
outbound_message.assert_called_once_with(context=ANY, message=_outbound())
deletemeeting.assert_called_once_with(context=ANY, channel=CHANNEL, network=NETWORK, save=False)
@patch("HcoopMeetbot.plugin.handler.recent")
@patch("HcoopMeetbot.plugin.handler.outbound_message")
@patch("HcoopMeetbot.plugin.handler.irc_message")
@patch("HcoopMeetbot.plugin.now")
@patch("HcoopMeetbot.plugin.uuid4")
def test_recent(self, uuid4, now, irc_message, outbound_message, recent) -> None:
"""Test the recent command"""
uuid4.return_value = MagicMock(hex=ID)
now.return_value = TIMESTAMP
recent.side_effect = _stub
self.assertNotError("recent")
irc_message.assert_called_once_with(context=ANY, message=_inbound("recent"))
outbound_message.assert_called_once_with(context=ANY, message=_outbound())
recent.assert_called_once_with(context=ANY)
@patch("HcoopMeetbot.plugin.handler.commands")
@patch("HcoopMeetbot.plugin.handler.outbound_message")
@patch("HcoopMeetbot.plugin.handler.irc_message")
@patch("HcoopMeetbot.plugin.now")
@patch("HcoopMeetbot.plugin.uuid4")
def test_commands(self, uuid4, now, irc_message, outbound_message, commands) -> None:
"""Test the commands command"""
uuid4.return_value = MagicMock(hex=ID)
now.return_value = TIMESTAMP
commands.side_effect = _stub
self.assertNotError("commands")
irc_message.assert_called_once_with(context=ANY, message=_inbound("commands"))
outbound_message.assert_called_once_with(context=ANY, message=_outbound())
commands.assert_called_once_with(context=ANY)