From a2b33f2e8679a8062453325dab2e4362c99d933a Mon Sep 17 00:00:00 2001 From: gqueiroz Date: Mon, 14 Sep 2020 09:52:33 -0300 Subject: [PATCH] Improve tests (ref #43); Improve text conversion methods (ref #25) --- docs/sphinx/conf.py | 5 ++ pytest.ini | 2 +- tests/test_wtss.py | 36 ++++++++++++++- wtss/coverage.py | 108 ++++++++++++++++++++++++-------------------- wtss/timeseries.py | 3 +- wtss/wtss.py | 24 +++++++--- 6 files changed, 120 insertions(+), 58 deletions(-) diff --git a/docs/sphinx/conf.py b/docs/sphinx/conf.py index 6704377..8e1252a 100644 --- a/docs/sphinx/conf.py +++ b/docs/sphinx/conf.py @@ -122,6 +122,11 @@ #def setup(app): # app.add_stylesheet('wtss.css') +doctest_global_setup = ''' +import os + +WTSS_EXAMPLE_URL = os.getenv('WTSS_EXAMPLE_URL', None) +''' #todo_include_todos = True #todo_emit_warnings = True diff --git a/pytest.ini b/pytest.ini index 9b107b8..7aa3dac 100644 --- a/pytest.ini +++ b/pytest.ini @@ -7,5 +7,5 @@ # [pytest] -addopts = --color=auto --cov=wtss --cov-report=term-missing +addopts = --strict --color=auto --cov=wtss --cov-report=term-missing testpaths = tests \ No newline at end of file diff --git a/tests/test_wtss.py b/tests/test_wtss.py index 568e4d5..f52e00f 100644 --- a/tests/test_wtss.py +++ b/tests/test_wtss.py @@ -8,16 +8,23 @@ """Unit-test for the WTSS Python Client Library for.""" +import pytest +from requests import ConnectionError as _ConnectionError + from wtss import * +@pytest.mark.xfail(raises=_ConnectionError, + reason='WTSS server not reached!') def test_list_coverages(URL, ListCoverageResponse): service = WTSS(URL) assert set(service.coverages) == set(ListCoverageResponse['coverages']) -def test_list_coverages(URL, MOD13Q1): +@pytest.mark.xfail(raises=_ConnectionError, + reason='WTSS server not reached!') +def test_describe_coverage(URL, MOD13Q1): service = WTSS(URL) cov = service['MOD13Q1'] @@ -26,3 +33,30 @@ def test_list_coverages(URL, MOD13Q1): assert cov.spatial_extent == MOD13Q1['spatial_extent'] assert cov.spatial_resolution == MOD13Q1['spatial_resolution'] assert cov.attributes == MOD13Q1['attributes'] + + +@pytest.mark.xfail(raises=_ConnectionError, + reason='WTSS server not reached!') +@pytest.mark.parametrize( + 'coverage, attr, location, start_date, end_date, result', + [ + ('MOD13Q1', 'nir', dict(latitude=-12, longitude=-54), + '2001-01-01', '2001-12-31', + [ + 3463.0, 3656.0, 2883.0, 4130.0, 2910.0, 3300.0, + 3281.0, 2979.0, 2953.0, 2876.0, 2872.0, 2854.0, + 2977.0, 3008.0, 3040.0, 3201.0, 3297.0, 2815.0, + 3546.0, 4161.0, 4097.0, 3901.0, 2948.0 + ]) + ] +) +def test_st(URL, coverage, attr, location, start_date, end_date, result): + service = WTSS(URL) + + cov = service[coverage] + + ts = cov.ts(attributes=attr, + latitude=location['latitude'], longitude=location['longitude'], + start_date=start_date, end_date=end_date) + + assert ts.values(attr) == result \ No newline at end of file diff --git a/wtss/coverage.py b/wtss/coverage.py index abe0e35..632b32e 100644 --- a/wtss/coverage.py +++ b/wtss/coverage.py @@ -106,9 +106,10 @@ def ts(self, **options): Retrieves a time series for MODIS13Q1 data product: .. doctest:: - :skipif: True + :skipif: WTSS_EXAMPLE_URL is None - >>> service = WTSS('http://localhost') + >>> from wtss import * + >>> service = WTSS(WTSS_EXAMPLE_URL) >>> coverage = service['MOD13Q1'] >>> ts = coverage.ts(attributes=('red', 'nir'), ... latitude=-12.0, longitude=-54.0, @@ -201,51 +202,62 @@ def _repr_html_(self): timeline_htlm += ''.join(timeline_options) + '' html = '''\ - - - - - - - - - - - - - - - - - - - - - - {attributes} - - - - - - - - - - - - - - - - - - -
Coverage{name}
Description{description}
Attributesnamedescriptiondatatypevalid rangescalenodata
Extentxminyminxmaxymax
{xmin}{ymin}{xmax}{ymax}
Timeline{timeline}
'''.format(name=self['name'], - description=self['description'], - attributes=''.join(attr_rows), - nattrs=len(attr_rows) + 1, - timeline=timeline_htlm, - **self['spatial_extent']) +
+
+ Coverage {name} +
+
+ Description {description} +
+
+
+ Attributes +
+
+ + + + + + + + + + + + {attributes} +
namedescriptiondatatypevalid rangescalenodata
+
+
+
+ Extent +
+
+ + + + + + + + + + + + + +
xminyminxmaxymax
{xmin}{ymin}{xmax}{ymax}
+
+

+ Timeline +
+
+ {timeline} +
+
'''.format(name=self['name'], + description=self['description'], + attributes=''.join(attr_rows), + timeline=timeline_htlm, + **self['spatial_extent']) return html \ No newline at end of file diff --git a/wtss/timeseries.py b/wtss/timeseries.py index 5047e9b..dc8453a 100644 --- a/wtss/timeseries.py +++ b/wtss/timeseries.py @@ -76,7 +76,8 @@ def plot(self, **options): .. doctest:: :skipif: True - >>> service = WTSS('http://localhost') # doctest: +SKIP + >>> from wtss import * + >>> service = WTSS(WTSS_EXAMPLE_URL) >>> coverage = service['MOD13Q1'] >>> ts = coverage.ts(attributes=('red', 'nir'), ... latitude=-12.0, longitude=-54.0, diff --git a/wtss/wtss.py b/wtss/wtss.py index 6771c7f..ae73dc9 100644 --- a/wtss/wtss.py +++ b/wtss/wtss.py @@ -18,14 +18,14 @@ .. doctest:: - :skipif: True + :skipif: WTSS_EXAMPLE_URL is None >>> from wtss import * - >>> service = WTSS('http://localhost') + >>> service = WTSS(WTSS_EXAMPLE_URL) >>> for cv in service: ... print(cv) ... - {'name': ...} + Coverage... ... """ @@ -69,6 +69,7 @@ def coverages(self): list: A list with the names of available coverages in the service. Raises: + ConnectionError: If the server is not reachable. HTTPError: If the server response indicates an error. ValueError: If the response body is not a json document. """ @@ -82,6 +83,7 @@ def _list_coverages(self): list: A list with the names of available coverages in the service. Raises: + ConnectionError: If the server is not reachable. HTTPError: If the server response indicates an error. ValueError: If the response body is not a json document. """ @@ -100,6 +102,7 @@ def _describe_coverage(self, name): dict: The coverage metadata as a dictionary. Raises: + ConnectionError: If the server is not reachable. HTTPError: If the server response indicates an error. ValueError: If the response body is not a json document. """ @@ -126,6 +129,7 @@ def _time_series(self, **options): dict: A time series object as a dictionary. Raises: + ConnectionError: If the server is not reachable. HTTPError: If the server response indicates an error. ValueError: If the response body is not a json document. """ @@ -143,6 +147,7 @@ def __getitem__(self, key): Coverage: A coverage metadata object. Raises: + ConnectionError: If the server is not reachable. HTTPError: If the server response indicates an error. ValueError: If the response body is not a json document. @@ -151,11 +156,12 @@ def __getitem__(self, key): Get a coverage object named ``MOD13Q1``: .. doctest:: - :skipif: True + :skipif: WTSS_EXAMPLE_URL is None >>> from wtss import * - >>> service = WTSS('http://localhost') + >>> service = WTSS(WTSS_EXAMPLE_URL) >>> service['MOD13Q1'] + Coverage... """ cv_meta = self._describe_coverage(key) @@ -177,11 +183,13 @@ def __getattr__(self, name): Get a coverage object named ``MOD13Q1``: .. doctest:: - :skipif: True + :skipif: WTSS_EXAMPLE_URL is None >>> from wtss import * - >>> service = WTSS('http://localhost') + >>> service = WTSS(WTSS_EXAMPLE_URL) >>> service.MOD13Q1 + Coverage... + """ try: return self[name] @@ -222,6 +230,7 @@ def _ipython_key_completions_(self): list: The list of available coverages in the service. Raises: + ConnectionError: If the server is not reachable. HTTPError: If the server response indicates an error. ValueError: If the response body is not a json document. """ @@ -267,6 +276,7 @@ def _get(url, op, **params): A JSON document. Raises: + ConnectionError: If the server is not reachable. HTTPError: If the server response indicates an error. ValueError: If the response body does not contain a valid json or geojson. """