Beispiel #1
0
    def test_(schema, schemas, expected_valid, expected_reasons):
        """
        GIVEN schema, schemas, and expected valid and reasons
        WHEN _check_duplicate_foreign_key is called with the schema and schemas
        THEN the expected valid and reasons are returned.
        """
        name = "Schema"
        association = association_helper.TParentPropertySchema(
            parent=types.TNameSchema(name="ParentSchema", schema={}),
            property=types.TNameSchema(name="PropertySchema", schema={}),
        )

        returned_result = validation.association._check_duplicate_foreign_key(
            name=name, schema=schema, association=association, schemas=schemas)

        assert returned_result.valid == expected_valid
        if expected_reasons is not None:
            expected_reasons = (
                *expected_reasons,
                name,
                association.parent.name,
                association.property.name,
                "duplicate",
                "foreign key",
            )

            for reason in expected_reasons:
                assert reason in returned_result.reason
    def test_(schema, schemas, expected_valid):
        """
        GIVEN schema, schemas, and expected valid
        WHEN _check_2_or_fewer_primary_key is called with the schema and schemas
        THEN the expected valid and reasons are returned.
        """
        name = "Schema"
        association = association_helper.TParentPropertySchema(
            parent=types.TNameSchema(name="ParentSchema", schema={}),
            property=types.TNameSchema(name="PropertySchema", schema={}),
        )

        returned_result = validation.association._check_2_or_fewer_primary_key(
            name=name, schema=schema, association=association, schemas=schemas
        )

        assert returned_result.valid == expected_valid
        if not expected_valid:
            expected_reasons = (
                name,
                association.parent.name,
                association.property.name,
                "too many",
                "primary key",
            )

            for reason in expected_reasons:
                assert reason in returned_result.reason
Beispiel #3
0
    def test_(schema, schemas, expected_valid, expected_reasons):
        """
        GIVEN schema, schemas, and expected valid and reasons
        WHEN _check_properties_valid is called with the schema and schemas
        THEN the expected valid and reasons are returned.
        """
        name = "Schema"
        association = helpers.association.TParentPropertySchema(
            parent=types.TNameSchema(
                name="ParentSchema",
                schema={
                    "x-tablename": "parent_table",
                    "properties": {
                        "parent_column": {
                            "type": "parent type",
                            "x-primary-key": True,
                            "format": "parent format",
                            "maxLength": 1,
                        }
                    },
                },
            ),
            property=types.TNameSchema(
                name="PropertySchema",
                schema={"items": {
                    "$ref": "#/components/schemas/RefSchema"
                }},
            ),
        )
        schemas["RefSchema"] = {
            "x-tablename": "ref_table",
            "x-secondary": "association",
            "properties": {
                "ref_column": {
                    "type": "ref type",
                    "x-primary-key": True
                }
            },
        }

        returned_result = validation.association._check_properties_valid(
            name=name, schema=schema, association=association, schemas=schemas)

        assert returned_result.valid == expected_valid
        if expected_reasons is not None:
            expected_reasons = (
                *expected_reasons,
                name,
                association.parent.name,
                association.property.name,
            )

            for reason in expected_reasons:
                assert reason in returned_result.reason
Beispiel #4
0
    def test_(schema, expected_valid):
        """
        GIVEN schema, schemas, and expected valid
        WHEN _validate_schema is called with the schema and schemas
        THEN the expected valid are returned.
        """
        name = "Schema"
        association = helpers.association.TParentPropertySchema(
            parent=types.TNameSchema(
                name="ParentSchema",
                schema={
                    "x-tablename": "parent_table",
                    "properties": {
                        "parent_column": {
                            "type": "parent type",
                            "x-primary-key": True,
                            "format": "parent format",
                            "maxLength": 1,
                        }
                    },
                },
            ),
            property=types.TNameSchema(
                name="PropertySchema",
                schema={"items": {
                    "$ref": "#/components/schemas/RefSchema"
                }},
            ),
        )
        schemas = {
            "RefSchema": {
                "x-tablename": "ref_table",
                "x-secondary": "association",
                "properties": {
                    "ref_column": {
                        "type": "ref type",
                        "x-primary-key": True
                    }
                },
            }
        }

        returned_result = validation.association._validate_schema(
            name=name, schema=schema, association=association, schemas=schemas)

        assert returned_result.valid == expected_valid
