# Path: tests/conftest.py
# def assert_naive_datetime_equal_to_tz_datetime(naive_datetime, tz_datetime):
#     # mktime appears to ignore microseconds, so do this manually
#     microseconds = int(time.mktime(naive_datetime.timetuple())) * 1000 * 1000
#     microseconds += naive_datetime.microsecond
#     aware_datetime = datetime(1970, 1, 1, tzinfo=timezone.utc) + timedelta(
#         microseconds=microseconds
#     )
#     assert aware_datetime == tz_datetime


import array
import datetime
import fastavro
from decimal import Decimal
from io import BytesIO
from uuid import uuid4
from .conftest import assert_naive_datetime_equal_to_tz_datetime


def deserialize(schema, binary):
    bytes_writer = BytesIO()
    bytes_writer.write(binary)
    bytes_writer.seek(0)

    res = fastavro.schemaless_reader(bytes_writer, schema)
    return res


def test_complex_schema():
    data1 = {
        "union_uuid": uuid4(),
        "array_string": ["a", "b", "c"],
        "multi_union_time": datetime.datetime.now(),
        "array_bytes_decimal": [Decimal("123.456")],
        "array_fixed_decimal": [Decimal("123.456")],
        "array_record": [{"f1": "1", "f2": Decimal("123.456")}],
    }
    binary = serialize(schema, data1)
    data2 = deserialize(schema, binary)
    assert len(data1) == len(data2)
    for field in [
        "array_string",
        "array_bytes_decimal",
        "array_fixed_decimal",
        "array_record",
    ]:
        assert data1[field] == data2[field]

# Path: tests/conftest.py
# def assert_naive_datetime_equal_to_tz_datetime(naive_datetime, tz_datetime):
#     # mktime appears to ignore microseconds, so do this manually
#     microseconds = int(time.mktime(naive_datetime.timetuple())) * 1000 * 1000
#     microseconds += naive_datetime.microsecond
#     aware_datetime = datetime(1970, 1, 1, tzinfo=timezone.utc) + timedelta(
#         microseconds=microseconds
#     )
#     assert aware_datetime == tz_datetime


# Path: tests/test_complex.py
import array
import datetime
import fastavro
from decimal import Decimal
from io import BytesIO
from uuid import uuid4
from .conftest import assert_naive_datetime_equal_to_tz_datetime



def deserialize(schema, binary):
    bytes_writer = BytesIO()
    bytes_writer.write(binary)
    bytes_writer.seek(0)

    res = fastavro.schemaless_reader(bytes_writer, schema)
    return res


def test_complex_schema():
    data1 = {
        "union_uuid": uuid4(),
        "array_string": ["a", "b", "c"],
        "multi_union_time": datetime.datetime.now(),
        "array_bytes_decimal": [Decimal("123.456")],
        "array_fixed_decimal": [Decimal("123.456")],
        "array_record": [{"f1": "1", "f2": Decimal("123.456")}],
    }
    binary = serialize(schema, data1)
    data2 = deserialize(schema, binary)
    assert len(data1) == len(data2)
    for field in [
        "array_string",
        "array_bytes_decimal",
        "array_fixed_decimal",
        "array_record",
    ]:
        assert data1[field] == data2[field]

    assert_naive_datetime_equal_to_tz_datetime(