def test__build_setup_py(self, mocker): from openapi_python_client import MetaType, Project project = Project(openapi=mocker.MagicMock(title="My Test API"), meta=MetaType.SETUP) project.project_dir = mocker.MagicMock() setup_path = mocker.MagicMock(autospec=pathlib.Path) paths = { "setup.py": setup_path, } project.project_dir.__truediv__.side_effect = lambda x: paths[x] setup_template = mocker.MagicMock(autospec=jinja2.Template) project.env = mocker.MagicMock(autospec=jinja2.Environment) templates = { "setup.py.jinja": setup_template, } project.env.get_template.side_effect = lambda x: templates[x] project._build_setup_py() project.env.get_template.assert_called_once_with("setup.py.jinja") setup_template.render.assert_called_once_with( project_name=project.project_name, package_name=project.package_name, version=project.version, description=project.package_description, ) setup_path.write_text.assert_called_once_with(setup_template.render(), encoding="utf-8")
def test__build_pyproject_toml(self, mocker, use_poetry): from openapi_python_client import MetaType, Project project = Project(openapi=mocker.MagicMock(title="My Test API"), meta=MetaType.POETRY) project.project_dir = mocker.MagicMock() pyproject_path = mocker.MagicMock(autospec=pathlib.Path) paths = { "pyproject.toml": pyproject_path, } project.project_dir.__truediv__.side_effect = lambda x: paths[x] pyproject_template = mocker.MagicMock(autospec=jinja2.Template) project.env = mocker.MagicMock(autospec=jinja2.Environment) template_path = "pyproject.toml.jinja" if use_poetry else "pyproject_no_poetry.toml.jinja" templates = { template_path: pyproject_template, } project.env.get_template.side_effect = lambda x: templates[x] project._build_pyproject_toml(use_poetry=use_poetry) project.env.get_template.assert_called_once_with(template_path) 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(), encoding="utf-8")
def test__create_package(self, mocker): from openapi_python_client import Project project = Project(openapi=mocker.MagicMock(title="My Test API")) project.package_dir = mocker.MagicMock() package_init_template = mocker.MagicMock() project.env = mocker.MagicMock() project.env.get_template.return_value = package_init_template package_init_path = mocker.MagicMock(autospec=pathlib.Path) pytyped_path = mocker.MagicMock(autospec=pathlib.Path) paths = { "__init__.py": package_init_path, "py.typed": pytyped_path, } project.package_dir.__truediv__.side_effect = lambda x: paths[x] project._create_package() project.package_dir.mkdir.assert_called_once() project.env.get_template.assert_called_once_with("package_init.pyi") package_init_template.render.assert_called_once_with( description=project.package_description) package_init_path.write_text.assert_called_once_with( package_init_template.render()) pytyped_path.write_text.assert_called_once_with( "# Marker file for PEP 561")
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_models(self, mocker): from openapi_python_client import GeneratorData, Project openapi = mocker.MagicMock(autospec=GeneratorData, title="My Test API") model_1 = mocker.MagicMock() model_2 = mocker.MagicMock() openapi.schemas.models = {"1": model_1, "2": model_2} enum_1 = mocker.MagicMock() enum_2 = mocker.MagicMock() openapi.enums = {"1": enum_1, "2": enum_2} project = Project(openapi=openapi) project.package_dir = mocker.MagicMock() models_init = mocker.MagicMock() types_py = mocker.MagicMock() models_dir = mocker.MagicMock() model_1_module_path = mocker.MagicMock() model_2_module_path = mocker.MagicMock() enum_1_module_path = mocker.MagicMock() enum_2_module_path = mocker.MagicMock() module_paths = { "__init__.py": models_init, "types.py": types_py, f"{model_1.reference.module_name}.py": model_1_module_path, f"{model_2.reference.module_name}.py": model_2_module_path, f"{enum_1.reference.module_name}.py": enum_1_module_path, f"{enum_2.reference.module_name}.py": enum_2_module_path, } def models_dir_get(x): return module_paths[x] models_dir.__truediv__.side_effect = models_dir_get project.package_dir.__truediv__.return_value = models_dir model_render_1 = mocker.MagicMock() model_render_2 = mocker.MagicMock() model_template = mocker.MagicMock() model_template.render.side_effect = [model_render_1, model_render_2] enum_render_1 = mocker.MagicMock() enum_render_2 = mocker.MagicMock() enum_template = mocker.MagicMock() enum_renders = { enum_1: enum_render_1, enum_2: enum_render_2, } enum_template.render.side_effect = lambda enum: enum_renders[enum] models_init_template = mocker.MagicMock() types_template = mocker.MagicMock() templates = { "types.py": types_template, "model.pyi": model_template, "enum.pyi": enum_template, "models_init.pyi": models_init_template, } project.env = mocker.MagicMock() project.env.get_template.side_effect = lambda x: templates[x] imports = [ "import_schema_1", "import_schema_2", "import_enum_1", "import_enum_2", ] import_string_from_reference = mocker.patch( "openapi_python_client.import_string_from_reference", side_effect=imports) project._build_models() project.package_dir.__truediv__.assert_called_once_with("models") models_dir.mkdir.assert_called_once() models_dir.__truediv__.assert_has_calls( [mocker.call(key) for key in module_paths]) project.env.get_template.assert_has_calls( [mocker.call(key) for key in templates]) model_template.render.assert_has_calls( [mocker.call(model=model_1), mocker.call(model=model_2)]) model_1_module_path.write_text.assert_called_once_with(model_render_1) model_2_module_path.write_text.assert_called_once_with(model_render_2) import_string_from_reference.assert_has_calls([ mocker.call(model_1.reference), mocker.call(model_2.reference), mocker.call(enum_1.reference), mocker.call(enum_2.reference), ]) models_init_template.render.assert_called_once_with(imports=imports) types_template.render.assert_called_once() enum_1_module_path.write_text.assert_called_once_with(enum_render_1) enum_2_module_path.write_text.assert_called_once_with(enum_render_2)