class TestCombineDefinedExpectedSchemas:
    """Tests for _combine_defined_expected_schemas."""

    # pylint: disable=protected-access

    TESTS = [
        pytest.param([], {}, [], id="empty"),
        pytest.param(
            [
                types.TNameSchema(
                    name="Association1",
                    schema={
                        "x-tablename": "association",
                        "properties": {
                            "prop_1": {
                                "x-foreign-key": "foreign.key1"
                            },
                            "prop_2": {
                                "x-foreign-key": "foreign.key2"
                            },
                        },
                        "required": ["prop_1", "prop_2"],
                    },
                )
            ],
            {},
            [
                types.TNameSchema(
                    name="Association1",
                    schema={
                        "x-tablename": "association",
                        "properties": {
                            "prop_1": {
                                "x-foreign-key": "foreign.key1"
                            },
                            "prop_2": {
                                "x-foreign-key": "foreign.key2"
                            },
                        },
                        "required": ["prop_1", "prop_2"],
                    },
                )
            ],
            id="single not defined",
        ),
        pytest.param(
            [
                types.TNameSchema(
                    name="Association1",
                    schema={
                        "x-tablename": "association",
                        "properties": {
                            "prop_1": {
                                "x-foreign-key": "foreign.key1"
                            },
                            "prop_2": {
                                "x-foreign-key": "foreign.key2"
                            },
                        },
                        "required": ["prop_1", "prop_2"],
                    },
                )
            ],
            {
                "Schema1": {
                    "x-tablename": "association",
                    "properties": {
                        "prop_1": {
                            "x-foreign-key": "foreign.key1"
                        }
                    },
                }
            },
            [
                types.TNameSchema(
                    name="Schema1",
                    schema={
                        "allOf": [
                            {
                                "x-tablename": "association",
                                "properties": {
                                    "prop_2": {
                                        "x-foreign-key": "foreign.key2"
                                    },
                                },
                            },
                            {
                                "x-tablename": "association",
                                "properties": {
                                    "prop_1": {
                                        "x-foreign-key": "foreign.key1"
                                    }
                                },
                            },
                        ]
                    },
                )
            ],
            id="single defined",
        ),
        pytest.param(
            [
                types.TNameSchema(
                    name="Association1",
                    schema={
                        "x-tablename": "association1",
                        "properties": {
                            "prop_1": {
                                "x-foreign-key": "foreign1.key1"
                            },
                            "prop_2": {
                                "x-foreign-key": "foreign1.key2"
                            },
                        },
                        "required": ["prop_1", "prop_2"],
                    },
                ),
                types.TNameSchema(
                    name="Association2",
                    schema={
                        "x-tablename": "association2",
                        "properties": {
                            "prop_1": {
                                "x-foreign-key": "foreign2.key1"
                            },
                            "prop_2": {
                                "x-foreign-key": "foreign2.key2"
                            },
                        },
                        "required": ["prop_1", "prop_2"],
                    },
                ),
            ],
            {},
            [
                types.TNameSchema(
                    name="Association1",
                    schema={
                        "x-tablename": "association1",
                        "properties": {
                            "prop_1": {
                                "x-foreign-key": "foreign1.key1"
                            },
                            "prop_2": {
                                "x-foreign-key": "foreign1.key2"
                            },
                        },
                        "required": ["prop_1", "prop_2"],
                    },
                ),
                types.TNameSchema(
                    name="Association2",
                    schema={
                        "x-tablename": "association2",
                        "properties": {
                            "prop_1": {
                                "x-foreign-key": "foreign2.key1"
                            },
                            "prop_2": {
                                "x-foreign-key": "foreign2.key2"
                            },
                        },
                        "required": ["prop_1", "prop_2"],
                    },
                ),
            ],
            id="multiple not defined",
        ),
        pytest.param(
            [
                types.TNameSchema(
                    name="Association1",
                    schema={
                        "x-tablename": "association1",
                        "properties": {
                            "prop_1": {
                                "x-foreign-key": "foreign1.key1"
                            },
                            "prop_2": {
                                "x-foreign-key": "foreign1.key2"
                            },
                        },
                        "required": ["prop_1", "prop_2"],
                    },
                ),
                types.TNameSchema(
                    name="Association2",
                    schema={
                        "x-tablename": "association2",
                        "properties": {
                            "prop_1": {
                                "x-foreign-key": "foreign2.key1"
                            },
                            "prop_2": {
                                "x-foreign-key": "foreign2.key2"
                            },
                        },
                        "required": ["prop_1", "prop_2"],
                    },
                ),
            ],
            {
                "Schema1": {
                    "x-tablename": "association1",
                    "properties": {
                        "prop_1": {
                            "x-foreign-key": "foreign1.key1"
                        }
                    },
                }
            },
            [
                types.TNameSchema(
                    name="Schema1",
                    schema={
                        "allOf": [
                            {
                                "x-tablename": "association1",
                                "properties": {
                                    "prop_2": {
                                        "x-foreign-key": "foreign1.key2"
                                    },
                                },
                            },
                            {
                                "x-tablename": "association1",
                                "properties": {
                                    "prop_1": {
                                        "x-foreign-key": "foreign1.key1"
                                    }
                                },
                            },
                        ]
                    },
                ),
                types.TNameSchema(
                    name="Association2",
                    schema={
                        "x-tablename": "association2",
                        "properties": {
                            "prop_1": {
                                "x-foreign-key": "foreign2.key1"
                            },
                            "prop_2": {
                                "x-foreign-key": "foreign2.key2"
                            },
                        },
                        "required": ["prop_1", "prop_2"],
                    },
                ),
            ],
            id="multiple first defined",
        ),
        pytest.param(
            [
                types.TNameSchema(
                    name="Association1",
                    schema={
                        "x-tablename": "association1",
                        "properties": {
                            "prop_1": {
                                "x-foreign-key": "foreign1.key1"
                            },
                            "prop_2": {
                                "x-foreign-key": "foreign1.key2"
                            },
                        },
                        "required": ["prop_1", "prop_2"],
                    },
                ),
                types.TNameSchema(
                    name="Association2",
                    schema={
                        "x-tablename": "association2",
                        "properties": {
                            "prop_1": {
                                "x-foreign-key": "foreign2.key1"
                            },
                            "prop_2": {
                                "x-foreign-key": "foreign2.key2"
                            },
                        },
                        "required": ["prop_1", "prop_2"],
                    },
                ),
            ],
            {
                "Schema2": {
                    "x-tablename": "association2",
                    "properties": {
                        "prop_1": {
                            "x-foreign-key": "foreign2.key1"
                        }
                    },
                }
            },
            [
                types.TNameSchema(
                    name="Association1",
                    schema={
                        "x-tablename": "association1",
                        "properties": {
                            "prop_1": {
                                "x-foreign-key": "foreign1.key1"
                            },
                            "prop_2": {
                                "x-foreign-key": "foreign1.key2"
                            },
                        },
                        "required": ["prop_1", "prop_2"],
                    },
                ),
                types.TNameSchema(
                    name="Schema2",
                    schema={
                        "allOf": [
                            {
                                "x-tablename": "association2",
                                "properties": {
                                    "prop_2": {
                                        "x-foreign-key": "foreign2.key2"
                                    },
                                },
                            },
                            {
                                "x-tablename": "association2",
                                "properties": {
                                    "prop_1": {
                                        "x-foreign-key": "foreign2.key1"
                                    }
                                },
                            },
                        ]
                    },
                ),
            ],
            id="multiple second defined",
        ),
        pytest.param(
            [
                types.TNameSchema(
                    name="Association1",
                    schema={
                        "x-tablename": "association1",
                        "properties": {
                            "prop_1": {
                                "x-foreign-key": "foreign1.key1"
                            },
                            "prop_2": {
                                "x-foreign-key": "foreign1.key2"
                            },
                        },
                        "required": ["prop_1", "prop_2"],
                    },
                ),
                types.TNameSchema(
                    name="Association2",
                    schema={
                        "x-tablename": "association2",
                        "properties": {
                            "prop_1": {
                                "x-foreign-key": "foreign2.key1"
                            },
                            "prop_2": {
                                "x-foreign-key": "foreign2.key2"
                            },
                        },
                        "required": ["prop_1", "prop_2"],
                    },
                ),
            ],
            {
                "Schema1": {
                    "x-tablename": "association1",
                    "properties": {
                        "prop_1": {
                            "x-foreign-key": "foreign1.key1"
                        }
                    },
                },
                "Schema2": {
                    "x-tablename": "association2",
                    "properties": {
                        "prop_1": {
                            "x-foreign-key": "foreign2.key1"
                        }
                    },
                },
            },
            [
                types.TNameSchema(
                    name="Schema1",
                    schema={
                        "allOf": [
                            {
                                "x-tablename": "association1",
                                "properties": {
                                    "prop_2": {
                                        "x-foreign-key": "foreign1.key2"
                                    },
                                },
                            },
                            {
                                "x-tablename": "association1",
                                "properties": {
                                    "prop_1": {
                                        "x-foreign-key": "foreign1.key1"
                                    }
                                },
                            },
                        ]
                    },
                ),
                types.TNameSchema(
                    name="Schema2",
                    schema={
                        "allOf": [
                            {
                                "x-tablename": "association2",
                                "properties": {
                                    "prop_2": {
                                        "x-foreign-key": "foreign2.key2"
                                    },
                                },
                            },
                            {
                                "x-tablename": "association2",
                                "properties": {
                                    "prop_1": {
                                        "x-foreign-key": "foreign2.key1"
                                    }
                                },
                            },
                        ]
                    },
                ),
            ],
            id="multiple all defined",
        ),
    ]

    @staticmethod
    @pytest.mark.parametrize("association_schemas, schemas, expected_schemas",
                             TESTS)
    @pytest.mark.schemas
    @pytest.mark.association
    def test_(association_schemas, schemas, expected_schemas):
        """
        GIVEN association_schemas, schemas and expected schemas
        WHEN _combine_defined_expected_schemas is called with the association_schemas
            and schemas
        THEN the expected schemas are returned.
        """
        returned_schemas = association._combine_defined_expected_schemas(
            association_schemas=association_schemas, schemas=schemas)

        assert list(returned_schemas) == expected_schemas
