Exemple #1
0
    def test__add_parameters_parse_error(self, mocker):
        from openapi_python_client.parser.openapi import Endpoint

        endpoint = Endpoint(
            path="path",
            method="method",
            description=None,
            name="name",
            requires_security=False,
            tag="tag",
        )
        initial_schemas = mocker.MagicMock()
        parse_error = ParseError(data=mocker.MagicMock())
        property_schemas = mocker.MagicMock()
        mocker.patch(f"{MODULE_NAME}.property_from_data",
                     return_value=(parse_error, property_schemas))
        param = oai.Parameter.construct(name="test",
                                        required=True,
                                        param_schema=mocker.MagicMock(),
                                        param_in="cookie")

        result = Endpoint._add_parameters(
            endpoint=endpoint,
            data=oai.Operation.construct(parameters=[param]),
            schemas=initial_schemas)
        assert result == (
            ParseError(
                data=parse_error.data,
                detail=f"cannot parse parameter of endpoint {endpoint.name}"),
            property_schemas,
        )
Exemple #2
0
    def test_add_body_bad_data(self, mocker):
        from openapi_python_client.parser.openapi import Endpoint, Schemas

        mocker.patch.object(Endpoint, "parse_request_form_body")
        parse_error = ParseError(data=mocker.MagicMock())
        other_schemas = mocker.MagicMock()
        mocker.patch.object(Endpoint,
                            "parse_request_json_body",
                            return_value=(parse_error, other_schemas))
        endpoint = Endpoint(
            path="path",
            method="method",
            description=None,
            name="name",
            requires_security=False,
            tag="tag",
            relative_imports={"import_3"},
        )
        request_body = mocker.MagicMock()
        schemas = Schemas()

        result = Endpoint._add_body(
            endpoint=endpoint,
            data=oai.Operation.construct(requestBody=request_body),
            schemas=schemas)

        assert result == (
            ParseError(detail=f"cannot parse body of endpoint {endpoint.name}",
                       data=parse_error.data),
            other_schemas,
        )
Exemple #3
0
    def test__add_responses_error(self, mocker):
        from openapi_python_client.parser.openapi import Endpoint, Schemas

        schemas = Schemas()
        response_1_data = mocker.MagicMock()
        response_2_data = mocker.MagicMock()
        data = {
            "200": response_1_data,
            "404": response_2_data,
        }
        endpoint = self.make_endpoint()
        parse_error = ParseError(data=mocker.MagicMock())
        response_from_data = mocker.patch(f"{MODULE_NAME}.response_from_data", return_value=(parse_error, schemas))
        config = MagicMock()

        response, schemas = Endpoint._add_responses(endpoint=endpoint, data=data, schemas=schemas, config=config)

        response_from_data.assert_has_calls(
            [
                mocker.call(status_code=200, data=response_1_data, schemas=schemas, parent_name="name", config=config),
                mocker.call(status_code=404, data=response_2_data, schemas=schemas, parent_name="name", config=config),
            ]
        )
        assert response.errors == [
            ParseError(
                detail=f"Cannot parse response for status code 200, response will be ommitted from generated client",
                data=parse_error.data,
            ),
            ParseError(
                detail=f"Cannot parse response for status code 404, response will be ommitted from generated client",
                data=parse_error.data,
            ),
        ]
Exemple #4
0
    def test_add_body_bad_data(self, mocker):
        from openapi_python_client.parser.openapi import Endpoint, Schemas

        mocker.patch.object(Endpoint, "parse_request_form_body")
        parse_error = ParseError(data=mocker.MagicMock())
        other_schemas = mocker.MagicMock()
        mocker.patch.object(Endpoint,
                            "parse_request_json_body",
                            return_value=(parse_error, other_schemas))
        endpoint = self.make_endpoint()
        request_body = mocker.MagicMock()
        schemas = Schemas()

        result = Endpoint._add_body(
            endpoint=endpoint,
            data=oai.Operation.construct(requestBody=request_body),
            schemas=schemas,
            config=MagicMock(),
        )

        assert result == (
            ParseError(detail=f"cannot parse body of endpoint {endpoint.name}",
                       data=parse_error.data),
            other_schemas,
        )
