Skip to content

Commit 6667def

Browse files
committed
Add test coverage for stac_api_io.py error handling
- Test decode error in request() method - Test write to URL error in write_text_to_href() - Test unknown STAC object type error in stac_object_from_dict()
1 parent f59d69e commit 6667def

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

tests/test_stac_api_io.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import typing
22
from pathlib import Path
3+
from typing import Any
34
from urllib.parse import parse_qs, urlsplit
45

56
import pystac
@@ -289,3 +290,58 @@ def test_stac_io_in_pystac() -> None:
289290
stac_io = root._stac_io
290291
assert isinstance(stac_io, StacApiIO)
291292
assert stac_io.timeout == 42
293+
294+
295+
def test_request_decode_error(requests_mock: Mocker) -> None:
296+
"""Test that decode errors in request() are properly handled."""
297+
url = "https://example.com/bad-encoding"
298+
# Mock a response with invalid UTF-8 content
299+
requests_mock.get(url, status_code=200, content=b"\xff\xfe\x00\x00")
300+
301+
stac_api_io = StacApiIO()
302+
303+
with pytest.raises(APIError) as excinfo:
304+
stac_api_io.request(url)
305+
306+
assert (
307+
"decode" in str(excinfo.value).lower() or "utf-8" in str(excinfo.value).lower()
308+
)
309+
310+
311+
def test_write_text_to_href_url_error() -> None:
312+
"""Test that write_text_to_href raises APIError for URLs."""
313+
stac_api_io = StacApiIO()
314+
315+
with pytest.raises(APIError, match="Transactions not supported"):
316+
stac_api_io.write_text_to_href("https://example.com/write", "content")
317+
318+
319+
def test_stac_object_from_dict_unknown_type(monkeypatch: MonkeyPatch) -> None:
320+
"""Test that unknown STAC object types raise ValueError."""
321+
stac_api_io = StacApiIO()
322+
323+
import json
324+
325+
with open("tests/data/planetary-computer-collection.json") as f:
326+
real_stac_data = json.load(f)
327+
328+
# Mock identify_stac_object to return an unknown type
329+
class MockInfo:
330+
object_type = "UNKNOWN_TYPE"
331+
332+
def mock_identify_stac_object(d: dict[str, Any]) -> MockInfo:
333+
return MockInfo()
334+
335+
# Mock migrate_to_latest to just return the data unchanged
336+
def mock_migrate_to_latest(d: dict[str, Any], info: MockInfo) -> dict[str, Any]:
337+
return d
338+
339+
monkeypatch.setattr(
340+
"pystac_client.stac_api_io.identify_stac_object", mock_identify_stac_object
341+
)
342+
monkeypatch.setattr(
343+
"pystac_client.stac_api_io.migrate_to_latest", mock_migrate_to_latest
344+
)
345+
346+
with pytest.raises(ValueError, match="Unknown STAC object type"):
347+
stac_api_io.stac_object_from_dict(real_stac_data)

0 commit comments

Comments
 (0)