Skip to content

Commit

Permalink
Unit tests and test runner action (#16)
Browse files Browse the repository at this point in the history
* Add basic unit tests for Database, healthcheck
* Add GitHub Action that runs pytest on PRs
  • Loading branch information
machikoyasuda committed Oct 6, 2021
2 parents b788fd1 + fbc084f commit 2719d3d
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 6 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Run pytests

on:
pull_request:
branches: [ "*" ]
paths:
- 'data/**'
- 'eligibility_server/**'
- 'keys/**'
- 'tests/**'
- 'requirements.txt'

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- name: Install package and dependencies
run: |
python -m pip install --upgrade pip
pip install -r tests/requirements.txt
pip install -r requirements.txt
- name: Test with pytest
run: |
coverage run -m pytest
coverage report -m
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ docker compose build server
- Build and Open in Container on VS Code
- Check the Ports tab to find the dynamically-assigned localhost port, or run `docker ps -f name=eligibility-server` and go to `http://localhost:XXXX`

## Tests

### Run tests
```bash
coverage run -m pytest
```

### Check test coverage

```bash
coverage report -m
```

## License

[AGPL-3.0 License](./LICENSE)
10 changes: 6 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import pytest

from eligibility_server.app import app as server
from eligibility_server.app import app
from eligibility_server.app import Database as Database


@pytest.fixture
def app():
yield server
def database():
db = Database()
return db


@pytest.fixture
def client(app):
def client():
return app.test_client()
2 changes: 2 additions & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
pytest
9 changes: 7 additions & 2 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
def test_healthcheck(app, client):
response = client.get("/healthcheck")
"""
Test app
"""


def test_healthcheck(client):
response = client.get("healthcheck")
assert response.status_code == 200
assert response.data == b"Healthy"
57 changes: 57 additions & 0 deletions tests/test_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
Test database class and methods
"""

import json

with open("data/server.json", encoding="utf8") as file:
DATA = json.load(file)


def test_database_init(database):
assert database._config
assert database._merchants
assert database._users


def test_database_properties(database):
assert database.auth_header
assert database.auth_token
assert database.token_header
assert database.jwe_cek_enc
assert database.jwe_encryption_alg
assert database.jws_signing_alg
assert database.request_access


def test_database_check_merchant_in_database(database):
merchant_id = DATA["merchants"][1]
response = database.check_merchant(merchant_id)
assert response is True


def test_database_check_merchant_not_in_database(database):
merchant_id_not_in_database = "123"
response = database.check_merchant(merchant_id_not_in_database)
assert response is False


def test_database_check_user_in_database(database):
key = min(DATA["users"])
user = DATA["users"][key][0]
types = DATA["users"][key][1]
response = database.check_user(key, user, types)
assert response == types


def test_database_check_user_in_database_not_eligible(database):
key = min(DATA["users"])
user = DATA["users"][key][0]
types = ["type2"]
response = database.check_user(key, user, types)
assert response == []


def test_database_check_user_not_in_database(database):
response = database.check_user("G7778889", "Thomas", ["type1"])
assert response == []

0 comments on commit 2719d3d

Please sign in to comment.