Exemplo n.º 1
0
    def test_swagger_document_invalid_schema(self, mocker):
        Schemas = mocker.patch(f"{MODULE_NAME}.Schemas")
        config = mocker.MagicMock()

        in_dict = {"swagger": "2.0"}

        from openapi_python_client.parser.openapi import GeneratorData

        generator_data = GeneratorData.from_dict(in_dict, config=config)

        assert generator_data == GeneratorError(
            header="Failed to parse OpenAPI document",
            detail=(
                "You may be trying to use a Swagger document; this is not supported by this project.\n\n"
                "3 validation errors for OpenAPI\n"
                "info\n"
                "  field required (type=value_error.missing)\n"
                "paths\n"
                "  field required (type=value_error.missing)\n"
                "openapi\n"
                "  field required (type=value_error.missing)"
            ),
        )
        Schemas.build.assert_not_called()
        Schemas.assert_not_called()
Exemplo n.º 2
0
    def test__get_document_no_url_or_path(self, mocker):
        get = mocker.patch("httpx.get")
        _Path = mocker.patch("openapi_python_client.Path")
        loads = mocker.patch("yaml.safe_load")

        from openapi_python_client import _get_document

        result = _get_document(url=None, path=None)

        assert result == GeneratorError(header="No URL or Path provided")
        get.assert_not_called()
        _Path.assert_not_called()
        loads.assert_not_called()
Exemplo n.º 3
0
    def test_build_file_exists(self, mocker):
        project = make_project()
        project.project_dir = mocker.MagicMock()
        project.project_dir.mkdir.side_effect = FileExistsError
        result = project.build()

        project.project_dir.mkdir.assert_called_once()

        assert result == [
            GeneratorError(
                detail=
                "Directory already exists. Delete it or use the update command."
            )
        ]
Exemplo n.º 4
0
    def test__get_document_url_and_path(self, mocker):
        get = mocker.patch("httpx.get")
        _Path = mocker.patch("openapi_python_client.Path")
        loads = mocker.patch("yaml.safe_load")

        from openapi_python_client import _get_document

        result = _get_document(url=mocker.MagicMock(), path=mocker.MagicMock())

        assert result == GeneratorError(
            header="Provide URL or Path, not both.")
        get.assert_not_called()
        _Path.assert_not_called()
        loads.assert_not_called()
Exemplo n.º 5
0
    def test__get_document_bad_yaml(self, mocker):
        get = mocker.patch("httpx.get")
        loads = mocker.patch("yaml.safe_load", side_effect=yaml.YAMLError)

        from openapi_python_client import _get_document

        path = mocker.MagicMock()
        result = _get_document(url=None, path=path)

        get.assert_not_called()
        path.read_bytes.assert_called_once()
        loads.assert_called_once_with(path.read_bytes())
        assert result == GeneratorError(
            header="Invalid YAML from provided source")
def test_create_new_client_project_error(mocker):
    error = GeneratorError()
    _get_project_for_url_or_path = mocker.patch(
        "openapi_python_client._get_project_for_url_or_path",
        return_value=error)
    url = mocker.MagicMock()
    path = mocker.MagicMock()

    from openapi_python_client import create_new_client

    result = create_new_client(url=url, path=path)

    _get_project_for_url_or_path.assert_called_once_with(url=url, path=path)
    assert result == [error]
Exemplo n.º 7
0
    def test__get_document_bad_url(self, mocker):
        get = mocker.patch("httpx.get", side_effect=httpcore.NetworkError)
        _Path = mocker.patch("openapi_python_client.Path")
        loads = mocker.patch("yaml.safe_load")

        from openapi_python_client import _get_document

        url = mocker.MagicMock()
        result = _get_document(url=url, path=None)

        assert result == GeneratorError(
            header="Could not get OpenAPI document from provided URL")
        get.assert_called_once_with(url)
        _Path.assert_not_called()
        loads.assert_not_called()
Exemplo n.º 8
0
def test_create_new_client_project_error(mocker):
    error = GeneratorError()
    _get_project_for_url_or_path = mocker.patch(
        "openapi_python_client._get_project_for_url_or_path",
        return_value=error)
    url = mocker.MagicMock()
    path = mocker.MagicMock()

    from openapi_python_client import MetaType, create_new_client

    result = create_new_client(url=url, path=path, meta=MetaType.POETRY)

    _get_project_for_url_or_path.assert_called_once_with(
        url=url, path=path, custom_template_path=None, meta=MetaType.POETRY)
    assert result == [error]
Exemplo n.º 9
0
def test_update_existing_client_project_error(mocker):
    error = GeneratorError()
    _get_project_for_url_or_path = mocker.patch(
        "openapi_python_client._get_project_for_url_or_path",
        return_value=error)
    url = mocker.MagicMock()
    path = mocker.MagicMock()

    from openapi_python_client import update_existing_client

    result = update_existing_client(url=url, path=path)

    _get_project_for_url_or_path.assert_called_once_with(
        url=url, path=path, custom_template_path=None)
    assert result == [error]
