Skip to content

Commit

Permalink
add test for gino backend
Browse files Browse the repository at this point in the history
  • Loading branch information
turalpb committed Jun 25, 2021
1 parent 3644965 commit 8570e58
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 10 deletions.
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import inspect
from fastapi.testclient import TestClient

from .implementations import *
Expand Down
22 changes: 14 additions & 8 deletions tests/implementations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,32 @@
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 = [
memory_implementation,
sqlalchemy_implementation,
databases_implementation,
ormar_implementation,
gino_implementation,
]

try:
Expand Down
159 changes: 159 additions & 0 deletions tests/implementations/gino_.py
Original file line number Diff line number Diff line change
@@ -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:[email protected]/testdb"
SQLALCHEMY_DATABASE_URL = "postgresql://postgres:[email protected]/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
10 changes: 8 additions & 2 deletions tests/test_base.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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():
Expand Down

0 comments on commit 8570e58

Please sign in to comment.