Exemple #5
0
    def test__add_parameters_parse_error(self, mocker):
        from openapi_python_client.parser.openapi import Endpoint

        endpoint = self.make_endpoint()
        initial_schemas = mocker.MagicMock()
        parse_error = ParseError(data=mocker.MagicMock())
        property_schemas = mocker.MagicMock()
        mocker.patch(f"{MODULE_NAME}.property_from_data",
                     return_value=(parse_error, property_schemas))
        param = oai.Parameter.construct(name="test",
                                        required=True,
                                        param_schema=mocker.MagicMock(),
                                        param_in="cookie")
        config = MagicMock()

        result = Endpoint._add_parameters(
            endpoint=endpoint,
            data=oai.Operation.construct(parameters=[param]),
            schemas=initial_schemas,
            config=config)
        assert result == (
            ParseError(
                data=parse_error.data,
                detail=f"cannot parse parameter of endpoint {endpoint.name}"),
            property_schemas,
        )
Exemple #6
0
    def test__add_responses_status_code_error(self, mocker):
        from openapi_python_client.parser.openapi import Endpoint, Schemas

        schemas = Schemas()
        response_1_data = mocker.MagicMock()
        response_2_data = mocker.MagicMock()
        data = {
            "not_a_number": response_1_data,
        }
        endpoint = Endpoint(
            path="path",
            method="method",
            description=None,
            name="name",
            requires_security=False,
            tag="tag",
            relative_imports={"import_3"},
        )
        parse_error = ParseError(data=mocker.MagicMock())
        response_from_data = mocker.patch(f"{MODULE_NAME}.response_from_data",
                                          return_value=(parse_error, schemas))

        response, schemas = Endpoint._add_responses(endpoint=endpoint,
                                                    data=data,
                                                    schemas=schemas)

        assert response.errors == [
            ParseError(
                detail=
                f"Invalid response status code not_a_number (not a number), response will be ommitted from generated client"
            )
        ]
Exemple #7
0
    def test__add_responses_status_code_error(self, mocker):
        from openapi_python_client.parser.openapi import Endpoint, Schemas

        schemas = Schemas()
        response_1_data = mocker.MagicMock()
        data = {
            "not_a_number": response_1_data,
        }
        endpoint = self.make_endpoint()
        parse_error = ParseError(data=mocker.MagicMock())
        response_from_data = mocker.patch(f"{MODULE_NAME}.response_from_data",
                                          return_value=(parse_error, schemas))
        config = MagicMock()

        response, schemas = Endpoint._add_responses(endpoint=endpoint,
                                                    data=data,
                                                    schemas=schemas,
                                                    config=config)

        assert response.errors == [
            ParseError(
                detail=
                f"Invalid response status code not_a_number (not a number), response will be ommitted from generated client"
            )
        ]
        response_from_data.assert_not_called()
Exemple #8
0
    def test_from_data_errors(self, mocker):
        from openapi_python_client.parser.openapi import Endpoint, EndpointCollection, ParseError

        path_1_put = oai.Operation.construct()
        path_1_post = oai.Operation.construct(tags=["tag_2", "tag_3"])
        path_2_get = oai.Operation.construct()
        data = {
            "path_1": oai.PathItem.construct(post=path_1_post, put=path_1_put),
            "path_2": oai.PathItem.construct(get=path_2_get),
        }
        schemas_1 = mocker.MagicMock()
        schemas_2 = mocker.MagicMock()
        schemas_3 = mocker.MagicMock()
        endpoint_from_data = mocker.patch.object(
            Endpoint,
            "from_data",
            side_effect=[
                (ParseError(data="1"), schemas_1),
                (ParseError(data="2"), schemas_2),
                (mocker.MagicMock(errors=[ParseError(data="3")]), schemas_3),
            ],
        )
        schemas = mocker.MagicMock()
        config = MagicMock()

        result, result_schemas = EndpointCollection.from_data(data=data,
                                                              schemas=schemas,
                                                              config=config)

        endpoint_from_data.assert_has_calls([
            mocker.call(data=path_1_put,
                        path="path_1",
                        method="put",
                        tag="default",
                        schemas=schemas,
                        config=config),
            mocker.call(data=path_1_post,
                        path="path_1",
                        method="post",
                        tag="tag_2",
                        schemas=schemas_1,
                        config=config),
            mocker.call(data=path_2_get,
                        path="path_2",
                        method="get",
                        tag="default",
                        schemas=schemas_2,
                        config=config),
        ], )
        assert result["default"].parse_errors[0].data == "1"
        assert result["default"].parse_errors[1].data == "3"
        assert result["tag_2"].parse_errors[0].data == "2"
        assert result_schemas == schemas_3
