def test_fromjson_namedtuple_empty_data() -> None: class Position(NamedTuple): x: int y: int data = {"x": 1} with pytest.raises(KeyError): utils.fromjson(Position, data)
def test_fromjson_dataclass_empty_data() -> None: @dataclass class Sudoku: size: int data: Dict[str, Any] = {} with pytest.raises(KeyError): utils.fromjson(Sudoku, data)
def test_fromjson_dataclass_invalid_data() -> None: @dataclass class Album: songs: List[int] data = [1] with pytest.raises(ValueError) as excinfo: utils.fromjson(Album, data) assert str(excinfo.value) == "Expected `Mapping`, got: <class 'list'>"
def test_fromjson_namedtuple_invalid_data() -> None: class Position(NamedTuple): x: int y: int = 0 data = 1 with pytest.raises(ValueError) as excinfo: utils.fromjson(Position, data) assert str( excinfo.value) == "Expected `List` or `Mapping`, got: <class 'int'>"
def test_fromjson_enum() -> None: class Genre(enum.Enum): INDIE = 1 SHOEGAZE = 2 genre = utils.fromjson(Genre, 1) assert genre == Genre.INDIE
def test_fromjson_namedtuple(data) -> None: class Position(NamedTuple): x: int y: int position = utils.fromjson(Position, data) assert position == Position(1, 0)
def test_fromjson_callable() -> None: def get_x(data) -> int: return cast(int, data["x"]) data = {"x": 1} value = utils.fromjson(get_x, data) assert value == 1
def test_fromjson_dataclass_with_defaults() -> None: @dataclass class Sudoku: size: int = field(default=81) data: Dict[str, Any] = {} sudoku = utils.fromjson(Sudoku, data) assert sudoku == Sudoku(size=81)
def test_fromjson_namedtuple_with_defaults() -> None: class Position(NamedTuple): x: int y: int = 0 data = {"x": 1} position = utils.fromjson(Position, data) assert position == Position(1, 0)
def test_fromjson_dataclass_with_default_factory() -> None: @dataclass class Sudoku: cells: List[int] = field(default_factory=list) data: Dict[str, Any] = {} sudoku = utils.fromjson(Sudoku, data) assert sudoku == Sudoku(cells=[])
def test_fromjson_dataclass() -> None: @dataclass class Song: title: str @dataclass class Album: title: str songs: List[Song] data = { "title": "The Apiwrappers", "songs": [{ "title": "Waiting for my driver" }] } album = utils.fromjson(Album, data) assert album == Album(title="The Apiwrappers", songs=[Song(title="Waiting for my driver")])
def wrapper(): resp = driver.fetch(request, timeout=timeout) if model is None: return resp return utils.fromjson(model, utils.getitem(resp.json(), source))
def test_fromjson_generic_types(tp, given, expected) -> None: assert utils.fromjson(tp, given) == expected
def test_fromjson_generic_types_invalid_data(tp, given, expected): with pytest.raises(ValueError) as excinfo: utils.fromjson(tp, given) assert str(excinfo.value) == expected
def test_fromjson_union_are_not_supported() -> None: with pytest.raises(TypeError) as excinfo: utils.fromjson(Union[int, float], {}) # type: ignore assert str(excinfo.value) == "Union is not supported"
def test_fromjson_abstract_types() -> None: with pytest.raises(TypeError) as excinfo: utils.fromjson(Mapping[str, str], {}) assert str(excinfo.value) == "Abstract types is not supported"
def __init__(self, response: Response): content = cast(Dict[str, Any], response.json()) super().__init__(content["message"]) self.response = response self.errors = fromjson(Optional[List[Error]], content.get("errors"))