Skip to content

Commit 19d637f

Browse files
committed
Add some more tests
1 parent a625106 commit 19d637f

File tree

3 files changed

+97
-15
lines changed

3 files changed

+97
-15
lines changed

pystac/collection.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ def get_item(self, id: str, recursive: bool = False) -> Item | None:
734734
raise e
735735

736736
@property
737-
def item_assets(self) -> dict[str, ItemAssetDefinition] | None:
737+
def item_assets(self) -> dict[str, ItemAssetDefinition]:
738738
"""Accessor for `item_assets
739739
<https://github.com/radiantearth/stac-spec/blob/v1.1.0/collection-spec/collection-spec.md#item_assets>`__
740740
on this collection.
@@ -763,14 +763,14 @@ def item_assets(self) -> dict[str, ItemAssetDefinition] | None:
763763
764764
.. code-block:: python
765765
766-
>>> collection.item_assets["B4"] = {
767-
'type': 'image/tiff; application=geotiff; profile=cloud-optimized',
768-
'eo:bands': [{'name': 'B4', 'common_name': 'red'}]
769-
}
770-
>>> collection.item_assets["B4"].owner == collection
771-
True
766+
>>> collection.item_assets["B4"] = {
767+
'type': 'image/tiff; application=geotiff; profile=cloud-optimized',
768+
'eo:bands': [{'name': 'B4', 'common_name': 'red'}]
769+
}
770+
>>> collection.item_assets["B4"].owner == collection
771+
True
772772
"""
773-
if self._item_assets is None and "item_assets" in self.extra_fields:
773+
if self._item_assets is None:
774774
self._item_assets = _ItemAssets(self)
775775
return self._item_assets
776776

pystac/extensions/item_assets.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,21 @@
2222

2323

2424
class AssetDefinition(ItemAssetDefinition):
25+
"""
26+
DEPRECATED
27+
28+
.. deprecated:: 1.12.0
29+
Use :class:`~pystac.ItemAssetDefinition` instead.
30+
"""
31+
2532
def __init__(cls, *args: Any, **kwargs: Any) -> None:
26-
# TODO: deprecation warning in here.
33+
warnings.warn(
34+
(
35+
"``AssetDefinition`` is deprecated. "
36+
"Please use ``pystac.ItemAssetDefinition`` instead."
37+
),
38+
DeprecationWarning,
39+
)
2740
super().__init__(*args, **kwargs)
2841

2942

tests/test_item_assets.py

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from pystac import Collection
66
from pystac.errors import DeprecatedWarning
7-
from pystac.extensions.item_assets import ItemAssetsExtension
7+
from pystac.extensions.item_assets import AssetDefinition, ItemAssetsExtension
88
from pystac.item_assets import ItemAssetDefinition
99
from tests.utils import TestCases
1010

@@ -23,7 +23,6 @@ def setUp(self) -> None:
2323
def test_example(self) -> None:
2424
collection = self.collection.clone()
2525

26-
assert collection.item_assets
2726
self.assertEqual(len(collection.item_assets), 13)
2827

