1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
"""Tests for pystac.tests.extensions.mgrs"""
import json
import pytest
import pystac
from pystac.extensions.mgrs import MgrsExtension
from tests.conftest import get_data_file
@pytest.fixture
def ext_item_uri() -> str:
return get_data_file("mgrs/item.json")
@pytest.fixture
def ext_item(ext_item_uri: str) -> pystac.Item:
return pystac.Item.from_file(ext_item_uri)
def test_stac_extensions(ext_item: pystac.Item) -> None:
assert MgrsExtension.has_extension(ext_item)
def test_get_schema_uri(ext_item: pystac.Item) -> None:
assert MgrsExtension.get_schema_uri() in ext_item.stac_extensions
def test_ext_raises_if_item_does_not_conform(item: pystac.Item) -> None:
with pytest.raises(pystac.errors.ExtensionNotImplemented):
MgrsExtension.ext(item)
def test_ext_raises_on_collection(collection: pystac.Collection) -> None:
with pytest.raises(
pystac.errors.ExtensionTypeError,
match="MgrsExtension does not apply to type 'Collection'",
) as e:
MgrsExtension.ext(collection) # type: ignore
assert "Hint" not in str(e.value)
def test_to_from_dict(ext_item_uri: str, ext_item: pystac.Item) -> None:
with open(ext_item_uri) as f:
d = json.load(f)
actual = ext_item.to_dict(include_self_link=False)
assert actual == d
def test_add_to(item: pystac.Item) -> None:
assert not MgrsExtension.has_extension(item)
MgrsExtension.add_to(item)
assert MgrsExtension.has_extension(item)
def test_apply(item: pystac.Item) -> None:
MgrsExtension.add_to(item)
MgrsExtension.ext(item).apply(latitude_band="X", grid_square="DH")
assert MgrsExtension.ext(item).latitude_band
assert MgrsExtension.ext(item).grid_square
def test_apply_without_required_fields_raises(item: pystac.Item) -> None:
MgrsExtension.add_to(item)
with pytest.raises(TypeError, match="missing 2 required positional arguments"):
MgrsExtension.ext(item).apply() # type: ignore
@pytest.mark.vcr()
def test_validate(ext_item: pystac.Item) -> None:
assert ext_item.validate()
@pytest.mark.parametrize("field", ["latitude_band", "grid_square", "utm_zone"])
def test_get_field(ext_item: pystac.Item, field: str) -> None:
prop = ext_item.properties[f"mgrs:{field}"]
attr = getattr(MgrsExtension.ext(ext_item), field)
assert attr is not None
assert attr == prop
@pytest.mark.vcr()
@pytest.mark.parametrize(
"field,value",
[
("latitude_band", "C"),
("grid_square", "ZA"),
("utm_zone", 59),
],
)
def test_set_field(ext_item: pystac.Item, field: str, value) -> None: # type: ignore
original = ext_item.properties[f"mgrs:{field}"]
setattr(MgrsExtension.ext(ext_item), field, value)
new = ext_item.properties[f"mgrs:{field}"]
assert new != original
assert new == value
assert ext_item.validate()
def test_utm_zone_set_to_none_pops_from_dict(ext_item: pystac.Item) -> None:
assert "mgrs:utm_zone" in ext_item.properties
MgrsExtension.ext(ext_item).utm_zone = None
assert "mgrs:utm_zone" not in ext_item.properties
def test_invalid_latitude_band_raises_informative_error(ext_item: pystac.Item) -> None:
with pytest.raises(ValueError, match="must be str"):
MgrsExtension.ext(ext_item).latitude_band = 2 # type: ignore
with pytest.raises(ValueError, match="must be str"):
MgrsExtension.ext(ext_item).latitude_band = None
with pytest.raises(ValueError, match="a is not in "):
MgrsExtension.ext(ext_item).latitude_band = "a"
def test_invalid_grid_square_raises_informative_error(ext_item: pystac.Item) -> None:
with pytest.raises(ValueError, match="must be str"):
MgrsExtension.ext(ext_item).grid_square = 2 # type: ignore
with pytest.raises(ValueError, match="must be str"):
MgrsExtension.ext(ext_item).grid_square = None
with pytest.raises(ValueError, match="nv does not match the regex "):
MgrsExtension.ext(ext_item).grid_square = "nv"
def test_invalid_utm_zone_raises_informative_error(ext_item: pystac.Item) -> None:
with pytest.raises(ValueError, match="must be None or int"):
MgrsExtension.ext(ext_item).utm_zone = "foo" # type: ignore
with pytest.raises(ValueError, match="61 is not in "):
MgrsExtension.ext(ext_item).utm_zone = 61
|