def test_dump_complex_document_json() -> None: dumps = yatiml.dumps_json_function( Document2, Color2, Shape, Rectangle, Circle, Vector2D) shape1 = Circle(Vector2D(5.0, 6.0), 12.0) shape2 = Rectangle(Vector2D(-2.0, -5.0), 3.0, 7.0) data = Document2(Vector2D(3.0, 4.0), [shape1, shape2]) text = dumps(data, indent=2) assert text == ( '{\n' ' "cursor_at": {\n' ' "x": 3.0,\n' ' "y": 4.0\n' ' },\n' ' "shapes": [\n' ' {\n' ' "center": {\n' ' "x": 5.0,\n' ' "y": 6.0\n' ' },\n' ' "radius": 12.0\n' ' },\n' ' {\n' ' "center": {\n' ' "x": -2.0,\n' ' "y": -5.0\n' ' },\n' ' "width": 3.0,\n' ' "height": 7.0\n' ' }\n' ' ],\n' ' "color": "red",\n' ' "extra_shape": null\n' '}\n' )
def test_dump_custom_attributes_json() -> None: dumps = yatiml.dumps_json_function(Extensible) extra_attributes = OrderedDict([('b', 5), ('c', 3)]) data = Extensible(10, _yatiml_extra=extra_attributes) text = dumps(data, indent=2) assert text == ( '{\n' ' "a": 10,\n' ' "b": 5,\n' ' "c": 3\n' '}\n')
def test_dump_json_to_string() -> None: dumps_json = yatiml.dumps_json_function() json_text = dumps_json({'x': 1}) assert json_text == '{"x":1}' json_text = dumps_json({'x': 2}, indent=4) assert json_text == ('{\n' ' "x": 2\n' '}\n') json_text = dumps_json('\u0410\u043d\u043d\u0430') assert json_text == '"\\u0410\\u043d\\u043d\\u0430"' json_text = dumps_json('\u0410\u043d\u043d\u0430', ensure_ascii=False) assert json_text == '"\u0410\u043d\u043d\u0430"'
def test_dump_str_json() -> None: dumps = yatiml.dumps_json_function() text = dumps('test') assert text == '"test"'
def test_dump_path_json() -> None: dumps = yatiml.dumps_json_function() text = dumps(Path('/tmp/testing.txt')) assert text == '"/tmp/testing.txt"'
def test_dump_parsed_class_json() -> None: dumps = yatiml.dumps_json_function(Postcode) text = dumps(Postcode(1098, 'XG')) assert text == '"1098 XG"'
def test_dump_dashed_attribute_json() -> None: dumps = yatiml.dumps_json_function(DashedAttribute) text = dumps(DashedAttribute(34)) assert text == '{"dashed-attribute":34}'
def test_dump_bool_json() -> None: dumps = yatiml.dumps_json_function() text = dumps(True) assert text == 'true'
def test_dump_int_json() -> None: dumps = yatiml.dumps_json_function() text = dumps(42) assert text == '42'
def test_dump_enum_json() -> None: dumps = yatiml.dumps_json_function(Color) text = dumps(Color.green) assert text == '"green"'
def test_private_attributes_json() -> None: dumps = yatiml.dumps_json_function(BrokenPrivateAttributes) with pytest.raises(AttributeError): dumps(BrokenPrivateAttributes(10, 42.0))
def test_yatiml_attributes_json() -> None: dumps = yatiml.dumps_json_function(PrivateAttributes) text = dumps(PrivateAttributes(10, 42.0)) assert text == '{"a":10,"b":42.0}'
def test_broken_custom_attributes_json() -> None: dumps = yatiml.dumps_json_function(Universal) data = Universal(3, [4]) with pytest.raises(RuntimeError): dumps(data)
def test_dump_document1_json() -> None: dumps = yatiml.dumps_json_function(Document1) text = dumps(Document1('test')) assert text == '{"attr1":"test"}'
def test_dump_datetime() -> None: dumps = yatiml.dumps_json_function() text = dumps(datetime(2018, 10, 27)) assert text == '"2018-10-27 00:00:00"'
def test_enum_sweeten_json() -> None: dumps = yatiml.dumps_json_function(Color2) text = dumps(Color2.YELLOW) assert text == '"yellow"'
def test_dump_datetime_json() -> None: dumps = yatiml.dumps_json_function() text = dumps(datetime(2020, 10, 16)) assert text == '"2020-10-16 00:00:00"'
def test_dump_user_string_json() -> None: dumps = yatiml.dumps_json_function(ConstrainedString) text = dumps(ConstrainedString('abc')) assert text == '"abc"'
def test_dump_float_json() -> None: dumps = yatiml.dumps_json_function() text = dumps(13.5) assert text == '13.5'
def test_dump_user_string_like_json() -> None: dumps = yatiml.dumps_json_function(StringLike) text = dumps(StringLike('abcd')) assert text == '"abcd"'
def test_dump_null_json() -> None: dumps = yatiml.dumps_json_function() text = dumps(None) assert text == 'null'
def test_sweeten_json() -> None: dumps = yatiml.dumps_json_function(Super2, SubA2, SubB2) text = dumps(SubA2()) assert text == '{"subclass":"A2"}'