Skip to content

Commit

Permalink
tests: use pytest syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Feb 10, 2023
1 parent ac84bce commit e7a4d0d
Showing 1 changed file with 71 additions and 67 deletions.
138 changes: 71 additions & 67 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from datetime import datetime, timedelta, timezone
from typing import Optional

import pytest
from dateutil import tz

from pystac import utils
from pystac.utils import (
is_absolute_href,
iso8601_str_to_datetime,
make_absolute_href,
make_relative_href,
now_in_utc,
Expand Down Expand Up @@ -353,74 +353,78 @@ def test_geojson_bbox(self) -> None:
got = utils.geometry_to_bbox(geom)
self.assertNotEqual(got, None)

def test_parse_invalid_rfc3339_str_to_datetime(self) -> None:
invalid_datetimes = [
"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
]

for dt in invalid_datetimes:
try:
iso8601_str_to_datetime(dt)
self.fail(f"parsed {dt} as ISO 8601 datetime, but shouldn't have")
except ValueError:
pass

def test_parse_invalid_rfc3339_but_valid_iso8601_str_to_datetime(self) -> None:
invalid_datetimes = [
"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
]
@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)

for dt in invalid_datetimes:
self.assertTrue(iso8601_str_to_datetime(dt))

def test_parse_valid_iso8601_str_to_datetime(self) -> None:
valid_datetimes = [
"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",
]
for dt in valid_datetimes:
self.assertTrue(str_to_datetime(dt))
self.assertTrue(iso8601_str_to_datetime(dt))

def test_now_functions(self) -> None:
now1 = now_in_utc()
time.sleep(1)
now2 = now_in_utc()
def test_now_functions() -> None:
now1 = now_in_utc()
time.sleep(1)
now2 = now_in_utc()

self.assertTrue(now1 < now2)
self.assertTrue(now1.tzinfo == timezone.utc)
assert now1 < now2
assert now1.tzinfo == timezone.utc

self.assertTrue(iso8601_str_to_datetime(now_to_rfc3339_str()))
assert str_to_datetime(now_to_rfc3339_str())

0 comments on commit e7a4d0d

Please sign in to comment.