Пример #1
0
def test_dataclass_invalid_for_json_types():
    # we do not accept a union other than optional includes a type of list or dict.
    # json structure needs to be explicit.
    @dataclass
    class DictItem:
        val: Union[str, dict]

    @dataclass
    class ListItem:
        val: Union[str, list]

    json = {"val": "hello"}

    want = (
        "Schemas defined with unions containing anything other than optional "
        "(NoneType + other) fields are not currently supported. Got "
        "typing.Union[str, dict]  with  (<class 'str'>, <class 'dict'>)")
    with pytest.raises(UnmarshalError) as exc_info:
        unmarshal(json, DictItem)
    assert str(exc_info.value) == want

    want = (
        "Schemas defined with unions containing anything other than optional "
        "(NoneType + other) fields are not currently supported. Got "
        "typing.Union[str, list]  with  (<class 'str'>, <class 'list'>)")
    with pytest.raises(UnmarshalError) as exc_info:
        unmarshal(json, ListItem)
    assert str(exc_info.value) == want
Пример #2
0
def test_simple_invalid_enum():
    class Colour(enum.Enum):
        RED = "RED"
        BLUE = "BLUE"

    json = "GREEN"

    with pytest.raises(UnmarshalError) as exc_info:
        unmarshal(json, Colour)
    assert str(exc_info.value
               ) == "Unable to use data value 'GREEN' as Enum <enum 'Colour'>"
Пример #3
0
def test_unexpected_type():
    @dataclass
    class Item:
        val: str

    json = {"val": 1.234}

    with pytest.raises(UnmarshalError) as exc_info:
        unmarshal(json, Item)
    want = "Invalid schema. schema = <class 'str'>, data = '1.234' " "(<class 'float'>) at location = val"
    assert str(exc_info.value) == want
Пример #4
0
def test_simple_uuid_is_invalid(invalid_uuid):
    @dataclass
    class Item:
        value: UUID

    json = {"value": invalid_uuid}
    with pytest.raises(UnmarshalError) as exc_info:
        unmarshal(json, Item)
    assert str(
        exc_info.value
    ) == f"Unable to use data value '{invalid_uuid}' as UUID <class 'uuid.UUID'>"
Пример #5
0
def test_simple_bool_is_invalid(invalid_bool):
    @dataclass
    class Item:
        value: bool

    json = {"value": invalid_bool}
    with pytest.raises(UnmarshalError) as exc_info:
        unmarshal(json, Item)
    want = (
        f"Invalid schema. schema = <class 'bool'>, data = '{invalid_bool}' "
        f"({type(invalid_bool)}) at location = value")
    assert str(exc_info.value) == want
Пример #6
0
def test_unexpected_type_in_union():
    @dataclass
    class Item:
        val: Union[None, int]

    json = {"val": "test"}

    with pytest.raises(UnmarshalError) as exc_info:
        unmarshal(json, Item)
    want = ("Invalid schema. schema = typing.Union[NoneType, int], data = "
            "'test' (<class 'str'>) at location = val")
    assert str(exc_info.value) == want
Пример #7
0
def test_item_not_present_in_json():
    @dataclass
    class Item:
        val: str
        other_val: str = json_field(json="otherVal")

    json = {"val": "test"}

    with pytest.raises(UnmarshalError) as exc_info:
        unmarshal(json, Item)
    want = "Expected json key is not present in object at position ''. 'otherVal' not in ['val']"
    assert str(exc_info.value) == want
Пример #8
0
def test_unknown_datatype():
    class Impossible:
        pass

    @dataclass
    class Item:
        first_value: float = json_field(json="firstValue")
        second_value: Impossible = json_field(json="secondValue")

    json = {"firstValue": 123.213, "secondValue": 999}

    with pytest.raises(UnmarshalError) as exc_info:
        unmarshal(json, Item)
    assert str(exc_info.value
               ) == f"Schema type '{Impossible}' is not currently supported."
Пример #9
0
def test_simple_array_with_multiple_simple_objects():
    @dataclass
    class Item:
        first_val: str = json_field(json="firstVal")

    json = [
        {
            "firstVal": "hello",
            "secondVal": "there"
        },
        {
            "firstVal": "test",
            "secondVal": "there"
        },
        {
            "firstVal": "var",
            "secondVal": "there"
        },
    ]

    want = [
        Item(first_val="hello"),
        Item(first_val="test"),
        Item(first_val="var")
    ]
    got = unmarshal(json, List[Item])

    assert got == want
Пример #10
0
def test_unmarshal_integration(fixture_name, marshalled, schema, unmarshalled,
                               date_fmt, datetime_fmt):
    got = unmarshal(marshalled,
                    schema,
                    date_fmt=date_fmt,
                    datetime_fmt=datetime_fmt)
    assert got == unmarshalled
Пример #11
0
def test_simple_null_value():
    @dataclass
    class Item:
        val: None

    json = {"val": None}
    want = Item(val=None)
    got = unmarshal(json, Item)
    assert got == want
Пример #12
0
def test_simple_enum():
    class Colour(enum.Enum):
        RED = "RED"
        BLUE = "BLUE"

    json = "RED"

    want = Colour.RED
    got = unmarshal(json, Colour)
    assert got == want
