Ejemplo n.º 1
0
    def test_existent_file_fails(self) -> None:
        """Tests nonexistent file throws the right error"""
        with TemporaryDirectory() as tempdir:
            filename = os.path.join(
                tempdir,
                "delete_this_1{__file___basename}".format(
                    __file___basename=os.path.basename(__file__)
                ),
            )
            open(filename, "a").close()

            run_cli_test(
                self,
                [
                    "gen",
                    "--name-tpl",
                    "{name}Config",
                    "--input-mapping",
                    "cdd.pure_utils.simple_types",
                    "--emit",
                    "class",
                    "--output-filename",
                    filename,
                ],
                exception=OSError,
                exit_code=2,
                output="File exists and this is a destructive operation. Delete/move {filename!r} then rerun.".format(
                    filename=filename
                ),
            )
Ejemplo n.º 2
0
 def test_incorrect_arg_fails(self) -> None:
     """Tests CLI interface failure cases"""
     run_cli_test(
         self,
         ["sync", "--wrong"],
         exit_code=2,
         output="the following arguments are required: --truth\n",
     )
Ejemplo n.º 3
0
 def test_gen_fails(self) -> None:
     """Tests CLI interface failure cases"""
     run_cli_test(
         self,
         ["gen", "--wrong"],
         exit_code=2,
         output="the following arguments are required: --name-tpl, --input-mapping, --emit, --output-filename/-o\n",
     )
Ejemplo n.º 4
0
 def test_missing_argument_fails(self) -> None:
     """Tests missing argument throws the right error"""
     run_cli_test(
         self,
         ["sync", "--truth", "class"],
         exit_code=2,
         output="--truth must be an existent file. Got: None\n",
     )
Ejemplo n.º 5
0
 def test_version(self) -> None:
     """Tests CLI interface gives version"""
     run_cli_test(
         self,
         ["--version"],
         exit_code=0,
         output=__version__,
         output_checker=lambda output: output[output.rfind(" ") + 1:][:-1],
     )
Ejemplo n.º 6
0
 def test_exmod_fails(self) -> None:
     """Tests CLI interface exmod failure cases"""
     run_cli_test(
         self,
         ["exmod", "--wrong"],
         exit_code=2,
         output=
         "the following arguments are required: --module/-m, --emit, --output-directory/-o\n",
     )
Ejemplo n.º 7
0
 def test_gen_routes_fails(self) -> None:
     """Tests CLI interface failure cases"""
     run_cli_test(
         self,
         ["gen_routes", "--wrong"],
         exit_code=2,
         output=
         "the following arguments are required: --crud, --model-path, --model-name, --routes-path\n",
     )
 def test_sync_properties_fails(self) -> None:
     """Tests CLI interface failure cases"""
     run_cli_test(
         self,
         ["sync_properties", "--wrong"],
         exit_code=2,
         output="the following arguments are required:"
         " --input-filename, --input-param, --output-filename, --output-param\n",
     )
Ejemplo n.º 9
0
    def test_doctrans_fails_with_wrong_args(self) -> None:
        """Tests CLI interface wrong args failure case"""

        run_cli_test(
            self,
            ["doctrans", "--wrong"],
            exit_code=2,
            output="the following arguments are required: --filename, --format\n",
        )
Ejemplo n.º 10
0
    def test_non_existent_file_fails(self) -> None:
        """Tests nonexistent file throws the right error"""
        with TemporaryDirectory() as tempdir:
            filename = os.path.join(
                tempdir,
                "delete_this_1{}".format(os.path.basename(__file__)),
            )

            run_cli_test(
                self,
                [
                    "sync_properties",
                    "--input-file",
                    filename,
                    "--input-param",
                    "Foo.g.f",
                    "--output-file",
                    filename,
                    "--output-param",
                    "f.h",
                ],
                exit_code=2,
                output=
                "--input-file must be an existent file. Got: {filename!r}\n".
                format(filename=filename),
            )

        with TemporaryDirectory() as tempdir:
            input_filename = os.path.join(
                tempdir,
                "input_filename{extsep}py".format(extsep=extsep),
            )
            output_filename = os.path.join(
                tempdir,
                "output_filename{extsep}py".format(extsep=extsep),
            )
            open(input_filename, "wt").close()

            run_cli_test(
                self,
                [
                    "sync_properties",
                    "--input-file",
                    input_filename,
                    "--input-param",
                    "Foo.g.f",
                    "--output-file",
                    output_filename,
                    "--output-param",
                    "f.h",
                ],
                exit_code=2,
                output=
                "--output-file must be an existent file. Got: {output_filename!r}\n"
                .format(output_filename=output_filename),
            )