2928
self.assertEqual(
@@ -48,7 +47,6 @@ def test_example(self) -> None:
4847
def test_set_using_dict(self) -> None:
4948
collection = self.collection.clone()
5049

51-
assert collection.item_assets
5250
self.assertEqual(len(collection.item_assets), 13)
5351

5452
collection.item_assets["Bx"] = {
@@ -75,6 +73,9 @@ def setUp(self) -> None:
7573
TestCases.get_path("data-files/item-assets/example-landsat8.json")
7674
)
7775

76+
def test_eq(self) -> None:
77+
assert self.collection.item_assets["B1"] != {"title": "Coastal Band (B1)"}
78+
7879
def test_create(self) -> None:
7980
title = "Coastal Band (B1)"
8081
description = "Coastal Band Top Of the Atmosphere"
@@ -124,6 +125,25 @@ def test_roles(self) -> None:
124125
self.assertEqual(asset_defn.roles, roles)
125126
self.assertEqual(asset_defn.to_dict()["roles"], roles)
126127

128+
def test_set_owner(self) -> None:
129+
asset_definition = ItemAssetDefinition(
130+
{
131+
"type": "image/tiff; application=geotiff",
132+
"eo:bands": [
133+
{
134+
"name": "B1",
135+
"common_name": "coastal",
136+
"center_wavelength": 0.44,
137+
"full_width_half_max": 0.02,
138+
}
139+
],
140+
"title": "Coastal Band (B1)",
141+
"description": "Coastal Band Top Of the Atmosphere",
142+
}
143+
)
144+
asset_definition.set_owner(self.collection)
145+
assert asset_definition.owner == self.collection
146+
127147

128148
def test_extra_fields(collection: Collection) -> None:
129149
asset_definition = ItemAssetDefinition.create(
@@ -133,7 +153,10 @@ def test_extra_fields(collection: Collection) -> None:
133153
roles=None,
134154
extra_fields={"raster:bands": [{"nodata": 42}]},
135155
)
156+
136157
collection.item_assets = {"data": asset_definition}
158+
assert collection.item_assets["data"].owner == collection
159+
137160
collection_as_dict = collection.to_dict()
138161
assert collection_as_dict["item_assets"]["data"]["raster:bands"] == [{"nodata": 42}]
139162
asset = asset_definition.create_asset("asset.tif")
@@ -147,19 +170,65 @@ def test_extra_fields(collection: Collection) -> None:
147170
assert collection.ext.has("raster")
148171

149172

173+
def test_set_item_asset(collection: Collection) -> None:
174+
asset_definition = ItemAssetDefinition.create(
175+
title=None,
176+
description=None,
177+
media_type=None,
178+
roles=None,
179+
extra_fields={"raster:bands": [{"nodata": 42}]},
180+
)
181+
182+
collection.item_assets["data"] = asset_definition
183+
assert collection.item_assets["data"].owner == collection
184+
185+
150186
def test_item_assets_extension_is_deprecated() -> None:
151187
collection = Collection.from_file(CLASSIFICATION_COLLECTION_RASTER_URI)
152188

153189
assert ItemAssetsExtension.get_schema_uri() not in collection.stac_extensions
154190

155191
with pytest.warns(DeprecatedWarning, match="top-level property of"):
156-
item_asset = ItemAssetsExtension.ext(
157-
collection, add_if_missing=True
158-
).item_assets["cloud-mask-raster"]
192+
item_asset_ext = ItemAssetsExtension.ext(collection, add_if_missing=True)
193+
item_asset = item_asset_ext.item_assets["cloud-mask-raster"]
194+
195+
assert collection.id in repr(item_asset_ext)
159196

160197
assert item_asset.ext.has("eo")
161198

162199
with pytest.warns(DeprecatedWarning, match="top-level property of"):
163200
assert collection.ext.item_assets["cloud-mask-raster"].ext.has("eo")
164201

165202
assert ItemAssetsExtension.get_schema_uri() in collection.stac_extensions
203+
204+
with pytest.warns(DeprecationWarning):
205+
asset_definition = AssetDefinition(
206+
{"title": "Thumbnail image", "type": "image/jpeg"}
207+
)
208+
item_asset_ext.item_assets["thumbnail"] = asset_definition
209+
210+
211+
def test_item_assets_extension_asset_definition_is_deprecated() -> None:
212+
with pytest.warns(
213+
DeprecationWarning, match="Please use ``pystac.ItemAssetDefinition``"
214+
):
215+
asset_definition = AssetDefinition(
216+
{
217+
"type": "image/tiff; application=geotiff",
218+
"eo:bands": [
219+
{
220+
"name": "B1",
221+
"common_name": "coastal",
222+
"center_wavelength": 0.44,
223+
"full_width_half_max": 0.02,
224+
}
225+
],
226+
"title": "Coastal Band (B1)",
227+
"description": "Coastal Band Top Of the Atmosphere",
228+
}
229+
)
230+
231+
assert asset_definition.title == "Coastal Band (B1)"
232+
assert asset_definition.ext.eo.bands
233+
assert asset_definition.ext.eo.bands[0].name == "B1"
234+
assert asset_definition.owner is None

0 commit comments

Comments
 (0)