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
|
import pytest
import pystac
from pystac.cache import CollectionCache
from pystac.serialization import (
identify_stac_object,
identify_stac_object_type,
merge_common_properties,
)
from pystac.serialization.identify import STACVersionID, STACVersionRange
from tests.utils import TestCases
from tests.utils.test_cases import ExampleInfo
class TestIdentify:
@pytest.mark.parametrize("example", TestCases.get_examples_info())
def test_identify(self, example: ExampleInfo) -> None:
collection_cache = CollectionCache()
path = example.path
d = pystac.StacIO.default().read_json(path)
if identify_stac_object_type(d) == pystac.STACObjectType.ITEM:
merge_common_properties(
d, json_href=path, collection_cache=collection_cache
)
actual = identify_stac_object(d)
# Explicitly cover __repr__ functions in tests
str_info = str(actual)
assert isinstance(str_info, str)
msg = f"Failed {path}:"
assert actual.object_type == example.object_type, msg
version_contained_in_range = actual.version_range.contains(example.stac_version)
assert version_contained_in_range, msg
assert set(actual.extensions) == set(example.extensions), msg
def test_identify_non_stac_type(self) -> None:
plain_feature_dict = {
"type": "Feature",
"properties": {},
"geometry": {"type": "Point", "coordinates": [0, 0]},
}
assert identify_stac_object_type(plain_feature_dict) is None
def test_identify_invalid_stac_object_with_version(self) -> None:
# Has stac_version but is not a valid STAC object
invalid_dict = {
"id": "concepts",
"title": "Concepts catalogs",
"links": [
{
"rel": "self",
"type": "application/json",
"href": "https://tamn.snapplanet.io/catalogs/concepts",
},
{
"rel": "root",
"type": "application/json",
"href": "https://tamn.snapplanet.io",
},
],
"stac_version": "1.1.0",
}
with pytest.raises(pystac.STACTypeError) as ctx:
identify_stac_object(invalid_dict)
assert "JSON (id = concepts) does not represent a STACObject instance." in str(
ctx.value.args[0]
)
def test_identify_non_stac_raises_error(self) -> None:
plain_feature_dict = {
"type": "Feature",
"properties": {},
"geometry": {"type": "Point", "coordinates": [0, 0]},
}
with pytest.raises(pystac.STACTypeError) as ctx:
identify_stac_object(plain_feature_dict)
assert "JSON (id = unknown) does not represent a STACObject instance." in str(
ctx.value.args[0]
)
def test_identify_invalid_with_stac_version(self) -> None:
not_stac = {"stac_version": "0.9.0", "type": "Custom"}
assert identify_stac_object_type(not_stac) is None
def test_version_ordering() -> None:
assert STACVersionID("0.9.0") == STACVersionID("0.9.0")
assert not STACVersionID("0.9.0") != STACVersionID("0.9.0")
assert not STACVersionID("0.9.0") > STACVersionID("0.9.0")
assert STACVersionID("1.0.0-beta.2") < "1.0.0"
assert STACVersionID("0.9.1") > "0.9.0"
assert not STACVersionID("0.9.0") > "0.9.0"
assert STACVersionID("0.9.0") <= "0.9.0"
assert STACVersionID("1.0.0-beta.1") <= STACVersionID("1.0.0-beta.2")
assert not STACVersionID("1.0.0") < STACVersionID("1.0.0-beta.2")
def test_version_range_ordering() -> None:
version_range = STACVersionRange("0.9.0", "1.0.0-beta.2")
assert isinstance(str(version_range), str)
assert version_range.contains("1.0.0-beta.1")
assert not version_range.contains("1.0.0")
assert version_range.is_later_than("0.8.9")
version_range = STACVersionRange("0.9.0", "1.0.0-beta.1")
assert not version_range.contains("1.0.0-beta.2")
version_range = STACVersionRange(min_version="0.6.0-rc1", max_version="0.9.0")
assert version_range.contains("0.9.0")
def test_version_range_set_to_single() -> None:
version_range = STACVersionRange()
version_range.set_min("1.0.0-beta.1")
version_range.set_to_single("1.0.0")
assert version_range.contains("1.0.0")
def test_version_range_set_min_and_max_directly() -> None:
version_range = STACVersionRange()
version_range.min_version = "1.0.0-beta.1" # type:ignore
version_range.max_version = "1.1.0" # type:ignore
assert version_range.contains("1.0.0")
|