예제 #1
0
def test_integration_array_ref_fk_def():
    """
    GIVEN schema that references another object schema with a foreign key already
        defined from an array and schemas
    WHEN column_factory is called with the schema and schemas
    THEN schemas is not modified and relationship is returned with the spec.
    """
    spec = {
        "type": "array",
        "items": {
            "$ref": "#/components/schemas/RefSchema"
        }
    }
    schemas = {
        "RefSchema": {
            "type": "object",
            "x-tablename": "table 1",
            "properties": {
                "id": {
                    "type": "integer"
                },
                "schema_id": {
                    "type": "integer",
                    "x-foreign-key": "schema.id"
                },
            },
        }
    }
    model_schema = {
        "type": "object",
        "x-tablename": "schema",
        "properties": {
            "id": {
                "type": "integer"
            }
        },
    }
    logical_name = "ref_schema"

    column_factory.column_factory(spec=spec,
                                  schemas=schemas,
                                  logical_name=logical_name,
                                  model_schema=model_schema)

    assert schemas == {
        "RefSchema": {
            "type": "object",
            "x-tablename": "table 1",
            "properties": {
                "id": {
                    "type": "integer"
                },
                "schema_id": {
                    "type": "integer",
                    "x-foreign-key": "schema.id"
                },
            },
        }
    }
예제 #2
0
def test_integration_object_ref_uselist():
    """
    GIVEN schema that references another object schema with a uselist and schemas
    WHEN column_factory is called with the schema and schemas
    THEN relationship with uselist is returned with the spec.
    """
    spec = {"$ref": "#/components/schemas/RefSchema"}
    schemas = {
        "RefSchema": {
            "type": "object",
            "x-tablename": "ref_schema",
            "properties": {
                "id": {
                    "type": "integer"
                }
            },
            "x-backref": "ref_schemas",
            "x-uselist": False,
        }
    }
    ([_, (_, relationship)], spec) = column_factory.column_factory(
        spec=spec,
        schemas=schemas,
        logical_name="ref_schema",
        model_schema={"properties": {}},
    )

    assert relationship.backref == ("ref_schemas", {"uselist": False})
예제 #3
0
def test_integration_object_all_of_ref():
    """
    GIVEN schema with allOf that references another object schema and schemas
    WHEN column_factory is called with the schema and schemas
    THEN foreign key reference and relationship is returned.
    """
    spec = {"allOf": [{"$ref": "#/components/schemas/RefSchema"}]}
    schemas = {
        "RefSchema": {
            "type": "object",
            "x-tablename": "table 1",
            "properties": {
                "id": {
                    "type": "integer"
                }
            },
        }
    }
    [  # pylint: disable=unbalanced-tuple-unpacking
        (fk_logical_name, fk_column),
        (tbl_logical_name, relationship),
    ] = column_factory.column_factory(spec=spec,
                                      schemas=schemas,
                                      logical_name="column_1")

    assert fk_logical_name == "column_1_id"
    assert isinstance(fk_column.type, sqlalchemy.Integer)
    assert len(fk_column.foreign_keys) == 1
    assert tbl_logical_name == "column_1"
    assert relationship.argument == "RefSchema"
    assert relationship.backref is None
예제 #4
0
def test_integration_relationship():
    """
    GIVEN relationship property artifacts
    WHEN column_factory is called with the artifacts
    THEN a relationship is returned.
    """
    artifacts = types.ManyToOneRelationshipPropertyArtifacts(
        type=types.PropertyType.RELATIONSHIP,
        sub_type=types.RelationshipType.MANY_TO_ONE,
        parent="RefSchema",
        backref_property=None,
        kwargs=None,
        write_only=None,
        description=None,
        required=False,
        schema={"type": "object"},
        foreign_key="foreign.key",
        foreign_key_property="foreign_key",
        nullable=None,
    )

    relationship = column_factory.column_factory(artifacts=artifacts)

    assert relationship.argument == "RefSchema"
    assert relationship.backref is None
    assert relationship.uselist is None
예제 #5
0
def test_integration_json():
    """
    GIVEN json column artifacts
    WHEN column_factory is called with the artifacts
    THEN a SQLAlchemy JSON column is returned.
    """
    artifacts = types.JsonPropertyArtifacts(
        type=types.PropertyType.JSON,
        open_api=types.OpenApiJsonPropertyArtifacts(
            nullable=None,
            read_only=None,
            write_only=None,
        ),
        extension=types.ExtensionJsonPropertyArtifacts(
            primary_key=False,
            index=None,
            unique=None,
            foreign_key=None,
            kwargs=None,
            foreign_key_kwargs=None,
        ),
        schema={"type": "boolean"},
        required=False,
        description=None,
    )

    column = column_factory.column_factory(artifacts=artifacts)

    assert isinstance(column, sqlalchemy.types.Column)
    assert isinstance(column.type, sqlalchemy.types.JSON)
