Skip to content

Commit

Permalink
added test lifespan
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephRana11 committed Apr 1, 2024
1 parent 77cc726 commit cd982f7
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions autonomous_agent_api/backend/app/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ async def lifespan(app: FastAPI):

logger.info("Starting Server")

if not os.environ.get("TESTING"): # check if its a testing enironment , if not then connect to databse
await prisma_connection.connect()
await prisma_connection.connect()

yield
log.debug("Execute FastAPI shutdown event handler.")
Expand All @@ -38,12 +37,29 @@ async def lifespan(app: FastAPI):

await AiohttpClient.close_aiohttp_client()

if not os.environ.get("TESTING"): # check if it's not a testing env , if not close databse connection
await prisma_connection.disconnect()
await prisma_connection.disconnect()

logger.info("Stopping Server")


# Lifespan used for Test Environmnet : Configurations such as Live Database and Redis is Disabled.
# todo : Mock Database setup required for testing
@asynccontextmanager
async def test_lifespan(app: FastAPI):
log.debug("Execute FastAPI startup event handler.")
if settings.USE_REDIS:
await RedisClient.open_redis_client()

AiohttpClient.get_aiohttp_client()

logger.info("Starting Test Server")

yield
log.debug("Execute FastAPI shutdown event handler.")

logger.info("Stopping Test Server")


def get_application() -> FastAPI:
"""Initialize FastAPI application.
Expand All @@ -52,6 +68,7 @@ def get_application() -> FastAPI:
"""
log.debug("Initialize FastAPI application node.")

app = FastAPI(
title=settings.PROJECT_NAME,
debug=settings.DEBUG,
Expand All @@ -69,7 +86,7 @@ def get_application() -> FastAPI:

def get_test_application() -> FastAPI:
"""
Initialize FastApi application for testing
Initialize FastApi application for testing environment
Returns:
FastAPI : Application object instance
Expand All @@ -83,7 +100,7 @@ def get_test_application() -> FastAPI:
debug=settings.DEBUG,
version=settings.VERSION,
docs_url=settings.DOCS_URL,
lifespan=lifespan,
lifespan=test_lifespan,
)
log.debug("Add application routes.")
app.include_router(root_api_router)
Expand Down

0 comments on commit cd982f7

Please sign in to comment.