Skip to content

Commit ae61a36

Browse files
committed
Write tests for termdates plugin
1 parent 6a91dfc commit ae61a36

File tree

3 files changed

+139
-1
lines changed

3 files changed

+139
-1
lines changed

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ aioresponses==0.7.3
66
pytest-cov
77
asynctest==0.13.0
88
aiofastforward==0.0.24
9+
time-machine==2.6.0
910
mongomock
1011

1112
# Requirements for documentation

tests/conftest.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import annotations
12
import asyncio
23
from textwrap import dedent
34
from unittest import mock
@@ -205,7 +206,7 @@ def bot_helper_class():
205206

206207

207208
@pytest.fixture
208-
def bot_helper(irc_client, bot_helper_class):
209+
def bot_helper(irc_client, bot_helper_class) -> BotHelper:
209210
irc_client.bot_setup()
210211
return bot_helper_class(irc_client)
211212

tests/test_plugin_termdates.py

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import asyncio
2+
import datetime
3+
4+
import pytest
5+
6+
7+
pytestmark = [
8+
pytest.mark.bot(config="""\
9+
["@bot"]
10+
plugins = ["mongodb", "termdates"]
11+
12+
[mongodb]
13+
mode = "mock"
14+
"""),
15+
pytest.mark.usefixtures("run_client"),
16+
]
17+
18+
19+
def say(msg):
20+
return f":Nick!~user@hostname PRIVMSG #channel :{msg}"
21+
22+
23+
async def test_term_dates(bot_helper, time_machine):
24+
bot_helper.reset_mock()
25+
26+
# Nothing configured yet, !termdates should error
27+
await asyncio.wait(bot_helper.receive(say("!termdates")))
28+
bot_helper.assert_sent(lambda line: line.endswith("error: no term dates (see termdates.set)"))
29+
30+
# Save dates
31+
await asyncio.wait(bot_helper.receive([
32+
say("!termdates.set 2021-09-27 2022-01-10 2022-04-19"),
33+
say("!termdates"),
34+
]))
35+
bot_helper.assert_sent([
36+
lambda line: line.endswith("Aut 2021-09-27 -- 2021-12-03, "
37+
"Spr 2022-01-10 -- 2022-03-18, "
38+
"Sum 2022-04-19 -- 2022-06-24"),
39+
])
40+
41+
42+
async def test_week_command(bot_helper, time_machine):
43+
bot_helper.reset_mock()
44+
45+
# Nothing configured yet, !week should error
46+
await asyncio.wait(bot_helper.receive(say("!week")))
47+
bot_helper.assert_sent(lambda line: line.endswith("error: no term dates (see termdates.set)"))
48+
49+
# Configure term dates
50+
await asyncio.wait(bot_helper.receive(say("!termdates.set 2021-09-27 2022-01-10 2022-04-19")))
51+
52+
# `!week term n` should give the correct dates for the specified week in the specified term
53+
await asyncio.wait(bot_helper.receive(say("!week aut 3")))
54+
bot_helper.assert_sent(lambda line: line.endswith("Aut 3: 2021-10-11"))
55+
await asyncio.wait(bot_helper.receive(say("!week spr 10")))
56+
bot_helper.assert_sent(lambda line: line.endswith("Spr 10: 2022-03-14"))
57+
await asyncio.wait(bot_helper.receive(say("!week sum 4")))
58+
# TODO: should actually be bot_helper.assert_sent(lambda line: line.endswith("Sum 4: 2022-05-09"))
59+
bot_helper.assert_sent(lambda line: line.endswith("Sum 4: 2022-05-10"))
60+
# `!week n term` means the same as `!week term n`
61+
await asyncio.wait(bot_helper.receive(say("!week 3 aut")))
62+
bot_helper.assert_sent(lambda line: line.endswith("Aut 3: 2021-10-11"))
63+
await asyncio.wait(bot_helper.receive(say("!week 10 spr")))
64+
bot_helper.assert_sent(lambda line: line.endswith("Spr 10: 2022-03-14"))
65+
await asyncio.wait(bot_helper.receive(say("!week 4 sum")))
66+
# TODO: should actually be bot_helper.assert_sent(lambda line: line.endswith("Sum 4: 2022-05-09"))
67+
bot_helper.assert_sent(lambda line: line.endswith("Sum 4: 2022-05-10"))
68+
69+
# Time travel to before the start of the Autumn term
70+
time_machine.move_to(datetime.datetime(2021, 8, 1, 12, 0))
71+
# `!week` should give "Nth week before Aut"
72+
await asyncio.wait(bot_helper.receive(say("!week")))
73+
bot_helper.assert_sent(lambda line: line.endswith("9th week before Aut (starts 2021-09-27)"))
74+
# `!week n` should give the start of the Nth week in the Autumn term
75+
await asyncio.wait(bot_helper.receive(say("!week 3")))
76+
bot_helper.assert_sent(lambda line: line.endswith("Aut 3: 2021-10-11"))
77+
78+
# Time travel to during the Autumn term, week 4
79+
time_machine.move_to(datetime.datetime(2021, 10, 21, 12, 0))
80+
# `!week` should give "Aut 4: ..."
81+
await asyncio.wait(bot_helper.receive(say("!week")))
82+
bot_helper.assert_sent(lambda line: line.endswith("Aut 4: 2021-10-18"))
83+
# `!week n` should give the start of the Nth week in the Autumn term
84+
await asyncio.wait(bot_helper.receive(say("!week 3")))
85+
bot_helper.assert_sent(lambda line: line.endswith("Aut 3: 2021-10-11"))
86+
87+
# Time travel to after the Autumn term
88+
time_machine.move_to(datetime.datetime(2021, 12, 15, 12, 0))
89+
# `!week` should give "Nth week before Spr"
90+
await asyncio.wait(bot_helper.receive(say("!week")))
91+
bot_helper.assert_sent(lambda line: line.endswith("4th week before Spr (starts 2022-01-10)"))
92+
# `!week n` should give the start of the Nth week in the Spring term
93+
await asyncio.wait(bot_helper.receive(say("!week 3")))
94+
bot_helper.assert_sent(lambda line: line.endswith("Spr 3: 2022-01-24"))
95+
96+
# Time travel to during the Spring term, week 10
97+
time_machine.move_to(datetime.datetime(2022, 3, 16, 12, 0))
98+
# `!week` should give "Spr 10: ..."
99+
await asyncio.wait(bot_helper.receive(say("!week")))
100+
bot_helper.assert_sent(lambda line: line.endswith("Spr 10: 2022-03-14"))
101+
# `!week n` should give the start of the Nth week in the Spring term
102+
await asyncio.wait(bot_helper.receive(say("!week 3")))
103+
bot_helper.assert_sent(lambda line: line.endswith("Spr 3: 2022-01-24"))
104+
105+
# Time travel to after the Spring term
106+
time_machine.move_to(datetime.datetime(2022, 4, 4, 12, 0))
107+
# `!week` should give "Nth week before Sum"
108+
await asyncio.wait(bot_helper.receive(say("!week")))
109+
# TODO: should actually be
110+
# bot_helper.assert_sent(lambda line: line.endswith("2nd week before Sum (starts 2022-04-18)"))
111+
bot_helper.assert_sent(lambda line: line.endswith("3rd week before Sum (starts 2022-04-19)"))
112+
# `!week n` should give the start of the Nth week in the Summer term
113+
await asyncio.wait(bot_helper.receive(say("!week 3")))
114+
# TODO: should actually be bot_helper.assert_sent(lambda line: line.endswith("Sum 3: 2022-05-02"))
115+
bot_helper.assert_sent(lambda line: line.endswith("Sum 3: 2022-05-03"))
116+
117+
# Time travel to during the Summer term, week 7
118+
time_machine.move_to(datetime.datetime(2022, 5, 31, 12, 0))
119+
# `!week` should give "Sum 7: ..."
120+
await asyncio.wait(bot_helper.receive(say("!week")))
121+
# TODO: should actually be bot_helper.assert_sent(lambda line: line.endswith("Sum 7: 2022-05-30"))
122+
bot_helper.assert_sent(lambda line: line.endswith("Sum 7: 2022-05-31"))
123+
# `!week n` should give the start of the Nth week in the Summer term
124+
await asyncio.wait(bot_helper.receive(say("!week 3")))
125+
# TODO: should actually be bot_helper.assert_sent(lambda line: line.endswith("Sum 3: 2022-05-02"))
126+
bot_helper.assert_sent(lambda line: line.endswith("Sum 3: 2022-05-03"))
127+
128+
# TODO: currently just throws an exception when after the end of Summer term, fix code and enable these tests
129+
# time_machine.move_to(datetime.datetime(2022, 8, 15, 12, 0))
130+
# # `!week` should give "Sum 18"
131+
# await asyncio.wait(bot_helper.receive(say("!week")))
132+
# bot_helper.assert_sent(lambda line: line.endswith("Sum 18: 2022-08-15"))
133+
# # `!week n` should give the start of the Nth week in the Summer term
134+
# await asyncio.wait(bot_helper.receive(say("!week 3")))
135+
# # TODO: should actually be bot_helper.assert_sent(lambda line: line.endswith("Sum 3: 2022-05-02"))
136+
# bot_helper.assert_sent(lambda line: line.endswith("Sum 3: 2022-05-03"))

0 commit comments

Comments
 (0)