|
1
|
|
|
""" |
|
2
|
|
|
Tests for the Marvin NameDay action |
|
3
|
|
|
""" |
|
4
|
|
|
|
|
5
|
|
|
from unittest import mock |
|
6
|
|
|
|
|
7
|
|
|
from datetime import date |
|
8
|
|
|
from test_action import ActionTest |
|
9
|
|
|
from irc2phpbb import marvin_actions |
|
10
|
|
|
|
|
11
|
|
|
class NameDayTest(ActionTest): |
|
12
|
|
|
"""Tests for the Marvin Joke action""" |
|
13
|
|
|
def assertNameDayOutput(self, exampleFile, expectedOutput): |
|
14
|
|
|
"""Assert that the proper nameday message is returned, given an inputfile""" |
|
15
|
|
|
response = self.createResponseFrom("nameday", exampleFile) |
|
16
|
|
|
with mock.patch("irc2phpbb.marvin_actions.requests") as r: |
|
17
|
|
|
r.get.return_value = response |
|
18
|
|
|
self.assertActionOutput(marvin_actions.marvinNameday, "nameday", expectedOutput) |
|
19
|
|
|
|
|
20
|
|
|
def testNameDayReaction(self): |
|
21
|
|
|
"""Test that marvin only responds to nameday when asked""" |
|
22
|
|
|
self.assertActionSilent(marvin_actions.marvinNameday, "anything") |
|
23
|
|
|
|
|
24
|
|
|
def testNameDayRequest(self): |
|
25
|
|
|
"""Test that marvin sends a proper request for nameday info""" |
|
26
|
|
|
with mock.patch("irc2phpbb.marvin_actions.requests") as r, mock.patch("irc2phpbb.marvin_actions.datetime") as d: |
|
27
|
|
|
d.datetime.now.return_value = date(2024, 1, 2) |
|
28
|
|
|
self.executeAction(marvin_actions.marvinNameday, "namnsdag") |
|
29
|
|
|
self.assertEqual(r.get.call_args.args[0], "https://api.dryg.net/dagar/v2.1/2024/1/2") |
|
30
|
|
|
|
|
31
|
|
|
def testNameDayResponse(self): |
|
32
|
|
|
"""Test that marvin properly parses nameday responses""" |
|
33
|
|
|
self.assertNameDayOutput("single", "Idag har Svea namnsdag") |
|
34
|
|
|
self.assertNameDayOutput("double", "Idag har Alfred och Alfrida namnsdag") |
|
35
|
|
|
self.assertNameDayOutput("triple", "Idag har Kasper, Melker och Baltsar namnsdag") |
|
36
|
|
|
self.assertNameDayOutput("nobody", "Ingen har namnsdag idag") |
|
37
|
|
|
|
|
38
|
|
|
def testNameDayError(self): |
|
39
|
|
|
"""Tests that marvin returns the proper error message when nameday API is down""" |
|
40
|
|
|
with mock.patch("irc2phpbb.marvin_actions.requests.get", side_effect=Exception("API Down!")): |
|
41
|
|
|
self.assertStringsOutput( |
|
42
|
|
|
marvin_actions.marvinNameday, |
|
43
|
|
|
"har någon namnsdag idag?", |
|
44
|
|
|
"nameday", |
|
45
|
|
|
"error") |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
|