def test_single_property_column_factory_call(mocked_column_factory: mock.MagicMock): """ GIVEN mocked column_factory and schemas with schema that has single item properties key and does not have the required key WHEN model_factory is called with the name of the schema THEN column_factory is called with required as None. """ model_schema = { "x-tablename": "table 1", "type": "object", "properties": {"id": {"type": "integer"}}, } model_name = "SingleProperty" schemas = {model_name: model_schema} artifacts = schemas_artifacts.get_from_schemas( schemas=schemas, stay_within_model=True ) model_factory.model_factory( name=model_name, get_base=_mock_get_base, schemas=copy.deepcopy(schemas), artifacts=artifacts, ) mocked_column_factory.assert_called_once_with( artifacts=artifacts[model_name].properties[0][1] )
def test_single_property_required(mocked_column_factory: mock.MagicMock): """ GIVEN mocked column_factory and schemas with schema that has single item properties key and a required key with the key in properties WHEN model_factory is called with the name of the schema THEN column_factory is called with required reset. """ model_schema = { "x-tablename": "table 1", "type": "object", "properties": {"id": {"type": "integer"}}, "required": ["id"], } model_name = "SingleProperty" schemas = {model_name: model_schema} model_factory.model_factory( name=model_name, base=mock.MagicMock, schemas=copy.deepcopy(schemas) ) mocked_column_factory.assert_called_once_with( spec={"type": "integer"}, schemas=schemas, logical_name="id", required=True, model_name=model_name, model_schema=model_schema, )
def test_schema_relationship_invalid(): """ GIVEN schema with x-backrefs with invalid schema WHEN model_factory is called with the schema THEN MalformedExtensionPropertyError is raised. """ schemas = { "Schema": { "x-tablename": "table 1", "type": "object", "properties": { "property_1": { "type": "integer" } }, "x-backrefs": { "ref_schema": "RefSchema" }, } } with pytest.raises(exceptions.MalformedExtensionPropertyError): model_factory.model_factory(name="Schema", get_base=_mock_get_base, schemas=schemas)
def test_single_property_required_missing( mocked_column_factory: mock.MagicMock): """ GIVEN mocked column_factory and schemas with schema that has single item properties key and does not have the required key WHEN model_factory is called with the name of the schema THEN column_factory is called with required as None. """ schemas = { "SingleProperty": { "x-tablename": "table 1", "type": "object", "properties": { "id": { "type": "integer" } }, } } model_factory.model_factory(name="SingleProperty", base=mock.MagicMock, schemas=copy.deepcopy(schemas)) mocked_column_factory.assert_called_once_with(spec={"type": "integer"}, schemas=schemas, logical_name="id", required=None)
def test_missing_schema(): """ GIVEN schemas and name that is not in schemas WHEN model_factory is called THEN SchemaNotFoundError is raised. """ with pytest.raises(exceptions.SchemaNotFoundError): model_factory.model_factory(name="Missing", base=None, schemas={})
def test_missing_tablename(): """ GIVEN schemas and name that refers to a schema without the x-tablename key WHEN model_factory is called THEN MalformedSchemaError is raised. """ with pytest.raises(exceptions.MalformedSchemaError): model_factory.model_factory( name="MissingTablename", base=None, schemas={"MissingTablename": {}} )
def test_properties_missing(): """ GIVEN schemas with schema that does not have the properties key WHEN model_factory is called with the name of the schema THEN MalformedSchemaError is raised. """ with pytest.raises(exceptions.MalformedSchemaError): model_factory.model_factory( name="MissingProperty", base=None, schemas={"MissingProperty": {"x-tablename": "table 1", "type": "object"}}, )
def test_not_object(): """ GIVEN schemas with schema that is not an object WHEN model_factory is called with the name of the schema THEN FeatureNotImplementedError is raised. """ with pytest.raises(exceptions.FeatureNotImplementedError): model_factory.model_factory( name="NotObject", base=None, schemas={"NotObject": {"x-tablename": "table 1", "type": "not_object"}}, )
def test_all_of(): """ GIVEN schemas with schema that has allOf and the referenced schema WHEN model_factory is called with the name of the schema THEN a model with the property and tablename is returned. """ model = model_factory.model_factory( name="Schema", base=mock.MagicMock, schemas={ "Schema": { "allOf": [{ "x-tablename": "table 1", "type": "object", "properties": { "property_1": { "type": "integer" } }, }] } }, ) assert hasattr(model, "property_1") assert model.__tablename__ == "table 1"
def test_multiple_property(): """ GIVEN schemas with schema that has multiple item properties key WHEN model_factory is called with the name of the schema THEN a model with the properties is returned. """ model = model_factory.model_factory( name="SingleProperty", base=mock.MagicMock, schemas={ "SingleProperty": { "x-tablename": "table 1", "type": "object", "properties": { "property_1": { "type": "integer" }, "property_2": { "type": "integer" }, }, } }, ) assert hasattr(model, "property_1") assert hasattr(model, "property_2")
def test_table_args_unique(): """ GIVEN schemas with schema that has a unique constraint WHEN model_factory is called with the name of the schema THEN a model with a unique constraint is returned. """ model = model_factory.model_factory( name="SingleProperty", get_base=_mock_get_base, schemas={ "SingleProperty": { "x-tablename": "table 1", "type": "object", "properties": { "property_1": { "type": "integer" } }, "x-composite-unique": ["property_1"], } }, ) (unique, ) = model.__table_args__ assert isinstance(unique, sql_schema.UniqueConstraint)
def test_ref(): """ GIVEN schemas with schema that has $ref and the referenced schema WHEN model_factory is called with the name of the schema THEN a model with the property and tablename is returned. """ model = model_factory.model_factory( name="Schema", get_base=_mock_get_base, schemas={ "Schema": { "$ref": "#/components/schemas/RefSchema" }, "RefSchema": { "x-tablename": "table 1", "type": "object", "properties": { "property_1": { "type": "integer" } }, }, }, ) assert hasattr(model, "property_1") assert model.__tablename__ == "table 1"
def test_tablename(): """ GIVEN schemas with schema WHEN model_factory is called with the name of the schema THEN a model where __tablename__ has been set to the x-tablename value. """ schemas = { "SingleProperty": { "x-tablename": "table 1", "type": "object", "properties": {"property_1": {"type": "integer"}}, } } artifacts = schemas_artifacts.get_from_schemas( schemas=schemas, stay_within_model=True ) model = model_factory.model_factory( name="SingleProperty", get_base=_mock_get_base, schemas=schemas, artifacts=artifacts, ) assert model.__tablename__ == "table 1"
def test_table_args_index(): """ GIVEN schemas with schema that has a composite index WHEN model_factory is called with the name of the schema THEN a model with a composite index is returned. """ model = model_factory.model_factory( name="SingleProperty", base=mock.MagicMock, schemas={ "SingleProperty": { "x-tablename": "table 1", "type": "object", "properties": { "property_1": { "type": "integer" } }, "x-composite-index": ["property_1"], } }, ) (index, ) = model.__table_args__ assert isinstance(index, schema.Index)
def test_kwargs(): """ GIVEN schemas with schema that has kwargs WHEN model_factory is called with the name of the schema THEN a model with the kwargs is returned. """ schemas = { "SingleProperty": { "x-tablename": "table 1", "type": "object", "properties": {"property_1": {"type": "integer"}}, "x-kwargs": {"__mapper_args__": {"passive_deletes": True}}, } } artifacts = schemas_artifacts.get_from_schemas( schemas=schemas, stay_within_model=True ) model = model_factory.model_factory( name="SingleProperty", get_base=_mock_get_base, schemas=schemas, artifacts=artifacts, ) assert model.__mapper_args__ == {"passive_deletes": True}
def test_table_args_index(): """ GIVEN schemas with schema that has a composite index WHEN model_factory is called with the name of the schema THEN a model with a composite index is returned. """ schemas = { "SingleProperty": { "x-tablename": "table 1", "type": "object", "properties": {"property_1": {"type": "integer"}}, "x-composite-index": ["property_1"], } } artifacts = schemas_artifacts.get_from_schemas( schemas=schemas, stay_within_model=True ) model = model_factory.model_factory( name="SingleProperty", get_base=_mock_get_base, schemas=schemas, artifacts=artifacts, ) (index,) = model.__table_args__ assert isinstance(index, sql_schema.Index)
def test_multiple_property(): """ GIVEN schemas with schema that has multiple item properties key WHEN model_factory is called with the name of the schema THEN a model with the properties is returned. """ schemas = { "SingleProperty": { "x-tablename": "table 1", "type": "object", "properties": { "property_1": {"type": "integer"}, "property_2": {"type": "integer"}, }, } } artifacts = schemas_artifacts.get_from_schemas( schemas=schemas, stay_within_model=True ) model = model_factory.model_factory( name="SingleProperty", get_base=_mock_get_base, schemas=schemas, artifacts=artifacts, ) assert hasattr(model, "property_1") assert hasattr(model, "property_2")
def test_inherits(): """ GIVEN schemas with schema that inherits WHEN model_factory is called with the name of the schema THEN a model which inherits from the parent and with only the child properties defined is returned. """ schemas = { "Child": { "allOf": [ { "x-inherits": True, "type": "object", "properties": {"property_2": {"type": "integer"}}, }, {"$ref": "#/components/schemas/Parent"}, ] }, "Parent": { "x-tablename": "parent", "type": "object", "properties": {"property_1": {"type": "string"}}, }, } artifacts = schemas_artifacts.get_from_schemas( schemas=schemas, stay_within_model=True ) model = model_factory.model_factory( name="Child", get_base=_mock_get_base, schemas=schemas, artifacts=artifacts ) assert not hasattr(model, "property_1") assert hasattr(model, "property_2") assert getattr(model, "__tablename__", None) is None
def test_schema_name(): """ GIVEN schemas with schema that has a custom name WHEN model_factory is called with the name of the schema THEN a model with a custom schema name is returned. """ schemas = { "SchemaName": { "x-tablename": "table 1", "x-schema-name": "schema 1", "type": "object", "properties": {"property_1": {"type": "integer"}}, } } artifacts = schemas_artifacts.get_from_schemas( schemas=schemas, stay_within_model=True ) model = model_factory.model_factory( name="SchemaName", get_base=_mock_get_base, schemas=schemas, artifacts=artifacts, ) assert model.__table_args__[0]["schema"] == "schema 1"
def test_all_of(): """ GIVEN schemas with schema that has allOf and the referenced schema WHEN model_factory is called with the name of the schema THEN a model with the property and tablename is returned. """ schemas = { "Schema": { "allOf": [ { "x-tablename": "table 1", "type": "object", "properties": {"property_1": {"type": "integer"}}, } ] } } artifacts = schemas_artifacts.get_from_schemas( schemas=schemas, stay_within_model=True ) model = model_factory.model_factory( name="Schema", get_base=_mock_get_base, schemas=schemas, artifacts=artifacts ) assert hasattr(model, "property_1") assert model.__tablename__ == "table 1"
def test_properties_empty(): """ GIVEN schemas with schema that has empty properties key WHEN model_factory is called with the name of the schema THEN MalformedSchemaError is raised. """ with pytest.raises(exceptions.MalformedSchemaError): model_factory.model_factory( name="EmptyProperty", base=None, schemas={ "EmptyProperty": { "x-tablename": "table 1", "type": "object", "properties": {}, } }, )
def test_tablename_none(): """ GIVEN schemas with schema that has None for the tablename WHEN model_factory is called with the name of the schema THEN MalformedExtensionPropertyError is raised. """ with pytest.raises(exceptions.MalformedExtensionPropertyError): model_factory.model_factory( name="SingleProperty", base=mock.MagicMock, schemas={ "SingleProperty": { "x-tablename": None, "type": "object", "properties": {"property_1": {"type": "integer"}}, } }, )
def test_schema(schemas, expected_schema): """ GIVEN schemas and expected schema WHEN model_factory is called with the schemas and the name of a schema THEN a model with _schema set to the expected schema is returned. """ model = model_factory.model_factory( name="Schema", base=mock.MagicMock, schemas=schemas ) assert model._schema == expected_schema
def test_schema(schemas, expected_schema): """ GIVEN schemas and expected schema WHEN model_factory is called with the schemas and the name of a schema THEN a model with _schema set to the expected schema is returned. """ artifacts = schemas_artifacts.get_from_schemas( schemas=schemas, stay_within_model=True ) model = model_factory.model_factory( name="Schema", get_base=_mock_get_base, schemas=schemas, artifacts=artifacts ) assert model._schema == expected_schema
def test_tablename(): """ GIVEN schemas with schema WHEN model_factory is called with the name of the schema THEN a model where __tablename__ has been set to the x-tablename value. """ model = model_factory.model_factory( name="SingleProperty", base=mock.MagicMock, schemas={ "SingleProperty": { "x-tablename": "table 1", "type": "object", "properties": {"property_1": {"type": "integer"}}, } }, ) assert model.__tablename__ == "table 1"
def test_mixin(monkeypatch): """ GIVEN schemas with schema that has a mixin WHEN model_factory is called with the name of the schema THEN a model with the property is returned. """ # Define mixin mock_import_module = mock.MagicMock() mixin_class = type( "Mixin1", (), { "property_2": sqlalchemy.types.Column(sqlalchemy.types.Integer), "__abstract__": True, }, ) mock_import_module.return_value.Mixin1 = mixin_class monkeypatch.setattr(importlib, "import_module", mock_import_module) schemas = { "SingleProperty": { "x-tablename": "table 1", "type": "object", "x-mixins": "module.Mixin1", "properties": { "property_1": { "type": "integer" } }, } } artifacts = schemas_artifacts.get_from_schemas(schemas=schemas, stay_within_model=True) model = model_factory.model_factory( name="SingleProperty", get_base=_mock_get_base, schemas=schemas, artifacts=artifacts, ) assert hasattr(model, "property_2") assert model.__abstract__ is False
def test_kwargs(): """ GIVEN schemas with schema that has kwargs WHEN model_factory is called with the name of the schema THEN a model with the kwargs is returned. """ model = model_factory.model_factory( name="SingleProperty", base=mock.MagicMock, schemas={ "SingleProperty": { "x-tablename": "table 1", "type": "object", "properties": {"property_1": {"type": "integer"}}, "x-kwargs": {"__mapper_args__": {"passive_deletes": True}}, } }, ) assert model.__mapper_args__ == {"passive_deletes": True}