|
1
|
|
|
""" |
|
2
|
|
|
Tests for the Marvin Joke action |
|
3
|
|
|
""" |
|
4
|
|
|
|
|
5
|
|
|
from unittest import mock |
|
6
|
|
|
|
|
7
|
|
|
from test_action import ActionTest |
|
8
|
|
|
from irc2phpbb import marvin_actions |
|
9
|
|
|
|
|
10
|
|
|
class JokeTest(ActionTest): |
|
11
|
|
|
"""Tests for the Marvin Joke action""" |
|
12
|
|
|
def assertJokeOutput(self, exampleFile, expectedOutput): |
|
13
|
|
|
"""Assert that a joke is returned, given an input file""" |
|
14
|
|
|
response = self.createResponseFrom("joke", exampleFile) |
|
15
|
|
|
with mock.patch("irc2phpbb.marvin_actions.requests") as r: |
|
16
|
|
|
r.get.return_value = response |
|
17
|
|
|
self.assertActionOutput(marvin_actions.marvinJoke, "joke", expectedOutput) |
|
18
|
|
|
|
|
19
|
|
|
def testJokeRequest(self): |
|
20
|
|
|
"""Test that marvin sends a proper request for a joke""" |
|
21
|
|
|
with mock.patch("irc2phpbb.marvin_actions.requests") as r: |
|
22
|
|
|
self.executeAction(marvin_actions.marvinJoke, "joke") |
|
23
|
|
|
self.assertEqual( |
|
24
|
|
|
r.get.call_args.args[0], |
|
25
|
|
|
"https://api.chucknorris.io/jokes/random?category=dev") |
|
26
|
|
|
|
|
27
|
|
|
def testJoke(self): |
|
28
|
|
|
"""Test that marvin sends a joke when requested""" |
|
29
|
|
|
self.assertJokeOutput("joke", "There is no Esc key on Chuck Norris' keyboard, because no one escapes Chuck Norris.") |
|
30
|
|
|
|
|
31
|
|
|
def testJokeError(self): |
|
32
|
|
|
"""Tests that marvin returns the proper error message when joke API is down""" |
|
33
|
|
|
with mock.patch("irc2phpbb.marvin_actions.requests.get", side_effect=Exception("API Down!")): |
|
34
|
|
|
self.assertStringsOutput(marvin_actions.marvinJoke, "kör ett skämt", "joke", "error") |
|
35
|
|
|
|