Exemplo n.º 10
0
    def test__get_document_bad_json(self, mocker):
        class FakeResponse:
            content = b'{"foo"}'
            headers = {"content-type": "application/json; encoding=utf8"}

        get = mocker.patch("httpx.get", return_value=FakeResponse())

        from openapi_python_client import _get_document

        url = mocker.MagicMock()
        result = _get_document(url=url, path=None)

        get.assert_called_once()
        assert result == GeneratorError(
            header="Invalid JSON from provided source: "
            "Expecting ':' delimiter: line 1 column 7 (char 6)")
Exemplo n.º 11
0
    def test_build_file_exists(self, mocker):
        from openapi_python_client import Project

        project = Project(openapi=mocker.MagicMock(title="My Test API"))
        project.project_dir = mocker.MagicMock()
        project.project_dir.mkdir.side_effect = FileExistsError
        result = project.build()

        project.project_dir.mkdir.assert_called_once()

        assert result == [
            GeneratorError(
                detail=
                "Directory already exists. Delete it or use the update command."
            )
        ]
Exemplo n.º 12
0
def test__get_project_for_url_or_path_document_error(mocker):
    error = GeneratorError()
    _get_document = mocker.patch("openapi_python_client._get_document",
                                 return_value=error)

    from_dict = mocker.patch(
        "openapi_python_client.parser.GeneratorData.from_dict")
    url = mocker.MagicMock()
    path = mocker.MagicMock()

    from openapi_python_client import _get_project_for_url_or_path

    project = _get_project_for_url_or_path(url=url, path=path)

    _get_document.assert_called_once_with(url=url, path=path)
    from_dict.assert_not_called()
    assert project == error
Exemplo n.º 13
0
    def test_from_dict_invalid_version(self, mocker):
        Schemas = mocker.patch(f"{MODULE_NAME}.Schemas")
        OpenAPI = mocker.patch(f"{MODULE_NAME}.oai.OpenAPI")
        openapi = OpenAPI.parse_obj.return_value
        openapi.openapi = oai.SemVer("2.1.3")
        in_dict = mocker.MagicMock()
        config = mocker.MagicMock()

        from openapi_python_client.parser.openapi import GeneratorData

        generator_data = GeneratorData.from_dict(in_dict, config=config)

        assert generator_data == GeneratorError(
            header="openapi-python-client only supports OpenAPI 3.x",
            detail="The version of the provided document was 2.1.3",
        )
        Schemas.build.assert_not_called()
        Schemas.assert_not_called()
Exemplo n.º 14
0
    def test_from_dict_invalid_schema(self, mocker):
        Schemas = mocker.patch(f"{MODULE_NAME}.Schemas")

        in_dict = {}

        from openapi_python_client.parser.openapi import GeneratorData

        generator_data = GeneratorData.from_dict(in_dict)

        assert generator_data == GeneratorError(
            header="Failed to parse OpenAPI document",
            detail=("2 validation errors for OpenAPI\n"
                    "info\n"
                    "  field required (type=value_error.missing)\n"
                    "paths\n"
                    "  field required (type=value_error.missing)"),
        )
        Schemas.build.assert_not_called()
        Schemas.assert_not_called()
Exemplo n.º 15
0
def test__get_project_for_url_or_path_generator_error(mocker):
    data_dict = mocker.MagicMock()
    _get_document = mocker.patch("openapi_python_client._get_document",
                                 return_value=data_dict)
    error = GeneratorError()
    from_dict = mocker.patch(
        "openapi_python_client.parser.GeneratorData.from_dict",
        return_value=error)
    _Project = mocker.patch("openapi_python_client.Project")
    url = mocker.MagicMock()
    path = mocker.MagicMock()

    from openapi_python_client import MetaType, _get_project_for_url_or_path

    project = _get_project_for_url_or_path(url=url,
                                           path=path,
                                           meta=MetaType.POETRY)

    _get_document.assert_called_once_with(url=url, path=path)
    from_dict.assert_called_once_with(data_dict)
    _Project.assert_not_called()
    assert project == error
Exemplo n.º 16
0
def test_update_existing_client_project_error(mocker):
    error = GeneratorError()
    _get_project_for_url_or_path = mocker.patch(
        "openapi_python_client._get_project_for_url_or_path",
        return_value=error)
    url = mocker.MagicMock()
    path = mocker.MagicMock()
    config = mocker.MagicMock()

    from openapi_python_client import MetaType, update_existing_client

    result = update_existing_client(url=url,
                                    path=path,
                                    meta=MetaType.POETRY,
                                    config=config)

    _get_project_for_url_or_path.assert_called_once_with(
        url=url,
        path=path,
        custom_template_path=None,
        meta=MetaType.POETRY,
        file_encoding="utf-8",
        config=config)
    assert result == [error]