Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
name: backend

on:
pull_request:

permissions:
contents: read
issues: write

jobs:
tests:
name: pytest
runs-on: ubuntu-latest

defaults:
run:
working-directory: ./backend

services:
postgres:
image: postgres:16
env:
POSTGRES_DB: postgres
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
ports:
- 5432:5432
options: >-
--health-cmd "pg_isready -U admin -d postgres"
--health-interval 10s
--health-timeout 5s
--health-retries 10

env:
POSTGRES_MODE: local
POSTGRES_HOST: 127.0.0.1
POSTGRES_PORT: 5432
POSTGRES_DATABASE: postgres
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
POSTGRES_CONNECT_TIMEOUT: 10
STORAGE_MODE: local
LOCAL_STORAGE_BASE_PATH: /tmp/can-sr-uploads
AZURE_OPENAI_MODE: key
AZURE_DOC_INT_MODE: key
PYTHONUNBUFFERED: 1
AZURE_DOC_INT_ENDPOINT: fake
ENTREZ_EMAIL: fake
ENTREZ_API_KEY: fake
AZURE_STORAGE_CONNECTION_STRING: fake
SCOPUS_API_URL: fake


steps:
- name: Check out repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: pip
cache-dependency-path: |
backend/requirements.txt
backend/requirements_dev.txt

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt -r requirements_dev.txt

- name: Run pytest with coverage
run: |
mkdir -p /tmp/can-sr-uploads
python -m coverage run -m pytest tests/
python -m coverage xml -o coverage.xml
python -m coverage report

- name: Generate diff coverage
if: github.event.pull_request.base.sha != ''
continue-on-error: true
run: |
diff-cover coverage.xml \
--compare-branch=${{ github.event.pull_request.base.sha }} \
--markdown-report diff-cover.md \
--html-report diff-cover.html

- name: Build diff coverage comment body
if: github.event.pull_request.base.sha != ''
run: |
{
echo "<!-- can-sr-diff-coverage -->"
echo "## Diff coverage"
if [ -f diff-cover.md ]; then
cat diff-cover.md
else
echo "_Diff coverage report could not be generated for this run._"
fi
echo
echo "Coverage XML and HTML reports are attached to this workflow run as artifacts."
} > diff-coverage-comment.md

- name: Publish diff coverage comment
if: github.event.pull_request.base.sha != ''
continue-on-error: true
uses: edumserrano/find-create-or-update-comment@v2
with:
issue-number: ${{ github.event.pull_request.number }}
body-includes: '<!-- can-sr-diff-coverage -->'
comment-author: 'github-actions[bot]'
body-path: backend/diff-coverage-comment.md
edit-mode: replace

- name: Upload coverage artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: coverage-reports
if-no-files-found: ignore
path: |
backend/coverage.xml
backend/diff-cover.html
Comment on lines +105 to +124
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, this action would add a comment on each PR listing the coverage of its diff. Unfortunately I can't easily get this working, running into permission issues I don't have time to debug.

It does hoever successfully upload an artifact you can download, but that's not much faster than checking coverage yourself locally

13 changes: 13 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,16 @@ The importer currently supports these common include names:

If you put **rispy keys directly** in `include:` (e.g. `doi`, `authors`), the
importer will also attempt to copy them through as-is.



## Running tests

From the backend/ directory,
1. `pip install -r requirements_dev.txt`
2. `pytest`

To calculate coverage,
1. `coverage run -m pytest`
2. `coverage html`
3. Open `./htmlcov/index.html` in a browser to view the report and browse around line by line
Comment on lines +525 to +534
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note testing instructions

Empty file added backend/api/__init__.py
Empty file.
Empty file added backend/api/auth/__init__.py
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised we've gotten this far without __init__.py modules, but they're at least required for coverage to properly index/identify files, so please add __init__.py files all around from now on

Empty file.
Empty file.
Empty file added backend/api/core/__init__.py
Empty file.
Empty file.
Empty file added backend/api/extract/__init__.py
Empty file.
Empty file added backend/api/files/__init__.py
Empty file.
Empty file added backend/api/screen/__init__.py
Empty file.
Empty file.
Empty file added backend/api/sr/__init__.py
Empty file.
Empty file added backend/api/utils/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tool.pytest.ini_options]
asyncio_mode = "auto"

[tool.coverage.run]
source = ["."]
omit = [
"tests/*",
"venv/",
]
[tool.coverage.report]
show_missing = true
skip_covered = true
5 changes: 5 additions & 0 deletions backend/requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pytest==9.0.2
pytest-asyncio==1.3.0
coverage==7.6.12
diff-cover==9.1.1
ipython==9
Empty file added backend/tests/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions backend/tests/test_smoke.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import asyncio


def test_smoke():
assert True



async def test_my_async_code():
result = await asyncio.sleep(0.01, result="success")
assert result == "success"
Empty file.
Loading
Loading