Exemplo n.º 1
0
    def test_deep_nested_tree_ts_repr(self):
        @dataclass
        class Baz:
            nano: int

        @dataclass
        class Bar:
            micro: list[Baz]

        @dataclass
        class Foo:
            my_field: Bar

        tree = Object.match(Foo)
        ctx = CodeSnippetContext()
        assert tree.ts_repr(ctx) == "Foo"

        assert "Foo" in ctx
        assert ctx.get_snippet("Foo") == """export interface Foo {
  myField: Bar;
}"""
        assert "Bar" in ctx
        assert ctx.get_snippet("Bar") == """export interface Bar {
  micro: Baz[];
}"""

        assert "Baz" in ctx
        assert ctx.get_snippet("Baz") == """export interface Baz {
  nano: number;
}"""

        assert ctx.topological_dependencies("Foo") == ["Baz", "Bar", "Foo"]
Exemplo n.º 2
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.º 3
0
 def test_ts_create_dto_generic(self):
     ctx = CodeSnippetContext()
     t = Object("GenObj", lambda: None, {
         "d1": DummyTypeNode(),
     })
     parse_expr = t.ts_create_dto(ctx, "*dtoVar*")
     assert parse_expr == "{d1: *makeDummyDto*(*dtoVar*.d1)}"
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_snippet_context_subcontext():
    ctx = CodeSnippetContext()
    ctx.add("foo", "dummy")
    sub = ctx.subcontext("foo")
    sub.add("bar", "dummy")
    ctx.add("baz", "dummy")
    assert "foo" in ctx
    assert "bar" in ctx
    assert ctx.top_level_snippets() == {"foo", "baz"}
    assert ctx.natural_order() == ["baz", "bar", "foo"]
Exemplo n.º 6
0
    def test_object_ts_repr(self):
        ctx = CodeSnippetContext()

        @dataclass()
        class Foo:
            my_field: int
            other_field: str

        tree = Object.match(Foo)
        assert tree.ts_repr(ctx) == "Foo"
        assert ctx.top_level_snippets() == {"Foo"}
        assert ctx.get_snippet("Foo") == """export interface Foo {
Exemplo n.º 7
0
def test_api_gen():
    foo_type_tree = get_type_tree(Foo)
    ctx = CodeSnippetContext()
    func_code = build_ts_func("getFoo", foo_type_tree, None,
                              "/api/foo/<my_id>", ["my_id"], "GET", ctx)
    expected_func_code = """
export const getFoo = async (myId: string): Promise<Foo> => {
  const response = await fetch(`/api/foo/${myId}`, {
    method: 'GET'
  });
  if (!response.ok) {
    throw new ApiError("HTTP status code: " + response.status, response);
  }
  return await response.json();
}"""
    assert func_code == expected_func_code
    assert ctx.natural_order() == ["ApiError", "Foo"]
Exemplo n.º 8
0
 def test_ts_repr(self):
     assert Dict(DummyTypeNode()).ts_repr(
         CodeSnippetContext()) == "{ [key: string]: *Dummy* }"
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_parse_dto(self):
     ctx = CodeSnippetContext()
     t = List(DummyTypeNode())
     parse_expr = t.ts_parse_dto(ctx, "*dtoVar*")
     assert "*dtoVar*.map(item => (*parseDummyDto*(item)))" == parse_expr
Exemplo n.º 12
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.º 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[][]"
Exemplo n.º 14
0
def test_snippet_context_single_snippet():
    ctx = CodeSnippetContext()
    ctx.add("foo", "dummy")
    assert "foo" in ctx
    assert ctx.top_level_snippets() == {"foo"}
    assert ctx.natural_order() == ["foo"]
Exemplo n.º 15
0
 def test_ts_create_dto_generic(self):
     ctx = CodeSnippetContext()
     parse_expr = Dict(DummyTypeNode()).ts_create_dto(ctx, "*dtoVar*")
     assert parse_expr == "_mapObject(*dtoVar*, val => (*makeDummyDto*(val)))"
Exemplo n.º 16
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.º 17
0
 def test_ts_repr(self):
     ctx = CodeSnippetContext()
     t = List(DummyTypeNode())
     assert t.ts_repr(ctx) == "*Dummy*[]"
Exemplo n.º 18
0
 def test_ts_create_dto(self):
     ctx = CodeSnippetContext()
     t = List(DummyTypeNode())
     assert t.ts_create_dto(
         ctx,
         "*listVar*") == "*listVar*.map(item => (*makeDummyDto*(item)))"
Exemplo n.º 19
0
def test_ts_repr():
    t = OptionalNode(Primitive(str))
    assert t.ts_repr(CodeSnippetContext()) == "string | null"