Skip to content

Commit 24cb4f9

Browse files
authored
Add more tests (#7)
Add more tests and CI coverage reporting. Closes #1, remaining missing tests are divided out into their own issues: #8, #9
1 parent 3705b03 commit 24cb4f9

File tree

7 files changed

+121
-10
lines changed

7 files changed

+121
-10
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ jobs:
4747

4848
- name: run tests
4949
id: status
50-
run: pytest -v .
50+
run: pytest -v --cov xpystac --cov-report term-missing --vcr-record=none .

environment.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ dependencies:
1616
- stackstac
1717
- zarr
1818
# testing
19-
- pytest
19+
- pytest
20+
- pytest-cov
21+
- pytest-vcr
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
interactions:
2+
- request:
3+
body: null
4+
headers:
5+
Accept:
6+
- '*/*'
7+
Accept-Encoding:
8+
- gzip, deflate, br
9+
Connection:
10+
- keep-alive
11+
User-Agent:
12+
- python-requests/2.28.2
13+
method: GET
14+
uri: https://earth-search.aws.element84.com/v0
15+
response:
16+
body:
17+
string: '{"stac_version":"1.0.0-beta.2","stac_api_version":"0.9.0","id":"earth-search","title":"Earth
18+
Search","description":"A STAC API of AWS Public Datasets powered by stac-server","links":[{"rel":"child","href":"https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a"},{"rel":"child","href":"https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l1c"},{"rel":"child","href":"https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a-cogs"},{"rel":"child","href":"https://earth-search.aws.element84.com/v0/collections/landsat-8-l1-c1"},{"rel":"service-desc","type":"application/vnd.oai.openapi+json;version=3.0","href":"https://earth-search.aws.element84.com/v0/api"},{"rel":"conformance","type":"application/json","href":"https://earth-search.aws.element84.com/v0/conformance"},{"rel":"children","type":"application/json","href":"https://earth-search.aws.element84.com/v0/collections"},{"rel":"self","type":"application/json","href":"https://earth-search.aws.element84.com/v0/"},{"rel":"search","type":"application/json","href":"https://earth-search.aws.element84.com/v0/search"},{"rel":"docs","href":"https://stac-utils.github.io/stac-server/"}]}'
19+
headers:
20+
Access-Control-Allow-Credentials:
21+
- 'true'
22+
Access-Control-Allow-Origin:
23+
- '*'
24+
Connection:
25+
- keep-alive
26+
Content-Length:
27+
- '1180'
28+
Content-Type:
29+
- application/json
30+
Date:
31+
- Fri, 24 Feb 2023 14:00:56 GMT
32+
Via:
33+
- 1.1 b4fecc0ccf7e0c6aa3eab83d70c5766c.cloudfront.net (CloudFront)
34+
X-Amz-Cf-Id:
35+
- aRQ9whW8xQDfRCiAkz8yXsApL7yIJauKHxLbONigmD_tgfPI5MurTg==
36+
X-Amz-Cf-Pop:
37+
- EWR52-C2
38+
X-Amzn-Trace-Id:
39+
- Root=1-63f8c318-3c0a337a4a04985a0b3d99b0;Sampled=0
40+
X-Cache:
41+
- Miss from cloudfront
42+
x-amz-apigw-id:
43+
- A2Nr5HdjPHcF3ew=
44+
x-amzn-RequestId:
45+
- f186aeb2-6a03-4649-aca8-e92a03efc730
46+
status:
47+
code: 200
48+
message: OK
49+
version: 1

tests/conftest.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
11
import pystac
2+
import pystac_client
23
import pytest
4+
import vcr
5+
6+
# copied from https://github.com/stac-utils/pystac-client/blob/v0.6.0/tests/helpers.py#L7-L11
7+
STAC_URLS = {
8+
"PLANETARY-COMPUTER": "https://planetarycomputer.microsoft.com/api/stac/v1",
9+
"EARTH-SEARCH": "https://earth-search.aws.element84.com/v0",
10+
"MLHUB": "https://api.radiant.earth/mlhub/v1",
11+
}
312

413

514
@pytest.fixture(scope="module")
615
def simple_item() -> pystac.Item:
716
path = "https://raw.githubusercontent.com/stac-utils/pystac/2.0/tests/data-files/examples/1.0.0/simple-item.json"
817
return pystac.Item.from_file(path)
18+
19+
20+
@pytest.fixture(scope="module")
21+
def simple_cog(simple_item) -> pystac.Asset:
22+
asset = simple_item.assets["visual"]
23+
assert asset.media_type == pystac.MediaType.COG
24+
return asset
25+
26+
27+
@pytest.fixture(scope="module")
28+
def simple_search() -> pystac_client.ItemSearch:
29+
with vcr.use_cassette("tests/cassettes/fixtures/simple_search.yaml"):
30+
client = pystac_client.Client.open(STAC_URLS["EARTH-SEARCH"])
31+
return client.search(
32+
intersects=dict(type="Point", coordinates=[-105.78, 35.79]),
33+
collections=["sentinel-s2-l2a-cogs"],
34+
datetime="2020-05-01",
35+
)

tests/test_core.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
import pystac
1+
import pytest
22

33
from xpystac.core import to_xarray
44

55

6-
def test_asset_to_xarray(simple_item):
7-
asset = simple_item.assets["visual"]
8-
assert asset.media_type == pystac.MediaType.COG
9-
ds = to_xarray(asset)
6+
def test_to_xarray_with_cog_asset(simple_cog):
7+
ds = to_xarray(simple_cog)
108
assert ds
9+
10+
11+
def test_to_xarray_with_pystac_client_search(simple_search):
12+
ds = to_xarray(simple_search)
13+
assert ds
14+
15+
16+
def test_to_xarray_with_drop_variables_raises(simple_search):
17+
with pytest.raises(KeyError, match="not implemented for pystac items"):
18+
to_xarray(simple_search, drop_variables=["B0"])
19+
20+
21+
def test_to_xarray_with_bad_type():
22+
with pytest.raises(TypeError):
23+
to_xarray("foo")

tests/test_xarray_plugin.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pytest
2+
import xarray
3+
4+
5+
def test_xarray_open_dataset_can_guess_for_pystac_objects(simple_cog):
6+
ds = xarray.open_dataset(simple_cog)
7+
assert ds
8+
9+
10+
def test_xarray_open_dataset_cannot_guess_for_pystac_client_objects(simple_search):
11+
with pytest.raises(
12+
ValueError, match="Consider explicitly selecting one of the installed engines"
13+
):
14+
xarray.open_dataset(simple_search)
15+
16+
17+
def test_xarray_open_dataset_with_drop_variables_raises(simple_search):
18+
with pytest.raises(KeyError, match="not implemented for pystac items"):
19+
xarray.open_dataset(simple_search, engine="stac", drop_variables=["B0"])

xpystac/core.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
@functools.singledispatch
1111
def to_xarray(item, **kwargs) -> xarray.Dataset:
1212
"""Given a pystac object return an xarray dataset"""
13-
if hasattr(item, "get_all_items"):
14-
item_collection = item.get_all_items()
15-
return to_xarray(item_collection)
13+
is_pystac_client_obj = hasattr(item, "item_collection")
14+
if is_pystac_client_obj:
15+
item_collection = item.item_collection()
16+
return to_xarray(item_collection, **kwargs)
1617
raise TypeError
1718

1819

0 commit comments

Comments
 (0)