def test_build_enums(mocker):
    from openapi_python_client.parser.openapi import build_schemas

    build_model_property = mocker.patch(f"{MODULE_NAME}.build_model_property")
    schemas = mocker.MagicMock()
    build_enum_property = mocker.patch(f"{MODULE_NAME}.build_enum_property", return_value=(mocker.MagicMock(), schemas))
    in_data = {"1": mocker.MagicMock(enum=["val1", "val2", "val3"])}

    build_schemas(components=in_data)

    build_enum_property.assert_called()
    build_model_property.assert_not_called()
    def test_retries_failing_properties_while_making_progress(self, mocker):
        from openapi_python_client.parser.properties import Schemas, build_schemas
        from openapi_python_client.schema import Schema

        components = {
            "first": Schema.construct(),
            "second": Schema.construct()
        }
        update_schemas_with_data = mocker.patch(
            f"{MODULE_NAME}.update_schemas_with_data",
            side_effect=[PropertyError(),
                         Schemas(),
                         PropertyError()])
        parse_reference_path = mocker.patch(
            f"{MODULE_NAME}.parse_reference_path")
        config = Config()

        result = build_schemas(components=components,
                               schemas=Schemas(),
                               config=config)
        parse_reference_path.assert_has_calls([
            call("#/components/schemas/first"),
            call("#/components/schemas/second"),
            call("#/components/schemas/first"),
        ])
        assert update_schemas_with_data.call_count == 3
        assert result.errors == [PropertyError()]
    def test_records_bad_uris_and_keeps_going(self, mocker):
        from openapi_python_client.parser.properties import Schemas, build_schemas
        from openapi_python_client.schema import Schema

        components = {
            "first": Schema.construct(),
            "second": Schema.construct()
        }
        update_schemas_with_data = mocker.patch(
            f"{MODULE_NAME}.update_schemas_with_data")
        parse_reference_path = mocker.patch(
            f"{MODULE_NAME}.parse_reference_path",
            side_effect=[PropertyError(detail="some details"), "a_path"])
        config = Config()

        result = build_schemas(components=components,
                               schemas=Schemas(),
                               config=config)
        parse_reference_path.assert_has_calls([
            call("#/components/schemas/first"),
            call("#/components/schemas/second"),
        ])
        update_schemas_with_data.assert_called_once_with(
            ref_path="a_path",
            config=config,
            data=components["second"],
            schemas=Schemas(errors=[
                PropertyError(detail="some details", data=components["first"])
            ]),
        )
        assert result == update_schemas_with_data.return_value
    def test_skips_references_and_keeps_going(self, mocker):
        from openapi_python_client.parser.properties import Schemas, build_schemas
        from openapi_python_client.schema import Reference, Schema

        components = {
            "a_ref": Reference.construct(),
            "a_schema": Schema.construct()
        }
        update_schemas_with_data = mocker.patch(
            f"{MODULE_NAME}.update_schemas_with_data")
        parse_reference_path = mocker.patch(
            f"{MODULE_NAME}.parse_reference_path")
        config = Config()

        result = build_schemas(components=components,
                               schemas=Schemas(),
                               config=config)
        # Should not even try to parse a path for the Reference
        parse_reference_path.assert_called_once_with(
            "#/components/schemas/a_schema")
        update_schemas_with_data.assert_called_once_with(
            ref_path=parse_reference_path.return_value,
            config=config,
            data=components["a_schema"],
            schemas=Schemas(errors=[
                PropertyError(detail="Reference schemas are not supported.",
                              data=components["a_ref"])
            ]),
        )
        assert result == update_schemas_with_data.return_value
def test_build_schemas(mocker):
    build_model_property = mocker.patch(f"{MODULE_NAME}.build_model_property")
    in_data = {"1": mocker.MagicMock(enum=None), "2": mocker.MagicMock(enum=None), "3": mocker.MagicMock(enum=None)}
    model_1 = mocker.MagicMock()
    schemas_1 = mocker.MagicMock()
    model_2 = mocker.MagicMock()
    schemas_2 = mocker.MagicMock(errors=[])
    error = PropertyError()
    schemas_3 = mocker.MagicMock()

    # This loops through one for each, then again to retry the error
    build_model_property.side_effect = [
        (model_1, schemas_1),
        (model_2, schemas_2),
        (error, schemas_3),
        (error, schemas_3),
    ]

    from openapi_python_client.parser.properties import Schemas, build_schemas

    result = build_schemas(components=in_data)

    build_model_property.assert_has_calls(
        [
            mocker.call(data=in_data["1"], name="1", schemas=Schemas(), required=True, parent_name=None),
            mocker.call(data=in_data["2"], name="2", schemas=schemas_1, required=True, parent_name=None),
            mocker.call(data=in_data["3"], name="3", schemas=schemas_2, required=True, parent_name=None),
            mocker.call(data=in_data["3"], name="3", schemas=schemas_2, required=True, parent_name=None),
        ]
    )
    # schemas_3 was the last to come back from build_model_property, but it should be ignored because it's an error
    assert result == schemas_2
    assert result.errors == [error]
def test_build_parse_error_on_reference():
    from openapi_python_client.parser.openapi import build_schemas

    ref_schema = oai.Reference.construct()
    in_data = {"1": ref_schema}
    result = build_schemas(components=in_data)
    assert result.errors[0] == PropertyError(data=ref_schema, detail="Reference schemas are not supported.")