Beispiel #6
0
class TestGetTablenameForeignKeys:
    """Tests for _combine_defined_expected_schema."""

    # pylint: disable=protected-access

    TESTS = [
        pytest.param(
            association._TParentNameForeignKeys(parent_name="Parent1",
                                                foreign_keys=set()),
            types.TNameSchema(
                name="Association1",
                schema={
                    "properties": {
                        "prop_1": {
                            "x-foreign-key": "foreign.key1"
                        },
                        "prop_2": {
                            "x-foreign-key": "foreign.key2"
                        },
                    },
                    "required": ["prop_1", "prop_2"],
                },
            ),
            {"Parent1": {
                "key": "value"
            }},
            types.TNameSchema(
                name="Parent1",
                schema={
                    "allOf": [
                        {
                            "properties": {
                                "prop_1": {
                                    "x-foreign-key": "foreign.key1"
                                },
                                "prop_2": {
                                    "x-foreign-key": "foreign.key2"
                                },
                            }
                        },
                        {
                            "key": "value"
                        },
                    ]
                },
            ),
            id="foreign key empty",
        ),
        pytest.param(
            association._TParentNameForeignKeys(parent_name="Parent2",
                                                foreign_keys=set()),
            types.TNameSchema(
                name="Association1",
                schema={
                    "properties": {
                        "prop_1": {
                            "x-foreign-key": "foreign.key1"
                        },
                        "prop_2": {
                            "x-foreign-key": "foreign.key2"
                        },
                    },
                    "required": ["prop_1", "prop_2"],
                },
            ),
            {"Parent2": {
                "key_2": "value 2"
            }},
            types.TNameSchema(
                name="Parent2",
                schema={
                    "allOf": [
                        {
                            "properties": {
                                "prop_1": {
                                    "x-foreign-key": "foreign.key1"
                                },
                                "prop_2": {
                                    "x-foreign-key": "foreign.key2"
                                },
                            }
                        },
                        {
                            "key_2": "value 2"
                        },
                    ]
                },
            ),
            id="foreign key empty different parent",
        ),
        pytest.param(
            association._TParentNameForeignKeys(parent_name="Parent1",
                                                foreign_keys={"foreign.key3"}),
            types.TNameSchema(
                name="Association1",
                schema={
                    "properties": {
                        "prop_1": {
                            "x-foreign-key": "foreign.key1"
                        },
                        "prop_2": {
                            "x-foreign-key": "foreign.key2"
                        },
                    },
                    "required": ["prop_1", "prop_2"],
                },
            ),
            {"Parent1": {
                "key": "value"
            }},
            types.TNameSchema(
                name="Parent1",
                schema={
                    "allOf": [
                        {
                            "properties": {
                                "prop_1": {
                                    "x-foreign-key": "foreign.key1"
                                },
                                "prop_2": {
                                    "x-foreign-key": "foreign.key2"
                                },
                            }
                        },
                        {
                            "key": "value"
                        },
                    ]
                },
            ),
            id="single foreign key miss",
        ),
        pytest.param(
            association._TParentNameForeignKeys(parent_name="Parent1",
                                                foreign_keys={"foreign.key1"}),
            types.TNameSchema(
                name="Association1",
                schema={
                    "properties": {
                        "prop_1": {
                            "x-foreign-key": "foreign.key1"
                        },
                        "prop_2": {
                            "x-foreign-key": "foreign.key2"
                        },
                    },
                    "required": ["prop_1", "prop_2"],
                },
            ),
            {"Parent1": {
                "key": "value"
            }},
            types.TNameSchema(
                name="Parent1",
                schema={
                    "allOf": [
                        {
                            "properties": {
                                "prop_2": {
                                    "x-foreign-key": "foreign.key2"
                                },
                            }
                        },
                        {
                            "key": "value"
                        },
                    ]
                },
            ),
            id="single foreign key first hit",
        ),
        pytest.param(
            association._TParentNameForeignKeys(parent_name="Parent1",
                                                foreign_keys={"foreign.key2"}),
            types.TNameSchema(
                name="Association1",
                schema={
                    "properties": {
                        "prop_1": {
                            "x-foreign-key": "foreign.key1"
                        },
                        "prop_2": {
                            "x-foreign-key": "foreign.key2"
                        },
                    },
                    "required": ["prop_1", "prop_2"],
                },
            ),
            {"Parent1": {
                "key": "value"
            }},
            types.TNameSchema(
                name="Parent1",
                schema={
                    "allOf": [
                        {
                            "properties": {
                                "prop_1": {
                                    "x-foreign-key": "foreign.key1"
                                },
                            }
                        },
                        {
                            "key": "value"
                        },
                    ]
                },
            ),
            id="single foreign key second hit",
        ),
        pytest.param(
            association._TParentNameForeignKeys(
                parent_name="Parent1",
                foreign_keys={"foreign.key1", "foreign.key2"}),
            types.TNameSchema(
                name="Association1",
                schema={
                    "properties": {
                        "prop_1": {
                            "x-foreign-key": "foreign.key1"
                        },
                        "prop_2": {
                            "x-foreign-key": "foreign.key2"
                        },
                    },
                    "required": ["prop_1", "prop_2"],
                },
            ),
            {"Parent1": {
                "key": "value"
            }},
            types.TNameSchema(
                name="Parent1",
                schema={"allOf": [
                    {
                        "properties": {}
                    },
                    {
                        "key": "value"
                    },
                ]},
            ),
            id="multiple foreign key all hit",
        ),
    ]

    @staticmethod
    @pytest.mark.parametrize(
        "parent_name_foreign_keys, expected_schema, schemas, expected_returned_schema",
        TESTS,
    )
    @pytest.mark.schemas
    @pytest.mark.association
    def test_(parent_name_foreign_keys, expected_schema, schemas,
              expected_returned_schema):
        """
        GIVEN parent name and foreign keys, expected schema and expected returned schema
        WHEN _combine_defined_expected_schema is called with the parent name and foreign
            keys and expected schema
        THEN the expected returned schema is returned.
        """
        returned_schema = association._combine_defined_expected_schema(
            parent_name_foreign_keys=parent_name_foreign_keys,
            expected_schema=expected_schema,
            schemas=schemas,
        )

        assert returned_schema == expected_returned_schema