示例#1
0
    def test_config_and_dataset_cache(self):
        ctx = new_test_service_context()
        self.assertNotIn('demo', ctx.dataset_cache)

        ctx.get_dataset('demo')
        self.assertIn('demo', ctx.dataset_cache)

        ctx.config = dict(Datasets=[
            dict(Identifier='demo',
                 Path="../../../xcube_server/res/demo/cube.nc"),
            dict(Identifier='demo2',
                 Path="../../../xcube_server/res/demo/cube.nc"),
        ])
        self.assertIn('demo', ctx.dataset_cache)
        self.assertNotIn('demo2', ctx.dataset_cache)

        ctx.get_dataset('demo2')
        self.assertIn('demo', ctx.dataset_cache)
        self.assertIn('demo2', ctx.dataset_cache)

        ctx.config = dict(Datasets=[
            dict(Identifier='demo2',
                 Path="../../../xcube_server/res/demo/cube.nc"),
        ])
        self.assertNotIn('demo', ctx.dataset_cache)
        self.assertIn('demo2', ctx.dataset_cache)

        ctx.config = dict()
        self.assertNotIn('demo', ctx.dataset_cache)
        self.assertNotIn('demo2', ctx.dataset_cache)
示例#2
0
    def test_find_features_by_geojson(self):
        ctx = new_test_service_context()

        geojson_obj = {
            'type':
            'Polygon',
            'coordinates': (((2.0, 49.0), (2.0, 55.0), (-1.0, 55.0),
                             (-1.0, 49.0), (2.0, 49.0)), )
        }
        feature_collection = find_features(ctx, "all", geojson_obj=geojson_obj)
        self._assertFeatureCollection(feature_collection, 2, {'0', '3'})

        geojson_obj = {
            'type': 'Feature',
            'properties': {},
            'geometry': geojson_obj
        }
        feature_collection = find_features(ctx, "all", geojson_obj=geojson_obj)
        self._assertFeatureCollection(feature_collection, 2, {'0', '3'})

        geojson_obj = {'type': 'FeatureCollection', 'features': [geojson_obj]}
        feature_collection = find_features(ctx, "all", geojson_obj=geojson_obj)
        self._assertFeatureCollection(feature_collection, 2, {'0', '3'})

        with self.assertRaises(ServiceBadRequestError) as cm:
            geojson_obj = {'type': 'FeatureCollection', 'features': []}
            find_features(ctx, "all", geojson_obj=geojson_obj)
        self.assertEqual("HTTP 400: Received invalid GeoJSON object",
                         f"{cm.exception}")
示例#3
0
    def test_get_dataset_tile_grid(self):
        self.maxDiff = None

        ctx = new_test_service_context()
        tile_grid = get_dataset_tile_grid(ctx, 'demo', 'conc_chl', 'ol4',
                                          'http://bibo')
        self.assertEqual(
            {
                'url': self.base_url + '/tile/demo/conc_chl/{z}/{x}/{y}.png',
                'projection': 'EPSG:4326',
                'minZoom': 0,
                'maxZoom': 2,
                'tileGrid': {
                    'extent': [2.168404344971009e-19, 50.0, 5.0, 52.5],
                    'origin': [2.168404344971009e-19, 52.5],
                    'resolutions': [0.01, 0.005, 0.0025],
                    'tileSize': [250, 250]
                },
            }, tile_grid)

        tile_grid = get_dataset_tile_grid(ctx, 'demo', 'conc_chl', 'cesium',
                                          'http://bibo')
        self.assertEqual(
            {
                'url':
                self.base_url + '/tile/demo/conc_chl/{z}/{x}/{y}.png',
                'rectangle':
                dict(west=2.168404344971009e-19,
                     south=50.0,
                     east=5.0,
                     north=52.5),
                'minimumLevel':
                0,
                'maximumLevel':
                2,
                'tileWidth':
                250,
                'tileHeight':
                250,
                'tilingScheme': {
                    'rectangle':
                    dict(west=2.168404344971009e-19,
                         south=50.0,
                         east=5.0,
                         north=52.5),
                    'numberOfLevelZeroTilesX':
                    2,
                    'numberOfLevelZeroTilesY':
                    1
                },
            }, tile_grid)

        with self.assertRaises(ServiceBadRequestError) as cm:
            get_dataset_tile_grid(ctx, 'demo', 'conc_chl', 'ol2.json',
                                  'http://bibo')
        self.assertEqual(400, cm.exception.status_code)
        self.assertEqual('Unknown tile schema format "ol2.json"',
                         cm.exception.reason)
