|
1 | 1 | import typing |
2 | 2 | from pathlib import Path |
| 3 | +from typing import Any |
3 | 4 | from urllib.parse import parse_qs, urlsplit |
4 | 5 |
|
5 | 6 | import pystac |
@@ -289,3 +290,58 @@ def test_stac_io_in_pystac() -> None: |
289 | 290 | stac_io = root._stac_io |
290 | 291 | assert isinstance(stac_io, StacApiIO) |
291 | 292 | 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