From 5a51fa49e72265e1f888f333358c090a32d6430d Mon Sep 17 00:00:00 2001 From: Jarrett Keifer Date: Wed, 19 Apr 2023 13:58:09 -0700 Subject: [PATCH] setup pytest with gh action (#4) * setup pytest with gh action * Fixes whitespace linter changes * Adds pyenv to gitignore. Adds testing link to README --------- Co-authored-by: Paul Pilone --- .github/workflows/python-test.yml | 43 +++++++++++++++++++ .gitignore | 2 +- CONTRIBUTING.md | 5 +++ README.md | 6 ++- .../20230328180857_initial_setup.sql | 2 +- pyproject.toml | 3 ++ requirements.txt | 2 +- src/swoop/api/routers/root.py | 18 ++++++-- tests/__init__.py | 0 tests/api/__init__.py | 0 tests/api/test_main.py | 25 +++++++++++ 11 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/python-test.yml create mode 100644 tests/__init__.py create mode 100644 tests/api/__init__.py create mode 100644 tests/api/test_main.py diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml new file mode 100644 index 0000000..da32dc8 --- /dev/null +++ b/.github/workflows/python-test.yml @@ -0,0 +1,43 @@ +name: Python Test +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build: + strategy: + matrix: + python-version: + - "3.11" + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + pip install '.[dev]' + + - name: Lint with ruff + run: | + ruff check . + + - name: Test with pytest + run: pytest --cov=swoop --cov-report=xml + + - name: "Upload coverage to Codecov" + uses: codecov/codecov-action@v3 + with: + fail_ci_if_error: true + verbose: true diff --git a/.gitignore b/.gitignore index 68bc17f..4b069eb 100644 --- a/.gitignore +++ b/.gitignore @@ -85,7 +85,7 @@ ipython_config.py # pyenv # For a library or package, you might want to ignore these files since the code is # intended to run in multiple environments; otherwise, check them in: -# .python-version +.python-version # pipenv # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 10343fc..3bdae93 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -31,6 +31,11 @@ pre-commit checks, this can be done with: git commit -m "message" --no-verify ``` +## Testing + +Tests are run using `pytest`. Put pytest python modules and other resource in +the `/tests` directory. + ## Adding/updating dependencies ### Updating `requirements.txt` to latest versions diff --git a/README.md b/README.md index 5b66b8b..197f0ee 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@ brew install dbmate ```
- ## Database Setup / Migrations The DB schema and migrations are managed by [Dbmate](https://github.com/amacneil/dbmate#commands). @@ -28,8 +27,11 @@ Create the database and tables: ``` dbmate up ``` +
+## Environment Setup and Testing +Refer to [Contributing.md](./CONTRIBUTING.md) for environment setup and testing instructions.


-This project is a work in progress. \ No newline at end of file +This project is a work in progress. diff --git a/db/migrations/20230328180857_initial_setup.sql b/db/migrations/20230328180857_initial_setup.sql index 1caab87..f8d37a2 100644 --- a/db/migrations/20230328180857_initial_setup.sql +++ b/db/migrations/20230328180857_initial_setup.sql @@ -22,4 +22,4 @@ DROP TABLE IF EXISTS jobs; DROP TABLE IF EXISTS task_executions; -DROP TABLE IF EXISTS workflows; \ No newline at end of file +DROP TABLE IF EXISTS workflows; diff --git a/pyproject.toml b/pyproject.toml index 42a6319..feb1dc6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,9 +36,12 @@ dev = [ "pre-commit >=3.1.1", "pre-commit-hooks >=4.4.0", "black >=23.1.0", + "httpx >=0.24.0", "ruff >=0.0.253", "mypy >=1.0.1", "pip-tools >= 6.12.3", + "pytest >=7.2.2", + "pytest-cov >=4.0.0", "pyupgrade >=3.3.1", ] diff --git a/requirements.txt b/requirements.txt index 6b6962b..e0238f6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,7 @@ anyio==3.6.2 # via starlette click==8.1.3 # via uvicorn -fastapi==0.95.0 +fastapi==0.95.1 # via swoop (pyproject.toml) h11==0.14.0 # via uvicorn diff --git a/src/swoop/api/routers/root.py b/src/swoop/api/routers/root.py index a51251f..e55a530 100644 --- a/src/swoop/api/routers/root.py +++ b/src/swoop/api/routers/root.py @@ -1,12 +1,13 @@ from __future__ import annotations -from fastapi import APIRouter +from fastapi import APIRouter, Request from ..models import ( ConfClasses, Exception as APIException, LandingPage, + Link, ) @@ -19,11 +20,22 @@ responses={"500": {"model": APIException}}, tags=["Capabilities"], ) -def get_landing_page() -> LandingPage | APIException: +def get_landing_page(request: Request) -> LandingPage | APIException: """ landing page of this API """ - pass + return LandingPage( + title="Example processing server", + description="Example server implementing the OGC API - Processes 1.0 Standard", + links=[ + Link( + href=str(request.url_for("get_conformance_classes")), + rel="http://www.opengis.net/def/rel/ogc/1.0/conformance", + type="application/json", + hreflang=None, + ), + ], + ) @router.get( diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/api/__init__.py b/tests/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/api/test_main.py b/tests/api/test_main.py new file mode 100644 index 0000000..41704c5 --- /dev/null +++ b/tests/api/test_main.py @@ -0,0 +1,25 @@ +from fastapi.testclient import TestClient + +from swoop.api.main import app + +client = TestClient(app) + + +def test_read_main(): + response = client.get("/") + assert response.status_code == 200 + assert response.json() == { + "description": ( + "Example server implementing the OGC API - Processes 1.0 Standard" + ), + "links": [ + { + "href": "http://testserver/conformance", + "hreflang": None, + "rel": "http://www.opengis.net/def/rel/ogc/1.0/conformance", + "title": None, + "type": "application/json", + }, + ], + "title": "Example processing server", + }