import dataclasses

import pytest

from dataclasses_avroschema import fields

from . import consts


@pytest.mark.parametrize("primitive_type", fields.PYTHON_INMUTABLE_TYPES)
def test_primitive_types(primitive_type):
    name = "a_field"
    field = fields.Field(name, primitive_type, dataclasses.MISSING)
    avro_type = fields.PYTHON_TYPE_TO_AVRO[primitive_type]

    assert {"name": name, "type": avro_type} == field.to_dict()


@pytest.mark.parametrize("primitive_type", fields.PYTHON_INMUTABLE_TYPES)
def test_primitive_types_with_default_value_none(primitive_type):
    name = "a_field"
    field = fields.Field(name, primitive_type, None)
    avro_type = [fields.NULL, fields.PYTHON_TYPE_TO_AVRO[primitive_type]]

    assert {"name": name, "type": avro_type, "default": fields.NULL} == field.to_dict()


@pytest.mark.parametrize("primitive_type,default", consts.PRIMITIVE_TYPES_AND_DEFAULTS)
def test_primitive_types_with_default_value(primitive_type, default):
    name = "a_field"
    field = fields.Field(name, primitive_type, default)
    avro_type = [fields.PYTHON_TYPE_TO_AVRO[primitive_type], fields.NULL]

    assert {"name": name, "type": avro_type, "default": default} == field.to_dict()


@pytest.mark.parametrize(
    "primitive_type,invalid_default", consts.PRIMITIVE_TYPES_AND_INVALID_DEFAULTS
)
def test_invalid_default_values(primitive_type, invalid_default):
    name = "a_field"
    field = fields.Field(name, primitive_type, invalid_default)

    msg = f"Invalid default type. Default should be {primitive_type}"
    with pytest.raises(AssertionError, match=msg):
        field.to_dict()

[((10, 1, 10, 73), 'pytest.mark.parametrize', 'pytest.mark.parametrize', ({(10, 25, 10, 41): '"""primitive_type"""', (10, 43, 10, 72): 'fields.PYTHON_INMUTABLE_TYPES'}, {}), "('primitive_type', fields.PYTHON_INMUTABLE_TYPES)", False, 'import pytest\n'), ((19, 1, 19, 73), 'pytest.mark.parametrize', 'pytest.mark.parametrize', ({(19, 25, 19, 41): '"""primitive_type"""', (19, 43, 19, 72): 'fields.PYTHON_INMUTABLE_TYPES'}, {}), "('primitive_type', fields.PYTHON_INMUTABLE_TYPES)", False, 'import pytest\n'), ((28, 1, 28, 87), 'pytest.mark.parametrize', 'pytest.mark.parametrize', ({(28, 25, 28, 49): '"""primitive_type,default"""', (28, 51, 28, 86): 'consts.PRIMITIVE_TYPES_AND_DEFAULTS'}, {}), "('primitive_type,default', consts.\n    PRIMITIVE_TYPES_AND_DEFAULTS)", False, 'import pytest\n'), ((37, 1, 39, 1), 'pytest.mark.parametrize', 'pytest.mark.parametrize', ({(38, 4, 38, 36): '"""primitive_type,invalid_default"""', (38, 38, 38, 81): 'consts.PRIMITIVE_TYPES_AND_INVALID_DEFAULTS'}, {}), "('primitive_type,invalid_default', consts.\n    PRIMITIVE_TYPES_AND_INVALID_DEFAULTS)", False, 'import pytest\n'), ((13, 12, 13, 67), 'dataclasses_avroschema.fields.Field', 'fields.Field', ({(13, 25, 13, 29): 'name', (13, 31, 13, 45): 'primitive_type', (13, 47, 13, 66): 'dataclasses.MISSING'}, {}), '(name, primitive_type, dataclasses.MISSING)', False, 'from dataclasses_avroschema import fields\n'), ((22, 12, 22, 52), 'dataclasses_avroschema.fields.Field', 'fields.Field', ({(22, 25, 22, 29): 'name', (22, 31, 22, 45): 'primitive_type', (22, 47, 22, 51): 'None'}, {}), '(name, primitive_type, None)', False, 'from dataclasses_avroschema import fields\n'), ((31, 12, 31, 55), 'dataclasses_avroschema.fields.Field', 'fields.Field', ({(31, 25, 31, 29): 'name', (31, 31, 31, 45): 'primitive_type', (31, 47, 31, 54): 'default'}, {}), '(name, primitive_type, default)', False, 'from dataclasses_avroschema import fields\n'), ((42, 12, 42, 63), 'dataclasses_avroschema.fields.Field', 'fields.Field', ({(42, 25, 42, 29): 'name', (42, 31, 42, 45): 'primitive_type', (42, 47, 42, 62): 'invalid_default'}, {}), '(name, primitive_type, invalid_default)', False, 'from dataclasses_avroschema import fields\n'), ((45, 9, 45, 49), 'pytest.raises', 'pytest.raises', (), '', False, 'import pytest\n')]