|
| 1 | +"""Test Advanced PySTAC client.""" |
| 2 | +import json |
| 3 | +import os |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from titiler.stacapi.pystac import Client |
| 9 | + |
| 10 | +catalog_json = os.path.join(os.path.dirname(__file__), "fixtures", "catalog.json") |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def mock_stac_io(): |
| 15 | + """STAC IO mock""" |
| 16 | + return MagicMock() |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def client(mock_stac_io): |
| 21 | + """STAC client mock""" |
| 22 | + client = Client(id="pystac-client", description="pystac-client") |
| 23 | + |
| 24 | + with open(catalog_json, "r") as f: |
| 25 | + catalog = json.loads(f.read()) |
| 26 | + client.open = MagicMock() |
| 27 | + client.open.return_value = catalog |
| 28 | + client._collections_href = MagicMock() |
| 29 | + client._collections_href.return_value = "http://example.com/collections" |
| 30 | + |
| 31 | + client._stac_io = mock_stac_io |
| 32 | + return client |
| 33 | + |
| 34 | + |
| 35 | +def test_get_supported_aggregations(client, mock_stac_io): |
| 36 | + """Test supported STAC aggregation methods""" |
| 37 | + mock_stac_io.read_json.return_value = { |
| 38 | + "aggregations": [{"name": "aggregation1"}, {"name": "aggregation2"}] |
| 39 | + } |
| 40 | + supported_aggregations = client.get_supported_aggregations() |
| 41 | + assert supported_aggregations == ["aggregation1", "aggregation2"] |
| 42 | + |
| 43 | + |
| 44 | +@patch( |
| 45 | + "titiler.stacapi.pystac.advanced_client.Client.get_supported_aggregations", |
| 46 | + return_value=["datetime_frequency"], |
| 47 | +) |
| 48 | +def test_get_aggregation_unsupported(supported_aggregations, client): |
| 49 | + """Test handling of unsupported aggregation types""" |
| 50 | + collection_id = "sentinel-2-l2a" |
| 51 | + aggregation = "unsupported-aggregation" |
| 52 | + |
| 53 | + with pytest.warns( |
| 54 | + UserWarning, match="Aggregation type unsupported-aggregation is not supported" |
| 55 | + ): |
| 56 | + aggregation_data = client.get_aggregation(collection_id, aggregation) |
| 57 | + assert aggregation_data == [] |
| 58 | + |
| 59 | + |
| 60 | +@patch( |
| 61 | + "titiler.stacapi.pystac.advanced_client.Client.get_supported_aggregations", |
| 62 | + return_value=["datetime_frequency"], |
| 63 | +) |
| 64 | +def test_get_aggregation(supported_aggregations, client, mock_stac_io): |
| 65 | + """Test handling aggregation response""" |
| 66 | + collection_id = "sentinel-2-l2a" |
| 67 | + aggregation = "datetime_frequency" |
| 68 | + aggregation_params = {"datetime_frequency_interval": "day"} |
| 69 | + |
| 70 | + mock_stac_io.read_json.return_value = { |
| 71 | + "aggregations": [ |
| 72 | + { |
| 73 | + "name": "datetime_frequency", |
| 74 | + "buckets": [ |
| 75 | + { |
| 76 | + "key": "2023-12-11T00:00:00.000Z", |
| 77 | + "data_type": "frequency_distribution", |
| 78 | + "frequency": 1, |
| 79 | + "to": None, |
| 80 | + "from": None, |
| 81 | + } |
| 82 | + ], |
| 83 | + }, |
| 84 | + { |
| 85 | + "name": "unusable_aggregation", |
| 86 | + "buckets": [ |
| 87 | + { |
| 88 | + "key": "2023-12-11T00:00:00.000Z", |
| 89 | + } |
| 90 | + ], |
| 91 | + }, |
| 92 | + ] |
| 93 | + } |
| 94 | + |
| 95 | + aggregation_data = client.get_aggregation( |
| 96 | + collection_id, aggregation, aggregation_params |
| 97 | + ) |
| 98 | + assert aggregation_data[0]["key"] == "2023-12-11T00:00:00.000Z" |
| 99 | + assert aggregation_data[0]["data_type"] == "frequency_distribution" |
| 100 | + assert aggregation_data[0]["frequency"] == 1 |
| 101 | + assert len(aggregation_data) == 1 |
| 102 | + |
| 103 | + |
| 104 | +@patch( |
| 105 | + "titiler.stacapi.pystac.advanced_client.Client.get_supported_aggregations", |
| 106 | + return_value=["datetime_frequency"], |
| 107 | +) |
| 108 | +def test_get_aggregation_no_response(supported_aggregations, client, mock_stac_io): |
| 109 | + """Test handling of no aggregation response""" |
| 110 | + collection_id = "sentinel-2-l2a" |
| 111 | + aggregation = "datetime_frequency" |
| 112 | + aggregation_params = {"datetime_frequency_interval": "day"} |
| 113 | + |
| 114 | + mock_stac_io.read_json.return_value = [] |
| 115 | + |
| 116 | + aggregation_data = client.get_aggregation( |
| 117 | + collection_id, aggregation, aggregation_params |
| 118 | + ) |
| 119 | + assert aggregation_data == [] |
0 commit comments