Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
Signed-off-by: Aisuko <[email protected]>
  • Loading branch information
Aisuko committed Mar 20, 2024
1 parent d5923b4 commit b35fe77
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 100 deletions.
154 changes: 77 additions & 77 deletions .github/workflows/ci-backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,52 +78,52 @@ jobs:
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
# test:
# name: 'Test 🔬'
# needs: build
# strategy:
# matrix:
# os:
# - ubuntu-latest
# python-version:
# - "3.11"
# defaults:
# run:
# working-directory: backend/
# services:
# postgres:
# image: postgres:14.2-alpine
# env:
# POSTGRES_DB: ${{ secrets.POSTGRES_DB }}
# POSTGRES_USER: ${{ secrets.POSTGRES_USERNAME }}
# POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
# ports:
# - 5432:5432
# options: >-
# --health-cmd pg_isready
# --health-interval 10s
# --health-timeout 5s
# --health-retries 5
# env:
# ENVIRONMENT: ${{ secrets.ENVIRONMENT }}
# DEBUG: ${{ secrets.DEBUG }}
# POSTGRES_DB: ${{ secrets.POSTGRES_DB }}
# POSTGRES_HOST: ${{ secrets.POSTGRES_HOST }}
# POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
# POSTGRES_PORT: ${{ secrets.POSTGRES_PORT }}
# POSTGRES_SCHEMA: ${{ secrets.POSTGRES_SCHEMA }}
# POSTGRES_USERNAME: ${{ secrets.POSTGRES_USERNAME }}
# BACKEND_SERVER_HOST: ${{ secrets.BACKEND_SERVER_HOST }}
# BACKEND_SERVER_PORT: ${{ secrets.BACKEND_SERVER_PORT }}
# BACKEND_SERVER_WORKERS: ${{ secrets.BACKEND_SERVER_WORKERS }}
# DB_TIMEOUT: ${{ secrets.DB_TIMEOUT }}
# DB_POOL_SIZE: ${{ secrets.DB_POOL_SIZE }}
# DB_MAX_POOL_CON: ${{ secrets.DB_MAX_POOL_CON }}
# DB_POOL_OVERFLOW: ${{ secrets.DB_POOL_OVERFLOW }}
# IS_DB_ECHO_LOG: ${{ secrets.IS_DB_ECHO_LOG }}
# IS_DB_EXPIRE_ON_COMMIT: ${{ secrets.IS_DB_EXPIRE_ON_COMMIT }}
# IS_DB_FORCE_ROLLBACK: ${{ secrets.IS_DB_FORCE_ROLLBACK }}
# IS_ALLOWED_CREDENTIALS: ${{ secrets.IS_ALLOWED_CREDENTIALS }}
test:
name: 'Test 🔬'
needs: build
strategy:
matrix:
os:
- ubuntu-latest
python-version:
- "3.11"
defaults:
run:
working-directory: backend/
services:
postgres:
image: postgres:14.2-alpine
env:
POSTGRES_DB: ${{ secrets.POSTGRES_DB }}
POSTGRES_USER: ${{ secrets.POSTGRES_USERNAME }}
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
ENVIRONMENT: ${{ secrets.ENVIRONMENT }}
DEBUG: ${{ secrets.DEBUG }}
POSTGRES_DB: ${{ secrets.POSTGRES_DB }}
POSTGRES_HOST: ${{ secrets.POSTGRES_HOST }}
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
POSTGRES_PORT: ${{ secrets.POSTGRES_PORT }}
POSTGRES_SCHEMA: ${{ secrets.POSTGRES_SCHEMA }}
POSTGRES_USERNAME: ${{ secrets.POSTGRES_USERNAME }}
BACKEND_SERVER_HOST: ${{ secrets.BACKEND_SERVER_HOST }}
BACKEND_SERVER_PORT: ${{ secrets.BACKEND_SERVER_PORT }}
BACKEND_SERVER_WORKERS: ${{ secrets.BACKEND_SERVER_WORKERS }}
DB_TIMEOUT: ${{ secrets.DB_TIMEOUT }}
DB_POOL_SIZE: ${{ secrets.DB_POOL_SIZE }}
DB_MAX_POOL_CON: ${{ secrets.DB_MAX_POOL_CON }}
DB_POOL_OVERFLOW: ${{ secrets.DB_POOL_OVERFLOW }}
IS_DB_ECHO_LOG: ${{ secrets.IS_DB_ECHO_LOG }}
IS_DB_EXPIRE_ON_COMMIT: ${{ secrets.IS_DB_EXPIRE_ON_COMMIT }}
IS_DB_FORCE_ROLLBACK: ${{ secrets.IS_DB_FORCE_ROLLBACK }}
IS_ALLOWED_CREDENTIALS: ${{ secrets.IS_ALLOWED_CREDENTIALS }}
# API_TOKEN: ${{ secrets.API_TOKEN }}
# AUTH_TOKEN: ${{ secrets.AUTH_TOKEN }}
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
Expand All @@ -134,39 +134,39 @@ jobs:
# JWT_MIN: ${{ secrets.JWT_MIN }}
# JWT_HOUR: ${{ secrets.JWT_HOUR }}
# JWT_DAY: ${{ secrets.JWT_DAY }}
# HASHING_ALGORITHM_LAYER_1: ${{ secrets.HASHING_ALGORITHM_LAYER_1 }}
# HASHING_ALGORITHM_LAYER_2: ${{ secrets.HASHING_ALGORITHM_LAYER_2 }}
HASHING_ALGORITHM_LAYER_1: ${{ secrets.HASHING_ALGORITHM_LAYER_1 }}
HASHING_ALGORITHM_LAYER_2: ${{ secrets.HASHING_ALGORITHM_LAYER_2 }}
# HASHING_SALT: ${{ secrets.HASHING_SALT }}

