Beispiel #1
0
    def test_from_method_in_memory_return_complex(self) -> None:
        """
        Tests that `parse.function` produces properly from a function in memory of current interpreter with:
        - complex return type
        - kwonly args
        """

        method_complex_args_variety_with_imports_str = (
            "from sys import stdout\n"
            "from {package} import Literal\n"
            "{body}".format(
                package="typing" if PY_GTE_3_8 else "typing_extensions",
                body=function_adder_str,
            ))
        add_6_5 = getattr(
            inspectable_compile(method_complex_args_variety_with_imports_str),
            "add_6_5",
        )

        ir = parse.function(add_6_5)
        del ir["_internal"]  # Not needed for this test

        self.assertDictEqual(
            ir,
            function_adder_ir,
        )
 def test_read(self) -> None:
     """
     Tests whether `read_route` is produced by `emit.route`
     """
     _read_route = inspectable_compile(route_mock_prelude + read_route).read
     self.assertDictEqual(parse.bottle(_read_route),
                          openapi_dict["paths"][self.route_id_url]["get"])
Beispiel #3
0
 def test_infer_memory_class(self) -> None:
     """
     Test `infer` can figure out the right parser name when its expected to be `class_`
     """
     set_cli_args = getattr(
         inspectable_compile(imports_header + class_str),
         "ConfigClass",
     )
     self.assertEqual(infer(set_cli_args), "class_")
Beispiel #4
0
 def test_infer_memory_argparse_ast(self) -> None:
     """
     Test `infer` can figure out the right parser name when its expected to be `argparse_ast`
     """
     set_cli_args = getattr(
         inspectable_compile(argparse_func_str),
         "set_cli_args",
     )
     self.assertEqual(infer(set_cli_args), "argparse_ast")
Beispiel #5
0
 def test_infer_memory_function(self) -> None:
     """
     Test `infer` can figure out the right parser name when its expected to be `function`
     """
     call_cliff = getattr(
         inspectable_compile("\n".join((imports_header, "stdout = None",
                                        method_complex_args_variety_str))),
         "call_cliff",
     )
     self.assertEqual(infer(call_cliff), "function")
 def test_delete(self) -> None:
     """
     Tests whether `destroy_route` is produced by `emit.route`
     """
     _destroy_route = inspectable_compile(route_mock_prelude +
                                          destroy_route).destroy
     self.assertDictEqual(
         parse.bottle(_destroy_route),
         openapi_dict["paths"][self.route_id_url]["delete"],
     )
 def test_create(self) -> None:
     """
     Tests whether `create_route` is produced by `emit.route`
     """
     _create_route = inspectable_compile(route_mock_prelude +
                                         create_route).create
     self.assertDictEqual(
         parse.bottle(_create_route),
         openapi_dict["paths"][route_config["route"]]["post"],
     )
Beispiel #8
0
 def test_infer_memory_sqlalchemy(self) -> None:
     """
     Test `infer` can figure out the right parser name when its expected to be `sqlalchemy`
     """
     Config = getattr(
         inspectable_compile("\n".join(
             (sqlalchemy_imports_str, "Base = object",
              config_decl_base_str))),
         "Config",
     )
     self.assertEqual(infer(Config), "sqlalchemy")
Beispiel #9
0
    def test_param2argparse_param_default_function(self) -> None:
        """
        Tests that param2argparse_param works to change the type based on the default
          whence said default is an in-memory function
        """

        function_str = (
            "from operator import add\n"
            "def adder(a, b):\n"
            "{tab}return add(a, b)".format(tab=tab)
        )
        adder = getattr(
            inspectable_compile(function_str),
            "adder",
        )
        pickled_adder = pickle.dumps(adder)  # eww

        run_ast_test(
            gen_ast=param2argparse_param(
                (
                    "byo",
                    {
                        "default": adder,
                        "typ": "str",
                    },
                ),
            ),
            gold=Expr(
                Call(
                    args=[set_value("--byo")],
                    func=Attribute(
                        Name("argument_parser", Load()),
                        "add_argument",
                        Load(),
                    ),
                    keywords=[
                        keyword(
                            arg="type",
                            value=Name("pickle.loads", Load()),
                            identifier=None,
                        ),
                        keyword(
                            arg="default",
                            value=set_value(pickled_adder),
                            identifier=None,
                        ),
                    ],
                    expr=None,
                    expr_func=None,
                )
            ),
            test_case_instance=self,
        )
Beispiel #10
0
 def test_from_adadelta_class_in_memory(self) -> None:
     """
     Tests that parse.class produces properly from a `class` in memory of current interpreter
     """
     Adadelta = getattr(
         inspectable_compile(docstring_google_tf_adadelta_function_str),
         "Adadelta",
     )
     ir = parse.class_(Adadelta)
     del ir["_internal"]
     # self.assertDictEqual(ir, docstring_google_tf_adadelta_ir)
     self.assertDictEqual(
         ir,
         docstring_google_tf_adadelta_function_ir,
     )
Beispiel #11
0
    def test_from_method_in_memory(self) -> None:
        """
        Tests that `parse.function` produces properly from a function in memory of current interpreter with:
        - kw only args;
        - default args;
        - annotations
        - required;
        - unannotated;
        - splat
        """

        method_complex_args_variety_with_imports_str = (
            "from sys import stdout\n"
            "from {package} import Literal\n"
            "{body}".format(
                package="typing" if PY_GTE_3_8 else "typing_extensions",
                body=method_complex_args_variety_str,
            ))
        call_cliff = getattr(
            inspectable_compile(method_complex_args_variety_with_imports_str),
            "call_cliff",
        )

        ir = parse.function(call_cliff)
        del ir["_internal"]  # Not needed for this test

        # This is a hack because JetBrains wraps stdout
        self.assertIn(
            type(ir["params"]["writer"]["default"]).__name__,
            frozenset(("FlushingStringIO", "TextIOWrapper")),
        )

        # This extra typ is copied, for now. TODO: Update AST-level parser to set types when defaults are given.

        del ir["params"]["writer"]["typ"]
        ir["params"]["writer"]["default"] = "```{}```".format(
            paren_wrap_code("stdout"))

        self.assertDictEqual(
            ir,
            method_complex_args_variety_ir,
        )