diff --git a/tests/conftest.py b/tests/conftest.py index d44b6af..0582d44 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,5 @@ import pytest +import inspect from fastapi.testclient import TestClient from .implementations import * diff --git a/tests/implementations/__init__.py b/tests/implementations/__init__.py index e09fb78..9de387d 100644 --- a/tests/implementations/__init__.py +++ b/tests/implementations/__init__.py @@ -3,19 +3,24 @@ databases_implementation_custom_ids, databases_implementation_string_pk, ) -from .memory import memory_implementation -from .sqlalchemy_ import ( - sqlalchemy_implementation, - sqlalchemy_implementation_custom_ids, - sqlalchemy_implementation_string_pk, - sqlalchemy_implementation_integrity_errors, +from .gino_ import ( + gino_implementation, + gino_implementation_custom_ids, + gino_implementation_integrity_errors, + gino_implementation_string_pk, ) - +from .memory import memory_implementation from .ormar_ import ( ormar_implementation, - ormar_implementation_string_pk, ormar_implementation_custom_ids, ormar_implementation_integrity_errors, + ormar_implementation_string_pk, +) +from .sqlalchemy_ import ( + sqlalchemy_implementation, + sqlalchemy_implementation_custom_ids, + sqlalchemy_implementation_integrity_errors, + sqlalchemy_implementation_string_pk, ) implementations = [ @@ -23,6 +28,7 @@ sqlalchemy_implementation, databases_implementation, ormar_implementation, + gino_implementation, ] try: diff --git a/tests/implementations/gino_.py b/tests/implementations/gino_.py new file mode 100644 index 0000000..a7cc7fb --- /dev/null +++ b/tests/implementations/gino_.py @@ -0,0 +1,159 @@ +import asyncio +from fastapi import FastAPI +from fastapi_crudrouter import GinoCRUDRouter +from gino.ext.starlette import Gino +from sqlalchemy_utils import create_database, database_exists, drop_database +from tests import ( + CUSTOM_TAGS, + PAGINATION_SIZE, + Carrot, + CarrotCreate, + CarrotUpdate, + CustomPotato, + Potato, + PotatoType, +) + +GINO_DATABASE_URL = "asyncpg://postgres:postgres@127.0.0.1/testdb" +SQLALCHEMY_DATABASE_URL = "postgresql://postgres:postgres@127.0.0.1/testdb" + + +async def migrate(db): + async with db.with_bind(GINO_DATABASE_URL): + await db.gino.create_all() + + +def _setup_base_app(): + if database_exists(SQLALCHEMY_DATABASE_URL): + drop_database(SQLALCHEMY_DATABASE_URL) + + create_database(SQLALCHEMY_DATABASE_URL) + + app = FastAPI() + db = Gino(dsn=GINO_DATABASE_URL) + db.init_app(app) + return db, app + + +def gino_implementation(): + db, app = _setup_base_app() + + class PotatoModel(db.Model): + __tablename__ = "potatoes" + id = db.Column(db.Integer, primary_key=True, index=True) + thickness = db.Column(db.Float) + mass = db.Column(db.Float) + color = db.Column(db.String) + type = db.Column(db.String) + + class CarrotModel(db.Model): + __tablename__ = "carrots" + id = db.Column(db.Integer, primary_key=True, index=True) + length = db.Column(db.Float) + color = db.Column(db.String) + + asyncio.get_event_loop().run_until_complete(migrate(db)) + + router_settings = [ + dict( + schema=Potato, + db_model=PotatoModel, + db=db, + prefix="potato", + paginate=PAGINATION_SIZE, + ), + dict( + schema=Carrot, + db_model=CarrotModel, + db=db, + create_schema=CarrotCreate, + update_schema=CarrotUpdate, + prefix="carrot", + tags=CUSTOM_TAGS, + ), + ] + + return app, GinoCRUDRouter, router_settings + + +# noinspection DuplicatedCode +def gino_implementation_custom_ids(): + db, app = _setup_base_app() + + class PotatoModel(db.Model): + __tablename__ = "potatoes" + potato_id = db.Column(db.Integer, primary_key=True, index=True) + thickness = db.Column(db.Float) + mass = db.Column(db.Float) + color = db.Column(db.String) + type = db.Column(db.String) + + asyncio.get_event_loop().run_until_complete(migrate(db)) + + app.include_router(GinoCRUDRouter(schema=CustomPotato, db_model=PotatoModel, db=db)) + + return app + + +def gino_implementation_string_pk(): + db, app = _setup_base_app() + + class PotatoTypeModel(db.Model): + __tablename__ = "potato_type" + name = db.Column(db.String, primary_key=True, index=True) + origin = db.Column(db.String) + + asyncio.get_event_loop().run_until_complete(migrate(db)) + + app.include_router( + GinoCRUDRouter( + schema=PotatoType, + create_schema=PotatoType, + db_model=PotatoTypeModel, + db=db, + prefix="potato_type", + ) + ) + + return app + + +def gino_implementation_integrity_errors(): + db, app = _setup_base_app() + + class PotatoModel(db.Model): + __tablename__ = "potatoes" + id = db.Column(db.Integer, primary_key=True, index=True) + thickness = db.Column(db.Float) + mass = db.Column(db.Float) + color = db.Column(db.String, unique=True) + type = db.Column(db.String) + + class CarrotModel(db.Model): + __tablename__ = "carrots" + id = db.Column(db.Integer, primary_key=True, index=True) + length = db.Column(db.Float) + color = db.Column(db.String) + + asyncio.get_event_loop().run_until_complete(migrate(db)) + + app.include_router( + GinoCRUDRouter( + schema=Potato, + db_model=PotatoModel, + db=db, + create_schema=Potato, + prefix="potatoes", + ) + ) + app.include_router( + GinoCRUDRouter( + schema=Carrot, + db_model=CarrotModel, + db=db, + update_schema=CarrotUpdate, + prefix="carrots", + ) + ) + + return app diff --git a/tests/test_base.py b/tests/test_base.py index 413ad51..17111b3 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -1,8 +1,13 @@ import pytest from fastapi import APIRouter, FastAPI - -from fastapi_crudrouter import MemoryCRUDRouter, SQLAlchemyCRUDRouter, OrmarCRUDRouter +from fastapi_crudrouter import ( + GinoCRUDRouter, + MemoryCRUDRouter, + OrmarCRUDRouter, + SQLAlchemyCRUDRouter, +) from fastapi_crudrouter.core._base import CRUDGenerator + from tests import Potato @@ -11,6 +16,7 @@ def test_router_type(): assert issubclass(SQLAlchemyCRUDRouter, APIRouter) assert issubclass(MemoryCRUDRouter, APIRouter) assert issubclass(OrmarCRUDRouter, APIRouter) + assert issubclass(GinoCRUDRouter, APIRouter) def test_get_one():