Skip to content

Commit f6037c7

Browse files
authored
Merge pull request #7 from fbouliane/allow-calls-to-ubersmith-core
Refactor UbersmithCore.xx to remote.ubersmith.xx
2 parents f57bae0 + 17a6446 commit f6037c7

File tree

6 files changed

+79
-117
lines changed

6 files changed

+79
-117
lines changed

tests/Integration/test_router_ubersmith_core.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@
1717
from flexmock import flexmock, flexmock_teardown
1818

1919
from . import ContextMatcher
20-
from ubersmith_remote_module_server import ubersmith_core
20+
from ubersmith_remote_module_server import remote
2121
from ubersmith_remote_module_server.objects import RequestContext
2222
from ubersmith_remote_module_server.router import Router
23-
from ubersmith_remote_module_server.ubersmith_core import UbersmithCore
23+
from ubersmith_remote_module_server.remote import ubersmith
2424

2525

2626
class FakeModule(object):
2727
def call_uber_core(self):
28-
UbersmithCore.test()
28+
ubersmith.test()
2929

3030

3131
class RouterUbersmithCoreTest(unittest.TestCase):
3232
def test_a_module_can_call_ubersmith_core_without_context(self):
3333
fake_module = FakeModule()
3434
remote_executor_mock = flexmock()
3535

36-
flexmock(ubersmith_core).should_receive('RemoteExecutor').\
36+
flexmock(remote).should_receive('RemoteExecutor').\
3737
with_args(context=RequestContext).and_return(remote_executor_mock)
3838

3939
remote_executor_mock.should_receive('invoke_global').with_args('test', args={}).once()
@@ -49,7 +49,7 @@ def test_module_context_is_injected_when_service_module(self):
4949
'url': 'http://user:[email protected]/'
5050
'api/2.0/?method=service.module_call'}
5151

52-
flexmock(ubersmith_core).should_receive('RemoteExecutor'). \
52+
flexmock(remote).should_receive('RemoteExecutor'). \
5353
with_args(context=ContextMatcher(callback_url=example_callback['url'],
5454
module_id='44',
5555
service_id='1260'))\
@@ -70,7 +70,7 @@ def test_module_context_is_injected_when_device_module(self):
7070
'api/2.0/?method=device.module_call'}
7171

7272
remote_executor_mock = flexmock()
73-
flexmock(ubersmith_core).should_receive('RemoteExecutor'). \
73+
flexmock(remote).should_receive('RemoteExecutor'). \
7474
with_args(context=ContextMatcher(callback_url=example_callback['url'],
7575
module_id='173',
7676
device_id='200025'))\
Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2016 Internap.
1+
# Copyright 2016 Internap
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -16,18 +16,47 @@
1616

1717
from flexmock import flexmock, flexmock_teardown
1818

19-
from ubersmith_remote_module_server import _remote_executor
20-
from ubersmith_remote_module_server._remote_executor import RemoteExecutor
19+
from tests import mock_ubersmith_client, mock_ubersmith_api
20+
from ubersmith_remote_module_server import remote
21+
from ubersmith_remote_module_server.exceptions import NoRequestContext, NamedArgumentsOnly
2122
from ubersmith_remote_module_server.objects import RequestContext
22-
from . import mock_ubersmith_client, mock_ubersmith_api
23+
from ubersmith_remote_module_server.remote import RemoteExecutor, ubersmith, \
24+
ConfiguredRequestContext
25+
26+
27+
class UbersmithCoreTest(unittest.TestCase):
28+
def test_raises_when_out_of_context(self):
29+
remote_executor_mock = flexmock()
30+
flexmock(remote).should_receive('RemoteExecutor').and_return(remote_executor_mock)
31+
with self.assertRaises(NoRequestContext):
32+
ubersmith.test()
33+
34+
def test_raises_when_args(self):
35+
remote_executor_mock = flexmock()
36+
flexmock(remote).should_receive('RemoteExecutor').and_return(remote_executor_mock)
37+
with self.assertRaises(NamedArgumentsOnly):
38+
ubersmith.test('hello')
39+
40+
def test_calls_executor_with_context_when_called(self):
41+
remote_executor_mock = flexmock()
42+
flexmock(remote).should_receive('RemoteExecutor'). \
43+
with_args(context='context').and_return(remote_executor_mock).once()
44+
remote_executor_mock.should_receive('invoke_global').with_args('test', args={}).once()
45+
46+
with ConfiguredRequestContext(context='context'):
47+
ubersmith.test()
48+
49+
def tearDown(self):
50+
flexmock_teardown()
51+
super(UbersmithCoreTest, self).tearDown()
2352

