Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a TreeConfigBuilder and tests #1901

Merged
merged 3 commits into from
Jul 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ addons:
install:
- pip install -r requirements.txt
- pip install pylint
script: "python pylint-recursive.py"
script:
- python pylint-recursive.py
- python -m unittest discover -s pokemongo_bot -p "*_test.py"
2 changes: 1 addition & 1 deletion pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from pokemongo_bot.event_handlers import LoggingHandler, SocketIoHandler
from pokemongo_bot.socketio_server.runner import SocketIoRunner
from worker_result import WorkerResult

from tree_config_builder import ConfigException, TreeConfigBuilder

class PokemonGoBot(object):
@property
Expand Down
64 changes: 64 additions & 0 deletions pokemongo_bot/test/tree_config_builder_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import unittest
import json
from pokemongo_bot import PokemonGoBot, ConfigException, TreeConfigBuilder
from pokemongo_bot.cell_workers import SoftBanWorker, CatchLuredPokemonWorker

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_creating_worker(self):
obj = convert_from_json("""[{
"type": "SoftBanWorker"
}]""")

builder = TreeConfigBuilder(self.bot, obj)
tree = builder.build()

self.assertIsInstance(tree[0], SoftBanWorker)
self.assertIs(tree[0].bot, self.bot)

def test_creating_two_workers(self):
obj = convert_from_json("""[{
"type": "SoftBanWorker"
}, {
"type": "CatchLuredPokemonWorker"
}]""")

builder = TreeConfigBuilder(self.bot, obj)
tree = builder.build()

self.assertIsInstance(tree[0], SoftBanWorker)
self.assertIs(tree[0].bot, self.bot)
self.assertIsInstance(tree[1], CatchLuredPokemonWorker)
self.assertIs(tree[1].bot, self.bot)

if __name__ == '__main__':
unittest.main()
35 changes: 35 additions & 0 deletions pokemongo_bot/tree_config_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import cell_workers

class ConfigException(Exception):
pass

class TreeConfigBuilder(object):
def __init__(self, bot, tasks_raw):
self.bot = bot
self.tasks_raw = tasks_raw

def build(self):
print 'build'

def _get_worker_by_name(self, name):
try:
worker = getattr(cell_workers, name)
except AttributeError:
raise ConfigException('No worker named {} defined'.format(name))

return worker

def build(self):
workers = []

for task in self.tasks_raw:
task_type = task.get('type', None)
if task_type is None:
raise ConfigException('No type found for given task {}'.format(task))

worker = self._get_worker_by_name(task_type)
instance = worker(self.bot)
workers.append(instance)

return workers

2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ flask==0.11.1
socketIO_client==0.7.0
eventlet==0.19.0
universal-analytics-python==0.2.4
gpxpy==1.1.1
gpxpy==1.1.1