Exemple #9
0
    def test__add_responses_error(self, mocker):
        from openapi_python_client.parser.openapi import Endpoint, Schemas

        schemas = Schemas()
        response_1_data = mocker.MagicMock()
        response_2_data = mocker.MagicMock()
        data = {
            "200": response_1_data,
            "404": response_2_data,
        }
        endpoint = Endpoint(
            path="path",
            method="method",
            description=None,
            name="name",
            requires_security=False,
            tag="tag",
            relative_imports={"import_3"},
        )
        parse_error = ParseError(data=mocker.MagicMock())
        response_from_data = mocker.patch(f"{MODULE_NAME}.response_from_data",
                                          return_value=(parse_error, schemas))

        response, schemas = Endpoint._add_responses(endpoint=endpoint,
                                                    data=data,
                                                    schemas=schemas)

        response_from_data.assert_has_calls([
            mocker.call(status_code=200,
                        data=response_1_data,
                        schemas=schemas,
                        parent_name="name"),
            mocker.call(status_code=404,
                        data=response_2_data,
                        schemas=schemas,
                        parent_name="name"),
        ])
        assert response.errors == [
            ParseError(
                detail=
                f"Cannot parse response for status code 200, response will be ommitted from generated client",
                data=parse_error.data,
            ),
            ParseError(
                detail=
                f"Cannot parse response for status code 404, response will be ommitted from generated client",
                data=parse_error.data,
            ),
        ]
Exemple #10
0
    def test__add_parameters_fail_loudly_when_location_not_supported(
            self, mocker):
        from openapi_python_client.parser.openapi import Endpoint, Schemas

        endpoint = Endpoint(
            path="path",
            method="method",
            description=None,
            name="name",
            requires_security=False,
            tag="tag",
        )
        parsed_schemas = mocker.MagicMock()
        mocker.patch(f"{MODULE_NAME}.property_from_data",
                     return_value=(mocker.MagicMock(), parsed_schemas))
        param = oai.Parameter.construct(name="test",
                                        required=True,
                                        param_schema=mocker.MagicMock(),
                                        param_in="cookie")
        schemas = Schemas()

        result = Endpoint._add_parameters(
            endpoint=endpoint,
            data=oai.Operation.construct(parameters=[param]),
            schemas=schemas)
        assert result == (ParseError(
            data=param, detail="Parameter must be declared in path or query"),
                          parsed_schemas)
Exemple #11
0
    def test_from_data_bad_responses(self, mocker):
        from openapi_python_client.parser.openapi import Endpoint

        path = mocker.MagicMock()
        method = mocker.MagicMock()
        parse_error = ParseError(data=mocker.MagicMock())
        param_schemas = mocker.MagicMock()
        _add_parameters = mocker.patch.object(Endpoint,
                                              "_add_parameters",
                                              return_value=(mocker.MagicMock(),
                                                            param_schemas))
        response_schemas = mocker.MagicMock()
        _add_responses = mocker.patch.object(Endpoint,
                                             "_add_responses",
                                             return_value=(parse_error,
                                                           response_schemas))
        data = oai.Operation.construct(
            description=mocker.MagicMock(),
            operationId=mocker.MagicMock(),
            security={"blah": "bloo"},
            responses=mocker.MagicMock(),
        )
        initial_schemas = mocker.MagicMock()

        result = Endpoint.from_data(data=data,
                                    path=path,
                                    method=method,
                                    tag="default",
                                    schemas=initial_schemas)

        assert result == (parse_error, response_schemas)
    def test_response_from_data_unsupported_content_type(self):
        from openapi_python_client.parser.errors import ParseError
        from openapi_python_client.parser.responses import response_from_data

        content = {"not/real": {}}
        data = oai.Response.construct(content=content)

        assert response_from_data(status_code=200, data=data) == ParseError(
            data=data, detail=f"Unsupported content_type {content}")
