Skip to content

Commit

Permalink
Improve tests (ref brazil-data-cube#43); Improve text conversion meth…
Browse files Browse the repository at this point in the history
…ods (ref brazil-data-cube#25)
  • Loading branch information
gqueiroz committed Sep 14, 2020
1 parent 4d19979 commit a2b33f2
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 58 deletions.
5 changes: 5 additions & 0 deletions docs/sphinx/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 35 additions & 1 deletion tests/test_wtss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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
108 changes: 60 additions & 48 deletions wtss/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -201,51 +202,62 @@ def _repr_html_(self):
timeline_htlm += ''.join(timeline_options) + '</select>'

html = '''\
<table>
<thead>
<tr>
<th>Coverage</th>
<td colspan="6">{name}</td>
</tr>
</thead>
<tbody>
<tr>
<th>Description</th>
<td colspan="6">{description}</td>
</tr>
<tr>
<th rowspan="{nattrs}">Attributes</th>
<th>name</th>
<th>description</th>
<th>datatype</th>
<th>valid range</th>
<th>scale</th>
<th>nodata</th>
</tr>
{attributes}
<tr>
<th rowspan="2">Extent</th>
<th>xmin</th>
<th>ymin</th>
<th>xmax</th>
<th colspan="3">ymax</th>
</tr>
<tr>
<td>{xmin}</td>
<td>{ymin}</td>
<td>{xmax}</td>
<td colspan="3">{ymax}</td>
</tr>
<tr>
<th>Timeline</th>
<td>{timeline}</td>
</tr>
</tbody>
</table>'''.format(name=self['name'],
description=self['description'],
attributes=''.join(attr_rows),
nattrs=len(attr_rows) + 1,
timeline=timeline_htlm,
**self['spatial_extent'])
<div>
<div>
<b>Coverage</b> {name}
</div>
<div>
<b>Description</b> {description}
</div>
</br>
<div>
<b>Attributes</b>
</div>
<div>
<table>
<tr>
</tr>
<tr>
<th>name</th>
<th>description</th>
<th>datatype</th>
<th>valid range</th>
<th>scale</th>
<th>nodata</th>
</tr>
{attributes}
</table>
</div>
</br>
<div>
<b>Extent</b>
</div>
<div>
<table>
<tr>
<th>xmin</th>
<th>ymin</th>
<th>xmax</th>
<th>ymax</th>
</tr>
<tr>
<td>{xmin}</td>
<td>{ymin}</td>
<td>{xmax}</td>
<td colspan="3">{ymax}</td>
</tr>
</table>
</div>
<div></br>
<b>Timeline</b>
</div>
<div>
{timeline}
</div>
</div>'''.format(name=self['name'],
description=self['description'],
attributes=''.join(attr_rows),
timeline=timeline_htlm,
**self['spatial_extent'])

return html
3 changes: 2 additions & 1 deletion wtss/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 17 additions & 7 deletions wtss/wtss.py
Original file line number Diff line number Diff line change
Expand Up @@ -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...
...
"""

Expand Down Expand Up @@ -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.
"""
Expand All @@ -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.
"""
Expand All @@ -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.
"""
Expand All @@ -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.
"""
Expand All @@ -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.
Expand All @@ -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)

Expand All @@ -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]
Expand Down Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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.
"""
Expand Down

0 comments on commit a2b33f2

Please sign in to comment.