Added API tests, changed conftest, updated docs

master
Dylan DiGeronimo 2020-02-25 20:20:27 -05:00
parent 2e0f925714
commit 119680bf60
3 changed files with 71 additions and 17 deletions

View File

@ -68,22 +68,23 @@ After you have activated the virtual environment, press `ctrl-d` to exit. Your t
## Deploying the project locally
After activating the virtual environment, run the following commands from within the root directory of the project:
<!-- `gunicorn -w=4 wsgi:app` -->
$ export FLASK_ENV=development
*(This is optional, but enables useful debugging tools)*
$ flask run
After activating the virtual environment, run `flask run` within the root directory of the project:
The Flask app will then be running at [localhost:5000](localhost:5000).
Optionally, use `export FLASK_ENV=development` before running the app to enable useful debugging tools.
To close the application, end the process with `ctrl-c` in your terminal.
## Running Tests
In the root directory of the project, run `python -m pytest`. This will run the entire test suite. New test functions and files must be contained in the `tests/` directory.
In the root directory of the project, run
`python -m pytest`.
This will run the entire test suite. New test functions and files must be contained in the `tests/` directory.
To see test coverage data, run `python -m pytest --cov`. To generate an HTML coverage report, run `python -m pytest --cov-report html tests/ --cov=./`. Then, run `python -m http.server` and navigate to [localhost:8000](localhost:8000) to view it.
## Troubleshooting

View File

@ -13,16 +13,9 @@ from cce_search import create_app
# Create the client fixture used by tests
@pytest.fixture
def app():
# Create temp file to hold db for testing
db_fd, db_path = tempfile.mkstemp()
app = create_app({'TESTING': True, 'DATABASE': db_path}) # In __init__.py, 'DATABASE' is commented out --> issue?
# TODO: Create database --> our project differs here
app = create_app({'TESTING': True})
yield app
os.close(db_fd)
os.unlink(db_path)
# Create the test client
@pytest.fixture

60
tests/test_api.py Normal file
View File

@ -0,0 +1,60 @@
"""
Note:
Some of the values in the returned objects are lists (including many 1 element lists), e.g.:
{
"results": [
{
"authors": [
...
],
"pages": ...,
}
]
}
This is why many lines contain a mix of keys and indices, such as ["key_1"][1]["key_2"][0]["key_3"]
"""
import pytest
import requests
API = "http://sfr-bardo-copyright-development.us-east-1.elasticbeanstalk.com"
REG_UUID = "b055bda4-6f10-1014-90c3-93bd933bf4a5"
RENEW_UUID = "cce7b1b5-c98e-59cf-8063-f020da28cdd3"
REG_NUM = "A71297"
RENEW_NUM = "R673507"
# Test that the API can be reached successfully
def test_basic_api_connection(client):
res = requests.get(API)
assert res.status_code == 200
# Test the registration number lookup service
def test_registration_lookup(client):
res = requests.get(API + "/registration/" + REG_UUID)
json_res = res.json()
assert json_res["data"]["authors"] == ["Cary F. Baynes", "C. G. Jung", "Hellmut Wilhelm"]
# Test the renewal number lookup service
def test_renewal_lookup(client):
res = requests.get(API + "/renewal/" + RENEW_UUID)
json_res = res.json()
assert json_res["data"][0]["registrations"][0]["date"] == "1950-05-25"
# Test the fulltext search service is working
def test_fulltext_search(client):
res = requests.get(API + "/search/fulltext?query=I%20Ching")
json_res = res.json()
assert len(json_res["data"]["results"]) == 10 # TODO: Improve this
# Test the registration number search service is working
def test_registration_search(client):
res = requests.get(API + "/search/registration/" + REG_NUM)
json_res = res.json()
assert json_res["data"]["results"][1]["registrations"][0]["number"] == REG_NUM
# Test the renewal number search service is working
def test_renewal_search(client):
res = requests.get(API + "/search/renewal/" + RENEW_NUM)
json_res = res.json()
assert json_res["data"]["results"][0]["renewals"][0]["renewal_num"] == RENEW_NUM