11import json
22import os
3- import unittest
43from datetime import datetime
54from pathlib import Path
65from tempfile import TemporaryDirectory
7- from typing import Any
6+ from typing import Any , cast
87
98import pytest
109
1716
1817TEST_DATETIME : datetime = datetime (2020 , 3 , 14 , 16 , 32 )
1918
19+
2020def test_path_like () -> None :
2121 rel = "some-rel"
2222 target = os .path .abspath ("../elsewhere" )
2323 link = pystac .Link (rel , target )
2424 assert os .fspath (link ) == make_posix_style (target )
2525
26+
2627def test_minimal (item : pystac .Item ) -> None :
2728 rel = "my rel"
2829 target = "https://example.com/a/b"
@@ -72,6 +73,7 @@ def test_relative() -> None:
7273 }
7374 assert expected_dict == link .to_dict ()
7475
76+
7577def test_link_does_not_fail_if_href_is_none (item : pystac .Item ) -> None :
7678 """Test to ensure get_href does not fail when the href is None."""
7779 catalog = pystac .Catalog (id = "test" , description = "test desc" )
@@ -82,10 +84,12 @@ def test_link_does_not_fail_if_href_is_none(item: pystac.Item) -> None:
8284 assert link is not None
8385 assert link .get_href () is None
8486
87+
8588def test_resolve_stac_object_no_root_and_target_is_item (item : pystac .Item ) -> None :
8689 link = pystac .Link ("my rel" , target = item )
8790 link .resolve_stac_object ()
8891
92+
8993@pytest .mark .skipif (os .name == "nt" , reason = "Non-windows test" )
9094def test_resolve_stac_object_throws_informative_error () -> None :
9195 link = pystac .Link ("root" , target = "/a/b/foo.json" )
@@ -94,6 +98,7 @@ def test_resolve_stac_object_throws_informative_error() -> None:
9498 ):
9599 link .resolve_stac_object ()
96100
101+
97102def test_resolved_self_href () -> None :
98103 catalog = pystac .Catalog (id = "test" , description = "test desc" )
99104 with TemporaryDirectory () as temporary_directory :
@@ -105,6 +110,7 @@ def test_resolved_self_href() -> None:
105110 link .resolve_stac_object ()
106111 assert link .get_absolute_href () == make_posix_style (path )
107112
113+
108114def test_target_getter_setter (item : pystac .Item ) -> None :
109115 link = pystac .Link ("my rel" , target = "./foo/bar.json" )
110116 assert link .target == "./foo/bar.json"
@@ -117,12 +123,14 @@ def test_target_getter_setter(item: pystac.Item) -> None:
117123 link .target = "./bar/foo.json"
118124 assert link .target == "./bar/foo.json"
119125
126+
120127def test_get_target_str_no_href (item : pystac .Item ) -> None :
121128 item .remove_links ("self" )
122129 link = pystac .Link ("self" , target = item )
123130 item .add_link (link )
124131 assert link .get_target_str () is None
125132
133+
126134def test_relative_self_href (item : pystac .Item ) -> None :
127135 with TemporaryDirectory () as temporary_directory :
128136 pystac .write_file (
@@ -133,13 +141,14 @@ def test_relative_self_href(item: pystac.Item) -> None:
133141 previous = os .getcwd ()
134142 try :
135143 os .chdir (temporary_directory )
136- item = pystac .read_file ("item.json" )
144+ item = cast ( pystac .Item , pystac . read_file ("item.json" ) )
137145 href = item .get_self_href ()
138146 assert href
139147 assert os .path .isabs (href ), f"Not an absolute path: { href } "
140148 finally :
141149 os .chdir (previous )
142150
151+
143152def test_auto_title_when_resolved (item : pystac .Item ) -> None :
144153 extent = pystac .Extent .from_items ([item ])
145154 collection = pystac .Collection (
@@ -152,6 +161,7 @@ def test_auto_title_when_resolved(item: pystac.Item) -> None:
152161
153162 assert collection .title == link .title
154163
164+
155165def test_auto_title_not_found (item : pystac .Item ) -> None :
156166 extent = pystac .Extent .from_items ([item ])
157167 collection = pystac .Collection (
@@ -163,6 +173,7 @@ def test_auto_title_not_found(item: pystac.Item) -> None:
163173
164174 assert link .title is None
165175
176+
166177def test_auto_title_is_serialized (item : pystac .Item ) -> None :
167178 extent = pystac .Extent .from_items ([item ])
168179 collection = pystac .Collection (
@@ -175,13 +186,13 @@ def test_auto_title_is_serialized(item: pystac.Item) -> None:
175186
176187 assert link .to_dict ().get ("title" ) == collection .title
177188
189+
178190def test_no_auto_title_if_not_resolved () -> None :
179- link = pystac .Link (
180- "my rel" , target = "https://www.some-domain.com/path/to/thing.txt"
181- )
191+ link = pystac .Link ("my rel" , target = "https://www.some-domain.com/path/to/thing.txt" )
182192
183193 assert link .title is None
184194
195+
185196def test_title_as_init_argument (item : pystac .Item ) -> None :
186197 link_title = "Link title"
187198 extent = pystac .Extent .from_items ([item ])
@@ -196,6 +207,7 @@ def test_title_as_init_argument(item: pystac.Item) -> None:
196207 assert link .title == link_title
197208 assert link .to_dict ().get ("title" ) == link_title
198209
210+
199211def test_serialize_link () -> None :
200212 href = "https://some-domain/path/to/item.json"
201213 title = "A Test Link"
@@ -207,6 +219,7 @@ def test_serialize_link() -> None:
207219 assert link_dict ["title" ] == title
208220 assert link_dict ["href" ] == href
209221
222+
210223def test_static_from_dict_round_trip () -> None :
211224 test_cases : list [dict [str , Any ]] = [
212225 {"rel" : "" , "href" : "" }, # Not valid, but works.
@@ -221,27 +234,32 @@ def test_static_from_dict_round_trip() -> None:
221234 d2 = {"rel" : "self" , "href" : make_posix_style (os .path .join (os .getcwd (), "t" ))}
222235 assert pystac .Link .from_dict (d ).to_dict () == d2
223236
237+
224238def test_static_from_dict_failures () -> None :
225239 dicts : list [dict [str , Any ]] = [{}, {"href" : "t" }, {"rel" : "r" }]
226240 for d in dicts :
227241 with pytest .raises (KeyError ):
228242 pystac .Link .from_dict (d )
229243
244+
230245def test_static_collection (collection : pystac .Collection ) -> None :
231246 link = pystac .Link .collection (collection )
232247 expected = {"rel" : "collection" , "href" : None , "type" : "application/json" }
233248 assert expected == link .to_dict ()
234249
250+
235251def test_static_child (collection : pystac .Collection ) -> None :
236252 link = pystac .Link .child (collection )
237253 expected = {"rel" : "child" , "href" : None , "type" : "application/json" }
238254 assert expected == link .to_dict ()
239255
256+
240257def test_static_canonical_item (item : pystac .Item ) -> None :
241258 link = pystac .Link .canonical (item )
242259 expected = {"rel" : "canonical" , "href" : None , "type" : "application/json" }
243260 assert expected == link .to_dict ()
244261
262+
245263def test_static_canonical_collection (collection : pystac .Collection ) -> None :
246264 link = pystac .Link .canonical (collection )
247265 expected = {"rel" : "canonical" , "href" : None , "type" : "application/json" }
@@ -251,28 +269,34 @@ def test_static_canonical_collection(collection: pystac.Collection) -> None:
251269class CustomLink (pystac .Link ):
252270 pass
253271
272+
254273def test_inheritance_from_dict () -> None :
255274 link = CustomLink .from_dict (
256275 {"rel" : "r" , "href" : "t" , "type" : "a/b" , "title" : "t" , "c" : "d" , "1" : 2 }
257276 )
258277 assert isinstance (link , CustomLink )
259278
279+
260280def test_inheritance_collection (collection : Collection ) -> None :
261281 link = CustomLink .collection (collection )
262282 assert isinstance (link , CustomLink )
263283
284+
264285def test_inheritance_child (collection : Collection ) -> None :
265286 link = CustomLink .child (collection )
266287 assert isinstance (link , CustomLink )
267288
289+
268290def test_inheritance_canonical_item (item : Item ) -> None :
269291 link = CustomLink .canonical (item )
270292 assert isinstance (link , CustomLink )
271293
294+
272295def test_inheritance_canonical_collection (collection : Collection ) -> None :
273296 link = CustomLink .canonical (collection )
274297 assert isinstance (link , CustomLink )
275298
299+
276300def test_inheritance_clone () -> None :
277301 link = CustomLink .from_dict (
278302 {"rel" : "r" , "href" : "t" , "type" : "a/b" , "title" : "t" , "c" : "d" , "1" : 2 }
0 commit comments