def test_raises_for_unsupported_type(): class Person: name: str = "root" index: int = 1 with pytest.raises(TypeError): assert json.dumps(Person())
def test_encodes_dataclasses(): @dataclasses.dataclass class Person: name: str = "root" index: int = 1 assert json.dumps(Person()) == '{"name": "root", "index": 1}'
def test_encodes_jsonable_classes(): class Person: name: str = "root" index: int = 1 def to_json(self) -> dict: return {"name": self.name, "index": self.index} assert json.dumps(Person()) == '{"name": "root", "index": 1}'
async def write(self, data: t.Dict, session_id: t.Optional[str] = None) -> str: """The data is a session id in this backend.""" encoded_data = b64encode(json.dumps(data).encode("utf-8")) return self._signer.sign(encoded_data).decode("utf-8")
def test_encodes_decimal(): assert json.dumps(Decimal(100)) == '"100"'
def test_encodes_enums(): class Animals(enum.Enum): CAT = "cat" DOG = "dog" assert json.dumps(Animals.CAT) == '"cat"'
def test_encodes_set(): assert json.dumps({1}) == "[1]" assert json.dumps(frozenset([1])) == "[1]"
def test_encodes_uuid4(): value = uuid.uuid4() assert json.dumps(value) == f'"{value}"'
def test_encodes_time(): assert json.dumps(datetime.time(12, 30, 59)) == '"12:30:59"'
def test_encodes_date(): assert json.dumps(datetime.date(2021, 1, 1)) == '"2021-01-01"'
def test_dumps(): assert json.dumps(1) == "1"