Skip to content

Commit

Permalink
Fix multiple registration of 'init' event
Browse files Browse the repository at this point in the history
Refs #211.
  • Loading branch information
cheungpat authored and Steven-Chan committed Jun 1, 2018
1 parent 41e1a97 commit 9d8a6a1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 38 deletions.
15 changes: 7 additions & 8 deletions skygear/transmitter/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def handle_exception(exc):
return exc


def handle_init_event(**data):
return get_registry().func_list()


def encode_base64_json(data):
"""
Encode dict-like data into a base64 encoded JSON string.
Expand All @@ -87,13 +91,6 @@ def dict_from_base64_environ(name):
class CommonTransport:
def __init__(self, registry=None):
self._registry = registry or get_registry()
self.register_init_event()

def init_event_handler(self, **data):
return self._registry.func_list()

def register_init_event(self):
self._registry.register_event('init', self.init_event_handler)

@_wrap_result
def call_func(self, ctx, kind, name, param):
Expand Down Expand Up @@ -204,4 +201,6 @@ def run(self):
raise NotImplemented()


get_registry().register_exception_handler(Exception, handle_exception)
_registry = get_registry()
_registry.register_exception_handler(Exception, handle_exception)
_registry.register_event('init', handle_init_event)
3 changes: 0 additions & 3 deletions skygear/transmitter/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ def __init__(self, args, stdin=sys.stdin, stdout=sys.stdout,
self.input = stdin
self.output = stdout

def init_event_handler(self, **data):
return self._registry.func_list()

def run(self):
self.args = self.args
target = self.args[0]
Expand Down
9 changes: 9 additions & 0 deletions skygear/transmitter/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,12 @@ def testHandleException(self):
def testHandleSkygearException(self):
exc = SkygearException('Error occurred', 1, {'data': 'hello'})
assert common.handle_exception(exc) is exc


class TestHandleInitEvent(unittest.TestCase):
@patch('skygear.registry.Registry.func_list')
def testHandleInitEvent(self, mocker):
mocker.return_value = {'data': 'hello'}
output = common.handle_init_event(hello='world')
mocker.assert_called_once_with()
assert output == mocker.return_value
7 changes: 0 additions & 7 deletions skygear/transmitter/tests/test_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@ def testEvent(self, mocker):
mocker.assert_called_once_with('okay', {'data': 'haha'})
assert output == mocker.return_value

@patch('skygear.transmitter.console.ConsoleTransport.init_event_handler')
def testInitEvent(self, mocker):
mocker.return_value = {'data': 'hello'}
output = self.exec(['event', 'init'], {'config': {'hello': 'world'}})
mocker.assert_called_once_with(config={'hello': 'world'})
assert output.get('result') == mocker.return_value

@patch('skygear.transmitter.console.ConsoleTransport.call_func')
def testHook(self, mocker):
mocker.return_value = {}
Expand Down
20 changes: 0 additions & 20 deletions skygear/transmitter/tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,6 @@ def testEvent(self, mocker):
assert resp.status_code == 200
mocker.assert_called_once_with('funny', ANY)

@patch('skygear.transmitter.http.HttpTransport.init_event_handler')
def testInitEvent(self, mocker):
mocker.return_value = {'data': 'hello'}
data = {
'kind': 'event',
'name': 'init',
'param': {
'config': {'hello': 'world'}
}
}
transport = HttpTransport('127.0.0.1:8888', Registry())
client = self.get_client(transport.dispatch)
resp = client.post('/', data=json.dumps(data))

assert resp.status_code == 200
mocker.assert_called_once_with(config={'hello': 'world'})

resp_data = json.loads(resp.get_data(as_text=True))
assert resp_data.get('result') == mocker.return_value

@patch('skygear.transmitter.http.HttpTransport.call_func')
def testHook(self, mocker):
mocker.return_value = {}
Expand Down

0 comments on commit 9d8a6a1

Please sign in to comment.