|
1
|
|
|
|
|
2
|
|
|
""" |
|
3
|
|
|
Module containing common code for testing Marvin actions |
|
4
|
|
|
""" |
|
5
|
|
|
|
|
6
|
|
|
import json |
|
7
|
|
|
import os |
|
8
|
|
|
|
|
9
|
|
|
from unittest import TestCase |
|
10
|
|
|
|
|
11
|
|
|
import requests |
|
12
|
|
|
|
|
13
|
|
|
from irc2phpbb.bot import Bot |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
class ActionTest(TestCase): |
|
17
|
|
|
"""Base class with utility functions for testing marvin actions""" |
|
18
|
|
|
strings = {} |
|
19
|
|
|
|
|
20
|
|
|
@classmethod |
|
21
|
|
|
def setUpClass(cls): |
|
22
|
|
|
with open("irc2phpbb/data/marvin_strings.json", encoding="utf-8") as f: |
|
23
|
|
|
cls.strings = json.load(f) |
|
24
|
|
|
|
|
25
|
|
|
def executeAction(self, action, message): |
|
26
|
|
|
"""Execute an action for a message and return the response""" |
|
27
|
|
|
return action(Bot.tokenize(message)) |
|
28
|
|
|
|
|
29
|
|
|
def assertActionOutput(self, action, message, expectedOutput): |
|
30
|
|
|
"""Call an action on message and assert expected output""" |
|
31
|
|
|
actualOutput = self.executeAction(action, message) |
|
32
|
|
|
self.assertEqual(actualOutput, expectedOutput) |
|
33
|
|
|
|
|
34
|
|
|
def assertActionSilent(self, action, message): |
|
35
|
|
|
"""Call an action with provided message and assert no output""" |
|
36
|
|
|
self.assertActionOutput(action, message, None) |
|
37
|
|
|
|
|
38
|
|
|
def assertStringsOutput(self, action, message, expectedoutputKey, subkey=None): |
|
39
|
|
|
"""Call an action with provided message and assert the output is equal to DB""" |
|
40
|
|
|
expectedOutput = self.strings.get(expectedoutputKey) |
|
41
|
|
|
if subkey is not None: |
|
42
|
|
|
if isinstance(expectedOutput, list): |
|
43
|
|
|
expectedOutput = expectedOutput[subkey] |
|
44
|
|
|
else: |
|
45
|
|
|
expectedOutput = expectedOutput.get(subkey) |
|
46
|
|
|
self.assertActionOutput(action, message, expectedOutput) |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
def createResponseFrom(self, directory, filename): |
|
50
|
|
|
"""Create a response object with contect as contained in the specified file""" |
|
51
|
|
|
path = os.path.join(os.path.dirname(__file__), "resources", directory, f"{filename}.json") |
|
52
|
|
|
with open(path, "r", encoding="UTF-8") as f: |
|
53
|
|
|
response = requests.models.Response() |
|
54
|
|
|
response._content = str.encode(json.dumps(json.load(f))) |
|
55
|
|
|
return response |
|
56
|
|
|
|