def kickoff(host, port, dburi, handler, debug=False): engine = create_engine(dburi) app.register_blueprint(rest_blueprint(timeseries(dburi, handler=handler)), url_prefix='/api') app.register_blueprint(tsview(engine, has_permission=lambda perm: True)) historic(app, timeseries(dburi)) app.run(host=host, port=port, debug=debug, threaded=not debug)
def test_alternative_handler(pgapi): api = pgapi sapi = timeseries(api.uri, api.namespace, formula_class()) sapi.update( 'test-features', genserie(utcdt(2020, 1, 1), 'D', 3), 'Babar', ) sapi.tsh.register_formula(sapi.engine, 'test-formula', '(+ 1 (series "test-features"))') tsa = sapi.get('test-features') assert_df( """ 2020-01-01 00:00:00+00:00 0.0 2020-01-02 00:00:00+00:00 1.0 2020-01-03 00:00:00+00:00 2.0 """, tsa) tsb = sapi.get('test-formula') assert_df( """ 2020-01-01 00:00:00+00:00 1.0 2020-01-02 00:00:00+00:00 2.0 2020-01-03 00:00:00+00:00 3.0 """, tsb) class supervision_and_formula(supervision_class(), formula_class()): pass sapi = timeseries(api.uri, api.namespace, supervision_and_formula) tsa = sapi.get('test-features') assert_df( """ 2020-01-01 00:00:00+00:00 0.0 2020-01-02 00:00:00+00:00 1.0 2020-01-03 00:00:00+00:00 2.0 """, tsa) tsb = sapi.get('test-formula') assert_df( """ 2020-01-01 00:00:00+00:00 1.0 2020-01-02 00:00:00+00:00 2.0 2020-01-03 00:00:00+00:00 3.0 """, tsb) sapi.update('test-features', genserie(utcdt(2020, 1, 2), 'D', 3), 'Babar', manual=True) tsb = sapi.get('test-formula') assert_df( """ 2020-01-01 00:00:00+00:00 1.0 2020-01-02 00:00:00+00:00 1.0 2020-01-03 00:00:00+00:00 2.0 2020-01-04 00:00:00+00:00 3.0 """, tsb)
def httpapi(engine): from tshistory_rest import app wsgitester = WebTester( app.make_app( tsh_api.timeseries(str(engine.url), sources=[(DBURI, 'ns-test-mapi-2')]))) with responses.RequestsMock(assert_all_requests_are_fired=False) as resp: with_tester(URI, resp, wsgitester) # yields local-tsh + http ns-test-mapi-2 yield tsh_api.timeseries(URI, 'tsh')
def create(uri, ns, name): api = timeseries(uri, ns) series = pd.Series([1, 2, 3], index=pd.date_range(utcdt(2020, 1, 1), periods=3, freq='D')) api.update(name, series, 'Babar', insertion_date=utcdt(2019, 1, 1), metadata={'about': 'test'}) out = api.get(name) assert_df( """ 2020-01-01 00:00:00+00:00 1.0 2020-01-02 00:00:00+00:00 2.0 2020-01-03 00:00:00+00:00 3.0 """, out) series[utcdt(2020, 1, 4)] = 4 api.update(name, series, 'Babar', insertion_date=utcdt(2019, 1, 2)) out = api.get(name, from_value_date=utcdt(2020, 1, 2), to_value_date=utcdt(2020, 1, 3)) assert_df( """ 2020-01-02 00:00:00+00:00 2.0 2020-01-03 00:00:00+00:00 3.0 """, out)
def mapihttp(engine): from tshistory_rest import app from tshistory_formula import tsio fschema.formula_schema('ns-test-local').create(engine) fschema.formula_schema('ns-test-remote').create(engine) wsgitester = WebTester( app.make_app(tsh_api.timeseries(DBURI, namespace='ns-test-remote'))) with responses.RequestsMock(assert_all_requests_are_fired=False) as resp: with_tester(URI2, resp, wsgitester) yield tsh_api.timeseries(DBURI, namespace='ns-test-local', handler=tsio.timeseries, sources=[(URI2, 'ns-test-remote'), ('http://unavailable', 'ns-test-unavailable-remote')])
def mapi(engine): fschema.formula_schema('ns-test-mapi').create(engine) fschema.formula_schema('ns-test-mapi-2').create(engine) return tsh_api.timeseries(DBURI, namespace='ns-test-mapi', handler=tsio.timeseries, sources=[(DBURI, 'ns-test-mapi-2')])
def mapi(engine): formula_schema('test-mapi').create(engine) formula_schema('test-mapi-2').create(engine) return api.timeseries(str(engine.url), namespace='test-mapi', handler=timeseries, sources=[(str(engine.url), 'test-mapi-2')])
def client(engine): wsgi = app.make_app( api.timeseries( str(engine.url), handler=handler(), namespace='tsh', sources=[(DBURI, 'other')] ) ) yield WebTester(wsgi)
def client(engine): wsgitester = WebTester( app.make_app( tsapi.timeseries(str(engine.url), handler=handler(), sources=[(DBURI, 'other')]))) with responses.RequestsMock(assert_all_requests_are_fired=False) as resp: resp.add_callback(responses.GET, 'http://test-uri/series/state', callback=partial(read_request_bridge, wsgitester)) resp.add_callback(responses.PUT, 'http://test-uri/series/state', callback=write_request_bridge(wsgitester.put)) resp.add_callback(responses.DELETE, 'http://test-uri/series/state', callback=write_request_bridge(wsgitester.delete)) resp.add_callback(responses.GET, 'http://test-uri/series/staircase', callback=partial(read_request_bridge, wsgitester)) resp.add_callback(responses.GET, 'http://test-uri/series/history', callback=partial(read_request_bridge, wsgitester)) resp.add_callback(responses.GET, 'http://test-uri/series/catalog', callback=partial(read_request_bridge, wsgitester)) resp.add_callback(responses.PATCH, 'http://test-uri/series/state', callback=write_request_bridge(wsgitester.patch)) resp.add_callback(responses.GET, 'http://test-uri/series/metadata', callback=partial(read_request_bridge, wsgitester)) resp.add_callback(responses.PUT, 'http://test-uri/series/metadata', callback=write_request_bridge(wsgitester.put)) resp.add_callback(responses.GET, 'http://test-uri/series/formula', callback=partial(read_request_bridge, wsgitester)) resp.add_callback(responses.PATCH, 'http://test-uri/series/formula', callback=write_request_bridge(wsgitester.patch)) yield api.Client(URI)
def mapi(engine): tsschema('test-mapi').create(engine) tsschema('test-mapi-upstream').create(engine) tsschema('test-mapi-2').create(engine) tsschema('test-mapi-2-upstream').create(engine) return api.timeseries( str(engine.url), namespace='test-mapi', handler=timeseries, sources=[ (str(engine.url), 'test-mapi-2') ] )
def test_http_api(): tsh = timeseries('https://my.fancy.timeseries.store') for methname in ('get', 'update', 'replace', 'exists', 'type', 'history', 'staircase', 'catalog', 'interval', 'metadata', 'update_metadata', 'rename', 'delete'): assert getattr(tsh, methname, False), methname
def test_multisource(mapi): for methname in ('get', 'update', 'replace', 'exists', 'type', 'history', 'staircase', 'catalog', 'interval', 'metadata', 'update_metadata', 'rename', 'delete'): assert getattr(mapi, methname, False), methname def create(uri, ns, name): api = timeseries(uri, ns) series = pd.Series([1, 2, 3], index=pd.date_range(utcdt(2020, 1, 1), periods=3, freq='D')) api.update(name, series, 'Babar', insertion_date=utcdt(2019, 1, 1), metadata={'about': 'test'}) out = api.get(name) assert_df( """ 2020-01-01 00:00:00+00:00 1.0 2020-01-02 00:00:00+00:00 2.0 2020-01-03 00:00:00+00:00 3.0 """, out) series[utcdt(2020, 1, 4)] = 4 api.update(name, series, 'Babar', insertion_date=utcdt(2019, 1, 2)) out = api.get(name, from_value_date=utcdt(2020, 1, 2), to_value_date=utcdt(2020, 1, 3)) assert_df( """ 2020-01-02 00:00:00+00:00 2.0 2020-01-03 00:00:00+00:00 3.0 """, out) create(mapi.uri, mapi.namespace, 'api-1') create(mapi.uri, 'ns-test-mapi-2', 'api-2') assert not mapi.exists('i-dont-exist') assert mapi.exists('api-1') assert mapi.exists('api-2') series = pd.Series([10, 20, 30], index=pd.date_range(utcdt(2020, 1, 1), periods=3, freq='D')) mapi.update('api-1', series, 'auc') with pytest.raises(ValueError) as err: mapi.update('api-2', series, 'auc') assert err.value.args[0].startswith('not allowed to update') mapi.replace('api-1', series, 'auc') with pytest.raises(ValueError) as err: mapi.replace('api-2', series, 'auc') assert err.value.args[0].startswith('not allowed to replace') api = timeseries(mapi.uri, mapi.namespace) catalog = api.catalog() catalog2 = mapi.catalog() assert catalog == { ('db://localhost:5433/postgres', 'ns-test-mapi'): [('api-1', 'primary')] } assert catalog2 == { ('db://localhost:5433/postgres', 'ns-test-mapi'): [('api-1', 'primary')], ('db://localhost:5433/postgres', 'ns-test-mapi-2'): [('api-2', 'primary')] } catalog3 = mapi.catalog(allsources=False) assert catalog3 == { ('db://localhost:5433/postgres', 'ns-test-mapi'): [('api-1', 'primary')] } mapi.update_metadata('api-1', {'descr': 'for the mapi test'}) with pytest.raises(ValueError) as err: mapi.update_metadata('api-2', {'descr': 'for the mapi test'}) assert err.value.args[0].startswith('not allowed to update metadata') assert mapi.metadata('api-2', all=True) == { 'index_dtype': '|M8[ns]', 'index_type': 'datetime64[ns, UTC]', 'tzaware': True, 'value_dtype': '<f8', 'value_type': 'float64' } mapi.rename('api-1', 'renamed-api-1') assert not mapi.exists('api-1') with pytest.raises(ValueError) as err: mapi.rename('api-2', 'renamed-api-2') assert err.value.args[0].startswith('not allowed to rename') mapi.delete('renamed-api-1') with pytest.raises(ValueError) as err: mapi.delete('api-2') assert err.value.args[0].startswith('not allowed to delete') assert not mapi.exists('renamed-api-1')
def pgapi(engine): fschema.formula_schema('tsh').create(engine) schema.tsschema('tsh-upstream').create(engine) return tsh_api.timeseries(str(engine.url), 'tsh')