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 13b66f2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 64 deletions.
83 changes: 42 additions & 41 deletions .github/workflows/ci-backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ 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/
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
Expand Down Expand Up @@ -138,35 +138,36 @@ jobs:
# 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: |
cp ${{ github.repository }}/chat-backend/.env.example .env
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 13b66f2

Please sign in to comment.