import json
import pytest
import pytz

from datetime import datetime
from psi_utils.json_utils import JsonDecoder, JsonEncoder


class TestJsonUtils_UnitTests:
    @pytest.mark.unit
    def test_json_encoder(self):
        expected = '{"dt": "2022-01-01 11:12:13.141516-06:00", "date": "2022-01-01"}'
        dt = datetime(2022, 1, 1, 11, 12, 13, 141516)
        dt = pytz.timezone("America/Chicago").localize(dt)
        actual = json.dumps({"dt": dt, "date": dt.date()}, cls=JsonEncoder)
        assert actual == expected

    @pytest.mark.unit
    def test_json_decorder(self):
        expected_dt = datetime(2022, 1, 1, 11, 12, 13, 141516)
        expected_dt_tz = pytz.timezone("America/Chicago").localize(expected_dt)
        expected_date = expected_dt.date()
        expected = {
            "str": "string",
            "date": expected_date,
            "dt": expected_dt,
            "dt_iso": expected_dt,
            "dt_tz": expected_dt_tz,
            "dt_tz_iso": expected_dt_tz,
            "bad_date": "2022-30-40",
            "bad_dt": "2022-30-40 11:12:13.141516",
        }
        actual = json.loads(
            json.dumps(
                {
                    "str": "string",
                    "date": "2022-01-01",
                    "dt": "2022-01-01 11:12:13.141516",
                    "dt_iso": "2022-01-01T11:12:13.141516",
                    "dt_tz": "2022-01-01 11:12:13.141516-06:00",
                    "dt_tz_iso": "2022-01-01T11:12:13.141516-06:00",
                    "bad_date": "2022-30-40",
                    "bad_dt": "2022-30-40 11:12:13.141516",
                }
            ),
            object_hook=JsonDecoder,
        )
        assert actual == expected
