Files
pygeoapi/tests/test_util.py
T
Francesco Bartoli 4f52536051 Add tiles ogc api (#419)
* Add ogc api tiles implementation

* Add json response to tiles endpoint

* Return correct json payload

* Fix url generation

* Add vector tile layer with clickable items

* Fix typo

* Fix oafeat collection tiles link

* Add format to metadata link

* Fix almost all flake8 errors

* Fix metadata link

* Add routes to serving tiles

* Refactor tile implementation with an own module

* Add http client for mvt

* Fix visibility of tiles template

* Fix tiles in collection item page

* Change source to data coherently with stac provider

* Fix flake8 errors

* Fix test for conformance classes

* Fix flake8 errors


Fix flake8 errors

* Refactor to align with new configuration structure

* Fix MVT plugin name

* Add openapi tiles path and schemas

* Add ogc api tiles implementation

* Add json response to tiles endpoint

* Return correct json payload

* Fix url generation

* Add vector tile layer with clickable items

* Fix typo

* Fix oafeat collection tiles link

* Add format to metadata link

* Fix almost all flake8 errors

* Fix metadata link

* Add routes to serving tiles

* Refactor tile implementation with an own module

* Add http client for mvt

* Fix visibility of tiles template

* Fix tiles in collection item page

* Change source to data coherently with stac provider

* Fix flake8 errors

* Fix test for conformance classes

* Fix flake8 errors


Fix flake8 errors

* Refactor to align with new configuration structure

* Fix MVT plugin name

* Add openapi tiles path and schemas

* Fix flake errors

* Fix merge and tests

* Add test configuration and data for tiles

* Change mapbox vector tile provider name

* Fix provider inspection and filter

* Fix tiles rendering in collection page

* Add mimetype to the mvt provider

* Fix mimetype

* Rename functions accordingly to singular and plural forms

* Fix tiling schemes in tiles template

* Serve mvt through the pygeoapi route

* Refactor and add test for collection tiles

* Fix test for not being tiles

* Remove httpx dependency

* Fix missing comma

* Fix tests

* Fix flake8

* Add initial metadata implementation

* Fix route with preprocess

* Fix tiles template


Fix tiles template

* Complete tileset api and template

* Fix flake8

* Complete metadata with tilejson response

* Add support for local vector tiles

* Fix flake8

* Fix plugin tile object and provider name

* Add a todo comment

* testing alternate plugin setup, fix some HTML, add OpenAPI endpoint

* remove URI template

* add docs

* Fix failing test

* Fix flake8 error

Co-authored-by: Francesco Bartoli <francesco.bartoli@wfp.org>
Co-authored-by: Tom Kralidis <tomkralidis@gmail.com>
2020-09-20 09:50:40 -04:00

167 lines
5.1 KiB
Python

# =================================================================
#
# Authors: Tom Kralidis <tomkralidis@gmail.com>
#
# Copyright (c) 2020 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# =================================================================
from datetime import datetime, date, time
from decimal import Decimal
import os
import pytest
from pygeoapi import util
from pygeoapi.provider.base import ProviderTypeError
def get_test_file_path(filename):
"""helper function to open test file safely"""
if os.path.isfile(filename):
return filename
else:
return 'tests/{}'.format(filename)
def test_get_typed_value():
value = util.get_typed_value('2')
assert isinstance(value, int)
value = util.get_typed_value('1.2')
assert isinstance(value, float)
value = util.get_typed_value('1.c2')
assert isinstance(value, str)
def test_yaml_load():
with open(get_test_file_path('pygeoapi-test-config.yml')) as fh:
d = util.yaml_load(fh)
assert isinstance(d, dict)
with pytest.raises(FileNotFoundError):
with open(get_test_file_path('404.yml')) as fh:
d = util.yaml_load(fh)
def test_str2bool():
assert util.str2bool(False) is False
assert util.str2bool('0') is False
assert util.str2bool('no') is False
assert util.str2bool('yes') is True
assert util.str2bool('1') is True
assert util.str2bool(True) is True
assert util.str2bool('true') is True
assert util.str2bool('True') is True
assert util.str2bool('TRUE') is True
assert util.str2bool('tRuE') is True
assert util.str2bool('on') is True
assert util.str2bool('On') is True
assert util.str2bool('off') is False
def test_json_serial():
d = datetime(1972, 10, 30)
assert util.json_serial(d) == '1972-10-30T00:00:00'
d = date(2010, 7, 31)
assert util.json_serial(d) == '2010-07-31'
d = time(11)
assert util.json_serial(d) == '11:00:00'
d = Decimal(1.0)
assert util.json_serial(d) == 1.0
with pytest.raises(TypeError):
util.json_serial('foo')
def test_mimetype():
assert util.get_mimetype('file.xml') == 'application/xml'
assert util.get_mimetype('file.yml') == 'text/plain'
assert util.get_mimetype('file.yaml') == 'text/plain'
def test_get_breadcrumbs():
path = '/dataset/model-run/forecast-hour/variable.grib2'
breadcrumbs = util.get_breadcrumbs(path)
assert len(breadcrumbs) == 5
assert breadcrumbs[3]['href'] == 'dataset/model-run/forecast-hour'
def test_path_basename():
assert util.get_path_basename('/path/to/file.txt') == 'file.txt'
assert util.get_path_basename('/path/to/dir') == 'dir'
def test_filter_dict_by_key_value():
with open(get_test_file_path('pygeoapi-test-config.yml')) as fh:
d = util.yaml_load(fh)
collections = util.filter_dict_by_key_value(d['resources'],
'type', 'collection')
assert len(collections) == 3
notfound = util.filter_dict_by_key_value(d['resources'],
'type', 'foo')
assert len(notfound) == 0
def test_get_provider_by_type():
with open(get_test_file_path('pygeoapi-test-config.yml')) as fh:
d = util.yaml_load(fh)
p = util.get_provider_by_type(d['resources']['obs']['providers'],
'feature')
assert isinstance(p, dict)
assert p['type'] == 'feature'
assert p['name'] == 'CSV'
with pytest.raises(ProviderTypeError):
p = util.get_provider_by_type(d['resources']['obs']['providers'],
'something-else')
def test_get_provider_default():
with open(get_test_file_path('pygeoapi-test-config.yml')) as fh:
d = util.yaml_load(fh)
pd = util.get_provider_default(d['resources']['obs']['providers'])
assert pd['type'] == 'feature'
assert pd['name'] == 'CSV'
pd = util.get_provider_default(d['resources']['obs']['providers'])
def test_read_data():
data = util.read_data(get_test_file_path('pygeoapi-test-config.yml'))
assert isinstance(data, bytes)