Esempio n. 1
0
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'
            )
Esempio n. 2
0
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')
Esempio n. 3
0
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"'
Esempio n. 4
0
def test_dump_str_json() -> None:
    dumps = yatiml.dumps_json_function()
    text = dumps('test')
    assert text == '"test"'
Esempio n. 5
0
def test_dump_path_json() -> None:
    dumps = yatiml.dumps_json_function()
    text = dumps(Path('/tmp/testing.txt'))
    assert text == '"/tmp/testing.txt"'
Esempio n. 6
0
def test_dump_parsed_class_json() -> None:
    dumps = yatiml.dumps_json_function(Postcode)
    text = dumps(Postcode(1098, 'XG'))
    assert text == '"1098 XG"'
Esempio n. 7
0
def test_dump_dashed_attribute_json() -> None:
    dumps = yatiml.dumps_json_function(DashedAttribute)
    text = dumps(DashedAttribute(34))
    assert text == '{"dashed-attribute":34}'
Esempio n. 8
0
def test_dump_bool_json() -> None:
    dumps = yatiml.dumps_json_function()
    text = dumps(True)
    assert text == 'true'
Esempio n. 9
0
def test_dump_int_json() -> None:
    dumps = yatiml.dumps_json_function()
    text = dumps(42)
    assert text == '42'
Esempio n. 10
0
def test_dump_enum_json() -> None:
    dumps = yatiml.dumps_json_function(Color)
    text = dumps(Color.green)
    assert text == '"green"'
Esempio n. 11
0
def test_private_attributes_json() -> None:
    dumps = yatiml.dumps_json_function(BrokenPrivateAttributes)
    with pytest.raises(AttributeError):
        dumps(BrokenPrivateAttributes(10, 42.0))
Esempio n. 12
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}'
Esempio n. 13
0
def test_broken_custom_attributes_json() -> None:
    dumps = yatiml.dumps_json_function(Universal)
    data = Universal(3, [4])
    with pytest.raises(RuntimeError):
        dumps(data)
Esempio n. 14
0
def test_dump_document1_json() -> None:
    dumps = yatiml.dumps_json_function(Document1)
    text = dumps(Document1('test'))
    assert text == '{"attr1":"test"}'
Esempio n. 15
0
def test_dump_datetime() -> None:
    dumps = yatiml.dumps_json_function()
    text = dumps(datetime(2018, 10, 27))
    assert text == '"2018-10-27 00:00:00"'
Esempio n. 16
0
def test_enum_sweeten_json() -> None:
    dumps = yatiml.dumps_json_function(Color2)
    text = dumps(Color2.YELLOW)
    assert text == '"yellow"'
Esempio n. 17
0
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"'
Esempio n. 18
0
def test_dump_user_string_json() -> None:
    dumps = yatiml.dumps_json_function(ConstrainedString)
    text = dumps(ConstrainedString('abc'))
    assert text == '"abc"'
Esempio n. 19
0
def test_dump_float_json() -> None:
    dumps = yatiml.dumps_json_function()
    text = dumps(13.5)
    assert text == '13.5'
Esempio n. 20
0
def test_dump_user_string_like_json() -> None:
    dumps = yatiml.dumps_json_function(StringLike)
    text = dumps(StringLike('abcd'))
    assert text == '"abcd"'
Esempio n. 21
0
def test_dump_null_json() -> None:
    dumps = yatiml.dumps_json_function()
    text = dumps(None)
    assert text == 'null'
Esempio n. 22
0
def test_sweeten_json() -> None:
    dumps = yatiml.dumps_json_function(Super2, SubA2, SubB2)
    text = dumps(SubA2())
    assert text == '{"subclass":"A2"}'