示例#4
0
    def test_get_dataset_tile(self):
        ctx = new_test_service_context()
        tile = get_dataset_tile(ctx, 'demo', 'conc_tsm', '0', '0', '0',
                                RequestParamsMock())
        self.assertIsInstance(tile, bytes)

        tile = get_dataset_tile(ctx, 'demo', 'conc_tsm', '-20', '0', '0',
                                RequestParamsMock())
        self.assertIsInstance(tile, bytes)
示例#5
0
 def test_get_dataset_tile_with_all_params(self):
     ctx = new_test_service_context()
     tile = get_dataset_tile(
         ctx, 'demo', 'conc_tsm', '0', '0', '0',
         RequestParamsMock(time='current',
                           cbar='plasma',
                           vmin='0.1',
                           vmax='0.3'))
     self.assertIsInstance(tile, bytes)
示例#6
0
 def test_get_color_mapping(self):
     ctx = new_test_service_context()
     cm = ctx.get_color_mapping('demo', 'conc_chl')
     self.assertEqual(('plasma', 0., 24.), cm)
     cm = ctx.get_color_mapping('demo', 'conc_tsm')
     self.assertEqual(('PuBuGn', 0., 100.), cm)
     cm = ctx.get_color_mapping('demo', 'kd489')
     self.assertEqual(('jet', 0., 6.), cm)
     cm = ctx.get_color_mapping('demo', '_')
     self.assertEqual(('jet', 0., 1.), cm)
示例#7
0
    def test_get_dataset_tile_with_time_dim(self):
        ctx = new_test_service_context()
        tile = get_dataset_tile(ctx, 'demo', 'conc_tsm', '0', '0', '0',
                                RequestParamsMock(time='2017-01-26'))
        self.assertIsInstance(tile, bytes)

        ctx = new_test_service_context()
        tile = get_dataset_tile(ctx, 'demo', 'conc_tsm', '0', '0', '0',
                                RequestParamsMock(time='current'))
        self.assertIsInstance(tile, bytes)

        with self.assertRaises(ServiceBadRequestError) as cm:
            get_dataset_tile(ctx, 'demo', 'conc_tsm', '0', '0', '0',
                             RequestParamsMock(time='Gnaaark!'))
        self.assertEqual(400, cm.exception.status_code)
        self.assertEqual(
            "'Gnaaark!' is not a valid value for "
            "dimension 'time' of variable 'conc_tsm' of dataset 'demo'",
            cm.exception.reason)
示例#8
0
 def test_get_feature_collections(self):
     ctx = new_test_service_context()
     feature_collections = ctx.get_feature_collections()
     self.assertIsInstance(feature_collections, list)
     self.assertEqual([{
         'id': 'inside-cube',
         'title': 'Points inside the cube'
     }, {
         'id': 'outside-cube',
         'title': 'Points outside the cube'
     }], feature_collections)
示例#9
0
 def test_get_wmts_capabilities_xml(self):
     self.maxDiff = None
     with open(os.path.join(get_res_test_dir(),
                            'WMTSCapabilities.xml')) as fp:
         expected_capabilities = fp.read()
     ctx = new_test_service_context()
     capabilities = get_wmts_capabilities_xml(ctx, 'http://bibo')
     # print(80 * '=')
     # print(capabilities)
     # print(80 * '=')
     self.assertEqual(expected_capabilities.replace(' ', ''),
                      capabilities.replace(' ', ''))
示例#10
0
    def test_find_places_by_wkt(self):
        ctx = new_test_service_context()
        places = find_places(
            ctx, "all", geom_wkt="POLYGON ((2 49, 2 55, -1 55, -1 49, 2 49))")
        self._assertPlaceGroup(places, 2, {'0', '3'})

        with self.assertRaises(ServiceBadRequestError) as cm:
            find_places(ctx,
                        "all",
                        geom_wkt="POLYGLON ((2 49, 2 55, -1 55, -1 49, 2 49))")
        self.assertEqual("HTTP 400: Received invalid geometry WKT",
                         f"{cm.exception}")
