def test_ctx_extension(): mock = Mock() class ProxyExtension(CtxProxy): def __init__(self, *args): mock(*args) super().__init__(*args) def foo(self): return 'bar' score = init({ 'score.init': { 'modules': ['score.es7', 'score.ctx'] }, 'es7': { 'args.hosts': host, 'ctx.extensions': [ProxyExtension] }, }) with score.ctx.Context() as ctx: assert isinstance(ctx.es, CtxProxy) assert isinstance(ctx.es, ProxyExtension) assert ctx.es.foo() == 'bar' mock.assert_called_once() mock.assert_called_with(score.es7, ctx)
def test_start(): @asyncio.coroutine def handler(ctx): try: while True: message = yield from ctx.websocket.recv() yield from ctx.websocket.send(message) except websockets.exceptions.ConnectionClosed: pass app = init({ 'score.init': { 'modules': ['score.ctx', 'score.asyncio', 'score.websockets'], }, }) service = Service('test', Worker(app.websockets, app.asyncio, handler)) try: service.start() ws = app.asyncio. await (websockets.connect('ws://localhost:8081', loop=app.asyncio.loop)) app.asyncio. await (ws.send('test')) response = app.asyncio. await (ws.recv()) assert response == 'test' finally: service.stop()
def test_blank_start(): score = init({ 'score.init': { 'modules': ['score.pyfilesystem'], }, }) assert hasattr(score, 'pyfilesystem') assert isinstance(score.pyfilesystem, ConfiguredPyfilesystemModule)
def test_invalid_name_2(): init({ 'score.init': { 'modules': ['score.pyfilesystem'], }, 'pyfilesystem': { 'path.path': os.path.dirname(__file__), } }) # same init, but name starts with an underscore this time with pytest.raises(InitializationError): init({ 'score.init': { 'modules': ['score.pyfilesystem'], }, 'pyfilesystem': { 'path._path': os.path.dirname(__file__), } })
def test_destroy(): score = init({ 'score.init': { 'modules': 'score.es6' }, 'es6': { 'args.hosts': valid_host }, }) score.es6.destroy()
def test_create(): score = init({ 'score.init': { 'modules': 'score.es6' }, 'es6': { 'args.hosts': valid_host }, }) score.es6.create()
def test_defaults(): score = init({ 'score.init': { 'modules': 'score.es6' }, 'es6': { 'args.hosts': valid_host }, }) assert score.es6.client
def test_equality(): score = init({ 'score.init': { 'modules': ['score.pyfilesystem'], }, 'pyfilesystem': { 'path.foo': 'mem://', } }) assert score.pyfilesystem['foo'] is score.pyfilesystem.foo
def test_invalid_scope_1(): init({ 'score.init': { 'modules': ['score.pyfilesystem'], }, 'pyfilesystem': { 'path.path': 'mem://', } }) # same init, but scope is ctx this time with pytest.raises(InitializationError): init({ 'score.init': { 'modules': ['score.pyfilesystem'], }, 'pyfilesystem': { 'path.path': 'mem://?scope=ctx', } })
def test_modification_error(): score = init({ 'score.init': { 'modules': ['score.pyfilesystem'], }, 'pyfilesystem': { 'path.foo': 'mem://', } }) with pytest.raises(TypeError): del score.pyfilesystem['foo']
def test_simple_url(): score = init({ 'score.init': { 'modules': ['score.pyfilesystem'], }, 'pyfilesystem': { 'path.foo': 'mem://', } }) assert hasattr(score.pyfilesystem, 'foo') assert isinstance(score.pyfilesystem.foo, fs.memoryfs.MemoryFS)
def test_access(): score = init({ 'score.init': { 'modules': ['score.pyfilesystem'], }, 'pyfilesystem': { 'path.foo': 'mem://', } }) assert 'foo' in score.pyfilesystem assert isinstance(score.pyfilesystem['foo'], fs.memoryfs.MemoryFS)
def test_defaults(): score = init({ 'score.init': { 'modules': ['score.es7', 'score.ctx'] }, 'es7': { 'args.hosts': host }, }) with score.ctx.Context() as ctx: assert isinstance(ctx.es, CtxProxy)
def test_defaults(): score = init({ 'score.init': { 'modules': ['score.es6', 'score.ctx'] }, 'es6': { 'args.hosts': host }, }) with score.ctx.Context() as ctx: assert isinstance(ctx.es, DslExtension) assert isinstance(ctx.es.dsl.search(), Search)
def test_local_path(): score = init({ 'score.init': { 'modules': ['score.pyfilesystem'], }, 'pyfilesystem': { 'path.foo': os.path.dirname(__file__), } }) assert hasattr(score.pyfilesystem, 'foo') assert isinstance(score.pyfilesystem.foo, fs.osfs.OSFS) assert score.pyfilesystem.foo.isfile(os.path.basename(__file__))
def test_global_scope(): score = init({ 'score.init': { 'modules': ['score.pyfilesystem'], }, 'pyfilesystem': { 'path.mem': 'mem://', } }) with score.pyfilesystem.mem.open('foo.txt', 'w') as f: f.write('test') with score.pyfilesystem.mem.open('foo.txt') as f: assert f.read() == 'test'
def test_invalid_ctx_extension(): class ProxyExtension: # Error: does not inherit CtxProxy pass with pytest.raises(InitializationError): score = init({ 'score.init': { 'modules': ['score.es7', 'score.ctx'] }, 'es7': { 'args.hosts': host, 'ctx.extensions': [ProxyExtension] }, })
def test_scope_mismatch(): score = init({ 'score.init': { 'modules': [ 'score.ctx', 'score.pyfilesystem', ], }, 'pyfilesystem': { 'path.mem': 'mem://?scope=ctx', } }) with pytest.raises(AttributeError): score.pyfilesystem.mem
def test_scoped_ctx_members(): score = init({ 'score.init': { 'modules': [ 'score.ctx', 'score.pyfilesystem', ], }, 'pyfilesystem': { 'path.mem': 'mem://?scope=ctx', } }) with score.ctx.Context() as ctx: assert hasattr(ctx, 'fs') assert hasattr(ctx.fs, 'mem') assert isinstance(ctx.fs.mem, fs.memoryfs.MemoryFS)
def init_score(extra=None, *, finalize=True): conf = { 'score.init': { 'modules': [ 'score.tpl', 'score.jinja2', ], }, 'tpl': { 'rootdir': os.path.join(os.path.dirname(__file__), 'templates') } } if extra: for key in extra: conf[key] = extra[key] return init(conf, finalize=finalize)
def test_insert_and_retrieve(): score = init({ 'score.init': {'modules': 'score.es7'}, 'es7': {'args.hosts': valid_host}, }) client = score.es7.client client.indices.create( index=index, ignore=400) client.index( index=index, doc_type='text', id=1, body={ 'title': 'foo', 'body': 'bar', }) query = {"query": {"match_all": {}}} hits = client.search(index=index, body=query)['hits']['hits'] assert len(hits) == 1 assert hits[0]['_id'] == '1' assert hits[0]['_source']['title'] == 'foo' assert hits[0]['_source']['body'] == 'bar'
def test_ctx_scope_lifetime(): score = init({ 'score.init': { 'modules': [ 'score.ctx', 'score.pyfilesystem', ], }, 'pyfilesystem': { 'path.mem': 'mem://?scope=ctx', } }) with score.ctx.Context() as ctx: fs1 = ctx.fs.mem with fs1.open('foo.txt', 'w') as f: f.write('test') with score.ctx.Context() as ctx: fs2 = ctx.fs.mem assert fs1 is not fs2 with fs1.open('foo.txt') as f: assert f.read() == 'test' with pytest.raises(fs.errors.ResourceNotFound): fs2.open('foo.txt')