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
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 )
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
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()}
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
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
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
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
def test_can_decode_float() -> None: json = 1.234 assert typedjson.decode(float, json) == json
def test_can_decode_int() -> None: json = 1234 assert typedjson.decode(int, json) == json
def test_can_decode_str() -> None: json = "string" assert typedjson.decode(str, json) == json
def test_cannot_decode_none_as_tuple() -> None: json = None assert typedjson.decode(Tuple[str, ...], json) == DecodingError(TypeMismatch(()))
def test_can_decode_optional() -> None: json = None assert typedjson.decode(Optional[str], json) == json
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
def test_cannot_decode_generic_union() -> None: U = TypeVar("U") json = 100 expectation = DecodingError(UnsupportedDecoding(())) assert typedjson.decode(Union[int, U], json) == expectation
def test_cannot_decode_generic_list() -> None: U = TypeVar("U") json = list(range(10)) expectation = DecodingError(UnsupportedDecoding(())) assert typedjson.decode(List[U], json) == expectation
def test_cannot_decode_homogeneous_list_with_incompatible() -> None: json = [1, 2, 3] expectation = DecodingError(TypeMismatch(("0", ))) assert typedjson.decode(List[str], json) == expectation
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
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
def test_cannot_decode_none_as_list() -> None: json = None assert typedjson.decode(List[str], json) == DecodingError(TypeMismatch(()))
def test_can_decode_true() -> None: json = True assert typedjson.decode(bool, json) == json
def test_cannot_decode_none() -> None: json = None assert typedjson.decode(str, json) == DecodingError(TypeMismatch(()))
def test_can_decode_false() -> None: json = False assert typedjson.decode(bool, json) == json
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
def test_can_decode_homogeneous_fixed_tuple() -> None: json = (0, 1, 2, 3) assert typedjson.decode(Tuple[int, int, int, int], json) == json
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
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
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
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