Exemplo n.º 1
0
def test_handle_array_secondary(mocked_facades_models):
    """
    GIVEN schema with array referencing another schema and secondary set and schemas
    WHEN handle_array is called
    THEN table is set on models.
    """
    secondary = "association"
    tablename = "schema"
    model_schema = {
        "type": "object",
        "x-tablename": tablename,
        "properties": {
            "id": {
                "type": "integer",
                "x-primary-key": True
            }
        },
    }
    spec = {
        "type": "array",
        "items": {
            "allOf": [
                {
                    "$ref": "#/components/schemas/RefSchema"
                },
                {
                    "x-secondary": secondary
                },
            ]
        },
    }
    schemas = {
        "RefSchema": {
            "type": "object",
            "x-tablename": "ref_schema",
            "properties": {
                "id": {
                    "type": "integer",
                    "x-primary-key": True
                }
            },
        }
    }

    array_ref.handle_array(spec=spec,
                           model_schema=model_schema,
                           schemas=schemas,
                           logical_name="ref_schema")

    assert mocked_facades_models.set_association.call_count == 1
    if sys.version_info[1] == 8:
        name = mocked_facades_models.set_association.call_args.kwargs["name"]
        table = mocked_facades_models.set_association.call_args.kwargs["table"]
    else:
        _, kwargs = mocked_facades_models.set_association.call_args
        name = kwargs["name"]
        table = kwargs["table"]
    assert name == secondary
    assert table.name == secondary
Exemplo n.º 2
0
def test_handle_array_schemas_foreign_key_column():
    """
    GIVEN schema with array referencing another schema with foreign key and schemas
    WHEN handle_array is called
    THEN foreign key is added to the referenced schema.
    """
    tablename = "schema"
    model_schema = {
        "type": "object",
        "x-tablename": tablename,
        "properties": {
            "fk_column": {
                "type": "integer"
            }
        },
    }
    spec = {
        "type": "array",
        "items": {
            "$ref": "#/components/schemas/RefSchema"
        }
    }
    schemas = {
        "RefSchema": {
            "type": "object",
            "x-tablename": "ref_schema",
            "x-foreign-key-column": "fk_column",
            "properties": {},
        }
    }

    array_ref.handle_array(spec=spec,
                           model_schema=model_schema,
                           schemas=schemas,
                           logical_name="ref_schema")

    assert schemas == {
        "RefSchema": {
            "allOf": [
                {
                    "type": "object",
                    "x-tablename": "ref_schema",
                    "x-foreign-key-column": "fk_column",
                    "properties": {},
                },
                {
                    "type": "object",
                    "properties": {
                        f"{tablename}_fk_column": {
                            "type": "integer",
                            "x-foreign-key": f"{tablename}.fk_column",
                            "x-dict-ignore": True,
                        }
                    },
                },
            ]
        }
    }
Exemplo n.º 3
0
def test_handle_array_schemas_fk_def_all_of():
    """
    GIVEN schema with array referencing another schema which has allOf which already
        has foreign key and schemas
    WHEN handle_array is called
    THEN foreign key is not added to the referenced schema.
    """
    tablename = "schema"
    model_schema = {
        "type": "object",
        "x-tablename": tablename,
        "properties": {
            "id": {
                "type": "integer"
            }
        },
    }
    spec = {
        "type": "array",
        "items": {
            "$ref": "#/components/schemas/RefSchema"
        }
    }
    schemas = {
        "RefSchema": {
            "allOf": [{
                "type": "object",
                "x-tablename": "ref_schema",
                "properties": {
                    "schema_id": {
                        "type": "integer",
                        "x-foreign-key": "schema.id"
                    }
                },
            }]
        }
    }

    array_ref.handle_array(spec=spec,
                           model_schema=model_schema,
                           schemas=schemas,
                           logical_name="ref_schema")

    assert schemas == {
        "RefSchema": {
            "allOf": [{
                "type": "object",
                "x-tablename": "ref_schema",
                "properties": {
                    "schema_id": {
                        "type": "integer",
                        "x-foreign-key": "schema.id"
                    }
                },
            }]
        }
    }
Exemplo n.º 4
0
def test_handle_array_relationship(spec, schemas):
    """
    GIVEN schema with array referencing another schema and schemas
    WHEN handle_array is called
    THEN relationship is returned pointing to the referenced schema.
    """
    logical_name = "ref_schema"
    model_schema = {
        "type": "object",
        "x-tablename": "schema",
        "properties": {
            "id": {
                "type": "integer"
            }
        },
    }

    ([(tbl_logical_name, relationship)],
     schema_spec) = array_ref.handle_array(spec=spec,
                                           model_schema=model_schema,
                                           schemas=schemas,
                                           logical_name=logical_name)

    assert relationship.argument == "RefSchema"
    assert relationship.backref is None
    assert relationship.secondary is None
    assert tbl_logical_name == logical_name
    assert schema_spec == {
        "type": "array",
        "items": {
            "type": "object",
            "x-de-$ref": "RefSchema"
        },
    }
