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 task base class #1983

Merged
merged 1 commit into from
Jul 31, 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
1 change: 1 addition & 0 deletions pokemongo_bot/cell_workers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
from handle_soft_ban import HandleSoftBan
from follow_path import FollowPath
from follow_spiral import FollowSpiral
from base_task import BaseTask
14 changes: 14 additions & 0 deletions pokemongo_bot/cell_workers/base_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class BaseTask(object):
def __init__(self, bot, config):
self.bot = bot
self.config = config
self._validate_work_exists()
self.initialize()

def _validate_work_exists(self):
method = getattr(self, 'work', None)
if not method or not callable(method):
raise NotImplementedError('Missing "work" method')

def initialize(self):
pass
43 changes: 43 additions & 0 deletions pokemongo_bot/test/base_task_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import unittest
import json
from pokemongo_bot.cell_workers import BaseTask

class FakeTask(BaseTask):
def initialize(self):
self.foo = 'foo'

def work(self):
pass

class FakeTaskWithoutInitialize(BaseTask):
def work(self):
pass

class FakeTaskWithoutWork(BaseTask):
pass

class BaseTaskTest(unittest.TestCase):
def setUp(self):
self.bot = {}
self.config = {}

def test_initialize_called(self):
task = FakeTask(self.bot, self.config)
self.assertIs(task.bot, self.bot)
self.assertIs(task.config, self.config)
self.assertEquals(task.foo, 'foo')

def test_does_not_throw_without_initialize(self):
FakeTaskWithoutInitialize(self.bot, self.config)

def test_throws_without_work(self):
self.assertRaisesRegexp(
NotImplementedError,
'Missing "work" method',
FakeTaskWithoutWork,
self.bot,
self.config
)

if __name__ == '__main__':
unittest.main()