forked from stac-utils/pystac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_link.py
307 lines (250 loc) · 10.6 KB
/
test_link.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import datetime
import os
import unittest
from tempfile import TemporaryDirectory
from typing import Any, Dict, List
import pystac
from tests.utils.test_cases import ARBITRARY_EXTENT
TEST_DATETIME: datetime.datetime = datetime.datetime(2020, 3, 14, 16, 32)
class LinkTest(unittest.TestCase):
item: pystac.Item
def setUp(self) -> None:
self.item = pystac.Item(
id="test-item",
geometry=None,
bbox=None,
datetime=TEST_DATETIME,
properties={},
)
def test_path_like(self) -> None:
rel = "some-rel"
target = os.path.abspath("../elsewhere")
link = pystac.Link(rel, target)
self.assertEqual(os.fspath(link), target)
def test_minimal(self) -> None:
rel = "my rel"
target = "https://example.com/a/b"
link = pystac.Link(rel, target)
self.assertEqual(target, link.get_href())
self.assertEqual(target, link.get_absolute_href())
expected_repr = f"<Link rel={rel} target={target}>"
self.assertEqual(expected_repr, link.__repr__())
self.assertFalse(link.is_resolved())
expected_dict = {"rel": rel, "href": target}
self.assertEqual(expected_dict, link.to_dict())
# Run the same tests on the clone.
clone = link.clone()
self.assertNotEqual(link, clone)
self.assertEqual(target, clone.get_href())
self.assertEqual(target, clone.get_absolute_href())
self.assertEqual(expected_repr, clone.__repr__())
self.assertEqual(expected_dict, clone.to_dict())
# Try the modification methods.
self.assertIsNone(link.owner)
link.set_owner(None)
self.assertIsNone(link.owner)
link.set_owner(self.item)
self.assertEqual(self.item, link.owner)
def test_relative(self) -> None:
rel = "my rel"
target = "../elsewhere"
mime_type = "example/stac_thing"
link = pystac.Link(rel, target, mime_type, "a title", extra_fields={"a": "b"})
expected_dict = {
"rel": rel,
"href": target,
"type": "example/stac_thing",
"title": "a title",
"a": "b",
}
self.assertEqual(expected_dict, link.to_dict())
def test_link_does_not_fail_if_href_is_none(self) -> None:
"""Test to ensure get_href does not fail when the href is None."""
catalog = pystac.Catalog(id="test", description="test desc")
catalog.add_item(self.item)
catalog.set_self_href("/some/href")
link = catalog.get_single_link("item")
assert link is not None
self.assertIsNone(link.get_href())
def test_resolve_stac_object_no_root_and_target_is_item(self) -> None:
link = pystac.Link("my rel", target=self.item)
link.resolve_stac_object()
def test_resolved_self_href(self) -> None:
catalog = pystac.Catalog(id="test", description="test desc")
with TemporaryDirectory() as temporary_directory:
catalog.normalize_and_save(temporary_directory)
path = os.path.join(temporary_directory, "catalog.json")
catalog = pystac.Catalog.from_file(path)
link = catalog.get_single_link(pystac.RelType.SELF)
assert link
link.resolve_stac_object()
self.assertEqual(link.get_absolute_href(), path)
def test_target_getter_setter(self) -> None:
link = pystac.Link("my rel", target="./foo/bar.json")
self.assertEqual(link.target, "./foo/bar.json")
self.assertEqual(link.get_target_str(), "./foo/bar.json")
link.target = self.item
self.assertEqual(link.target, self.item)
self.assertEqual(link.get_target_str(), self.item.get_self_href())
link.target = "./bar/foo.json"
self.assertEqual(link.target, "./bar/foo.json")
def test_get_target_str_no_href(self) -> None:
self.item.remove_links("self")
link = pystac.Link("self", target=self.item)
self.item.add_link(link)
self.assertIsNone(link.get_target_str())
def test_relative_self_href(self) -> None:
with TemporaryDirectory() as temporary_directory:
pystac.write_file(
self.item,
include_self_link=False,
dest_href=os.path.join(temporary_directory, "item.json"),
)
previous = os.getcwd()
try:
os.chdir(temporary_directory)
item = pystac.read_file("item.json")
href = item.get_self_href()
assert href
self.assertTrue(os.path.isabs(href), f"Not an absolute path: {href}")
finally:
os.chdir(previous)
def test_auto_title_when_resolved(self) -> None:
extent = pystac.Extent.from_items([self.item])
collection = pystac.Collection(
id="my_collection",
description="Test Collection",
extent=extent,
title="Collection Title",
)
link = pystac.Link("my rel", target=collection)
self.assertEqual(collection.title, link.title)
def test_auto_title_not_found(self) -> None:
extent = pystac.Extent.from_items([self.item])
collection = pystac.Collection(
id="my_collection",
description="Test Collection",
extent=extent,
)
link = pystac.Link("my rel", target=collection)
self.assertEqual(None, link.title)
def test_auto_title_is_serialized(self) -> None:
extent = pystac.Extent.from_items([self.item])
collection = pystac.Collection(
id="my_collection",
description="Test Collection",
extent=extent,
title="Collection Title",
)
link = pystac.Link("my rel", target=collection)
assert link.to_dict().get("title") == collection.title
def test_no_auto_title_if_not_resolved(self) -> None:
link = pystac.Link(
"my rel", target="https://www.some-domain.com/path/to/thing.txt"
)
assert link.title is None
def test_title_as_init_argument(self) -> None:
link_title = "Link title"
extent = pystac.Extent.from_items([self.item])
collection = pystac.Collection(
id="my_collection",
description="Test Collection",
extent=extent,
title="Collection Title",
)
link = pystac.Link("my rel", title=link_title, target=collection)
assert link.title == link_title
assert link.to_dict().get("title") == link_title
def test_serialize_link(self) -> None:
href = "https://some-domain/path/to/item.json"
title = "A Test Link"
link = pystac.Link(pystac.RelType.SELF, href, pystac.MediaType.JSON, title)
link_dict = link.to_dict()
self.assertEqual(str(link_dict["rel"]), "self")
self.assertEqual(str(link_dict["type"]), "application/json")
self.assertEqual(link_dict["title"], title)
self.assertEqual(link_dict["href"], href)
class StaticLinkTest(unittest.TestCase):
def setUp(self) -> None:
self.item = pystac.Item(
id="test-item",
geometry=None,
bbox=None,
datetime=TEST_DATETIME,
properties={},
)
self.collection = pystac.Collection(
"collection id", "desc", extent=ARBITRARY_EXTENT
)
def test_from_dict_round_trip(self) -> None:
test_cases: List[Dict[str, Any]] = [
{"rel": "", "href": ""}, # Not valid, but works.
{"rel": "r", "href": "t"},
{"rel": "r", "href": "/t"},
{"rel": "r", "href": "t", "type": "a/b", "title": "t", "c": "d", "1": 2},
]
for d in test_cases:
d2 = pystac.Link.from_dict(d).to_dict()
self.assertEqual(d, d2)
d = {"rel": "self", "href": "t"}
d2 = {"rel": "self", "href": os.path.join(os.getcwd(), "t")}
self.assertEqual(pystac.Link.from_dict(d).to_dict(), d2)
def test_from_dict_failures(self) -> None:
dicts: List[Dict[str, Any]] = [{}, {"href": "t"}, {"rel": "r"}]
for d in dicts:
with self.assertRaises(KeyError):
pystac.Link.from_dict(d)
def test_collection(self) -> None:
link = pystac.Link.collection(self.collection)
expected = {"rel": "collection", "href": None, "type": "application/json"}
self.assertEqual(expected, link.to_dict())
def test_child(self) -> None:
link = pystac.Link.child(self.collection)
expected = {"rel": "child", "href": None, "type": "application/json"}
self.assertEqual(expected, link.to_dict())
def test_canonical_item(self) -> None:
link = pystac.Link.canonical(self.item)
expected = {"rel": "canonical", "href": None, "type": "application/json"}
self.assertEqual(expected, link.to_dict())
def test_canonical_collection(self) -> None:
link = pystac.Link.canonical(self.collection)
expected = {"rel": "canonical", "href": None, "type": "application/json"}
self.assertEqual(expected, link.to_dict())
class LinkInheritanceTest(unittest.TestCase):
def setUp(self) -> None:
self.maxDiff = None
self.collection = pystac.Collection(
"collection id", "desc", extent=ARBITRARY_EXTENT
)
self.item = pystac.Item(
id="test-item",
geometry=None,
bbox=None,
datetime=TEST_DATETIME,
properties={},
)
class CustomLink(pystac.Link):
pass
def test_from_dict(self) -> None:
link = self.CustomLink.from_dict(
{"rel": "r", "href": "t", "type": "a/b", "title": "t", "c": "d", "1": 2}
)
self.assertIsInstance(link, self.CustomLink)
def test_collection(self) -> None:
link = self.CustomLink.collection(self.collection)
self.assertIsInstance(link, self.CustomLink)
def test_child(self) -> None:
link = self.CustomLink.child(self.collection)
self.assertIsInstance(link, self.CustomLink)
def test_canonical_item(self) -> None:
link = self.CustomLink.canonical(self.item)
self.assertIsInstance(link, self.CustomLink)
def test_canonical_collection(self) -> None:
link = self.CustomLink.canonical(self.collection)
self.assertIsInstance(link, self.CustomLink)
def test_clone(self) -> None:
link = self.CustomLink.from_dict(
{"rel": "r", "href": "t", "type": "a/b", "title": "t", "c": "d", "1": 2}
)
cloned_link = link.clone()
self.assertIsInstance(cloned_link, self.CustomLink)