Exemplo n.º 5
0
def test_handle_array_backref():
    """
    GIVEN schema with array referencing another schema with a back reference and schemas
    WHEN handle_array is called
    THEN relationship is returned with a back reference and back reference is recorded
        in referenced schema.
    """
    tablename = "schema"
    schema = {
        "type": "array",
        "items": {
            "allOf": [
                {"$ref": "#/components/schemas/RefSchema"},
                {"x-backref": "schema"},
            ]
        },
    }
    schemas = {
        "RefSchema": {"type": "object", "x-tablename": "ref_schema", "properties": {}}
    }
    logical_name = "ref_schema"
    model_name = "Schema"
    model_schema = {
        "type": "object",
        "x-tablename": tablename,
        "properties": {"id": {"type": "integer"}},
    }

    ([(_, relationship)], _) = array_ref.handle_array(
        schema=schema,
        model_schema=model_schema,
        model_name=model_name,
        schemas=schemas,
        logical_name=logical_name,
    )

    assert relationship.backref == ("schema", {"uselist": None})
    assert schemas == {
        "RefSchema": {
            "allOf": [
                {"type": "object", "x-tablename": "ref_schema", "properties": {}},
                {
                    "type": "object",
                    "x-backrefs": {
                        "schema": {"type": "object", "x-de-$ref": model_name}
                    },
                },
                {
                    "type": "object",
                    "properties": {
                        f"{tablename}_ref_schema_id": {
                            "type": "integer",
                            "x-foreign-key": f"{tablename}.id",
                            "x-dict-ignore": True,
                        }
                    },
                },
            ]
        }
    }
Exemplo n.º 6
0
def test_handle_array():
    """
    GIVEN schema with array referencing another schema and schemas
    WHEN handle_array is called
    THEN relationship is returned pointing to the referenced schema.
    """
    tablename = "schema"
    schema = {"type": "array", "items": {"$ref": "#/components/schemas/RefSchema"}}
    schemas = {
        "RefSchema": {"type": "object", "x-tablename": "ref_schema", "properties": {}}
    }
    logical_name = "ref_schema"
    model_name = "Schema"
    model_schema = {
        "type": "object",
        "x-tablename": tablename,
        "properties": {"id": {"type": "integer"}},
    }

    ([(tbl_logical_name, relationship)], return_schema) = array_ref.handle_array(
        schema=schema,
        model_schema=model_schema,
        model_name=model_name,
        schemas=schemas,
        logical_name=logical_name,
    )

    assert relationship.argument == "RefSchema"
    assert relationship.backref is None
    assert relationship.secondary is None
    assert tbl_logical_name == logical_name
    assert return_schema == {
        "type": "array",
        "items": {"type": "object", "x-de-$ref": "RefSchema"},
    }
    assert schemas == {
        "RefSchema": {
            "allOf": [
                {"type": "object", "x-tablename": "ref_schema", "properties": {}},
                {
                    "type": "object",
                    "properties": {
                        f"{tablename}_ref_schema_id": {
                            "type": "integer",
                            "x-foreign-key": f"{tablename}.id",
                            "x-dict-ignore": True,
                        }
                    },
                },
            ]
        }
    }
Exemplo n.º 7
0
def test_handle_array_invalid(spec, schemas):
    """
    GIVEN array schema that is not valid and schemas
    WHEN handle_array is called
    THEN MalformedRelationshipError is raised.
    """
    model_schema = {
        "type": "object",
        "x-tablename": "schema",
        "properties": {
            "id": {
                "type": "integer"
            }
        },
    }

    with pytest.raises(exceptions.MalformedRelationshipError):
        array_ref.handle_array(
            spec=spec,
            model_schema=model_schema,
            schemas=schemas,
            logical_name="ref_schema",
        )
Exemplo n.º 8
0
def test_handle_array_relationship_secondary(_mocked_facades_models):
    """
    GIVEN schema with array referencing another schema with secondary and schemas
    WHEN handle_array is called
    THEN relationship with secondary is returned.
    """
    spec = {
        "type": "array",
        "items": {
            "$ref": "#/components/schemas/RefSchema"
        }
    }
    schemas = {
        "RefSchema": {
            "type": "object",
            "x-tablename": "ref_schema",
            "x-secondary": "association",
            "properties": {
                "id": {
                    "type": "integer",
                    "x-primary-key": True
                }
            },
        }
    }
    model_schema = {
        "type": "object",
        "x-tablename": "schema",
        "properties": {
            "id": {
                "type": "integer",
                "x-primary-key": True
            }
        },
    }

    ([(_, relationship)],
     _) = array_ref.handle_array(spec=spec,
                                 model_schema=model_schema,
                                 schemas=schemas,
                                 logical_name="ref_schema")

    assert relationship.secondary == "association"
Exemplo n.º 9
0
def test_handle_array_relationship_backref():
    """
    GIVEN schema with array referencing another schema with backref and schemas
    WHEN handle_array is called
    THEN relationship with backref is returned.
    """
    spec = {
        "type": "array",
        "items": {
            "$ref": "#/components/schemas/RefSchema"
        }
    }
    schemas = {
        "RefSchema": {
            "type": "object",
            "x-tablename": "ref_schema",
            "x-backref": "schema",
            "properties": {},
        }
    }
    model_schema = {
        "type": "object",
        "x-tablename": "schema",
        "properties": {
            "id": {
                "type": "integer"
            }
        },
    }

    ([(_, relationship)],
     _) = array_ref.handle_array(spec=spec,
                                 model_schema=model_schema,
                                 schemas=schemas,
                                 logical_name="ref_schema")

    assert relationship.backref == "schema"