def airtemp_rest(airtemp_ds): app_kws = dict( title='My Dataset', description='Dataset Description', version='1.0.0', openapi_url='/dataset.json', docs_url='/data-docs', ) return Rest(airtemp_ds, app_kws=app_kws)
def test_ds_dict_cache(ds_dict): rest = Rest(ds_dict, cache_kws={'available_bytes': 1e9}) client = TestClient(rest.app) response1 = client.get('/datasets/ds1/var/0') assert response1.status_code == 200 assert 'ds1/var/0' in rest.cache response2 = client.get('/datasets/ds2/info') assert response2.status_code == 200 assert 'ds2/zvariables' in rest.cache assert 'ds2/.zmetadata' in rest.cache
def test_cache(airtemp_ds): rest = Rest(airtemp_ds, cache_kws={'available_bytes': 1e9}) assert rest.cache.available_bytes == 1e9 client = TestClient(rest.app) response1 = client.get('/air/0.0.0') assert response1.status_code == 200 assert '/air/0.0.0' in rest.cache # test that we can retrieve response2 = client.get('/air/0.0.0') assert response2.status_code == 200 assert response1.content == response2.content
def test_custom_app_routers_conflict(airtemp_ds): router1 = APIRouter() @router1.get('/path') def func1(): pass router2 = APIRouter() @router2.get('/same/path') def func2(): pass with pytest.raises(ValueError, match='Found multiple routes.*'): Rest(airtemp_ds, routers=[(router1, {'prefix': '/same'}), router2])
def test_custom_app_routers(airtemp_ds, dims_router, router_kws, path): if router_kws is None: routers = [dims_router] else: routers = [(dims_router, router_kws)] rest = Rest(airtemp_ds, routers=routers) client = TestClient(rest.app) response = client.get(path) assert response.status_code == 200 assert response.json() == airtemp_ds.dims # test default routers not present response = client.get('/') assert response.status_code == 404
def test_roundtrip(start, end, freq, nlats, nlons, var_const, calendar, use_cftime): ds = create_dataset( start=start, end=end, nlats=nlats, nlons=nlons, var_const=var_const, use_cftime=use_cftime, calendar=calendar, ) ds = ds.chunk(ds.dims) mapper = TestMapper(Rest(ds).app) actual = xr.open_zarr(mapper, consolidated=True) xr.testing.assert_identical(actual, ds)
def test_roundtrip_custom_chunks(start, end, freq, nlats, nlons, var_const, calendar, use_cftime, chunks, decode_times): ds = create_dataset( start=start, end=end, freq=freq, nlats=nlats, nlons=nlons, var_const=var_const, use_cftime=use_cftime, calendar=calendar, decode_times=decode_times, ) ds = ds.chunk(chunks) mapper = TestMapper(Rest(ds).app) actual = xr.open_zarr(mapper, consolidated=True, decode_times=decode_times) xr.testing.assert_identical(actual, ds)
def test_zmetadata_identical(start, end, freq, nlats, nlons, var_const, calendar, use_cftime): ds = create_dataset( start=start, end=end, nlats=nlats, nlons=nlons, var_const=var_const, use_cftime=use_cftime, calendar=calendar, ) ds = ds.chunk(ds.dims) zarr_dict = {} ds.to_zarr(zarr_dict, consolidated=True) mapper = TestMapper(Rest(ds).app) actual = json.loads(mapper['.zmetadata'].decode()) expected = json.loads(zarr_dict['.zmetadata'].decode()) assert actual == expected
def test_init_app_kws(airtemp_ds): rest = Rest( airtemp_ds, app_kws=dict( title='My Dataset', description='Dataset Description', version='1.0.0', openapi_url='/dataset.json', docs_url='/data-docs', ), ) assert rest.app.title == 'My Dataset' assert rest.app.description == 'Dataset Description' assert rest.app.version == '1.0.0' client = TestClient(rest.app) response = client.get('/dataset.json') assert response.status_code == 200 response = client.get('/data-docs') assert response.status_code == 200
def test_init_cache_kws(airtemp_ds): rest = Rest(airtemp_ds, cache_kws={'available_bytes': 999}) assert rest.cache.available_bytes == 999
def ds_dict_rest(ds_dict): return Rest(ds_dict)
def test_custom_app_routers_error(airtemp_ds): with pytest.raises(TypeError, match='Invalid type/format.*'): Rest(airtemp_ds, routers=['not_a_router'])
def test_init_dataset_type_error(datasets): with pytest.raises(TypeError, match='Can only publish.*Dataset objects'): Rest(datasets)
def test_missing_key_raises_keyerror(airtemp_ds): mapper = TestMapper(Rest(airtemp_ds).app) with pytest.raises(KeyError): _ = mapper['notakey']
def test_get_zmetadata_key(airtemp_ds): mapper = TestMapper(Rest(airtemp_ds).app) actual = json.loads(mapper['.zmetadata'].decode()) expected = jsonify_zmetadata(airtemp_ds, create_zmetadata(airtemp_ds)) assert actual == expected