-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
tree_config_builder_test.py
157 lines (121 loc) · 4.98 KB
/
tree_config_builder_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
import unittest
import json
import os
from pokemongo_bot import PokemonGoBot, ConfigException, MismatchTaskApiVersion, TreeConfigBuilder, PluginLoader, BaseTask
from pokemongo_bot.cell_workers import HandleSoftBan, CatchPokemon
from pokemongo_bot.test.resources.plugin_fixture import FakeTask, UnsupportedApiTask
def convert_from_json(str):
return json.loads(str)
class TreeConfigBuilderTest(unittest.TestCase):
def setUp(self):
self.bot = {}
def test_should_throw_on_no_type_key(self):
obj = convert_from_json("""[{
"bad_key": "foo"
}]""")
builder = TreeConfigBuilder(self.bot, obj)
self.assertRaisesRegexp(
ConfigException,
"No type found for given task",
builder.build)
def test_should_throw_on_non_matching_type(self):
obj = convert_from_json("""[{
"type": "foo"
}]""")
builder = TreeConfigBuilder(self.bot, obj)
self.assertRaisesRegexp(
ConfigException,
"No worker named foo defined",
builder.build)
def test_should_throw_on_wrong_evolve_task_name(self):
obj = convert_from_json("""[{
"type": "EvolveAll"
}]""")
builder = TreeConfigBuilder(self.bot, obj)
self.assertRaisesRegexp(
ConfigException,
"The EvolveAll task has been renamed to EvolvePokemon",
builder.build)
def test_creating_worker(self):
obj = convert_from_json("""[{
"type": "HandleSoftBan"
}]""")
builder = TreeConfigBuilder(self.bot, obj)
tree = builder.build()
self.assertIsInstance(tree[0], HandleSoftBan)
self.assertIs(tree[0].bot, self.bot)
def test_creating_two_workers(self):
obj = convert_from_json("""[{
"type": "HandleSoftBan"
}, {
"type": "CatchPokemon"
}]""")
builder = TreeConfigBuilder(self.bot, obj)
tree = builder.build()
self.assertIsInstance(tree[0], HandleSoftBan)
self.assertIs(tree[0].bot, self.bot)
self.assertIsInstance(tree[1], CatchPokemon)
self.assertIs(tree[1].bot, self.bot)
def test_task_with_config(self):
obj = convert_from_json("""[{
"type": "IncubateEggs",
"config": {
"longer_eggs_first": true
}
}]""")
builder = TreeConfigBuilder(self.bot, obj)
tree = builder.build()
self.assertTrue(tree[0].config.get('longer_eggs_first', False))
def test_disabling_task(self):
obj = convert_from_json("""[{
"type": "HandleSoftBan",
"config": {
"enabled": false
}
}, {
"type": "CatchPokemon",
"config": {
"enabled": true
}
}]""")
builder = TreeConfigBuilder(self.bot, obj)
tree = builder.build()
self.assertTrue(len(tree) == 1)
self.assertIsInstance(tree[0], CatchPokemon)
def test_load_plugin_task(self):
package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'resources', 'plugin_fixture')
plugin_loader = PluginLoader()
plugin_loader.load_plugin(package_path)
obj = convert_from_json("""[{
"type": "plugin_fixture.FakeTask"
}]""")
builder = TreeConfigBuilder(self.bot, obj)
tree = builder.build()
result = tree[0].work()
self.assertEqual(result, 'FakeTask')
def setupUnsupportedBuilder(self):
package_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'pokemongo_bot', 'test', 'resources', 'plugin_fixture')
plugin_loader = PluginLoader()
plugin_loader.load_plugin(package_path)
obj = convert_from_json("""[{
"type": "plugin_fixture.UnsupportedApiTask"
}]""")
return TreeConfigBuilder(self.bot, obj)
def test_task_version_too_high(self):
builder = self.setupUnsupportedBuilder()
previous_version = BaseTask.TASK_API_VERSION
BaseTask.TASK_API_VERSION = 1
self.assertRaisesRegexp(
MismatchTaskApiVersion,
"Task plugin_fixture.UnsupportedApiTask only works with task api version 2, you are currently running version 1. Do you need to update the bot?",
builder.build)
BaseTask.TASK_API_VERSION = previous_version
def test_task_version_too_low(self):
builder = self.setupUnsupportedBuilder()
previous_version = BaseTask.TASK_API_VERSION
BaseTask.TASK_API_VERSION = 3
self.assertRaisesRegexp(
MismatchTaskApiVersion,
"Task plugin_fixture.UnsupportedApiTask only works with task api version 2, you are currently running version 3. Is there a new version of this task?",
builder.build)
BaseTask.TASK_API_VERSION = previous_version