def controller() -> layabase.CRUDController: class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(int, is_primary_key=True, should_auto_increment=True) int_choices_field = layabase.mongo.Column( int, description="Test Documentation", choices=[1, 2, 3]) str_choices_field = layabase.mongo.Column( str, description="Test Documentation", choices=["one", "two", "three"]) float_choices_field = layabase.mongo.Column( float, description="Test Documentation", choices=[1.25, 1.5, 1.75]) controller = layabase.CRUDController(TestCollection) layabase.load("mongomock", [controller]) return controller
def controller(): class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(is_primary_key=True) list_field = layabase.mongo.ListColumn( layabase.mongo.DictColumn( fields={ "first_key": layabase.mongo.Column(EnumTest, is_nullable=False), "second_key": layabase.mongo.Column(int, is_nullable=False), }), store_none=True, ) bool_field = layabase.mongo.Column(bool) controller = layabase.CRUDController(TestCollection) layabase.load("mongomock", [controller]) return controller
def test_tables_are_added_to_metadata(): class TestTable: __tablename__ = "test" key = sqlalchemy.Column(sqlalchemy.String, primary_key=True) @classmethod def _post_init(cls, base): pass db = layabase.load("sqlite:///:memory:", [layabase.CRUDController(TestTable)]) assert "sqlite:///:memory:" == str(db.metadata.bind.engine.url) assert ["test"] == list(db.metadata.tables.keys())
def test_multi_schema_not_handled(): class TestTable: __tablename__ = "test" __table_args__ = {u"schema": "schema_name1"} key = sqlalchemy.Column(sqlalchemy.String, primary_key=True) class TestTable2: __tablename__ = "test" __table_args__ = {u"schema": "schema_name2"} key = sqlalchemy.Column(sqlalchemy.String, primary_key=True) with pytest.raises(layabase.MultiSchemaNotSupported) as exception_info: layabase.load( "sqlite:///:memory:", [ layabase.CRUDController(TestTable), layabase.CRUDController(TestTable2) ], ) assert str( exception_info.value) == "SQLite does not manage multi-schemas.."
def controller(): class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(is_primary_key=True) dict_field = layabase.mongo.DictColumn( fields={ "first_key": layabase.mongo.DictColumn( fields={ "inner_key1": layabase.mongo.Column( EnumTest, is_nullable=False ), "inner_key2": layabase.mongo.Column(int, is_nullable=False), }, is_required=True, ), "second_key": layabase.mongo.Column(int, is_nullable=False), }, is_required=True, ) controller = layabase.CRUDController(TestCollection) layabase.load("mongomock", [controller]) return controller
def test_2entities_on_same_collection_without_pk(): class TestCollection: __collection_name__ = "test" key = layabase.mongo.Column(str, is_primary_key=True) mandatory = layabase.mongo.Column(int, is_nullable=False) optional = layabase.mongo.Column(str) controller = layabase.CRUDController(TestCollection, history=True) mongo_base = layabase.load("mongomock", [controller]) controller.post({"key": "1", "mandatory": 2}) controller.post({"key": "2", "mandatory": 2}) class TestCollection2: __collection_name__ = "test" controller = layabase.CRUDController(TestCollection2, history=True) with pytest.raises(pymongo.errors.DuplicateKeyError): layabase.mongo.link(controller, mongo_base)
def test_sqla_no_controllers_is_invalid(): with pytest.raises(layabase.NoRelatedControllers) as exception_info: layabase.load("sqlite:///:memory:", None) assert str(exception_info.value) == "A list of CRUDController must be provided."
def test_empty_connection_string_is_invalid(): with pytest.raises(layabase.NoDatabaseProvided) as exception_info: layabase.load("", None) assert str(exception_info.value) == "A database connection URL must be provided."
def controllers(controller_versioned, controller_unique, controller_versioned_unique): return layabase.load( "mongomock", [controller_versioned, controller_unique, controller_versioned_unique], )
def disconnected_database(controller: layabase.CRUDController): _db = layabase.load("sqlite:///:memory:", [controller]) _db.metadata.bind.dispose() yield _db
def controllers(controller_insert, controller_not_inserted, controller_retrieve): return layabase.load( "mongomock", [controller_insert, controller_not_inserted, controller_retrieve] )
def controllers(controller1: layabase.CRUDController, controller2: layabase.CRUDController): return layabase.load("sqlite:///:memory:", [controller1, controller2])
def test_open_api_definition_without_offset_support(monkeypatch): application = flask.Flask(__name__) application.testing = True api = flask_restx.Api(application) namespace = api.namespace("Test", path="/") class TestTable: __tablename__ = "test" key = sqlalchemy.Column(sqlalchemy.String, primary_key=True) mandatory = sqlalchemy.Column(sqlalchemy.Integer, nullable=False) optional = sqlalchemy.Column(sqlalchemy.String) controller = layabase.CRUDController(TestTable, audit=True) monkeypatch.setattr(layabase._database_sqlalchemy, "_supports_offset", lambda *args: False) layabase.load("sqlite:///:memory:", [controller]) controller.flask_restx.init_models(namespace) @namespace.route("/test") class TestResource(flask_restx.Resource): @namespace.expect(controller.flask_restx.query_get_parser) @namespace.marshal_with(controller.flask_restx.get_response_model) def get(self): return [] @namespace.expect(controller.flask_restx.json_post_model) def post(self): return [] @namespace.expect(controller.flask_restx.json_put_model) def put(self): return [] @namespace.expect(controller.flask_restx.query_delete_parser) def delete(self): return [] @namespace.route("/test/description") class TestDescriptionResource(flask_restx.Resource): @namespace.marshal_with( controller.flask_restx.get_model_description_response_model) def get(self): return {} @namespace.route("/test/audit") class TestAuditResource(flask_restx.Resource): @namespace.expect(controller.flask_restx.query_get_audit_parser) @namespace.marshal_with(controller.flask_restx.get_audit_response_model ) def get(self): return [] with application.test_client() as client: response = client.get("/swagger.json") assert response.json == { "swagger": "2.0", "basePath": "/", "paths": { "/test": { "delete": { "responses": { "200": { "description": "Success" } }, "operationId": "delete_test_resource", "parameters": [ { "name": "key", "in": "query", "type": "array", "items": { "type": "string" }, "collectionFormat": "multi", }, { "name": "mandatory", "in": "query", "type": "array", "items": { "type": "integer" }, "collectionFormat": "multi", }, { "name": "optional", "in": "query", "type": "array", "items": { "type": "string" }, "collectionFormat": "multi", }, ], "tags": ["Test"], }, "post": { "responses": { "200": { "description": "Success" } }, "operationId": "post_test_resource", "parameters": [{ "name": "payload", "required": True, "in": "body", "schema": { "$ref": "#/definitions/TestTable_PostRequestModel" }, }], "tags": ["Test"], }, "put": { "responses": { "200": { "description": "Success" } }, "operationId": "put_test_resource", "parameters": [{ "name": "payload", "required": True, "in": "body", "schema": { "$ref": "#/definitions/TestTable_PutRequestModel" }, }], "tags": ["Test"], }, "get": { "responses": { "200": { "description": "Success", "schema": { "$ref": "#/definitions/TestTable_GetResponseModel" }, } }, "operationId": "get_test_resource", "parameters": [ { "name": "key", "in": "query", "type": "array", "items": { "type": "string" }, "collectionFormat": "multi", }, { "name": "mandatory", "in": "query", "type": "array", "items": { "type": "integer" }, "collectionFormat": "multi", }, { "name": "optional", "in": "query", "type": "array", "items": { "type": "string" }, "collectionFormat": "multi", }, { "name": "limit", "in": "query", "type": "integer", "minimum": 0, "exclusiveMinimum": True, }, { "name": "order_by", "in": "query", "type": "array", "items": { "type": "string" }, "collectionFormat": "multi", }, { "name": "X-Fields", "in": "header", "type": "string", "format": "mask", "description": "An optional fields mask", }, ], "tags": ["Test"], }, }, "/test/audit": { "get": { "responses": { "200": { "description": "Success", "schema": { "$ref": "#/definitions/TestTable_GetAuditResponseModel" }, } }, "operationId": "get_test_audit_resource", "parameters": [ { "name": "key", "in": "query", "type": "array", "items": { "type": "string" }, "collectionFormat": "multi", }, { "name": "mandatory", "in": "query", "type": "array", "items": { "type": "integer" }, "collectionFormat": "multi", }, { "name": "optional", "in": "query", "type": "array", "items": { "type": "string" }, "collectionFormat": "multi", }, { "name": "audit_action", "in": "query", "type": "array", "items": { "type": "string" }, "collectionFormat": "multi", "enum": ["I", "U", "D"], }, { "name": "audit_date_utc", "in": "query", "type": "array", "format": "date-time", "items": { "type": "string" }, "collectionFormat": "multi", }, { "name": "audit_user", "in": "query", "type": "array", "items": { "type": "string" }, "collectionFormat": "multi", }, { "name": "revision", "in": "query", "type": "array", "items": { "type": "integer" }, "collectionFormat": "multi", }, { "name": "limit", "in": "query", "type": "integer", "minimum": 0, "exclusiveMinimum": True, }, { "name": "order_by", "in": "query", "type": "array", "items": { "type": "string" }, "collectionFormat": "multi", }, { "name": "X-Fields", "in": "header", "type": "string", "format": "mask", "description": "An optional fields mask", }, ], "tags": ["Test"], } }, "/test/description": { "get": { "responses": { "200": { "description": "Success", "schema": { "$ref": "#/definitions/TestTable_GetDescriptionResponseModel" }, } }, "operationId": "get_test_description_resource", "parameters": [{ "name": "X-Fields", "in": "header", "type": "string", "format": "mask", "description": "An optional fields mask", }], "tags": ["Test"], } }, }, "info": { "title": "API", "version": "1.0" }, "produces": ["application/json"], "consumes": ["application/json"], "tags": [{ "name": "Test" }], "definitions": { "TestTable_PostRequestModel": { "required": ["key", "mandatory"], "properties": { "key": { "type": "string", "readOnly": False, "example": "sample_value", }, "mandatory": { "type": "integer", "readOnly": False, "example": 1 }, "optional": { "type": "string", "readOnly": False, "example": "sample_value", }, }, "type": "object", }, "TestTable_PutRequestModel": { "required": ["key", "mandatory"], "properties": { "key": { "type": "string", "readOnly": False, "example": "sample_value", }, "mandatory": { "type": "integer", "readOnly": False, "example": 1 }, "optional": { "type": "string", "readOnly": False, "example": "sample_value", }, }, "type": "object", }, "TestTable_GetResponseModel": { "required": ["key", "mandatory"], "properties": { "key": { "type": "string", "readOnly": False, "example": "sample_value", }, "mandatory": { "type": "integer", "readOnly": False, "example": 1 }, "optional": { "type": "string", "readOnly": False, "example": "sample_value", }, }, "type": "object", }, "TestTable_GetDescriptionResponseModel": { "required": ["key", "mandatory", "table"], "properties": { "table": { "type": "string", "description": "Table name", "example": "table", }, "key": { "type": "string", "example": "column" }, "mandatory": { "type": "string", "example": "column" }, "optional": { "type": "string", "example": "column" }, }, "type": "object", }, "TestTable_GetAuditResponseModel": { "required": ["key", "mandatory"], "properties": { "key": { "type": "string", "readOnly": False, "example": "sample_value", }, "mandatory": { "type": "integer", "readOnly": False, "example": 1 }, "optional": { "type": "string", "readOnly": False, "example": "sample_value", }, "audit_action": { "type": "string", "readOnly": False, "example": "I", "enum": ["I", "U", "D"], }, "audit_date_utc": { "type": "string", "format": "date-time", "readOnly": False, "example": "2017-09-24T15:36:09", }, "audit_user": { "type": "string", "readOnly": False, "example": "sample audit_user", }, "revision": { "type": "integer", "readOnly": True, "example": 1 }, }, "type": "object", }, }, "responses": { "ParseError": { "description": "When a mask can't be parsed" }, "MaskError": { "description": "When any error occurs on mask" }, }, }
def database(controller): return layabase.load("sqlite:///:memory:", [controller])
def test_no_mongo_no_controllers_is_invalid(): with pytest.raises(layabase.NoRelatedControllers) as exception_info: layabase.load("mongomock", None) assert str(exception_info.value) == "A list of CRUDController must be provided."
def controllers(controller, controller_versioned): return layabase.load("mongomock", [controller, controller_versioned])
def database(controller): return layabase.load("mongomock", [controller])