Ejemplo n.º 11
0
    def test_sync_properties(self) -> None:
        """Tests CLI interface gets all the way to the sync_properties call without error"""
        with TemporaryDirectory() as tempdir:
            input_filename = os.path.join(
                tempdir, "class_{extsep}py".format(extsep=extsep))
            output_filename = os.path.join(
                tempdir, "method{extsep}py".format(extsep=extsep))
            open(input_filename, "wt").close()
            open(output_filename, "wt").close()

            with patch("cdd.__main__.sync_properties", mock_function):
                self.assertTrue(
                    run_cli_test(
                        self,
                        [
                            "sync_properties",
                            "--input-file",
                            input_filename,
                            "--input-param",
                            "Foo.g.f",
                            "--output-file",
                            output_filename,
                            "--output-param",
                            "f.h",
                        ],
                        exit_code=None,
                        output=None,
                    ), )
Ejemplo n.º 12
0
 def test_missing_argument_fails_insufficient_args(self) -> None:
     """Tests missing argument throws the right error"""
     with TemporaryDirectory() as tempdir:
         filename = os.path.join(
             os.path.realpath(tempdir),
             "delete_this_2{}".format(os.path.basename(__file__)),
         )
         with open(filename, "wt") as f:
             f.write(class_str)
         run_cli_test(
             self,
             ["sync", "--truth", "class", "--class", filename],
             exit_code=2,
             output=
             "Two or more of `--argparse-function`, `--class`, and `--function` must be specified\n",
         )
Ejemplo n.º 13
0
    def test_args_example1(self) -> None:
        """Tests CLI interface sets namespace correctly"""

        with TemporaryDirectory() as tempdir:
            argparse_filename = os.path.join(
                os.path.realpath(tempdir),
                "argparse{extsep}py".format(extsep=extsep),
            )
            class_filename = os.path.join(
                os.path.realpath(tempdir),
                "class_{extsep}py".format(extsep=extsep),
            )
            method_filename = os.path.join(
                os.path.realpath(tempdir),
                "method{extsep}py".format(extsep=extsep),
            )

            with open(argparse_filename, "wt") as f:
                f.write(argparse_func_str)
            with open(class_filename, "wt") as f:
                f.write(class_str)
            with open(method_filename, "wt") as f:
                f.write(class_with_method_types_str)

            _, args = run_cli_test(
                self,
                [
                    "sync",
                    "--class",
                    class_filename,
                    "--class-name",
                    "ConfigClass",
                    "--function",
                    method_filename,
                    "--function-name",
                    "train",
                    "--argparse-function",
                    argparse_filename,
                    "--argparse-function-name",
                    "set_cli_args",
                    "--truth",
                    "function",
                ],
                exit_code=None,
                output=None,
                return_args=True,
            )

            self.assertListEqual(args.argparse_functions, [argparse_filename])
            self.assertListEqual(args.argparse_function_names,
                                 ["set_cli_args"])

            self.assertListEqual(args.classes, [class_filename])
            self.assertListEqual(args.class_names, ["ConfigClass"])

            self.assertEqual(args.truth, "function")
Ejemplo n.º 14
0
    def test_non_existent_file_fails(self) -> None:
        """Tests nonexistent file throws the right error"""
        with TemporaryDirectory() as tempdir:
            filename = os.path.join(
                os.path.realpath(tempdir),
                "delete_this_1{}".format(os.path.basename(__file__)),
            )

            run_cli_test(
                self,
                [
                    "sync",
                    "--argparse-function",
                    filename,
                    "--class",
                    filename,
                    "--truth",
                    "class",
                ],
                exit_code=2,
                output="--truth must be an existent file. Got: {filename!r}\n".
                format(filename=filename),
            )
Ejemplo n.º 15
0
    def test_gen(self) -> None:
        """Tests CLI interface gets all the way to the gen call without error"""
        with TemporaryDirectory() as tempdir:
            output_filename = os.path.join(
                tempdir, "classes{extsep}py".format(extsep=extsep)
            )

            with patch("cdd.__main__.gen", mock_function):
                run_cli_test(
                    self,
                    [
                        "gen",
                        "--name-tpl",
                        "{name}Config",
                        "--input-mapping",
                        "cdd.pure_utils.simple_types",
                        "--emit",
                        "class",
                        "--output-filename",
                        output_filename,
                    ],
                    exit_code=None,
                    output=None,
                )
