def test__build_metadata_none(self, mocker):
        from openapi_python_client import MetaType, Project

        project = Project(openapi=mocker.MagicMock(title="My Test API"),
                          meta=MetaType.NONE)
        project._build_pyproject_toml = mocker.MagicMock()

        project._build_metadata()

        project._build_pyproject_toml.assert_not_called()
Example #2
0
    def test__build_metadata(self, mocker):
        from openapi_python_client import Project

        project = Project(openapi=mocker.MagicMock(title="My Test API"))
        project.project_dir = mocker.MagicMock()
        pyproject_path = mocker.MagicMock(autospec=pathlib.Path)
        readme_path = mocker.MagicMock(autospec=pathlib.Path)
        git_ignore_path = mocker.MagicMock(autospec=pathlib.Path)
        paths = {
            "pyproject.toml": pyproject_path,
            "README.md": readme_path,
            ".gitignore": git_ignore_path,
        }
        project.project_dir.__truediv__.side_effect = lambda x: paths[x]

        pyproject_template = mocker.MagicMock(autospec=jinja2.Template)
        readme_template = mocker.MagicMock(autospec=jinja2.Template)
        git_ignore_template = mocker.MagicMock(autospec=jinja2.Template)
        project.env = mocker.MagicMock(autospec=jinja2.Environment)
        templates = {
            "pyproject.toml": pyproject_template,
            "README.md": readme_template,
            ".gitignore": git_ignore_template,
        }
        project.env.get_template.side_effect = lambda x: templates[x]

        project._build_metadata()

        project.env.get_template.assert_has_calls([
            mocker.call("pyproject.toml"),
            mocker.call("README.md"),
            mocker.call(".gitignore")
        ])

        pyproject_template.render.assert_called_once_with(
            project_name=project.project_name,
            package_name=project.package_name,
            version=project.version,
            description=project.package_description,
        )
        pyproject_path.write_text.assert_called_once_with(
            pyproject_template.render())
        readme_template.render.assert_called_once_with(
            description=project.package_description,
            project_name=project.project_name,
            package_name=project.package_name,
        )
        readme_path.write_text.assert_called_once_with(
            readme_template.render())
        git_ignore_template.render.assert_called_once()
        git_ignore_path.write_text.assert_called_once_with(
            git_ignore_template.render())
    def test__build_metadata_setup(self, mocker):
        from openapi_python_client import MetaType, Project

        project = Project(openapi=mocker.MagicMock(title="My Test API"),
                          meta=MetaType.SETUP)
        project._build_pyproject_toml = mocker.MagicMock()
        project._build_setup_py = mocker.MagicMock()
        project.project_dir = mocker.MagicMock()
        readme_path = mocker.MagicMock(autospec=pathlib.Path)
        git_ignore_path = mocker.MagicMock(autospec=pathlib.Path)
        paths = {
            "README.md": readme_path,
            ".gitignore": git_ignore_path,
        }
        project.project_dir.__truediv__.side_effect = lambda x: paths[x]

        readme_template = mocker.MagicMock(autospec=jinja2.Template)
        git_ignore_template = mocker.MagicMock(autospec=jinja2.Template)
        project.env = mocker.MagicMock(autospec=jinja2.Environment)
        templates = {
            "README.md.jinja": readme_template,
            ".gitignore.jinja": git_ignore_template,
        }
        project.env.get_template.side_effect = lambda x: templates[x]

        project._build_metadata()

        project.env.get_template.assert_has_calls(
            [mocker.call("README.md.jinja"),
             mocker.call(".gitignore.jinja")])
        readme_template.render.assert_called_once_with(
            description=project.package_description,
            project_name=project.project_name,
            package_name=project.package_name,
        )
        readme_path.write_text.assert_called_once_with(
            readme_template.render(), encoding="utf-8")
        git_ignore_template.render.assert_called_once()
        git_ignore_path.write_text.assert_called_once_with(
            git_ignore_template.render(), encoding="utf-8")
        project._build_pyproject_toml.assert_called_once_with(use_poetry=False)
        project._build_setup_py.assert_called_once()
    def test_build_no_meta(self, mocker):
        from openapi_python_client import MetaType, Project

        project = Project(openapi=mocker.MagicMock(title="My Test API"),
                          meta=MetaType.NONE)
        project.project_dir = mocker.MagicMock()
        project.package_dir = mocker.MagicMock()
        project._build_metadata = mocker.MagicMock()
        project._build_models = mocker.MagicMock()
        project._build_api = mocker.MagicMock()
        project._create_package = mocker.MagicMock()
        project._reformat = mocker.MagicMock()
        project._get_errors = mocker.MagicMock()

        project.build()

        project.project_dir.mkdir.assert_not_called()
Example #5
0
    def test_update(self, mocker):
        from openapi_python_client import Project, shutil

        rmtree = mocker.patch.object(shutil, "rmtree")
        project = Project(openapi=mocker.MagicMock(title="My Test API"))
        project.package_dir = mocker.MagicMock()
        project._build_metadata = mocker.MagicMock()
        project._build_models = mocker.MagicMock()
        project._build_api = mocker.MagicMock()
        project._create_package = mocker.MagicMock()
        project._reformat = mocker.MagicMock()
        project._get_errors = mocker.MagicMock()

        result = project.update()

        rmtree.assert_called_once_with(project.package_dir)
        project._create_package.assert_called_once()
        project._build_models.assert_called_once()
        project._build_api.assert_called_once()
        project._reformat.assert_called_once()
        project._get_errors.assert_called_once()
        assert result == project._get_errors.return_value
Example #6
0
    def test_build(self, mocker):
        from openapi_python_client import Project

        project = Project(openapi=mocker.MagicMock(title="My Test API"))
        project.project_dir = mocker.MagicMock()
        project.package_dir = mocker.MagicMock()
        project._build_metadata = mocker.MagicMock()
        project._build_models = mocker.MagicMock()
        project._build_api = mocker.MagicMock()
        project._create_package = mocker.MagicMock()
        project._reformat = mocker.MagicMock()
        project._get_errors = mocker.MagicMock()

        result = project.build()

        project.project_dir.mkdir.assert_called_once()
        project._create_package.assert_called_once()
        project._build_metadata.assert_called_once()
        project._build_models.assert_called_once()
        project._build_api.assert_called_once()
        project._reformat.assert_called_once()
        project._get_errors.assert_called_once()
        assert result == project._get_errors.return_value