def test_call_returns_ribosome_return(): result = object() with patch.dict("brainslug.ribosome.RIBOSOMES"): remote = Remote(Symbol()) @define(Symbol()) def _(remote, *c_args, **c_kwargs): return result assert remote() is result
def test_special_ribosome_methods_are_reachable(special, convention, args): result = object() with patch.dict("brainslug.ribosome.RIBOSOMES"): remote = Remote(Symbol()) @define(Symbol((convention, ))) def _(remote, *c_args): assert c_args == args return result assert getattr(remote, special)(*args) is result
def test_call_pass_args_and_kwargs(): args = (object(), object()) kwargs = {'kw1': object(), 'kw2': object()} called = False with patch.dict("brainslug.ribosome.RIBOSOMES"): remote = Remote(Symbol()) @define(Symbol()) def _(remote, *c_args, **c_kwargs): nonlocal called called = True assert args == c_args assert kwargs == c_kwargs remote(*args, **kwargs) assert called
def test_symbol_is_aggregative(path): assume(not any(p[0] in digits or (p.startswith('__') and p.endswith('__')) for p in path)) result = Symbol() for part in path: result = getattr(result, part) assert tuple(result) == tuple(path)
def test_attributes_are_partial_if_ribosome_is_registered(path): assume(all(p.isidentifier() and not p.startswith('__') and not p.endswith('__') for p in path)) called = False with patch.dict("brainslug.ribosome.RIBOSOMES"): root = remote = Remote(Symbol((path[0], ))) @define(Symbol(path)) def _(remote): nonlocal called called = True assert remote is root for part in path[1:]: remote = getattr(remote, part) remote() assert called
def test_attributes_are_other_remotes_if_ribosome_is_not_registered(path): assume(all(p.isidentifier() and not p.startswith('__') and not p.endswith('__') for p in path)) with patch.dict("brainslug.ribosome.RIBOSOMES"): root = remote = Remote(Symbol()) for part in path: remote = getattr(remote, part) assert isinstance(remote, Remote) assert remote is not root
async def get_boot(request): ribosome = request.match_info['__ribosome__'] key = request.match_info.get('__key__', str(uuid4())) try: boot = RIBOSOMES[Symbol((ribosome, 'boot'))] except KeyError as exc: raise web.HTTPNotFound() from exc else: url = request.url url = request.url.with_path(f'/channel/{ribosome}/{key}') url = url.with_query(request.url.query) return web.Response( body=boot(remote=None, url=url, **request.rel_url.query))
async def get_launch(request): ribosome = request.match_info['__ribosome__'] try: launch = RIBOSOMES[Symbol((ribosome, 'launch'))] except KeyError as exc: raise web.HTTPNotFound() from exc else: url = request.url url = request.url.with_path(f'/boot/{ribosome}') url = url.with_query(request.url.query) return web.Response(body=launch(remote=None, url=url, **request.rel_url.query), content_type="text/html")
def test_symbol_invalid_attribute(attribute): assume(not attribute.isidentifier()) with pytest.raises(AttributeError): getattr(Symbol(), attribute)
def test_symbol_empty_attribute(): with pytest.raises(AttributeError): getattr(Symbol(), '')