示例#11
0
 def test_get_feature_collection(self):
     ctx = new_test_service_context()
     feature_collection = ctx.get_feature_collection()
     self.assertIsInstance(feature_collection, dict)
     self.assertIn("type", feature_collection)
     self.assertEqual("FeatureCollection", feature_collection["type"])
     self.assertIn("features", feature_collection)
     self.assertIsInstance(feature_collection["features"], list)
     self.assertEqual(6, len(feature_collection["features"]))
     self.assertIs(feature_collection, ctx.get_feature_collection())
     self.assertEqual(
         [str(i) for i in range(6)],
         [f["id"] for f in feature_collection["features"] if "id" in f])
示例#12
0
 def test_get_place_group(self):
     ctx = new_test_service_context()
     place_group = ctx.get_place_group()
     self.assertIsInstance(place_group, dict)
     self.assertIn("type", place_group)
     self.assertEqual("FeatureCollection", place_group["type"])
     self.assertIn("features", place_group)
     self.assertIsInstance(place_group["features"], list)
     self.assertEqual(6, len(place_group["features"]))
     self.assertIs(place_group, ctx.get_place_group())
     self.assertEqual(
         [str(i) for i in range(6)],
         [f["id"] for f in place_group["features"] if "id" in f])
示例#13
0
    def test_dataset_with_details(self):
        ctx = new_test_service_context()

        response = get_datasets(ctx, details=True, base_url="http://test")
        self.assertIsInstance(response, dict)
        self.assertIn("datasets", response)
        self.assertIsInstance(response["datasets"], list)
        self.assertEqual(2, len(response["datasets"]))
        dataset = response["datasets"][0]
        self.assertIsInstance(dataset, dict)
        self.assertIn("id", dataset)
        self.assertIn("title", dataset)
        self.assertIn("variables", dataset)
        self.assertIn("dimensions", dataset)
示例#14
0
    def test_datasets(self):
        ctx = new_test_service_context()

        response = get_datasets(ctx)
        self.assertIsInstance(response, dict)
        self.assertIn("datasets", response)
        self.assertIsInstance(response["datasets"], list)
        self.assertEqual(2, len(response["datasets"]))
        dataset = response["datasets"][0]
        self.assertIsInstance(dataset, dict)
        self.assertIn("id", dataset)
        self.assertIn("title", dataset)
        self.assertNotIn("variables", dataset)
        self.assertNotIn("dimensions", dataset)
示例#15
0
    def test_find_features_by_box(self):
        ctx = new_test_service_context()
        feature_collection = find_features(ctx, "all", box_coords="-1,49,2,55")
        self._assertFeatureCollection(feature_collection, 2, {'0', '3'})

        with self.assertRaises(ServiceBadRequestError) as cm:
            find_features(ctx, "all", box_coords="-1,49,55")
        self.assertEqual("HTTP 400: Received invalid bounding box geometry",
                         f"{cm.exception}")

        with self.assertRaises(ServiceBadRequestError) as cm:
            find_features(ctx, "all", box_coords="-1,49,x,55")
        self.assertEqual("HTTP 400: Received invalid bounding box geometry",
                         f"{cm.exception}")
示例#16
0
    def test_get_dataset_and_variable(self):
        ctx = new_test_service_context()
        ds, var = ctx.get_dataset_and_variable('demo', 'conc_tsm')
        self.assertIsInstance(ds, xr.Dataset)
        self.assertIsInstance(var, xr.DataArray)

        with self.assertRaises(ServiceResourceNotFoundError) as cm:
            ctx.get_dataset_and_variable('demox', 'conc_ys')
        self.assertEqual(404, cm.exception.status_code)
        self.assertEqual('Dataset "demox" not found', cm.exception.reason)

        with self.assertRaises(ServiceResourceNotFoundError) as cm:
            ctx.get_dataset_and_variable('demo', 'conc_ys')
        self.assertEqual(404, cm.exception.status_code)
        self.assertEqual('Variable "conc_ys" not found in dataset "demo"',
                         cm.exception.reason)
