Skip to content

Commit

Permalink
Make dateutil optional for test
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Feb 9, 2023
1 parent a40cf6e commit 270f805
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions test/test_datetime.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import datetime
import sys

import pytest
from dateutil import tz

import orjson

try:
import zoneinfo
except ImportError:
_ = zoneinfo.ZoneInfo("Europe/Amsterdam")
except Exception: # ImportError,ZoneInfoNotFoundError
zoneinfo = None # type: ignore

try:
Expand All @@ -23,8 +22,10 @@
except ImportError:
pendulum = None # type: ignore

if sys.version_info >= (3, 9):
import zoneinfo
try:
from dateutil import tz
except ImportError:
tz = None


AMSTERDAM_1937_DATETIMES = (
Expand Down Expand Up @@ -108,6 +109,7 @@ def test_datetime_two_digits(self):
== b'["0046-01-01T00:00:00+00:00"]'
)

@pytest.mark.skipif(tz is None, reason="dateutil optional")
def test_datetime_tz_assume(self):
"""
datetime.datetime tz with assume UTC uses tz
Expand Down Expand Up @@ -149,10 +151,7 @@ def test_datetime_pytz_utc(self):
== b'["2018-06-01T02:03:04+00:00"]'
)

@pytest.mark.skipif(
sys.version_info < (3, 9) or sys.platform.startswith("win"),
reason="zoneinfo not available",
)
@pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available")
def test_datetime_zoneinfo_utc(self):
"""
zoneinfo.ZoneInfo("UTC")
Expand All @@ -168,10 +167,7 @@ def test_datetime_zoneinfo_utc(self):
== b'["2018-06-01T02:03:04+00:00"]'
)

@pytest.mark.skipif(
sys.version_info < (3, 9) or sys.platform.startswith("win"),
reason="zoneinfo not available",
)
@pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available")
def test_datetime_zoneinfo_positive(self):
assert (
orjson.dumps(
Expand All @@ -191,10 +187,7 @@ def test_datetime_zoneinfo_positive(self):
== b'["2018-01-01T02:03:04+08:00"]'
)

@pytest.mark.skipif(
sys.version_info < (3, 9) or sys.platform.startswith("win"),
reason="zoneinfo not available",
)
@pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available")
def test_datetime_zoneinfo_negative(self):
assert (
orjson.dumps(
Expand Down Expand Up @@ -226,6 +219,7 @@ def test_datetime_pendulum_utc(self):
== b'["2018-06-01T02:03:04+00:00"]'
)

@pytest.mark.skipif(tz is None, reason="dateutil optional")
def test_datetime_arrow_positive(self):
"""
datetime.datetime positive UTC
Expand Down Expand Up @@ -319,10 +313,7 @@ def test_datetime_pendulum_negative_dst(self):
== b'["2018-06-01T02:03:04-04:00"]'
)

@pytest.mark.skipif(
sys.version_info < (3, 9) or sys.platform.startswith("win"),
reason="zoneinfo not available",
)
@pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available")
def test_datetime_zoneinfo_negative_non_dst(self):
"""
datetime.datetime negative UTC non-DST
Expand Down Expand Up @@ -391,10 +382,7 @@ def test_datetime_pendulum_negative_non_dst(self):
== b'["2018-12-01T02:03:04-05:00"]'
)

@pytest.mark.skipif(
sys.version_info < (3, 9) or sys.platform.startswith("win"),
reason="zoneinfo not available",
)
@pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available")
def test_datetime_zoneinfo_partial_hour(self):
"""
datetime.datetime UTC offset partial hour
Expand Down Expand Up @@ -488,10 +476,7 @@ def test_datetime_partial_second_pendulum_supported(self):
in AMSTERDAM_1937_DATETIMES
)

@pytest.mark.skipif(
sys.version_info < (3, 9) or sys.platform.startswith("win"),
reason="zoneinfo not available",
)
@pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available")
def test_datetime_partial_second_zoneinfo(self):
"""
datetime.datetime UTC offset round seconds
Expand Down Expand Up @@ -541,6 +526,7 @@ def test_datetime_partial_second_pytz(self):
in AMSTERDAM_1937_DATETIMES
)

@pytest.mark.skipif(tz is None, reason="dateutil optional")
def test_datetime_partial_second_dateutil(self):
"""
datetime.datetime UTC offset round seconds
Expand Down Expand Up @@ -648,6 +634,7 @@ def test_datetime_utc_z_without_tz(self):
== b'["2000-01-01T02:03:04.000123"]'
)

@pytest.mark.skipif(tz is None, reason="dateutil optional")
def test_datetime_utc_z_with_tz(self):
"""
datetime.datetime naive OPT_UTC_Z
Expand Down Expand Up @@ -741,13 +728,18 @@ def test_time(self):
assert orjson.dumps([datetime.time(12, 15, 59, 111)]) == b'["12:15:59.000111"]'
assert orjson.dumps([datetime.time(12, 15, 59)]) == b'["12:15:59"]'

@pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available")
def test_time_tz(self):
"""
datetime.time with tzinfo error
"""
with pytest.raises(orjson.JSONEncodeError):
orjson.dumps(
[datetime.time(12, 15, 59, 111, tzinfo=tz.gettz("Asia/Shanghai"))]
[
datetime.time(
12, 15, 59, 111, tzinfo=zoneinfo.ZoneInfo("Asia/Shanghai")
)
]
)

def test_time_microsecond_max(self):
Expand Down

0 comments on commit 270f805

Please sign in to comment.