From 13b66f2cbaf82f4acc2b5238fbe7eea7f086e8d7 Mon Sep 17 00:00:00 2001 From: Aisuko Date: Wed, 20 Mar 2024 12:04:57 +0000 Subject: [PATCH] Add test Signed-off-by: Aisuko --- .github/workflows/ci-backend.yaml | 83 ++++++++++++++++--------------- backend/tests/conftest.py | 49 +++++++++--------- 2 files changed, 68 insertions(+), 64 deletions(-) diff --git a/.github/workflows/ci-backend.yaml b/.github/workflows/ci-backend.yaml index 210688b..d7c1b86 100644 --- a/.github/workflows/ci-backend.yaml +++ b/.github/workflows/ci-backend.yaml @@ -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 @@ -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 🐳' diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index 5906541..bf1f293 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -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