From 1b8311226cf950182d0dd8702d6b732b6633af94 Mon Sep 17 00:00:00 2001 From: Amanda Eames Date: Thu, 21 Mar 2024 09:26:37 -0400 Subject: [PATCH] basic global-api testing --- .github/workflows/global-api-develop.yml | 51 +++++++++++++++++++++++- global-api/requirements.txt | 5 ++- global-api/tests/test_main.py | 24 +++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 global-api/tests/test_main.py diff --git a/.github/workflows/global-api-develop.yml b/.github/workflows/global-api-develop.yml index e5aea87b6..d1680f735 100644 --- a/.github/workflows/global-api-develop.yml +++ b/.github/workflows/global-api-develop.yml @@ -12,6 +12,55 @@ on: branches: ["develop"] jobs: + runTests: + runs-on: ubuntu-latest + defaults: + run: + working-directory: ./global-api + steps: + - uses: actions/checkout@v3 + + - name: Set up Python 3.8 + uses: actions/setup-python@v2 + with: + python-version: 3.8 + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install flake8 pytest pytest-cov + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + + - name: Set up database + run: | + docker run --name github_action_postgresql -d -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_PASSWORD="" postgres + sleep 10 + createuser -w -h localhost -p 5432 -U postgres citycatalyst + createdb -w -h localhost -p 5432 -U postgres citycatalyst -O citycatalyst + cp env.example .env + + - name: Build coverage file + run: | + pytest --junitxml=pytest.xml --cov-report=term-missing:skip-covered --cov=app tests/ | tee pytest-coverage.txt + + - name: Pytest coverage comment + uses: MishaKav/pytest-coverage-comment@main + with: + pytest-coverage-path: ./pytest-coverage.txt + junitxml-path: ./pytest.xml + + - name: Upload coverage reports to Codecov + continue-on-error: true + uses: codecov/codecov-action@v4.0.1 + with: + fail_ci_if_error: false + flags: unittests + name: citycatalyst-global-api + token: ${{ secrets.CODECOV_TOKEN }} + slug: Open-Earth-Foundation/CityCatalyst + + - name: Shut down database + run: docker stop github_action_postgresql pushToGHCR: runs-on: ubuntu-latest @@ -55,4 +104,4 @@ jobs: run: | kubectl create -f k8s/cc-global-api-migrate.yml -n default kubectl apply -f k8s/cc-global-api-deploy.yml -n default - kubectl rollout restart deployment cc-global-api-deploy -n default + kubectl rollout restart deployment cc-global-api-deploy -n default \ No newline at end of file diff --git a/global-api/requirements.txt b/global-api/requirements.txt index 95001222d..0b906a10b 100644 --- a/global-api/requirements.txt +++ b/global-api/requirements.txt @@ -23,4 +23,7 @@ openclimate==0.1.* nest_asyncio==1.6.* xlrd==2.0.* dns-cache==0.3.* -openpyxl==3.1.* \ No newline at end of file +openpyxl==3.1.* +pytest==8.1.1 +pytest-cov==4.1.0 +httpx==0.27.0 \ No newline at end of file diff --git a/global-api/tests/test_main.py b/global-api/tests/test_main.py new file mode 100644 index 000000000..05f627aab --- /dev/null +++ b/global-api/tests/test_main.py @@ -0,0 +1,24 @@ +import pytest +from fastapi.testclient import TestClient +from main import app + +# Create a TestClient instance with your FastAPI app +client = TestClient(app) + +# Test the root endpoint +def test_read_root(): + response = client.get("/") + assert response.status_code == 200 + assert response.json() == {"message": "Welcome"} + +# Test Health Check Endpoint +def test_health_check(): + response = client.get("/health") + assert response.status_code == 200 + assert response.json() == {'status': 'ok'} + +# Test the data catalogue end point has no data +def test_catalgue_no_data_available(): + response = client.get("/api/v0/catalogue") + assert response.status_code == 404 + assert response.json() == {"detail": "No data available"} \ No newline at end of file