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")
Beispiel #2
0
    def test_update_missing_dir(self, mocker):
        from openapi_python_client import Project

        project = Project(openapi=mocker.MagicMock(title="My Test API"))
        project.package_dir = mocker.MagicMock()
        project.package_dir.is_dir.return_value = False
        project._build_models = mocker.MagicMock()

        with pytest.raises(FileNotFoundError):
            project.update()

        project.package_dir.is_dir.assert_called_once()
        project._build_models.assert_not_called()
    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()
Beispiel #4
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
Beispiel #5
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
    def test__build_api(self, mocker):
        import pathlib

        from jinja2 import Template

        from openapi_python_client import GeneratorData, Project

        openapi = mocker.MagicMock(autospec=GeneratorData, title="My Test API")
        tag_1 = "a_tag"
        tag_2 = "another_tag"
        collection_1 = mocker.MagicMock()
        collection_2 = mocker.MagicMock()
        openapi.endpoint_collections_by_tag = {
            tag_1: collection_1,
            tag_2: collection_2
        }
        project = Project(openapi=openapi)
        project.package_dir = mocker.MagicMock()
        api_errors = mocker.MagicMock(autospec=pathlib.Path)
        client_path = mocker.MagicMock()
        api_init = mocker.MagicMock(autospec=pathlib.Path)
        collection_1_path = mocker.MagicMock(autospec=pathlib.Path)
        collection_2_path = mocker.MagicMock(autospec=pathlib.Path)
        async_api_init = mocker.MagicMock(autospec=pathlib.Path)
        async_collection_1_path = mocker.MagicMock(autospec=pathlib.Path)
        async_collection_2_path = mocker.MagicMock(autospec=pathlib.Path)
        api_paths = {
            "__init__.py": api_init,
            f"{tag_1}.py": collection_1_path,
            f"{tag_2}.py": collection_2_path,
        }
        async_api_paths = {
            "__init__.py": async_api_init,
            f"{tag_1}.py": async_collection_1_path,
            f"{tag_2}.py": async_collection_2_path,
        }
        api_dir = mocker.MagicMock(autospec=pathlib.Path)
        api_dir.__truediv__.side_effect = lambda x: api_paths[x]
        async_api_dir = mocker.MagicMock(autospec=pathlib.Path)
        async_api_dir.__truediv__.side_effect = lambda x: async_api_paths[x]

        package_paths = {
            "client.py": client_path,
            "api": api_dir,
            "async_api": async_api_dir,
            "errors.py": api_errors,
        }
        project.package_dir.__truediv__.side_effect = lambda x: package_paths[x
                                                                              ]
        client_template = mocker.MagicMock(autospec=Template)
        errors_template = mocker.MagicMock(autospec=Template)
        endpoint_template = mocker.MagicMock(autospec=Template)
        async_endpoint_template = mocker.MagicMock(autospec=Template)
        templates = {
            "client.pyi": client_template,
            "errors.pyi": errors_template,
            "endpoint_module.pyi": endpoint_template,
            "async_endpoint_module.pyi": async_endpoint_template,
        }
        mocker.patch.object(project.env,
                            "get_template",
                            autospec=True,
                            side_effect=lambda x: templates[x])
        endpoint_renders = {
            collection_1: mocker.MagicMock(),
            collection_2: mocker.MagicMock(),
        }
        endpoint_template.render.side_effect = lambda collection: endpoint_renders[
            collection]
        async_endpoint_renders = {
            collection_1: mocker.MagicMock(),
            collection_2: mocker.MagicMock(),
        }
        async_endpoint_template.render.side_effect = lambda collection: async_endpoint_renders[
            collection]

        project._build_api()

        project.package_dir.__truediv__.assert_has_calls(
            [mocker.call(key) for key in package_paths])
        project.env.get_template.assert_has_calls(
            [mocker.call(key) for key in templates])
        client_template.render.assert_called_once()
        client_path.write_text.assert_called_once_with(
            client_template.render())
        errors_template.render.assert_called_once()
        api_errors.write_text.assert_called_once_with(errors_template.render())
        api_dir.mkdir.assert_called_once()
        api_dir.__truediv__.assert_has_calls(
            [mocker.call(key) for key in api_paths])
        api_init.write_text.assert_called_once_with(
            '""" Contains synchronous methods for accessing the API """')
        endpoint_template.render.assert_has_calls([
            mocker.call(collection=collection_1),
            mocker.call(collection=collection_2)
        ])
        collection_1_path.write_text.assert_called_once_with(
            endpoint_renders[collection_1])
        collection_2_path.write_text.assert_called_once_with(
            endpoint_renders[collection_2])
        async_api_dir.mkdir.assert_called_once()
        async_api_dir.__truediv__.assert_has_calls(
            [mocker.call(key) for key in async_api_paths])
        async_api_init.write_text.assert_called_once_with(
            '""" Contains async methods for accessing the API """')
        async_endpoint_template.render.assert_has_calls([
            mocker.call(collection=collection_1),
            mocker.call(collection=collection_2)
        ])
        async_collection_1_path.write_text.assert_called_once_with(
            async_endpoint_renders[collection_1])
        async_collection_2_path.write_text.assert_called_once_with(
            async_endpoint_renders[collection_2])
    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)