Exemplo n.º 1
0
 def test_simple_dto_is_self(self):
     tree = Object("Simple",
                   lambda: None,
                   fields={
                       "foo": Primitive(int),
                       "bar": Primitive(str)
                   })
     assert tree.dto_tree() == tree
Exemplo n.º 2
0
    def test_simple(self):
        @dataclass
        class Foo:
            some_field: str

        assert get_type_tree(Foo, locals()) == Object(
            "Foo", Foo, {"some_field": Primitive(str)})
Exemplo n.º 3
0
 def test_ts_create_dto_json_compatible(self):
     ctx = CodeSnippetContext()
     t = Object("GenObj", lambda: None, {
         "d1": Primitive(bool),
     })
     parse_expr = t.ts_create_dto(ctx, "*dtoVar*")
     assert parse_expr == "*dtoVar*"
Exemplo n.º 4
0
 def test_ts_create_dto_partially_json_compatible(self):
     ctx = CodeSnippetContext()
     t = Object("GenObj", lambda: None, {
         "d1": Primitive(bool),
         "d2": DummyTypeNode(),
     })
     parse_expr = t.ts_create_dto(ctx, "*dtoVar*")
     assert parse_expr == "{...*dtoVar*, d2: *makeDummyDto*(*dtoVar*.d2)}"
Exemplo n.º 5
0
    def test_nested(self):
        @dataclass
        class Bar:
            other_field: str

        @dataclass
        class Foo:
            some_field: Bar
            list_field: list[Bar]

        expected_bar_node = Object("Bar", Bar, {"other_field": Primitive(str)})
        assert get_type_tree(Foo, locals()) == Object(
            "Foo", Foo, {
                "some_field": expected_bar_node,
                "list_field": List(expected_bar_node)
            })
Exemplo n.º 6
0
 def dto_tree(self) -> AbstractNode:
     return Primitive(str)
Exemplo n.º 7
0
def test_primitives():
    assert get_type_tree(int) == Primitive(int)
    assert get_type_tree(float) == Primitive(float)
    assert get_type_tree(str) == Primitive(str)
    assert get_type_tree(bool) == Primitive(bool)
Exemplo n.º 8
0
 def test_ts_create_dto_json_compatible(self):
     ctx = CodeSnippetContext()
     parse_expr = Dict(Primitive(str)).ts_create_dto(ctx, "*dtoVar*")
     assert parse_expr == "*dtoVar*"
Exemplo n.º 9
0
 def test_tree_parsing(self):
     assert get_type_tree(dict[str, int]) == Dict(Primitive(int))
     t = get_type_tree(dict[int, str])
     assert t == UnsupportedTypeNode(dict[int, str])
     with pytest.raises(UnsupportedTypeError):
         t.ts_repr(CodeSnippetContext())
Exemplo n.º 10
0
 def test_ts_parse_dto_with_json_compatible_element(self):
     ctx = CodeSnippetContext()
     t = List(Primitive(int))
     parse_expr = t.ts_parse_dto(ctx, "*dtoVar*")
     assert "*dtoVar*" == parse_expr
Exemplo n.º 11
0
 def test_ts_create_dto_with_json_compatible_element(self):
     ctx = CodeSnippetContext()
     t = List(Primitive(str))
     create_expr = t.ts_create_dto(ctx, "*listVar*")
     assert "*listVar*" == create_expr
Exemplo n.º 12
0
 def test_tree_parsing(self):
     assert get_type_tree(list[str]) == List(Primitive(str))
     assert get_type_tree(list[list[bool]]) == List(List(Primitive(bool)))
Exemplo n.º 13
0
 def test_list_ts_repr(self):
     ctx = CodeSnippetContext()
     assert List(Primitive(int)).ts_repr(ctx) == "number[]"
     assert List(Primitive(str)).ts_repr(ctx) == "string[]"
     assert List(List(Primitive(str))).ts_repr(ctx) == "string[][]"