Repository: stac-utils/xpystac (language: python)

Issue:
Add tests
I added one test and the CI infrastructure in #4 but it would be great to have more than one :)


Discussion / hints:

Fix patch:
diff --git a/environment.yaml b/environment.yaml
index 2e7d978..8438892 100644
--- a/environment.yaml
+++ b/environment.yaml
@@ -16,4 +16,6 @@ dependencies:
   - stackstac
   - zarr
   # testing
-  - pytest
\ No newline at end of file
+  - pytest
+  - pytest-cov
+  - pytest-vcr
diff --git a/xpystac/core.py b/xpystac/core.py
index b109eb4..00fb897 100644
--- a/xpystac/core.py
+++ b/xpystac/core.py
@@ -10,9 +10,10 @@
 @functools.singledispatch
 def to_xarray(item, **kwargs) -> xarray.Dataset:
     """Given a pystac object return an xarray dataset"""
-    if hasattr(item, "get_all_items"):
-        item_collection = item.get_all_items()
-        return to_xarray(item_collection)
+    is_pystac_client_obj = hasattr(item, "item_collection")
+    if is_pystac_client_obj:
+        item_collection = item.item_collection()
+        return to_xarray(item_collection, **kwargs)
     raise TypeError
 
 


Test patch:
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index f48f5e6..edfbdc7 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -47,4 +47,4 @@ jobs:
 
       - name: run tests
         id: status
-        run: pytest -v .
+        run: pytest -v --cov xpystac --cov-report term-missing --vcr-record=none .
diff --git a/tests/cassettes/fixtures/simple_search.yaml b/tests/cassettes/fixtures/simple_search.yaml
new file mode 100644
index 0000000..d0c70ee
--- /dev/null
+++ b/tests/cassettes/fixtures/simple_search.yaml
@@ -0,0 +1,49 @@
+interactions:
+- request:
+    body: null
+    headers:
+      Accept:
+      - '*/*'
+      Accept-Encoding:
+      - gzip, deflate, br
+      Connection:
+      - keep-alive
+      User-Agent:
+      - python-requests/2.28.2
+    method: GET
+    uri: https://earth-search.aws.element84.com/v0
+  response:
+    body:
+      string: '{"stac_version":"1.0.0-beta.2","stac_api_version":"0.9.0","id":"earth-search","title":"Earth
+        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/"}]}'
+    headers:
+      Access-Control-Allow-Credentials:
+      - 'true'
+      Access-Control-Allow-Origin:
+      - '*'
+      Connection:
+      - keep-alive
+      Content-Length:
+      - '1180'
+      Content-Type:
+      - application/json
+      Date:
+      - Fri, 24 Feb 2023 14:00:56 GMT
+      Via:
+      - 1.1 b4fecc0ccf7e0c6aa3eab83d70c5766c.cloudfront.net (CloudFront)
+      X-Amz-Cf-Id:
+      - aRQ9whW8xQDfRCiAkz8yXsApL7yIJauKHxLbONigmD_tgfPI5MurTg==
+      X-Amz-Cf-Pop:
+      - EWR52-C2
+      X-Amzn-Trace-Id:
+      - Root=1-63f8c318-3c0a337a4a04985a0b3d99b0;Sampled=0
+      X-Cache:
+      - Miss from cloudfront
+      x-amz-apigw-id:
+      - A2Nr5HdjPHcF3ew=
+      x-amzn-RequestId:
+      - f186aeb2-6a03-4649-aca8-e92a03efc730
+    status:
+      code: 200
+      message: OK
+version: 1
diff --git a/tests/conftest.py b/tests/conftest.py
index ab13820..3f3696a 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,8 +1,35 @@
 import pystac
+import pystac_client
 import pytest
+import vcr
+
+# copied from https://github.com/stac-utils/pystac-client/blob/v0.6.0/tests/helpers.py#L7-L11
+STAC_URLS = {
+    "PLANETARY-COMPUTER": "https://planetarycomputer.microsoft.com/api/stac/v1",
+    "EARTH-SEARCH": "https://earth-search.aws.element84.com/v0",
+    "MLHUB": "https://api.radiant.earth/mlhub/v1",
+}
 
 
 @pytest.fixture(scope="module")
 def simple_item() -> pystac.Item:
     path = "https://raw.githubusercontent.com/stac-utils/pystac/2.0/tests/data-files/examples/1.0.0/simple-item.json"
     return pystac.Item.from_file(path)
+
+
+@pytest.fixture(scope="module")
+def simple_cog(simple_item) -> pystac.Asset:
+    asset = simple_item.assets["visual"]
+    assert asset.media_type == pystac.MediaType.COG
+    return asset
+
+
+@pytest.fixture(scope="module")
+def simple_search() -> pystac_client.ItemSearch:
+    with vcr.use_cassette("tests/cassettes/fixtures/simple_search.yaml"):
+        client = pystac_client.Client.open(STAC_URLS["EARTH-SEARCH"])
+        return client.search(
+            intersects=dict(type="Point", coordinates=[-105.78, 35.79]),
+            collections=["sentinel-s2-l2a-cogs"],
+            datetime="2020-05-01",
+        )
diff --git a/tests/test_core.py b/tests/test_core.py
index 2db65d3..a026368 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -1,10 +1,23 @@
-import pystac
+import pytest
 
 from xpystac.core import to_xarray
 
 
-def test_asset_to_xarray(simple_item):
-    asset = simple_item.assets["visual"]
-    assert asset.media_type == pystac.MediaType.COG
-    ds = to_xarray(asset)
+def test_to_xarray_with_cog_asset(simple_cog):
+    ds = to_xarray(simple_cog)
     assert ds
+
+
+def test_to_xarray_with_pystac_client_search(simple_search):
+    ds = to_xarray(simple_search)
+    assert ds
+
+
+def test_to_xarray_with_drop_variables_raises(simple_search):
+    with pytest.raises(KeyError, match="not implemented for pystac items"):
+        to_xarray(simple_search, drop_variables=["B0"])
+
+
+def test_to_xarray_with_bad_type():
+    with pytest.raises(TypeError):
+        to_xarray("foo")
diff --git a/tests/test_xarray_plugin.py b/tests/test_xarray_plugin.py
new file mode 100644
index 0000000..ab2f02c
--- /dev/null
+++ b/tests/test_xarray_plugin.py
@@ -0,0 +1,19 @@
+import pytest
+import xarray
+
+
+def test_xarray_open_dataset_can_guess_for_pystac_objects(simple_cog):
+    ds = xarray.open_dataset(simple_cog)
+    assert ds
+
+
+def test_xarray_open_dataset_cannot_guess_for_pystac_client_objects(simple_search):
+    with pytest.raises(
+        ValueError, match="Consider explicitly selecting one of the installed engines"
+    ):
+        xarray.open_dataset(simple_search)
+
+
+def test_xarray_open_dataset_with_drop_variables_raises(simple_search):
+    with pytest.raises(KeyError, match="not implemented for pystac items"):
+        xarray.open_dataset(simple_search, engine="stac", drop_variables=["B0"])