示例#17
0
    def test_get_place_group_by_name(self):
        ctx = new_test_service_context()
        place_group = ctx.get_place_group(place_group_id="inside-cube")
        self.assertIsInstance(place_group, dict)
        self.assertIn("type", place_group)
        self.assertEqual("FeatureCollection", place_group["type"])
        self.assertIn("features", place_group)
        self.assertIsInstance(place_group["features"], list)
        self.assertEqual(3, len(place_group["features"]))
        self.assertIs(place_group,
                      ctx.get_place_group(place_group_id="inside-cube"))
        self.assertIsNot(place_group,
                         ctx.get_place_group(place_group_id="outside-cube"))

        with self.assertRaises(ServiceResourceNotFoundError) as cm:
            ctx.get_place_group(place_group_id="bibo")
        self.assertEqual('HTTP 404: Place group "bibo" not found',
                         f"{cm.exception}")
示例#18
0
    def test_get_time_series_for_geometries(self):
        ctx = new_test_service_context()
        time_series = get_time_series_for_geometry_collection(ctx,
                                                              'demo', 'conc_tsm',
                                                              dict(type="GeometryCollection",
                                                                   geometries=[
                                                                       dict(type="Point", coordinates=[2.1, 51.4])]),
                                                              start_date=np.datetime64('2017-01-15'),
                                                              end_date=np.datetime64('2017-01-29'))
        expected_dict = {'results': [[{'date': '2017-01-16T10:09:22Z',
                                       'result': {'average': 3.534773588180542,
                                                  'totalCount': 1,
                                                  'validCount': 1}},
                                      {'date': '2017-01-25T09:35:51Z',
                                       'result': {'average': None, 'totalCount': 1, 'validCount': 0}},
                                      {'date': '2017-01-26T10:50:17Z',
                                       'result': {'average': None, 'totalCount': 1, 'validCount': 0}},
                                      {'date': '2017-01-28T09:58:11Z',
                                       'result': {'average': 20.12085723876953,
                                                  'totalCount': 1,
                                                  'validCount': 1}}]]}
        self.assertEqual(expected_dict, time_series)

        time_series = get_time_series_for_geometry_collection(ctx,
                                                              'demo', 'conc_tsm',
                                                              dict(type="GeometryCollection",
                                                                   geometries=[dict(type="Polygon", coordinates=[[
                                                                       [1., 51.], [2., 51.], [2., 52.], [1., 52.],
                                                                       [1., 51.]
                                                                   ]])]))
        expected_dict = {'results': [[
            {'result': {'totalCount': 160801, 'validCount': 123540, 'average': 56.04547741839104},
             'date': '2017-01-16T10:09:22Z'},
            {'result': {'totalCount': 160801, 'validCount': 0, 'average': None},
             'date': '2017-01-25T09:35:51Z'},
            {'result': {'totalCount': 160801, 'validCount': 0, 'average': None},
             'date': '2017-01-26T10:50:17Z'},
            {'result': {'totalCount': 160801, 'validCount': 133267, 'average': 49.59349042604672},
             'date': '2017-01-28T09:58:11Z'},
            {'result': {'totalCount': 160801, 'validCount': 0, 'average': None},
             'date': '2017-01-30T10:46:34Z'}]]}

        self.assertEqual(expected_dict, time_series)
示例#19
0
 def test_get_time_series_for_point(self):
     ctx = new_test_service_context()
     time_series = get_time_series_for_point(ctx, 'demo', 'conc_tsm',
                                             lon=2.1, lat=51.4,
                                             start_date=np.datetime64('2017-01-15'),
                                             end_date=np.datetime64('2017-01-29'))
     expected_dict = {'results': [{'date': '2017-01-16T10:09:22Z',
                                   'result': {'average': 3.534773588180542,
                                              'totalCount': 1,
                                              'validCount': 1}},
                                  {'date': '2017-01-25T09:35:51Z',
                                   'result': {'average': None, 'totalCount': 1, 'validCount': 0}},
                                  {'date': '2017-01-26T10:50:17Z',
                                   'result': {'average': None, 'totalCount': 1, 'validCount': 0}},
                                  {'date': '2017-01-28T09:58:11Z',
                                   'result': {'average': 20.12085723876953,
                                              'totalCount': 1,
                                              'validCount': 1}}]}
     self.assertEqual(expected_dict, time_series)