Пример #13
0
def test_simple_bool_array():
    @dataclass
    class Item:
        value: List[bool]

    json = {"value": [True, False, True]}

    want = Item(value=[True, False, True])
    got = unmarshal(json, Item)
    assert got == want
Пример #14
0
def test_simple_bool_optional_null():
    @dataclass
    class Item:
        value: Optional[bool]

    json = {"value": None}

    want = Item(value=None)
    got = unmarshal(json, Item)
    assert got == want
Пример #15
0
def test_simple_bool_optional_valid():
    @dataclass
    class Item:
        value: Optional[bool]

    json = {"value": True}

    want = Item(value=True)
    got = unmarshal(json, Item)
    assert got == want
Пример #16
0
def test_simple_optional_datetime_is_valid():
    @dataclass
    class Item:
        value: Optional[datetime]

    json = {"value": "2020-06-23T07:59:30+00:00"}

    got = unmarshal(json, Item)
    want = Item(value=datetime(2020, 6, 23, 7, 59, 30, tzinfo=pytz.UTC))
    assert got == want
Пример #17
0
def test_simple_datetime(timestamp):
    @dataclass
    class Item:
        value: datetime

    json = {"value": timestamp}

    want = Item(value=datetime(2020, 6, 22, 8, 55, 5, tzinfo=pytz.UTC))
    got = unmarshal(json, Item)
    assert got == want
Пример #18
0
def test_simple_uuid_array():
    @dataclass
    class Item:
        value: List[UUID]

    json = {"value": ["cb637f6a-0dc0-4c42-8764-5b98137a8ea6"]}

    want = Item(value=[UUID("cb637f6a-0dc0-4c42-8764-5b98137a8ea6")])
    got = unmarshal(json, Item)
    assert got == want
Пример #19
0
def test_simple_uuid_optional_valid():
    @dataclass
    class Item:
        value: Optional[UUID]

    json = {"value": "cb637f6a-0dc0-4c42-8764-5b98137a8ea6"}

    want = Item(value=UUID("cb637f6a-0dc0-4c42-8764-5b98137a8ea6"))
    got = unmarshal(json, Item)
    assert got == want
Пример #20
0
def test_simple_optional_date_is_null():
    @dataclass
    class Item:
        value: Optional[date]

    json = {"value": None}

    got = unmarshal(json, Item)
    want = Item(value=None)
    assert got == want
Пример #21
0
def test_simple_date_explicit_format():
    @dataclass
    class Item:
        value: date

    json = {"value": "2020/11/02"}

    want = Item(value=date(2020, 11, 2))
    got = unmarshal(json, Item, date_fmt="%Y/%m/%d")
    assert got == want
Пример #22
0
def test_simple_object():
    @dataclass
    class Item:
        first_val: str = json_field(json="firstVal")

    json = {"firstVal": "hello"}

    want = Item(first_val="hello")
    got = unmarshal(json, Item)
    assert got == want
Пример #23
0
def test_simple_date():
    @dataclass
    class Item:
        value: date

    json = {"value": "2020-06-22"}

    want = Item(value=date(2020, 6, 22))
    got = unmarshal(json, Item)
    assert got == want
Пример #24
0
def test_simple_datetime_explicit_format():
    @dataclass
    class Item:
        value: datetime

    json = {"value": "2020/05/27 (09:34:36)"}

    want = Item(value=datetime(2020, 5, 27, 9, 34, 36))
    got = unmarshal(json, Item, datetime_fmt="%Y/%m/%d (%H:%M:%S)")
    assert got == want
Пример #25
0
def test_simple_datetime_optional_valid():
    @dataclass
    class Item:
        value: Optional[datetime]

    json = {"value": "2020-06-22T08:55:05+00:00"}

    want = Item(value=datetime(2020, 6, 22, 8, 55, 5, tzinfo=pytz.UTC))
    got = unmarshal(json, Item)
    assert got == want
Пример #26
0
def test_simple_bool():
    @dataclass
    class Item:
        value: bool

    json = {"value": False}

    want = Item(value=False)
    got = unmarshal(json, Item)
    assert got == want
Пример #27
0
def test_simple_date_optional_valid():
    @dataclass
    class Item:
        value: Optional[date]

    json = {"value": "2020-06-22"}

    want = Item(value=date(2020, 6, 22))
    got = unmarshal(json, Item)
    assert got == want
Пример #28
0
def test_simple_object_with_fields_we_dont_care_about():
    @dataclass
    class Item:
        first_val: str = json_field(json="firstVal")

    json = {"firstVal": "hello", "secondVal": "there"}

    want = Item(first_val="hello")
    got = unmarshal(json, Item)
    assert got == want
Пример #29
0
def test_simple_union():
    @dataclass
    class Item:
        value: Union[None, int]

    json = {"value": 120}

    want = Item(value=120)
    got = unmarshal(json, Item)
    assert got == want
Пример #30
0
def test_simple_object_with_simple_array_item():
    @dataclass
    class Item:
        items: List[str] = json_field(json="items")

    json = {"items": ["elem1", "elem2", "elem3"]}

    want = Item(items=["elem1", "elem2", "elem3"])
    got = unmarshal(json, Item)

    assert got == want