2453

2554
class RemoteExecutorTest(unittest.TestCase):
2655
def test_invoke_global_from_device_module(self):
2756
ubersmith_client = mock_ubersmith_client()
2857
ubersmith_api = mock_ubersmith_api()
2958

30-
flexmock(_remote_executor).should_receive('ubersmith_client').and_return(ubersmith_client)
59+
flexmock(remote).should_receive('ubersmith_client').and_return(ubersmith_client)
3160

3261
ubersmith_client.api.should_receive('init')\
3362
.with_args(url='http://ubersmith.example/url',
@@ -50,7 +79,7 @@ def test_invoke_global_from_service_module(self):
5079
ubersmith_client = mock_ubersmith_client()
5180
ubersmith_api = mock_ubersmith_api()
5281

53-
flexmock(_remote_executor).should_receive('ubersmith_client').and_return(ubersmith_client)
82+
flexmock(remote).should_receive('ubersmith_client').and_return(ubersmith_client)
5483

5584
ubersmith_client.api.should_receive('init')\
5685
.with_args(url='http://ubersmith.example/url',
@@ -74,5 +103,3 @@ def test_invoke_global_from_service_module(self):
74103
def tearDown(self):
75104
flexmock_teardown()
76105
super(RemoteExecutorTest, self).tearDown()
77-
78-

tests/test_ubersmith_core.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

ubersmith_remote_module_server/_remote_executor.py renamed to ubersmith_remote_module_server/remote.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,41 @@
1515
import ubersmith_client
1616
from six.moves.urllib.parse import urlparse
1717

18+
from ubersmith_remote_module_server.exceptions import NoRequestContext, NamedArgumentsOnly
19+
20+
_configuration = None
21+
22+
__all__ = ['ubersmith']
23+
24+
25+
class ConfiguredRequestContext(object):
26+
def __init__(self, **kwargs):
27+
self.kwargs = kwargs or {}
28+
29+
def __enter__(self):
30+
global _configuration
31+
_configuration = self.kwargs
32+
33+
def __exit__(self, exc_type, exc_val, exc_tb):
34+
global _configuration
35+
_configuration = None
36+
37+
38+
class UbersmithRemoteProxy(object):
39+
def _invoke_method(self, name, *args, **kwargs):
40+
global _configuration
41+
if args:
42+
raise NamedArgumentsOnly()
43+
if _configuration is None:
44+
raise NoRequestContext()
45+
executor = RemoteExecutor(**_configuration)
46+
executor.invoke_global(name, args=kwargs)
47+
48+
def __getattr__(self, func_name):
49+
def invoke_without_name(*args, **kwargs):
50+
return self._invoke_method(func_name, *args, **kwargs)
51+
return invoke_without_name
52+
1853

1954
class RemoteExecutor(object):
2055
def __init__(self, context):
@@ -36,3 +71,5 @@ def invoke_global(self, func_name, args):
3671
module_id=self.context.module_id,
3772
function='_web_hook_invoke_global',
3873
module_params=[func_name, [args]])
74+
75+
ubersmith = UbersmithRemoteProxy()

ubersmith_remote_module_server/router.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
from ubersmith_remote_module_server.objects import RequestContext
16-
from ubersmith_remote_module_server.ubersmith_core import ConfiguredRequestContext
16+
from ubersmith_remote_module_server.remote import ConfiguredRequestContext
1717

1818

1919
class Router(object):

ubersmith_remote_module_server/ubersmith_core.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)