test_datetime.py

Overview

`test_datetime.py` is a comprehensive test suite designed to validate the serialization of Python `datetime`, `date`, and `time` objects using the `orjson` library. The file contains multiple test classes, each focusing on different aspects of date and time serialization, including timezone handling, microsecond precision, edge cases (minimum and maximum values), and compatibility with various timezone libraries such as `zoneinfo`, `pytz`, `pendulum`, and `dateutil.tz`.

This suite ensures that `orjson` correctly serializes datetime-related objects into JSON-compliant ISO 8601 strings, respecting timezone offsets, handling optional serialization flags, and following RFC 3339 standards where applicable.


Detailed Explanations

Imported Modules and Setup

The code conditionally imports these timezone libraries and disables them if unavailable. This allows tests to run or skip accordingly.


Constants


Class: TestDatetime

This class contains tests for `datetime.datetime` objects serialization.

Key Methods:


Class: TestDate

Tests serialization of `datetime.date` objects.


Class: TestTime

Tests serialization of `datetime.time` objects.


Class: TestDateclassPassthrough

Tests behavior of `orjson.OPT_PASSTHROUGH_DATETIME` option.


Important Implementation Details and Algorithms


Interactions with Other Parts of the System


Usage Examples

import datetime
import orjson

# Serialize naive datetime (no timezone)
print(orjson.dumps([datetime.datetime(2024, 1, 1, 12, 0, 0)]))
# Output: b'["2024-01-01T12:00:00"]'

# Serialize naive datetime assuming UTC offset
print(
    orjson.dumps(
        [datetime.datetime(2024, 1, 1, 12, 0, 0)],
        option=orjson.OPT_NAIVE_UTC,
    )
)
# Output: b'["2024-01-01T12:00:00+00:00"]'

# Serialize datetime with timezone from zoneinfo
from zoneinfo import ZoneInfo
dt = datetime.datetime(2024, 1, 1, 12, 0, 0, tzinfo=ZoneInfo("Asia/Tokyo"))
print(orjson.dumps([dt]))
# Output: b'["2024-01-01T12:00:00+09:00"]'

Visual Diagram

classDiagram
    class TestDatetime {
        +test_datetime_naive()
        +test_datetime_naive_utc()
        +test_datetime_min()
        +test_datetime_max()
        +test_datetime_three_digits()
        +test_datetime_two_digits()
        +test_datetime_tz_assume()
        +test_datetime_timezone_utc()
        +test_datetime_pytz_utc()
        +test_datetime_zoneinfo_utc()
        +test_datetime_zoneinfo_positive()
        +test_datetime_zoneinfo_negative()
        +test_datetime_pendulum_utc()
        +test_datetime_arrow_positive()
        +test_datetime_pytz_positive()
        +test_datetime_pendulum_positive()
        +test_datetime_pytz_negative_dst()
        +test_datetime_pendulum_negative_dst()
        +test_datetime_zoneinfo_negative_non_dst()
        +test_datetime_pytz_negative_non_dst()
        +test_datetime_pendulum_negative_non_dst()
        +test_datetime_zoneinfo_partial_hour()
        +test_datetime_pytz_partial_hour()
        +test_datetime_pendulum_partial_hour()
        +test_datetime_partial_second_pendulum_supported()
        +test_datetime_partial_second_zoneinfo()
        +test_datetime_partial_second_pytz()
        +test_datetime_partial_second_dateutil()
        +test_datetime_microsecond_max()
        +test_datetime_microsecond_min()
        +test_datetime_omit_microseconds()
        +test_datetime_omit_microseconds_naive()
        +test_datetime_utc_z_naive_omit()
        +test_datetime_utc_z_naive()
        +test_datetime_utc_z_without_tz()
        +test_datetime_utc_z_with_tz()
        +test_datetime_roundtrip()
    }

    class TestDate {
        +test_date()
        +test_date_min()
        +test_date_max()
        +test_date_three_digits()
        +test_date_two_digits()
    }

    class TestTime {
        +test_time()
        +test_time_tz()
        +test_time_microsecond_max()
        +test_time_microsecond_min()
    }

    class TestDateclassPassthrough {
        +test_passthrough_datetime()
        +test_passthrough_date()
        +test_passthrough_time()
        +test_passthrough_datetime_default()
    }

    TestDatetime --> orjson : uses
    TestDate --> orjson : uses
    TestTime --> orjson : uses
    TestDateclassPassthrough --> orjson : uses

Summary

`test_datetime.py` is a pytest-based test suite for validating `orjson` serialization of datetime-related types across various scenarios including naive and timezone-aware datetimes, edge cases, and serialization options. It supports testing with multiple timezone libs and ensures JSON outputs conform to ISO 8601 and RFC 3339 standards. The suite is essential for maintaining the correctness and robustness of datetime serialization in `orjson`.