Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jpolchlo committed May 22, 2023
1 parent 90aef2f commit dd661f5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
2 changes: 0 additions & 2 deletions pystac/extensions/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,6 @@ def __init__(self, asset: pystac.Asset):
self.asset = asset
self.asset_href = asset.href
self.properties = asset.extra_fields
if asset.owner is not None and RasterExtension.has_extension(asset.owner):
RasterExtension.ext(self.asset).bands
if asset.owner and isinstance(asset.owner, pystac.Item):
self.additional_read_properties = [asset.owner.properties]

Expand Down
89 changes: 87 additions & 2 deletions tests/extensions/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,48 @@ def test_stac_extensions(landsat_item: Item) -> None:
assert ClassificationExtension.has_extension(landsat_item)


def test_fails_on_incompatible_object() -> None:
with pytest.raises(pystac.ExtensionTypeError):
ClassificationExtension.ext("dummy")


def test_classification_object() -> None:
c = Classification.create(
name="dummy",
description="empty class",
value=0,
color_hint="FF00AB"
)
assert c.name == "dummy"
assert c.description == "empty class"
assert c.color_hint == "FF00AB"
assert c.value == 0

assert Classification(c.to_dict()) == c
with pytest.raises(NotImplementedError):
c == "blah"


def test_bitfield_object() -> None:
b = Bitfield.create(
offset=0,
length=1,
classes=[
Classification.create(description="no", value=0),
Classification.create(description="yes", value=1),
],
roles = ["data"],
description = "dummy description",
name = "dummy"
)
assert b.offset == 0
assert b.length == 1
assert len(b.classes) == 2
assert b.roles == ["data"]
assert b.description == "dummy description"
assert b.name == "dummy"


def test_get_schema_uri(landsat_item: Item) -> None:
assert any(
[
Expand All @@ -69,7 +111,7 @@ def test_ext_raises_if_item_does_not_conform(plain_item: Item) -> None:
ClassificationExtension.ext(plain_item)


def test_apply(plain_item: Item) -> None:
def test_apply_bitfields(plain_item: Item) -> None:
ClassificationExtension.add_to(plain_item)
ClassificationExtension.ext(plain_item).apply(
bitfields=[
Expand Down Expand Up @@ -113,6 +155,37 @@ def test_apply(plain_item: Item) -> None:
)


def test_create_classes(plain_item: Item) -> None:
ClassificationExtension.add_to(plain_item)
ext = ClassificationExtension.ext(plain_item)
ext.apply(
bitfields=[
Bitfield.create(
offset=0,
length=1,
classes=[
Classification.create(description="no", value=0),
Classification.create(description="yes", value=1),
],
)
]
)
ext.classes = [
Classification.create(description="no", value=0),
Classification.create(description="yes", value=1),
]
assert ext.bitfields is None
ext.bitfields = [Bitfield.create(
offset=0,
length=1,
classes=[
Classification.create(description="no", value=0),
Classification.create(description="yes", value=1),
],
)]
assert ext.classes is None


def test_create() -> None:
field = Bitfield.create(
name="cloud_confidence",
Expand Down Expand Up @@ -200,6 +273,7 @@ def test_validate_classification(landsat_item: Item) -> None:

def test_add_item_classes(plain_item: Item) -> None:
item_ext = ClassificationExtension.ext(plain_item, add_if_missing=True)
item_ext.__repr__()
assert item_ext.classes is None
item_ext.classes = [Classification.create(description="dummy", value=0)]
assert item_ext.properties[CLASSES_PROP] == [{"value": 0, "description": "dummy"}]
Expand All @@ -210,6 +284,7 @@ def test_add_asset_classes(plain_item: Item) -> None:
asset = plain_item.assets["analytic"]
assert CLASSES_PROP not in asset.extra_fields.keys()
asset_ext = ClassificationExtension.ext(asset)
asset_ext.__repr__()
asset_ext.classes = [Classification.create(value=0, description="dummy")]
assert CLASSES_PROP in asset.extra_fields.keys()
assert asset.extra_fields[CLASSES_PROP] == [{"value": 0, "description": "dummy"}]
Expand All @@ -220,4 +295,14 @@ def test_item_asset_raster_classes(collection: Collection) -> None:
"cloud-mask-raster"
]
raster_bands = cast(List[RasterBand], RasterExtension.ext(item_asset).bands)
assert ClassificationExtension.ext(raster_bands[0]).classes is not None
raster_bands_ext = ClassificationExtension.ext(raster_bands[0])
raster_bands_ext.__repr__()
assert raster_bands_ext.classes is not None


def test_item_assets_extension(collection: Collection) -> None:
item_asset = ItemAssetsExtension.ext(collection, add_if_missing=True).item_assets[
"cloud-mask-raster"
]
ext = ClassificationExtension.ext(item_asset)
ext.__repr__()

0 comments on commit dd661f5

Please sign in to comment.