Exemple #13
0
def test_response_from_data_unsupported_content_type():
    from openapi_python_client.parser.responses import response_from_data

    data = oai.Response.construct(description="", content={"blah": None})
    response, schemas = response_from_data(status_code=200,
                                           data=data,
                                           schemas=Schemas(),
                                           parent_name="parent")

    assert response == ParseError(
        data=data, detail="Unsupported content_type {'blah': None}")
    def test_generate_handle_multiple_warnings(self, _create_new_client):
        error_1 = ParseError(data={"test": "data"}, detail="this is a message")
        error_2 = ParseError(data={"other": "data"}, detail="this is another message", header="Custom Header")
        _create_new_client.return_value = [error_1, error_2]
        path = "cool/path"
        from openapi_python_client.cli import app

        result = runner.invoke(app, ["generate", f"--path={path}"])

        assert result.exit_code == 0
        assert result.output == (
            "Warning(s) encountered while generating. Client was generated, but some pieces may be missing\n\n"
            "Unable to parse this part of your OpenAPI document: \n\n"
            "this is a message\n\n"
            "{'test': 'data'}\n\n"
            "Custom Header\n\n"
            "this is another message\n\n"
            "{'other': 'data'}\n\n"
            "If you believe this was a mistake or this tool is missing a feature you need, please open an issue at "
            "https://github.com/triaxtec/openapi-python-client/issues/new/choose\n"
        )
    def test_response_from_dict_unsupported_type(self):
        from openapi_python_client.parser.errors import ParseError
        from openapi_python_client.parser.responses import response_from_data

        data = oai.Response.construct(
            content={
                "text/html":
                oai.MediaType.construct(media_type_schema=oai.Schema.construct(
                    type="BLAH"))
            })

        assert response_from_data(status_code=200, data=data) == ParseError(
            data=data, detail="Unrecognized type BLAH")
Exemple #16
0
    def test__add_parameters_fail_loudly_when_location_not_supported(self, mocker):
        from openapi_python_client.parser.openapi import Endpoint, Schemas

        endpoint = self.make_endpoint()
        parsed_schemas = mocker.MagicMock()
        mocker.patch(f"{MODULE_NAME}.property_from_data", return_value=(mocker.MagicMock(), parsed_schemas))
        param = oai.Parameter.construct(
            name="test", required=True, param_schema=mocker.MagicMock(), param_in="error_location"
        )
        schemas = Schemas()
        config = MagicMock()

        result = Endpoint._add_parameters(
            endpoint=endpoint, data=oai.Operation.construct(parameters=[param]), schemas=schemas, config=config
        )
        assert result == (ParseError(data=param, detail="Parameter must be declared in path or query"), parsed_schemas)
Exemple #17
0
    def test__add_parameters_duplicate_properties(self, mocker):
        from openapi_python_client.parser.openapi import Endpoint, Schemas

        endpoint = self.make_endpoint()
        param = oai.Parameter.construct(
            name="test", required=True, param_schema=oai.Schema.construct(type="string"), param_in="path"
        )
        data = oai.Operation.construct(parameters=[param, param])
        schemas = Schemas()
        config = MagicMock()

        result = Endpoint._add_parameters(endpoint=endpoint, data=data, schemas=schemas, config=config)
        assert result == (
            ParseError(data=data, detail="Could not reconcile duplicate parameters named test_path"),
            schemas,
        )