def sqlite_db() -> Database:
    engine = sa.create_engine(SQLITE_DATABASE_URL)
    metadata.create_all(engine)

    yield Database(SQLITE_DATABASE_URL)

    metadata.drop_all(engine)
Esempio n. 2
0
def db(app):
    print("Initializing the Database...")
    print("\tDropping all tables")
    from app.models import metadata

    metadata.drop_all(bind=DB.engine)

    print("\tCreating all tables with alembic")
    metadata.create_all(bind=DB.engine)

    yield DB
def client(request):
    app = create_app('config.test.TestConfig')
    request.cls.app_context = app.app_context()
    request.cls.client = app.test_client()

    with app.app_context():
        metadata.drop_all(bind=db.engine)
        metadata.create_all(bind=db.engine)

    yield

    with app.app_context():
        metadata.drop_all(bind=db.engine)
def reset_db():
    from app.models import metadata
    from app.models.example_todo import ExampleTodo

    metadata.drop_all(bind=db.engine)
    metadata.create_all(bind=db.engine)

    # pour default records in
    todos = [
        'Book flights',
        'Buy power adapter',
    ]

    with DBSession() as session:
        for todo_desc in todos:
            todo = ExampleTodo(description=todo_desc)
            session.add(todo)
Esempio n. 5
0
def client():
    root_directory = os.path.join(os.path.dirname(os.getcwd()), "alembic")
    alembic_cfg = Config(root_directory + ".ini")
    alembic_cfg.set_main_option('script_location', root_directory)
    alembic_cfg.set_main_option('sqlalchemy.url', os.environ["DB_URI"])
    command.upgrade(alembic_cfg, 'head')

    client = __initialize_app()
    from app.models import engine, metadata
    metadata.create_all(engine)

    client.config.update({
        "TESTING": True,
        "DB_URI": "mysql+pymysql://root:root@localhost/favorite",
        "ENV": "TESTING"
    })

    _client = client.test_client()
    yield _client

    drop_database(os.environ["DB_URI"])
Esempio n. 6
0
def init_app(app):
    db_uri = app.config.get('SQLALCHEMY_DATABASE_URI')
    engine = create_engine(db_uri)
    metadata.create_all(engine)
    return engine
Esempio n. 7
0
from fastapi import FastAPI
from app.api import ping
from app.api.noteapp import notes
from app.models import metadata
from app.db import engine, database

metadata.create_all(engine)

app = FastAPI()


@app.on_event("startup")
async def startup():
    await database.connect()


@app.on_event('shutdown')
async def shutdown():
    await database.disconnect()


app.include_router(ping.router)
app.include_router(notes.router, prefix="/notes", tags=["notes"])
Esempio n. 8
0
def db():
    database = databases.Database(database_url, force_rollback=True)
    engine = create_engine(database_url,
                           connect_args={"check_same_thread": False})
    metadata.create_all(engine)
    return database
Esempio n. 9
0
def init(engine: Engine) -> None:
    """ init tables"""
    from app.models import metadata

    metadata.create_all(bind=engine)