Ejemplo n.º 1
0
def test_cannot_decode_dataclass_with_lack_of_property() -> None:
    json = {"id": "test-user", "age": 28, "name": {"last": "Kose"}}

    expectation = DecodingError(TypeMismatch(("name", "first")))

    assert typedjson.decode(UserJson, json) == expectation
Ejemplo n.º 2
0
def test_cannot_decode_parameterized_dataclass_with_wrong_parameter() -> None:
    json = {"t1": 100, "t2": "hello"}
    expectation = DecodingError(TypeMismatch(("t2", )))
    assert typedjson.decode(GenericJson[int, int], json) == expectation
Ejemplo n.º 3
0
def test_cannot_decode_heterogeneous_list_with_incompatible() -> None:
    json = [1, "foo"]
    expectation = DecodingError(TypeMismatch(("0", )))
    assert typedjson.decode(List[Union[str, str]], json) == expectation
Ejemplo n.º 4
0
def test_cannot_decode_generic_union() -> None:
    U = TypeVar("U")
    json = 100
    expectation = DecodingError(UnsupportedDecoding(()))
    assert typedjson.decode(Union[int, U], json) == expectation
Ejemplo n.º 5
0
def test_cannot_decode_generic_list() -> None:
    U = TypeVar("U")
    json = list(range(10))
    expectation = DecodingError(UnsupportedDecoding(()))
    assert typedjson.decode(List[U], json) == expectation
Ejemplo n.º 6
0
def test_cannot_decode_homogeneous_list_with_incompatible() -> None:
    json = [1, 2, 3]
    expectation = DecodingError(TypeMismatch(("0", )))
    assert typedjson.decode(List[str], json) == expectation
Ejemplo n.º 7
0
def test_cannot_decode_fixed_tuple_with_short_sequence() -> None:
    json = (0, 1, 2)
    expectation = DecodingError(TypeMismatch(()))
    assert typedjson.decode(Tuple[int, int, int, int], json) == expectation
Ejemplo n.º 8
0
def test_cannot_decode_with_wrong_type() -> None:
    json = True
    assert typedjson.decode(str, json) == DecodingError(TypeMismatch(()))
Ejemplo n.º 9
0
def test_cannot_decode_none_as_tuple() -> None:
    json = None
    assert typedjson.decode(Tuple[str, ...],
                            json) == DecodingError(TypeMismatch(()))
Ejemplo n.º 10
0
def test_cannot_decode_none_as_list() -> None:
    json = None
    assert typedjson.decode(List[str], json) == DecodingError(TypeMismatch(()))
Ejemplo n.º 11
0
def test_cannote_decode_tuple_with_incompatible() -> None:
    json = (0, 1, 2, 3)
    expectation = DecodingError(TypeMismatch(("1", )))
    assert typedjson.decode(Tuple[int, str, int, int], json) == expectation
Ejemplo n.º 12
0
def test_cannot_decode_none() -> None:
    json = None
    assert typedjson.decode(str, json) == DecodingError(TypeMismatch(()))
Ejemplo n.º 13
0
def test_cannot_decode_float_as_int() -> None:
    json = 1.234
    assert typedjson.decode(int, json) == DecodingError(TypeMismatch(()))
Ejemplo n.º 14
0
def test_cannot_decode_raw_dataclass() -> None:
    json = {"t1": 100, "t2": "hello"}
    expectation = DecodingError(UnsupportedDecoding(()))
    assert typedjson.decode(GenericJson, json) == expectation
Ejemplo n.º 15
0
def test_cannot_decode_generic_tuple() -> None:
    U = TypeVar("U")
    json = (0, 1, 2)
    expectation = DecodingError(UnsupportedDecoding(()))
    assert typedjson.decode(Tuple[int, U, int], json) == expectation
Ejemplo n.º 16
0
def test_cannot_decode_generic_dataclass() -> None:
    U1 = TypeVar("U1")
    U2 = TypeVar("U2")
    json = {"t1": 100, "t2": "hello"}
    expectation = DecodingError(UnsupportedDecoding(()))
    assert typedjson.decode(GenericJson[U1, U2], json) == expectation
Ejemplo n.º 17
0
def test_cannot_decode_variable_tuple_with_short_sequence() -> None:
    json: Tuple = tuple()
    expectation = DecodingError(TypeMismatch(()))
    assert typedjson.decode(Tuple[int, ...], json) == expectation