Skip to content

Commit

Permalink
Merge pull request #14 from sireto/github-workflow
Browse files Browse the repository at this point in the history
Github Actions Workflow setup
  • Loading branch information
mesudip authored Apr 2, 2024
2 parents f62f074 + 5dd8b47 commit f633e86
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 5 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/build-backend-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Backend Service Docker Image Builder

on:
push:
branches: ["dev"]
pull_request:
branches: ["dev"]

jobs:
build_on_pr:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout code with submodules
uses: actions/checkout@v2
with:
submodules: "recursive"

- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build Docker image
uses: docker/build-push-action@v5
with:
push: false
tags: backend:latest
context: autonomous_agent_api
41 changes: 41 additions & 0 deletions .github/workflows/test-backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

name: Backend Service Testing ( Pytest )

on:
push:
branches: ["master", "dev"]
pull_request:
branches: ["dev"]

jobs:
run_backend_tests:
runs-on: ubuntu-latest

steps:
- name: Enable Access for Branch for Workflow
uses: actions/checkout@v2
with:
submodules: "recursive"

- name: setup python
uses: actions/setup-python@v2
with:
python-version: 3.12.2

- name: Install Poetry
working-directory: autonomous_agent_api
run: pip install poetry==1.8.2

- name: Install Dependencies
working-directory: autonomous_agent_api
run: |
poetry install
poetry run prisma generate
- name: Run test cases
working-directory: autonomous_agent_api
env:
DATABASE_URL : ""
run: poetry run pytest -m github_actions


45 changes: 45 additions & 0 deletions autonomous_agent_api/backend/app/asgi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Application implementation - ASGI."""

import os
import logging
from contextlib import asynccontextmanager

Expand Down Expand Up @@ -27,6 +28,7 @@ async def lifespan(app: FastAPI):
logger.info("Starting Server")

await prisma_connection.connect()

yield
log.debug("Execute FastAPI shutdown event handler.")
# Gracefully close utilities.
Expand All @@ -36,9 +38,26 @@ async def lifespan(app: FastAPI):
await AiohttpClient.close_aiohttp_client()

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.")

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 @@ -47,6 +66,7 @@ def get_application() -> FastAPI:
"""
log.debug("Initialize FastAPI application node.")

app = FastAPI(
title=settings.PROJECT_NAME,
debug=settings.DEBUG,
Expand All @@ -60,3 +80,28 @@ def get_application() -> FastAPI:
app.add_exception_handler(HTTPException, http_exception_handler)

return app


def get_test_application() -> FastAPI:
"""
Initialize FastApi application for testing environment
Returns:
FastAPI : Application object instance
"""
os.environ.__setattr__("TESTING", True)

logging.info("Setting up Test Environment")
app = FastAPI(
title=settings.PROJECT_NAME,
debug=settings.DEBUG,
version=settings.VERSION,
docs_url=settings.DOCS_URL,
lifespan=test_lifespan,
)
log.debug("Add application routes.")
app.include_router(root_api_router)
log.debug("Register global exception handler for custom HTTPException.")
app.add_exception_handler(HTTPException, http_exception_handler)
return app
17 changes: 16 additions & 1 deletion autonomous_agent_api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion autonomous_agent_api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,10 @@ module = [
"gunicorn.*",
"redis.*",
]
ignore_missing_imports = true
ignore_missing_imports = true

[tool.pytest.ini_options]
markers = [
"ping: run demo test",
"github_actions: Tests for Github actions",
]
6 changes: 3 additions & 3 deletions autonomous_agent_api/tests/app/controllers/test_ping.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from backend.app.asgi import get_application
from backend.app.asgi import get_test_application
from fastapi.testclient import TestClient
from fastapi import status
import pytest

client = TestClient(get_application())
client = TestClient(get_test_application())


@pytest.mark.ping
@pytest.mark.github_actions
def test_ping():
response = client.get("/api/ping")
assert response.status_code == status.HTTP_200_OK

0 comments on commit f633e86

Please sign in to comment.