Exemplo n.º 1
0
def test_handle_object_error(spec, schemas):
    """
    GIVEN spec
    WHEN handle_object is called with the spec
    THEN MalformedRelationshipError is raised.
    """
    with pytest.raises(exceptions.MalformedRelationshipError):
        object_ref.handle_object(
            spec=spec,
            schemas=schemas,
            required=True,
            logical_name="name 1",
            model_schema={},
        )
Exemplo n.º 2
0
def test_integration_object_ref_required():
    """
    GIVEN schema that is required and references another object schema and schemas
    WHEN handle_object is called with the schema and schemas
    THEN foreign key which is not nullable is returned.
    """
    schema = {"$ref": "#/components/schemas/RefSchema"}
    schemas = {
        "RefSchema": {
            "type": "object",
            "x-tablename": "ref_schema",
            "properties": {
                "id": {
                    "type": "integer"
                }
            },
        }
    }
    logical_name = "ref_schema"

    ([(_, fk_column), _], _) = object_ref.handle_object(
        schema=schema,
        schemas=schemas,
        logical_name=logical_name,
        model_schema={"properties": {}},
        model_name="schema",
        required=True,
    )

    assert fk_column.nullable is False
Exemplo n.º 3
0
def test_integration_object_ref():
    """
    GIVEN schema that references another object schema and schemas
    WHEN handle_object is called with the schema and schemas
    THEN foreign key reference and relationship is returned with the spec.
    """
    schema = {"$ref": "#/components/schemas/RefSchema"}
    schemas = {
        "RefSchema": {
            "type": "object",
            "x-tablename": "ref_schema",
            "properties": {
                "id": {
                    "type": "integer"
                }
            },
        }
    }
    logical_name = "ref_schema"

    (
        [(fk_logical_name, fk_column), (tbl_logical_name, relationship)],
        returned_schema,
    ) = object_ref.handle_object(
        schema=schema,
        schemas=schemas,
        logical_name=logical_name,
        model_schema={"properties": {}},
        model_name="schema",
        required=False,
    )

    assert fk_logical_name == "ref_schema_id"
    assert isinstance(fk_column.type, facades.sqlalchemy.column.Integer)
    assert fk_column.nullable is True
    assert len(fk_column.foreign_keys) == 1
    assert tbl_logical_name == logical_name
    assert relationship.argument == "RefSchema"
    assert relationship.backref is None
    assert relationship.uselist is None
    assert returned_schema == {"type": "object", "x-de-$ref": "RefSchema"}
Exemplo n.º 4
0
def test_fk_def():
    """
    GIVEN schema that references another object schema which already has the foreign
        key defined and schemas
    WHEN handle_object is called with the schema and schemas
    THEN no foreign key column is returned.
    """
    model_schema = {
        "properties": {
            "ref_schema_id": {
                "type": "integer",
                "x-foreign-key": "ref_schema.id"
            }
        }
    }
    schema = {"$ref": "#/components/schemas/RefSchema"}
    schemas = {
        "RefSchema": {
            "type": "object",
            "x-tablename": "ref_schema",
            "properties": {
                "id": {
                    "type": "integer"
                }
            },
        }
    }
    logical_name = "ref_schema"

    ([(tbl_logical_name, relationship)], _) = object_ref.handle_object(
        schema=schema,
        schemas=schemas,
        logical_name=logical_name,
        model_schema=model_schema,
        model_name="model",
        required=None,
    )

    assert tbl_logical_name == logical_name
    assert relationship.argument == "RefSchema"
Exemplo n.º 5
0
def test_integration_object_ref_backref():
    """
    GIVEN schema that references another object schema with a back reference and schemas
    WHEN handle_object is called with the schema and schemas
    THEN the a relationship with a back reference is returned and the back reference is
        recorded on the referenced schema.
    """
    schema = {
        "allOf": [{
            "$ref": "#/components/schemas/RefSchema"
        }, {
            "x-backref": "schema"
        }]
    }
    schemas = {
        "RefSchema": {
            "type": "object",
            "x-tablename": "ref_schema",
            "properties": {
                "id": {
                    "type": "integer"
                }
            },
        }
    }
    logical_name = "ref_schema"
    model_schema = {"properties": {}}
    model_name = "Schema"

    ([_, (_, relationship)], _) = object_ref.handle_object(
        schema=schema,
        schemas=schemas,
        logical_name=logical_name,
        model_schema=model_schema,
        model_name=model_name,
        required=False,
    )

    assert relationship.backref == ("schema", {"uselist": None})
    assert schemas == {
        "RefSchema": {
            "allOf": [
                {
                    "type": "object",
                    "x-tablename": "ref_schema",
                    "properties": {
                        "id": {
                            "type": "integer"
                        }
                    },
                },
                {
                    "type": "object",
                    "x-backrefs": {
                        "schema": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "x-de-$ref": model_name
                            },
                        }
                    },
                },
            ]
        }
    }