-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48e0f5b
commit b0a7b7c
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |