Exemplo n.º 1
0
def test_can_decode_dataclass_with_optional() -> None:
    json_lack = {"id": "test-document", "content": "Hello, world!"}

    json_none = {
        "id": "test-document",
        "content": "Hello, world!",
        "owner": None
    }

    json_filled = {
        "id": "test-document",
        "content": "Hello, world!",
        "owner": {
            "id": "test-owner",
            "name": {
                "first": "Tomoya",
                "last": "Kose"
            }
        },
    }

    expectation_none = DocumentJson(id="test-document",
                                    content="Hello, world!",
                                    owner=None)

    expectation_filled = DocumentJson(
        id="test-document",
        content="Hello, world!",
        owner=OwnerJson(id="test-owner",
                        name=NameJson(first="Tomoya", last="Kose")),
    )

    assert typedjson.decode(DocumentJson, json_lack) == expectation_none
    assert typedjson.decode(DocumentJson, json_none) == expectation_none
    assert typedjson.decode(DocumentJson, json_filled) == expectation_filled
Exemplo n.º 2
0
def test_can_decode_union() -> None:
    json_user = {
        "id": "test-user",
        "age": 28,
        "name": {"first": "Tomoya", "last": "Kose"},
    }

    json_document = {
        "id": "test-document",
        "content": "Hello, world!",
        "owner": {"id": "test-owner", "name": {"first": "Tomoya", "last": "Kose"}},
    }

    expectation_user = UserJson(
        id="test-user", age=28, name=NameJson(first="Tomoya", last="Kose")
    )

    expectation_document = DocumentJson(
        id="test-document",
        content="Hello, world!",
        owner=OwnerJson(id="test-owner", name=NameJson(first="Tomoya", last="Kose")),
    )

    assert (
        typedjson.decode(Union[UserJson, DocumentJson], json_user) == expectation_user
    )
    assert (
        typedjson.decode(Union[UserJson, DocumentJson], json_document)
        == expectation_document
    )
Exemplo n.º 3
0
def test_can_decode_homogeneous_variable_tuple() -> None:
    json_empty: Any = tuple()
    json_short = (0, )
    json_long = (0, 1, 2, 3)
    assert typedjson.decode(Tuple[int, ...], json_empty) == json_empty
    assert typedjson.decode(Tuple[int, ...], json_short) == json_short
    assert typedjson.decode(Tuple[int, ...], json_long) == json_long
Exemplo n.º 4
0
def get_confs_for_campaign(campaignid, db_conf):
    parsers = {
        "stratum": lambda x: typedjson.decode(StratumConf, x),
        "audience": lambda x: typedjson.decode(AudienceConf, x),
        "creative": lambda x: typedjson.decode(CreativeConf, x),
        "opt": lambda x: typedjson.decode(CampaignConf, x),
    }

    confs = get_campaign_configs(campaignid, db_conf)
    confs = groupby(
        lambda x: x[0],
        [(c["conf_type"], parsers[c["conf_type"]](conf)) for c in confs
         for conf in c["conf"]],
    )
    return {k: [vv for _, vv in v] for k, v in confs.items()}
Exemplo n.º 5
0
def test_can_decode_dataclass() -> None:
    json = {"id": "test-user", "age": 28, "name": {"first": "Tomoya", "last": "Kose"}}

    expectation = UserJson(
        id="test-user", age=28, name=NameJson(first="Tomoya", last="Kose")
    )

    assert typedjson.decode(UserJson, json) == expectation
Exemplo n.º 6
0
def load(type_: Type[Decoded], file_: IO[str]) -> Decoded:
    import json

    from typedjson import decode
    from typedjson import DecodingError

    decoded = decode(type_, json.load(file_))
    if isinstance(decoded, DecodingError):
        raise decoded
    else:
        return decoded
Exemplo n.º 7
0
def loads(type_: Type[Decoded], string: str) -> Decoded:
    import json

    from typedjson import decode
    from typedjson import DecodingError

    decoded = decode(type_, json.loads(string))
    if isinstance(decoded, DecodingError):
        raise decoded
    else:
        return decoded
Exemplo n.º 8
0
def test_can_decode_dataclass_with_redundancy() -> None:
    json = {
        "id": "test-user",
        "age": 28,
        "name": {"first": "Tomoya", "last": "Kose"},
        "role": "administrator",
    }

    expectation = UserJson(
        id="test-user", age=28, name=NameJson(first="Tomoya", last="Kose")
    )

    assert typedjson.decode(UserJson, json) == expectation
Exemplo n.º 9
0
def test_can_decode_float() -> None:
    json = 1.234
    assert typedjson.decode(float, json) == json
Exemplo n.º 10
0
def test_can_decode_int() -> None:
    json = 1234
    assert typedjson.decode(int, json) == json
Exemplo n.º 11
0
def test_can_decode_str() -> None:
    json = "string"
    assert typedjson.decode(str, json) == json
Exemplo n.º 12
0
def test_cannot_decode_none_as_tuple() -> None:
    json = None
    assert typedjson.decode(Tuple[str, ...],
                            json) == DecodingError(TypeMismatch(()))
Exemplo n.º 13
0
def test_can_decode_optional() -> None:
    json = None
    assert typedjson.decode(Optional[str], json) == json
Exemplo n.º 14
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
Exemplo n.º 15
0
def test_cannot_decode_generic_union() -> None:
    U = TypeVar("U")
    json = 100
    expectation = DecodingError(UnsupportedDecoding(()))
    assert typedjson.decode(Union[int, U], json) == expectation
Exemplo n.º 16
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
Exemplo n.º 17
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
Exemplo n.º 18
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
Exemplo n.º 19
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
Exemplo n.º 20
0
def test_cannot_decode_none_as_list() -> None:
    json = None
    assert typedjson.decode(List[str], json) == DecodingError(TypeMismatch(()))
Exemplo n.º 21
0
def test_can_decode_true() -> None:
    json = True
    assert typedjson.decode(bool, json) == json
Exemplo n.º 22
0
def test_cannot_decode_none() -> None:
    json = None
    assert typedjson.decode(str, json) == DecodingError(TypeMismatch(()))
Exemplo n.º 23
0
def test_can_decode_false() -> None:
    json = False
    assert typedjson.decode(bool, json) == json
Exemplo n.º 24
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
Exemplo n.º 25
0
def test_can_decode_homogeneous_fixed_tuple() -> None:
    json = (0, 1, 2, 3)
    assert typedjson.decode(Tuple[int, int, int, int], json) == json
Exemplo n.º 26
0
def test_cannot_decode_raw_dataclass() -> None:
    json = {"t1": 100, "t2": "hello"}
    expectation = DecodingError(UnsupportedDecoding(()))
    assert typedjson.decode(GenericJson, json) == expectation
def get_serialized_media(json: Dict[str, Any]) -> Media:
    obj: Media = typedjson.decode(Media, json)
    return obj
Exemplo n.º 28
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
Exemplo n.º 29
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
Exemplo n.º 30
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