forked from stac-utils/pystac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
500 lines (445 loc) · 17.1 KB
/
test_utils.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import json
import os
import time
import unittest
from datetime import datetime, timedelta, timezone
import pytest
from dateutil import tz
from pystac import utils
from pystac.utils import (
JoinType,
StringEnum,
is_absolute_href,
is_file_path,
join_path_or_url,
make_absolute_href,
make_relative_href,
now_in_utc,
now_to_rfc3339_str,
safe_urlparse,
str_to_datetime,
)
from tests.utils import TestCases, path_includes_drive_letter
class UtilsTest(unittest.TestCase):
def test_make_relative_href(self) -> None:
# Test cases of (source_href, start_href, expected)
test_cases = [
("/a/b/c/d/catalog.json", "/a/b/c/catalog.json", "./d/catalog.json"),
("/a/b/catalog.json", "/a/b/c/catalog.json", "../catalog.json"),
("/a/catalog.json", "/a/b/c/catalog.json", "../../catalog.json"),
("/a/b/c/d/", "/a/b/c/catalog.json", "./d/"),
("/a/b/c/d/.dotfile", "/a/b/c/d/catalog.json", "./.dotfile"),
]
for source_href, start_href, expected in test_cases:
actual = make_relative_href(source_href, start_href)
self.assertEqual(actual, expected)
def test_make_relative_href_url(self) -> None:
test_cases = [
(
"http://stacspec.org/a/b/c/d/catalog.json",
"http://stacspec.org/a/b/c/catalog.json",
"./d/catalog.json",
),
(
"http://stacspec.org/a/b/catalog.json",
"http://stacspec.org/a/b/c/catalog.json",
"../catalog.json",
),
(
"http://stacspec.org/a/catalog.json",
"http://stacspec.org/a/b/c/catalog.json",
"../../catalog.json",
),
(
"http://stacspec.org/a/catalog.json",
"http://cogeo.org/a/b/c/catalog.json",
"http://stacspec.org/a/catalog.json",
),
(
"http://stacspec.org/a/catalog.json",
"https://stacspec.org/a/b/c/catalog.json",
"http://stacspec.org/a/catalog.json",
),
(
"http://stacspec.org/a/",
"https://stacspec.org/a/b/c/catalog.json",
"http://stacspec.org/a/",
),
(
"http://stacspec.org/a/b/.dotfile",
"http://stacspec.org/a/b/catalog.json",
"./.dotfile",
),
]
for source_href, start_href, expected in test_cases:
actual = make_relative_href(source_href, start_href)
self.assertEqual(actual, expected)
def test_make_relative_href_windows(self) -> None:
# Test cases of (source_href, start_href, expected)
test_cases = [
(
"C:\\a\\b\\c\\d\\catalog.json",
"C:\\a\\b\\c\\catalog.json",
"./d/catalog.json",
),
(
"C:\\a\\b\\catalog.json",
"C:\\a\\b\\c\\catalog.json",
"../catalog.json",
),
(
"C:\\a\\catalog.json",
"C:\\a\\b\\c\\catalog.json",
"../../catalog.json",
),
("a\\b\\c\\catalog.json", "a\\b\\catalog.json", "./c/catalog.json"),
("a\\b\\catalog.json", "a\\b\\c\\catalog.json", "../catalog.json"),
(
"http://stacspec.org/a/b/c/d/catalog.json",
"http://stacspec.org/a/b/c/catalog.json",
"./d/catalog.json",
),
(
"http://stacspec.org/a/b/catalog.json",
"http://stacspec.org/a/b/c/catalog.json",
"../catalog.json",
),
(
"http://stacspec.org/a/catalog.json",
"http://stacspec.org/a/b/c/catalog.json",
"../../catalog.json",
),
(
"http://stacspec.org/a/catalog.json",
"http://cogeo.org/a/b/c/catalog.json",
"http://stacspec.org/a/catalog.json",
),
(
"http://stacspec.org/a/catalog.json",
"https://stacspec.org/a/b/c/catalog.json",
"http://stacspec.org/a/catalog.json",
),
]
for source_href, start_href, expected in test_cases:
actual = make_relative_href(source_href, start_href)
self.assertEqual(actual, expected)
def test_make_absolute_href(self) -> None:
# Test cases of (source_href, start_href, expected)
test_cases = [
("item.json", "/a/b/c/catalog.json", "/a/b/c/item.json"),
("./item.json", "/a/b/c/catalog.json", "/a/b/c/item.json"),
("./z/item.json", "/a/b/c/catalog.json", "/a/b/c/z/item.json"),
("../item.json", "/a/b/c/catalog.json", "/a/b/item.json"),
(
"item.json",
"https://stacspec.org/a/b/c/catalog.json",
"https://stacspec.org/a/b/c/item.json",
),
(
"./item.json",
"https://stacspec.org/a/b/c/catalog.json",
"https://stacspec.org/a/b/c/item.json",
),
(
"./z/item.json",
"https://stacspec.org/a/b/c/catalog.json",
"https://stacspec.org/a/b/c/z/item.json",
),
(
"../item.json",
"https://stacspec.org/a/b/c/catalog.json",
"https://stacspec.org/a/b/item.json",
),
("http://localhost:8000", None, "http://localhost:8000"),
]
for source_href, start_href, expected in test_cases:
actual = make_absolute_href(source_href, start_href)
_, actual = os.path.splitdrive(actual)
self.assertEqual(actual, expected)
def test_make_absolute_href_on_vsitar(self) -> None:
rel_path = "some/item.json"
cat_path = "/vsitar//tmp/catalog.tar/catalog.json"
expected = "/vsitar//tmp/catalog.tar/some/item.json"
self.assertEqual(expected, make_absolute_href(rel_path, cat_path))
@pytest.mark.skipif(os.name != "nt", reason="Windows only test")
def test_make_absolute_href_windows(self) -> None:
# Test cases of (source_href, start_href, expected)
test_cases = [
("item.json", "C:\\a\\b\\c\\catalog.json", "C:/a/b/c/item.json"),
(".\\item.json", "C:\\a\\b\\c\\catalog.json", "C:/a/b/c/item.json"),
(
".\\z\\item.json",
"Z:\\a\\b\\c\\catalog.json",
"Z:/a/b/c/z/item.json",
),
("..\\item.json", "a:\\a\\b\\c\\catalog.json", "a:/a/b/item.json"),
(
"item.json",
"HTTPS://stacspec.org/a/b/c/catalog.json",
"https://stacspec.org/a/b/c/item.json",
),
(
"./item.json",
"https://stacspec.org/a/b/c/catalog.json",
"https://stacspec.org/a/b/c/item.json",
),
(
"./z/item.json",
"https://stacspec.org/a/b/c/catalog.json",
"https://stacspec.org/a/b/c/z/item.json",
),
(
"../item.json",
"https://stacspec.org/a/b/c/catalog.json",
"https://stacspec.org/a/b/item.json",
),
]
for source_href, start_href, expected in test_cases:
actual = make_absolute_href(source_href, start_href)
self.assertEqual(actual, expected)
def test_is_absolute_href(self) -> None:
# Test cases of (href, expected)
test_cases = [
("item.json", False),
("./item.json", False),
("../item.json", False),
("http://stacspec.org/item.json", True),
]
for href, expected in test_cases:
actual = is_absolute_href(href)
self.assertEqual(actual, expected)
def test_is_absolute_href_os_aware(self) -> None:
# Test cases of (href, expected)
is_windows = os.name == "nt"
incl_drive_letter = path_includes_drive_letter()
test_cases = [
("/item.json", not incl_drive_letter),
("/home/someuser/Downloads/item.json", not incl_drive_letter),
("d:/item.json", is_windows),
("c:/files/more_files/item.json", is_windows),
]
for href, expected in test_cases:
actual = is_absolute_href(href)
self.assertEqual(actual, expected)
@pytest.mark.skipif(os.name != "nt", reason="Windows only test")
def test_is_absolute_href_windows(self) -> None:
# Test cases of (href, expected)
test_cases = [
("item.json", False),
(".\\item.json", False),
("..\\item.json", False),
("c:\\item.json", True),
("http://stacspec.org/item.json", True),
]
for href, expected in test_cases:
actual = is_absolute_href(href)
self.assertEqual(actual, expected)
def test_datetime_to_str(self) -> None:
cases = (
(
"timezone naive, assume utc",
datetime(2000, 1, 1),
"2000-01-01T00:00:00Z",
),
(
"timezone aware, utc",
datetime(2000, 1, 1, tzinfo=timezone.utc),
"2000-01-01T00:00:00Z",
),
(
"timezone aware, utc -7",
datetime(2000, 1, 1, tzinfo=timezone(timedelta(hours=-7))),
"2000-01-01T00:00:00-07:00",
),
)
for title, dt, expected in cases:
with self.subTest(title=title):
got = utils.datetime_to_str(dt)
self.assertEqual(expected, got)
def test_datetime_to_str_with_microseconds_timespec(self) -> None:
cases = (
(
"timezone naive, assume utc",
datetime(2000, 1, 1, 0, 0, 0, 0),
"2000-01-01T00:00:00.000000Z",
),
(
"timezone aware, utc",
datetime(2000, 1, 1, 0, 0, 0, 0, tzinfo=timezone.utc),
"2000-01-01T00:00:00.000000Z",
),
(
"timezone aware, utc -7",
datetime(2000, 1, 1, 0, 0, 0, 0, tzinfo=timezone(timedelta(hours=-7))),
"2000-01-01T00:00:00.000000-07:00",
),
)
for title, dt, expected in cases:
with self.subTest(title=title):
got = utils.datetime_to_str(dt, timespec="microseconds")
self.assertEqual(expected, got)
def test_str_to_datetime(self) -> None:
def _set_tzinfo(tz_str: str | None) -> None:
if tz_str is None:
if "TZ" in os.environ:
del os.environ["TZ"]
else:
os.environ["TZ"] = tz_str
# time.tzset() only available for Unix/Linux
if hasattr(time, "tzset"):
time.tzset()
utc_timestamp = "2015-06-27T10:25:31Z"
prev_tz = os.environ.get("TZ")
with self.subTest(tz=None):
_set_tzinfo(None)
utc_datetime = str_to_datetime(utc_timestamp)
self.assertIs(utc_datetime.tzinfo, tz.tzutc())
self.assertIsNot(utc_datetime.tzinfo, tz.tzlocal())
with self.subTest(tz="UTC"):
_set_tzinfo("UTC")
utc_datetime = str_to_datetime(utc_timestamp)
self.assertIs(utc_datetime.tzinfo, tz.tzutc())
self.assertIsNot(utc_datetime.tzinfo, tz.tzlocal())
with self.subTest(tz="US/Central"):
_set_tzinfo("US/Central")
utc_datetime = str_to_datetime(utc_timestamp)
self.assertIs(utc_datetime.tzinfo, tz.tzutc())
self.assertIsNot(utc_datetime.tzinfo, tz.tzlocal())
if prev_tz is not None:
_set_tzinfo(prev_tz)
def test_geojson_bbox(self) -> None:
# Use sample Geojson from https://en.wikipedia.org/wiki/GeoJSON
with open(
TestCases.get_path("data-files/geojson/sample.geojson")
) as sample_geojson:
all_features = json.load(sample_geojson)
geom_dicts = [f["geometry"] for f in all_features["features"]]
for geom in geom_dicts:
got = utils.geometry_to_bbox(geom)
self.assertNotEqual(got, None)
@pytest.mark.parametrize(
"datetime",
[
"37-01-01T12:00:27.87Z", # invalid year, must be 4 digits
"21985-12-12T23:20:50.52Z", # year must be 4 digits
"1985-13-12T23:20:50.52Z", # month > 12
"1985-12-32T23:20:50.52Z", # day > 31
"1985-12-01T25:20:50.52Z", # hour > 24
"1985-12-01T00:60:50.52Z", # minute > 59
"1985-12-01T00:06:61.52Z", # second > 60
"1985-04-12T23:20:50.Z", # fractional sec . but no frac secs
"1985-04-12T23:20:50,Z", # fractional sec , but no frac secs
"1990-12-31T23:59:61Z", # second > 60 w/o fractional seconds
],
)
def test_parse_invalid_rfc3339_str_to_datetime(datetime: str) -> None:
with pytest.raises(ValueError):
str_to_datetime(datetime)
@pytest.mark.parametrize(
"datetime",
[
"1985-04-12", # date only
"1937-01-01T12:00:27.87+0100", # invalid TZ format, no sep :
"1985-12-12T23:20:50.52", # no TZ
"1985-04-12T23:20:50,52Z", # comma as frac sec sep disallowed in RFC3339
],
)
def test_parse_invalid_rfc3339_but_valid_iso8601_str_to_datetime(datetime: str) -> None:
assert str_to_datetime(datetime)
@pytest.mark.parametrize(
"datetime",
[
"1985-04-12T23:20:50.52Z",
"1996-12-19T16:39:57-00:00",
"1996-12-19T16:39:57+00:00",
"1996-12-19T16:39:57-08:00",
"1996-12-19T16:39:57+08:00",
"1937-01-01T12:00:27.87+01:00",
"1985-04-12T23:20:50.52Z",
"1937-01-01T12:00:27.8710+01:00",
"1937-01-01T12:00:27.8+01:00",
"1937-01-01T12:00:27.8Z",
"2020-07-23T00:00:00.000+03:00",
"2020-07-23T00:00:00+03:00",
"1985-04-12t23:20:50.000z",
"2020-07-23T00:00:00Z",
"2020-07-23T00:00:00.0Z",
"2020-07-23T00:00:00.01Z",
"2020-07-23T00:00:00.012Z",
"2020-07-23T00:00:00.0123Z",
"2020-07-23T00:00:00.01234Z",
"2020-07-23T00:00:00.012345Z",
"2020-07-23T00:00:00.0123456Z",
"2020-07-23T00:00:00.01234567Z",
"2020-07-23T00:00:00.012345678Z",
],
)
def test_parse_valid_iso8601_str_to_datetime(datetime: str) -> None:
assert str_to_datetime(datetime)
def test_now_functions() -> None:
now1 = now_in_utc()
time.sleep(1)
now2 = now_in_utc()
assert now1 < now2
assert now1.tzinfo == timezone.utc
assert str_to_datetime(now_to_rfc3339_str())
@pytest.mark.parametrize(
"test_href,expected",
[
("https://some/address/page.html", JoinType.URL),
("C:\\some\\windows\\path\\file.json", JoinType.PATH),
("some\\windows\\path\\file.json", JoinType.PATH),
(".\\some\\windows\\path\\file.json", JoinType.PATH),
("C:/some/windows/path/file.json", JoinType.PATH),
("/some/posix/path/file.json", JoinType.PATH),
("./some/posix/path/file.json", JoinType.PATH),
("posix/path/file.json", JoinType.PATH),
],
)
def test_join_type(test_href: str, expected: JoinType) -> None:
parsed = safe_urlparse(test_href)
with pytest.warns(DeprecationWarning):
assert JoinType.from_parsed_uri(parsed) == expected
def test_join_path_or_url() -> None:
path_args = ["some", "path", "file.json"]
with pytest.warns(DeprecationWarning):
joined_path = join_path_or_url(JoinType.PATH, *path_args)
if os.name != "nt":
assert joined_path == "some/path/file.json"
else:
assert joined_path == "some\\path\\file.json"
url_args = ["https://some", "page", "file.html"]
with pytest.warns(DeprecationWarning):
joined_url = join_path_or_url(JoinType.URL, *url_args)
assert joined_url == "https://some/page/file.html"
@pytest.mark.parametrize(
"href,expected",
[
("path/to/file.txt", True),
("path/to/nofile", False),
("./path/to/file.txt", True),
("./path/to/nofile", False),
("./path/to/", False),
("/path/to/file.txt", True),
("/path/to/nofile", False),
("/path", False),
("/", False),
("D:/path/to/file.txt", True),
("D:/path/to/nofile", False),
("D:\\path\\to\\file.txt", True),
("D:\\path\\to\\file.txt", True),
("D:\\path\\to\\nofile", False),
("D:", False),
("D:\\", False),
("https://example.com/absolutepath/to/file.txt", True),
("https://example.com/absolutepath/to/nofile", False),
("https://example.com", False),
("https://example.com/", False),
],
)
def test_is_file_path(href: str, expected: bool) -> None:
assert is_file_path(href) == expected
def test_stringenum_repr() -> None:
class SomeEnum(StringEnum):
THIS = "this"
assert repr(SomeEnum.THIS) == "'this'"