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
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'>"
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
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'>"
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
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
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
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."
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
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
def test_simple_null_value(): @dataclass class Item: val: None json = {"val": None} want = Item(val=None) got = unmarshal(json, Item) assert got == want
def test_simple_enum(): class Colour(enum.Enum): RED = "RED" BLUE = "BLUE" json = "RED" want = Colour.RED got = unmarshal(json, Colour) assert got == want
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
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
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
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
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
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
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
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
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
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
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
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
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
def test_simple_bool(): @dataclass class Item: value: bool json = {"value": False} want = Item(value=False) got = unmarshal(json, Item) assert got == want
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
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
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
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