|
1 | 1 | import pathlib |
2 | 2 | import pystac |
3 | 3 |
|
| 4 | +import logging |
4 | 5 | import s1grd |
| 6 | +from stactools.sentinel1.metadata_links import MetadataLinks |
5 | 7 | from pctasks.core.storage import StorageFactory |
| 8 | +import pytest |
6 | 9 |
|
7 | 10 |
|
8 | 11 | HERE = pathlib.Path(__file__).parent |
| 12 | +logging.basicConfig(level=logging.INFO) |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | +logger.setLevel(logging.INFO) |
| 15 | +if not logger.hasHandlers(): |
| 16 | + handler = logging.StreamHandler() |
| 17 | + handler.setFormatter(logging.Formatter("[%(levelname)s]:%(asctime)s: %(message)s")) |
| 18 | + logger.addHandler(handler) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize( |
| 22 | + "item_id, annotation_name, expected_key", |
| 23 | + [ |
| 24 | + ( |
| 25 | + "S1A_IW_GRDH_1SDV_20230628T210705_20230628T210730_049191_05EA4D_21D1", |
| 26 | + "s1a-iw-grd-vh-20230628t210705-20230628t210730-049191-05ea4d-002", |
| 27 | + "vh", |
| 28 | + ), |
| 29 | + ( |
| 30 | + "S1C_IW_GRDH_1SDV_20250708T025935_20250708T030005_003123_00655C_BA99", |
| 31 | + "s1c-iw-grd-vh-20250708t025935-20250708t030005-003123-00655c-002", |
| 32 | + "vh", |
| 33 | + ), |
| 34 | + ], |
| 35 | +) |
| 36 | +def test_metadata_links_annotation_pattern_parametrized( |
| 37 | + tmp_path, item_id: str, annotation_name: str, expected_key: str |
| 38 | +): |
| 39 | + # Setup: create a minimal manifest.safe with dataObjectSection and fileLocation |
| 40 | + archive_dir = tmp_path / item_id |
| 41 | + annotation_filename = f"{annotation_name}.xml" |
| 42 | + annotation_dir = archive_dir / "annotation" |
| 43 | + annotation_dir.mkdir(parents=True) |
| 44 | + annotation_file = annotation_dir / annotation_filename |
| 45 | + annotation_file.write_text("<xml></xml>") |
| 46 | + |
| 47 | + # The manifest must reference the annotation file |
| 48 | + manifest_content = f""" |
| 49 | + <manifest> |
| 50 | + <dataObjectSection> |
| 51 | + <dataObject> |
| 52 | + <byteStream> |
| 53 | + <fileLocation href="annotation/{annotation_filename}"/> |
| 54 | + </byteStream> |
| 55 | + </dataObject> |
| 56 | + </dataObjectSection> |
| 57 | + </manifest> |
| 58 | + """ |
| 59 | + manifest_file = archive_dir / "manifest.safe" |
| 60 | + manifest_file.write_text(manifest_content) |
| 61 | + try: |
| 62 | + logger.info(f"Creating MetadataLinks for {archive_dir}") |
| 63 | + ml = MetadataLinks(str(archive_dir)) |
| 64 | + annotation_hrefs = ml.annotation_hrefs |
| 65 | + logger.info(f"Annotation hrefs: {annotation_hrefs}") |
| 66 | + except Exception as e: |
| 67 | + assert False, f"MetadataLinks failed: {e}" |
| 68 | + |
| 69 | + assert any( |
| 70 | + expected_key in key and annotation_file.name in href |
| 71 | + for key, href in annotation_hrefs |
| 72 | + ) |
9 | 73 |
|
10 | 74 |
|
11 | 75 | def test_get_item_storage(): |
|
0 commit comments