def test_raises_for_unhandled_class(): class Example: def __init__(self, x, y): self.x = x self.y = y with raises(Exception): dumps(Example(10, 20))
def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') picture = req.params.get('picture') if not picture: return func.HttpResponse( "Missing `picture` parameter", status_code=400 ) try: gallerist = Gallerist(get_store()) except ConfigurationError: logging.exception('Configuration error') return func.HttpResponse( "Configuration error.", status_code=500 ) try: metadata = gallerist.process_image(picture) except AzureMissingResourceHttpError: return func.HttpResponse( "Source picture not found", status_code=404 ) return func.HttpResponse(dumps(metadata), mimetype='application/json')
def test_flag_enum_to_json(): value = dumps({ "color_one": Color.GREEN, "color_two": Color.GREEN | Color.RED, "color_three": Color.GREEN | Color.RED | Color.BLUE, }) assert '{"color_one": 4, "color_two": 5, "color_three": 7}' == value
def test_intflag_enum_to_json(): value = dumps({ "permission_one": Permission.R, "permission_two": Permission.R | Permission.W, "permission_three": Permission.W | Permission.X, }) assert '{"permission_one": 4, "permission_two": 6, "permission_three": 3}' == value
def test_int_enum_to_json(): value = dumps({ "power": Power.GREAT, "powers": [Power.MILD, Power.MODERATE] }) assert '"power": 3' in value assert '"powers": [1, 2]' in value
def test_enum_to_json(): value = dumps({ "fruit": Fruit.MANGO, "favorite_fruits": [Fruit.MANGO, Fruit.ANANAS, Fruit.BANANA], }) assert '"fruit": "mango"' in value assert '"favorite_fruits": ["mango", "ananas", "banana"]' in value
def test_class_with_to_dict_method(): class Example: def __init__(self, x, y): self.x = x self.y = y def dict(self): return { "x": self.x, "y": self.y, "something_else": True, "date": date(2016, 3, 26), } data = dumps(Example(10, 20)) assert '{"x": 10, "y": 20, "something_else": true, "date": "2016-03-26"}' == data
def test_datetime_serialization(value, expected_json): data = dumps(value) assert expected_json == data
def test_serialize_dataclass_no_spaces(): foo_id = uuid4() value = dumps(Foo(foo_id, "foo"), separators=(",", ":")) assert f'{{"id":"{foo_id}","name":"foo"}}' == value
def test_serialize_dataclass(): foo_id = uuid4() value = dumps(Foo(foo_id, "foo")) assert f'{{"id": "{foo_id}", "name": "foo"}}' == value
def write(self, session: Session) -> str: return dumps(session.to_dict(), separators=(",", ":"))