44
55from pystac import Collection
66from pystac .errors import DeprecatedWarning
7- from pystac .extensions .item_assets import ItemAssetsExtension
7+ from pystac .extensions .item_assets import AssetDefinition , ItemAssetsExtension
88from pystac .item_assets import ItemAssetDefinition
99from 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
128148def 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+
150186def 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