Skip to content

Commit

Permalink
Adding a task base class (#1983)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSavior authored and douglascamata committed Jul 31, 2016
1 parent 48e0f5b commit b0a7b7c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
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()

0 comments on commit b0a7b7c

Please sign in to comment.