예제 #6
0
def test_integration_array_ref_read_only():
    """
    GIVEN schema that references another object schema from an array that is read only
        and schemas
    WHEN column_factory is called with the schema and schemas
    THEN no columns and the schema is returned.
    """
    schema = {
        "type": "array",
        "items": {
            "$ref": "#/components/schemas/RefSchema"
        },
        "readOnly": True,
    }
    schemas = {
        "RefSchema": {
            "type": "object",
            "x-tablename": "table 1",
            "properties": {
                "id": {
                    "type": "integer"
                }
            },
        }
    }
    model_schema = {
        "type": "object",
        "x-tablename": "schema",
        "properties": {
            "id": {
                "type": "integer"
            }
        },
    }
    logical_name = "ref_schema"

    ([], returned_schema) = column_factory.column_factory(
        schema=schema,
        schemas=schemas,
        logical_name=logical_name,
        model_schema=model_schema,
        model_name="model",
    )

    assert returned_schema == {
        "type": "array",
        "items": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer"
                }
            }
        },
        "readOnly": True,
    }
예제 #7
0
def test_integration_all_of():
    """
    GIVEN schema with allOf statement
    WHEN column_factory is called with the schema and schemas
    THEN SQLAlchemy boolean column is returned in a dictionary with logical name.
    """
    spec = {"allOf": [{"type": "boolean"}]}
    schemas = {}
    [(logical_name, column)
     ] = column_factory.column_factory(spec=spec,
                                       schemas=schemas,
                                       logical_name="column_1")

    assert logical_name == "column_1"
    assert isinstance(column.type, sqlalchemy.Boolean)
예제 #8
0
def test_integration_ref():
    """
    GIVEN schema that references another schema and schemas
    WHEN column_factory is called with the schema and schemas
    THEN SQLAlchemy boolean column is returned in a dictionary with logical name.
    """
    spec = {"$ref": "#/components/schemas/RefSchema"}
    schemas = {"RefSchema": {"type": "boolean"}}
    [(logical_name, column)
     ] = column_factory.column_factory(spec=spec,
                                       schemas=schemas,
                                       logical_name="column_1")

    assert logical_name == "column_1"
    assert isinstance(column.type, sqlalchemy.Boolean)
예제 #9
0
def test_integration_kwargs():
    """
    GIVEN schema with kwargs
    WHEN column_factory is called with the schema
    THEN SQLAlchemy column is constructed with the kwargs.
    """
    schema = {"type": "boolean", "x-kwargs": {"doc": "doc 1"}}
    schemas = {}
    ([(_, column)], _) = column_factory.column_factory(
        schema=schema,
        schemas=schemas,
        logical_name="column_1",
        model_schema={},
        model_name="schema",
    )

    assert column.doc == "doc 1"
예제 #10
0
def test_integration():
    """
    GIVEN schema
    WHEN column_factory is called with the schema
    THEN SQLAlchemy boolean column is returned in a dictionary with logical name and
        spec.
    """
    spec = {"type": "boolean"}
    schemas = {}
    ([(logical_name, column)],
     spec) = column_factory.column_factory(spec=spec,
                                           schemas=schemas,
                                           logical_name="column_1",
                                           model_schema={})

    assert logical_name == "column_1"
    assert isinstance(column.type, sqlalchemy.Boolean)
    assert spec == {"type": "boolean"}
예제 #11
0
def test_integration_backref():
    """
    GIVEN backref property artifacts
    WHEN column_factory is called with the artifacts
    THEN None is returned.
    """
    artifacts = types.BackrefPropertyArtifacts(
        type=types.PropertyType.BACKREF,
        sub_type=types.BackrefSubType.OBJECT,
        properties=[],
        schema={},
        required=None,
        description=None,
    )

    backref = column_factory.column_factory(artifacts=artifacts)

    assert backref is None
예제 #12
0
def test_integration_simple_json(schema, expected_schema):
    """
    GIVEN JSON schema and expected schema
    WHEN column_factory is called with the schema
    THEN a SQLALchemy JSON column and the expected schema are returned.
    """
    schemas = {}
    ([(logical_name, column)],
     returned_schema) = column_factory.column_factory(
         schema=schema,
         schemas=schemas,
         logical_name="column_1",
         model_schema={},
         model_name="schema",
     )

    assert logical_name == "column_1"
    assert isinstance(column.type, facades.sqlalchemy.column.JSON)
    assert returned_schema == expected_schema
def test_integration_all_of():
    """
    GIVEN schema with allOf statement
    WHEN column_factory is called with the schema
    THEN SQLAlchemy boolean column is returned in a dictionary with logical name and
        spec.
    """
    spec = {"allOf": [{"type": "boolean"}]}
    schemas = {}
    ([(logical_name, column)], spec) = column_factory.column_factory(
        spec=spec,
        schemas=schemas,
        logical_name="column_1",
        model_schema={},
        model_name="schema",
    )

    assert logical_name == "column_1"
    assert isinstance(column.type, facades.sqlalchemy.column.Boolean)
    assert spec == {"type": "boolean"}