Ejemplo n.º 16
0
 def test_exmod_is_called(self):
     """Tests CLI interface exmod function gets called"""
     with patch("cdd.__main__.exmod", new_callable=MagicMock()):
         self.assertTrue(
             run_cli_test(
                 self,
                 [
                     "exmod",
                     "--module",
                     "foo",
                     "--emit",
                     "argparse",
                     "--output-directory",
                     "foo",
                 ],
                 exit_code=None,
                 output=None,
             ), )
Ejemplo n.º 17
0
    def test_doctrans_fails_with_file_missing(self) -> None:
        """Tests CLI interface file missing failure case"""

        with patch("cdd.__main__.doctrans", mock_function):
            self.assertTrue(
                run_cli_test(
                    self,
                    [
                        "doctrans",
                        "--filename",
                        "foo",
                        "--format",
                        "google",
                        "--no-type-annotations",
                    ],
                    exit_code=2,
                    output="--filename must be an existent file. Got: 'foo'\n",
                ),
            )
Ejemplo n.º 18
0
    def test_openapi(self) -> None:
        """Tests CLI interface gets all the way to the `openapi` call without error"""

        with patch("cdd.__main__.openapi_bulk", mock_function):
            self.assertTrue(
                run_cli_test(
                    self,
                    [
                        "openapi",
                        "--app-name",
                        "app",
                        "--model-paths",
                        "cdd.tests.mocks.sqlalchemy",
                        "--routes-paths",
                        "cdd.tests.mocks.routes",
                    ],
                    exit_code=None,
                    output=None,
                ), )
Ejemplo n.º 19
0
    def test_args_example0(self) -> None:
        """Tests CLI interface sets namespace correctly"""
        with TemporaryDirectory() as tempdir:
            filename = os.path.join(
                os.path.realpath(tempdir),
                "delete_this_0{}".format(os.path.basename(__file__)),
            )
            with open(filename, "wt") as f:
                f.write(class_str)
            try:
                _, args = run_cli_test(
                    self,
                    [
                        "sync",
                        "--class",
                        filename,
                        "--class-name",
                        "ConfigClass",
                        "--argparse-function",
                        filename,
                        "--argparse-function-name",
                        "set_cli_args",
                        "--truth",
                        "class",
                    ],
                    exit_code=None,
                    output=None,
                    return_args=True,
                )
            finally:
                if os.path.isfile(filename):
                    os.remove(filename)

        self.assertListEqual(args.argparse_functions, [filename])
        self.assertListEqual(args.argparse_function_names, ["set_cli_args"])

        self.assertListEqual(args.classes, [filename])
        self.assertListEqual(args.class_names, ["ConfigClass"])

        self.assertEqual(args.truth, "class")
Ejemplo n.º 20
0
    def test_doctrans_succeeds(self) -> None:
        """Tests CLI interface gets all the way to the doctrans call without error"""

        with TemporaryDirectory() as tempdir:
            filename = path.join(tempdir, "foo")
            open(filename, "a").close()
            with patch("cdd.__main__.doctrans", mock_function):
                self.assertTrue(
                    run_cli_test(
                        self,
                        [
                            "doctrans",
                            "--filename",
                            filename,
                            "--format",
                            "numpydoc",
                            "--type-annotations",
                        ],
                        exit_code=None,
                        output=None,
                    ),
                )
Ejemplo n.º 21
0
    def test_gen_routes(self) -> None:
        """Tests CLI interface gets all the way to the gen_routes call without error"""

        with patch("cdd.__main__.gen_routes", lambda *args, **kwargs:
                   (True, ) * 2), patch("cdd.__main__.upsert_routes",
                                        mock_function):
            self.assertTrue(
                run_cli_test(
                    self,
                    [
                        "gen_routes",
                        "--crud",
                        "CRD",
                        "--model-path",
                        "cdd.tests.mocks.sqlalchemy",
                        "--routes-path",
                        "/api/config",
                        "--model-name",
                        "Config",
                    ],
                    exit_code=None,
                    output=None,
                ), )