Example #1
0
def test_absolute_model_ref():
    test_model = ModelMeta({"field": int}, "A")
    test_model.name = "test_model"
    test_ptr = ModelPtr(test_model)
    assert test_ptr.to_typing_code({})[1] == "'TestModel'"
    with AbsoluteModelRef.inject({test_model: "Parent"}):
        assert test_ptr.to_typing_code({})[1] == "'Parent.TestModel'"
    assert test_ptr.to_typing_code({})[1] == "'TestModel'"
    with AbsoluteModelRef.inject({test_model: "Parent"}):
        assert test_ptr.to_typing_code({})[1] == "'Parent.TestModel'"
        with AbsoluteModelRef.inject({test_model: "AnotherParent"}):
            assert test_ptr.to_typing_code(
                {})[1] == "'AnotherParent.TestModel'"
        assert test_ptr.to_typing_code({})[1] == "'Parent.TestModel'"

    wrapper = DList(DList(test_ptr))
    assert wrapper.to_typing_code({})[1] == "List[List['TestModel']]"
    with AbsoluteModelRef.inject({test_model: test_model}):
        assert wrapper.to_typing_code(
            {})[1] == "List[List['TestModel.TestModel']]"
     },
     "generated": trim(f"""
     import attr
     
     
     @attr.s
     class Test:
         foo: int = attr.ib()
         bar: int = attr.ib({field_meta('Bar')})
         baz: float = attr.ib()
     """)
 },
 "complex": {
     "model": ("Test", {
         "foo": int,
         "baz": DOptional(DList(DList(str))),
         "bar": DOptional(IntString),
         "qwerty": FloatString,
         "asdfg": DOptional(int),
         "dict": DDict(int),
         "ID": int,
         "not": bool,
         "1day": int,
         "день_недели": str,
     }),
     "fields_data": {
         "foo": {
             "name": "foo",
             "type": "int",
             "body": "attr.ib()"
         },
Example #3
0
import pytest

from json_to_models.dynamic_typing import (BooleanString, DDict, DList, DUnion,
                                           FloatString, IntString, Null,
                                           StringLiteral, Unknown)
from json_to_models.generator import MetadataGenerator

# JSON data | MetaData
test_data = [
    pytest.param(1.0, float, id="float"),
    pytest.param(1, int, id="int"),
    pytest.param(True, bool, id="bool"),
    pytest.param("abc", StringLiteral({"abc"}), id="str"),
    pytest.param(None, Null, id="null"),
    pytest.param([], DList(Unknown), id="list_empty"),
    pytest.param([1], DList(int), id="list_single"),
    pytest.param([*range(100)], DList(int), id="list_single_type"),
    pytest.param([1, "a", 2, "c"],
                 DList(DUnion(int, StringLiteral({'a', 'c'}))),
                 id="list_multi"),
    pytest.param("1", IntString, id="int_str"),
    pytest.param("1.0", FloatString, id="float_str"),
    pytest.param("true", BooleanString, id="bool_str"),
    pytest.param({
        "test_dict_field_a": 1,
        "test_dict_field_b": "a"
    },
                 DDict(DUnion(int, StringLiteral({"a"}))),
                 id="simple_dict"),
    pytest.param({}, DDict(Unknown), id="empty_dict")
]
Example #4
0
 }], [{
     'a': int,
     'b': float
 }, {
     'd': int,
     'e': {
         'a': int,
         'b': float
     },
 }],
              id="merge_nested"),
 pytest.param(
     {
         'int': int,
         'items': DList({
             'int': int,
             'items': DList(Unknown)  # Empty list
         })
     },
     [{
         'int': int,
         'items': DList({
             'int': int,
             'items': DList(cycle_ref)
         })
     }],
     id="cycle"),
 pytest.param(
     {
         'int': int,
         'items': {
             'count': int,
Example #5
0
             "bar: int",
             "baz: float",
         ]
     },
     "generated":
     trim("""
     class Test:
         foo: int
         bar: int
         baz: float
     """)
 },
 "complex": {
     "model": ("Test", {
         "foo": int,
         "baz": DOptional(DList(DList(str))),
         "bar": IntString,
         "d": DDict(Unknown)
     }),
     "fields_data": {
         "foo": {
             "name": "foo",
             "type": "int"
         },
         "baz": {
             "name": "baz",
             "type": "Optional[List[List[str]]]"
         },
         "bar": {
             "name": "bar",
             "type": "IntString"
Example #6
0
     )},
     {'1': {'a': DOptional({'b': float})}},
     id="merge_nested_dicts"
 ),
 pytest.param(
     DUnion(FloatString, IntString),
     FloatString,
     id="str_types_merge"
 ),
 pytest.param(
     DUnion(FloatString, BooleanString),
     str,
     id="str_types_merge_not_resolved"
 ),
 pytest.param(
     DList(DUnion(FloatString, BooleanString)),
     DList(str),
     id="str_types_merge_not_resolved_simple_wrapper"
 ),
 pytest.param(
     DTuple(DUnion(FloatString, BooleanString), int),
     DTuple(str, int),
     id="str_types_merge_not_resolved_complex_wrapper"
 ),
 pytest.param(
     DUnion(FloatString, str),
     str,
     id="union_str_str_based"
 ),
 pytest.param(
     DUnion(DList(int), DList(str), str),