示例#20
0
    def test_get_feature_collection_by_name(self):
        ctx = new_test_service_context()
        feature_collection = ctx.get_feature_collection(
            collection_name="inside-cube")
        self.assertIsInstance(feature_collection, dict)
        self.assertIn("type", feature_collection)
        self.assertEqual("FeatureCollection", feature_collection["type"])
        self.assertIn("features", feature_collection)
        self.assertIsInstance(feature_collection["features"], list)
        self.assertEqual(3, len(feature_collection["features"]))
        self.assertIs(
            feature_collection,
            ctx.get_feature_collection(collection_name="inside-cube"))
        self.assertIsNot(
            feature_collection,
            ctx.get_feature_collection(collection_name="outside-cube"))

        with self.assertRaises(ServiceResourceNotFoundError) as cm:
            ctx.get_feature_collection(collection_name="bibo")
        self.assertEqual('HTTP 404: Feature collection "bibo" not found',
                         f"{cm.exception}")
示例#21
0
    def test_get_legend(self):
        ctx = new_test_service_context()
        image = get_legend(ctx, 'demo', 'conc_chl', RequestParamsMock())
        self.assertEqual("<class 'bytes'>", str(type(image)))

        with self.assertRaises(ServiceResourceNotFoundError) as cm:
            get_legend(ctx, 'demo', 'conc_chl',
                       RequestParamsMock(cbar='sun-shine'))
        self.assertEqual('color bar sun-shine not found', cm.exception.reason)

        with self.assertRaises(ServiceBadRequestError) as cm:
            get_legend(ctx, 'demo', 'conc_chl',
                       RequestParamsMock(vmin='sun-shine'))
        self.assertEqual(
            """Parameter "vmin" must be a number, but was 'sun-shine'""",
            cm.exception.reason)

        with self.assertRaises(ServiceBadRequestError) as cm:
            get_legend(ctx, 'demo', 'conc_chl',
                       RequestParamsMock(width='sun-shine'))
        self.assertEqual(
            """Parameter "width" must be an integer, but was 'sun-shine'""",
            cm.exception.reason)
示例#22
0
 def test_find_features_all(self):
     ctx = new_test_service_context()
     feature_collection = find_features(ctx, "all")
     self._assertFeatureCollection(feature_collection, 6,
                                   {'0', '1', '2', '3', '4', '5'})
示例#23
0
 def test_find_dataset_features(self):
     ctx = new_test_service_context()
     feature_collection = find_dataset_features(ctx, "all", "demo")
     self._assertFeatureCollection(feature_collection, 3, {'0', '1', '2'})
示例#24
0
 def test_find_dataset_features(self):
     ctx = new_test_service_context()
     places = find_dataset_places(ctx, "all", "demo")
     self._assertPlaceGroup(places, 3, {'0', '1', '2'})
示例#25
0
    def test_get_time_series_info(self):
        ctx = new_test_service_context()
        info = get_time_series_info(ctx)

        expected_dict = self._get_expected_info_dict()
        self.assertEqual(expected_dict, info)
示例#26
0
 def test_get_time_series_for_point_invalid_lat_and_lon(self):
     ctx = new_test_service_context()
     time_series = get_time_series_for_point(ctx, 'demo', 'conc_tsm',
                                             lon=-150.0, lat=-30.0)
     expected_dict = {'results': []}
     self.assertEqual(expected_dict, time_series)
示例#27
0
 def get_app(self):
     application = new_application()
     application.service_context = new_test_service_context()
     return application
示例#28
0
 def test_get_ne2_tile(self):
     ctx = new_test_service_context()
     tile = get_ne2_tile(ctx, '0', '0', '0', RequestParamsMock())
     self.assertIsInstance(tile, bytes)
示例#29
0
 def test_find_places_all(self):
     ctx = new_test_service_context()
     places = find_places(ctx, "all")
     self._assertPlaceGroup(places, 6, {'0', '1', '2', '3', '4', '5'})