Example #1
0
def test_ops_healthz_fail(session, client):  # pylint: disable=unused-argument
    """Assert that the service is unhealthy if a connection toThe database cannot be made."""
    engine = sqlalchemy.create_engine('postgresql://*****:*****@exist:5432/nada')
    SESSION.configure(bind=engine)
    rv = client.get('/ops/healthz')

    assert rv.status_code == 500
    assert rv.json() == {'message': 'api is down'}
Example #2
0
def session_fixture(engine):
    """Create and drop all database metadata."""
    def _drop_all():
        meta = sqlalchemy.MetaData()
        meta.reflect(bind=engine)
        meta.drop_all(bind=engine)

    _drop_all()
    BASE.metadata.create_all(engine)

    session = SESSION()

    yield session
    session.close()

    _drop_all()
Example #3
0
async def session_middleware(request: Request, call_next):
    """Close the session after each request, thus rolling back any not committed transactions."""
    added = False
    try:
        if not hasattr(request.state, 'session'):
            request.state.session = SESSION()
            added = True
        response = await call_next(request)
    finally:
        if added:
            # Only close a session if we added it, useful for testing
            request.state.session.close()
    return response
Example #4
0
 def __init__(
     self,
     bind: Union[str, Engine, Connection, None],
     debug: bool = False,
     routes: List[BaseRoute] = None,
     template_directory: str = None,
     title: str = 'Notify API',
     description: str = '',
     version: str = '1.0.0',
     openapi_url: Optional[str] = '/openapi.json',
     openapi_prefix: str = '',
     docs_url: Optional[str] = '/docs',
     redoc_url: Optional[str] = '/redoc',
     **extra: Dict[str, Any],
 ):  # pylint: disable=too-many-arguments
     """Init."""
     super().__init__(debug=debug,
                      routes=routes,
                      template_directory=template_directory,
                      title=title,
                      description=description,
                      version=version,
                      openapi_url=openapi_url,
                      openapi_prefix=openapi_prefix,
                      docs_url=docs_url,
                      redoc_url=redoc_url,
                      **extra)
     if bind is not None:
         if isinstance(bind, str):
             self.bind = create_engine(bind,
                                       pool_pre_ping=True,
                                       pool_size=20)
         else:
             self.bind = bind
         db_session.configure(bind=self.bind)
     else:
         self.bind = None
Example #5
0
def engine_fixture():
    """Connect to the database."""
    engine = sqlalchemy.create_engine(AppConfig.SQLALCHEMY_TEST_DATABASE_URI)
    SESSION.configure(bind=engine)
    return engine
Example #6
0
def engine_fixture():
    """Connect to the database."""
    engine = sqlalchemy.create_engine(
        get_api_settings().NOTIFY_DATABASE_TEST_URL)
    SESSION.configure(bind=engine)
    return engine