# runs-on: ${{ matrix.os }}
# steps:
# - name: Check repository
# uses: actions/checkout@v3
# - name: Set up Python ${{ matrix.python-version }}
# uses: actions/setup-python@v4
# with:
# python-version: ${{ matrix.python-version }}
# cache: 'pip'
# - name: Display Python version
# run: python -c "import sys; print(sys.version)"
# - name: Install dependencies
# run: |
# python -m pip install --upgrade pip
# if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
# - name: Install Dependencies for Testing
# run: |
# pip install pytest pytest-asyncio pytest pytest-xdist
# - name: Test with Pytest-Cov
# run: |
# pytest --cov --cov-report xml .
# - name: Upload coverage to Codecov
# uses: codecov/codecov-action@v3
# with:
# token: ${{ secrets.CODECOV_TOKEN }}
# fail_ci_if_error: false
# flags: backend_app_tests
# name: codecov-umbrella
# verbose: true
runs-on: ${{ matrix.os }}
steps:
- name: Check repository
uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
- name: Display Python version
run: python -c "import sys; print(sys.version)"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Install Dependencies for Testing
run: |
pip install pytest pytest-asyncio pytest pytest-xdist
- name: Test with Pytest-Cov
run: |
pytest --cov --cov-report xml .
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false
flags: backend_app_tests
name: codecov-umbrella
verbose: true

docker_build:
name: 'Docker Build 🐳'
Expand Down
49 changes: 26 additions & 23 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
import asgi_lifespan
import fastapi
import httpx
import pytest
from fastapi.testclient import TestClient

from src.main import initialize_backend_application
from src.main import app

client=TestClient(app)

@pytest.fixture(name="backend_test_app")
def backend_test_app() -> fastapi.FastAPI:
"""
A fixture that re-initializes the FastAPI instance for test application.
"""
# TODO more specific test cases for each endpoint
def test_chat():
response = client.get("/api/models")
assert response.status_code == 200

return initialize_backend_application()
# @pytest.fixture(name="backend_test_app")
# def backend_test_app() -> fastapi.FastAPI:
# """
# A fixture that re-initializes the FastAPI instance for test application.
# """

# return initialize_backend_application()

@pytest.fixture(name="initialize_backend_test_application")
async def initialize_backend_test_application(backend_test_app: fastapi.FastAPI) -> fastapi.FastAPI: # type: ignore
async with asgi_lifespan.LifespanManager(backend_test_app):
yield backend_test_app

# @pytest.fixture(name="initialize_backend_test_application")
# async def initialize_backend_test_application(backend_test_app: fastapi.FastAPI) -> fastapi.FastAPI: # type: ignore
# async with asgi_lifespan.LifespanManager(backend_test_app):
# yield backend_test_app

@pytest.fixture(name="async_client")
async def async_client(initialize_backend_test_application: fastapi.FastAPI) -> httpx.AsyncClient: # type: ignore
async with httpx.AsyncClient(
app=initialize_backend_test_application,
base_url="http://testserver",
headers={"Content-Type": "application/json"},
) as client:
yield client

# @pytest.fixture(name="async_client")
# async def async_client(initialize_backend_test_application: fastapi.FastAPI) -> httpx.AsyncClient: # type: ignore
# async with httpx.AsyncClient(
# app=initialize_backend_test_application,
# base_url="http://testserver",
# headers={"Content-Type": "application/json"},
# ) as client:
# yield client

0 comments on commit b35fe77

Please sign in to comment.