import datetime import uuid from dataclasses import dataclass from decimal import Decimal from typing import Any import pytest from fansifter_common.utils.encoders import JSONEncoder @dataclass class Record: id: str num: int @pytest.mark.parametrize( "o, expected", [ ( datetime.datetime(year=2022, month=4, day=10, tzinfo=datetime.UTC), "2022-04-10T00:00:00+00:00", ), ( datetime.datetime( year=2022, month=4, day=10, hour=6, minute=10, second=10, microsecond=100025, tzinfo=datetime.UTC, ), "2022-04-10T06:10:10.100025+00:00", ), (datetime.date(year=2022, month=4, day=10), "2022-04-10"), ( datetime.time( hour=6, minute=10, second=10, microsecond=100025, tzinfo=datetime.UTC, ), "06:10:10.100025+00:00", ), (datetime.timedelta(minutes=5), "300.0"), (Decimal("99.99"), "99.99"), ( uuid.UUID("3422b76f-3978-4adc-9896-58da61c3edb7"), "3422b76f-3978-4adc-9896-58da61c3edb7", ), (b"test", "test"), (Record(id="abc", num=100), {"id": "abc", "num": 100}), ], ) def test_json_encoder(o: Any, expected: Any) -> None: encoder = JSONEncoder() result = encoder.default(o) assert result == expected def test_json_encoder_invalid_type() -> None: encoder = JSONEncoder() with pytest.raises(TypeError): encoder.default(lambda: "invalid")