예제 #14
0
def test_integration_simple(schema, expected_schema):
    """
    GIVEN schema
    WHEN column_factory is called with the schema
    THEN a SQLAlchemy boolean column is returned in a tuple with logical name and
        schema.
    """
    schemas = {}
    ([(logical_name, column)],
     returned_schema) = column_factory.column_factory(
         schema=schema,
         schemas=schemas,
         logical_name="column_1",
         model_schema={},
         model_name="schema",
     )

    assert logical_name == "column_1"
    assert isinstance(column.type, facades.sqlalchemy.column.Boolean)
    assert returned_schema == expected_schema
예제 #15
0
def test_integration_ref():
    """
    GIVEN schema that references another schema and schemas
    WHEN column_factory is called with the schema and schemas
    THEN SQLAlchemy boolean column is returned in a dictionary with logical name and
        the schema.
    """
    schema = {"$ref": "#/components/schemas/RefSchema"}
    schemas = {"RefSchema": {"type": "boolean"}}
    ([(logical_name, column)],
     returned_schema) = column_factory.column_factory(
         schema=schema,
         schemas=schemas,
         logical_name="column_1",
         model_schema={},
         model_name="schema",
     )

    assert logical_name == "column_1"
    assert isinstance(column.type, facades.sqlalchemy.column.Boolean)
    assert returned_schema == {"type": "boolean"}
예제 #16
0
def test_integration_object_ref():
    """
    GIVEN schema that references another object schema and schemas
    WHEN column_factory 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,
    ) = column_factory.column_factory(
        schema=schema,
        schemas=schemas,
        logical_name=logical_name,
        model_schema={"properties": {}},
        model_name="schema",
    )

    assert fk_logical_name == "ref_schema_id"
    assert isinstance(fk_column.type, facades.sqlalchemy.column.Integer)
    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"}
예제 #17
0
def test_integration_object_ref_fk_def():
    """
    GIVEN schema that references another object schema which already has the foreign
        key defined and schemas
    WHEN column_factory 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"
            }
        }
    }
    spec = {"$ref": "#/components/schemas/RefSchema"}
    schemas = {
        "RefSchema": {
            "type": "object",
            "x-tablename": "ref_schema",
            "properties": {
                "id": {
                    "type": "integer"
                }
            },
        }
    }
    logical_name = "ref_schema"

    ([(tbl_logical_name, relationship)],
     _) = column_factory.column_factory(spec=spec,
                                        schemas=schemas,
                                        logical_name=logical_name,
                                        model_schema=model_schema)

    assert tbl_logical_name == logical_name
    assert relationship.argument == "RefSchema"
예제 #18
0
def test_integration_simple():
    """
    GIVEN simple column artifacts
    WHEN column_factory is called with the artifacts
    THEN a SQLAlchemy boolean column is returned.
    """
    artifacts = types.SimplePropertyArtifacts(
        type=types.PropertyType.SIMPLE,
        open_api=types.OpenApiSimplePropertyArtifacts(
            type="boolean",
            format=None,
            max_length=None,
            nullable=None,
            default=None,
            read_only=None,
            write_only=None,
        ),
        extension=types.ExtensionSimplePropertyArtifacts(
            primary_key=False,
            autoincrement=None,
            index=None,
            unique=None,
            server_default=None,
            foreign_key=None,
            kwargs=None,
            foreign_key_kwargs=None,
            dict_ignore=False,
        ),
        schema={"type": "boolean"},
        required=False,
        description=None,
    )

    column = column_factory.column_factory(artifacts=artifacts)

    assert isinstance(column, sqlalchemy.types.Column)
    assert isinstance(column.type, sqlalchemy.types.Boolean)
예제 #19
0
def test_integration_array_ref():
    """
    GIVEN schema that references another object schema from an array and schemas
    WHEN column_factory is called with the schema and schemas
    THEN foreign key reference  is added to the referenced schema and relationship is
        returned with the schema.
    """
    schema = {
        "type": "array",
        "items": {
            "$ref": "#/components/schemas/RefSchema"
        }
    }
    schemas = {
        "RefSchema": {
            "type": "object",
            "x-tablename": "table 1",
            "properties": {
                "id": {
                    "type": "integer"
                }
            },
        }
    }
    model_schema = {
        "type": "object",
        "x-tablename": "schema",
        "properties": {
            "id": {
                "type": "integer"
            }
        },
    }
    logical_name = "ref_schema"

    (
        [(tbl_logical_name, relationship)],
        returned_schema,
    ) = column_factory.column_factory(
        schema=schema,
        schemas=schemas,
        logical_name=logical_name,
        model_schema=model_schema,
        model_name="model",
    )

    assert tbl_logical_name == logical_name
    assert relationship.argument == "RefSchema"
    assert returned_schema == {
        "type": "array",
        "items": {
            "type": "object",
            "x-de-$ref": "RefSchema"
        },
    }
    assert schemas == {
        "RefSchema": {
            "allOf": [
                {
                    "type": "object",
                    "x-tablename": "table 1",
                    "properties": {
                        "id": {
                            "type": "integer"
                        }
                    },
                },
                {
                    "type": "object",
                    "properties": {
                        "schema_ref_schema_id": {
                            "type": "integer",
                            "x-foreign-key": "schema.id",
                            "x-dict-ignore": True,
                        }
                    },
                },
            ]
        }
    }