-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit tests and test runner action (#16)
* Add basic unit tests for Database, healthcheck * Add GitHub Action that runs pytest on PRs
- Loading branch information
Showing
6 changed files
with
112 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
coverage | ||
pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 == [] |