From bae9fb80e42dd9ac7953be695510dad836e2bd11 Mon Sep 17 00:00:00 2001 From: Alan Orth Date: Sun, 11 Nov 2018 11:51:22 +0200 Subject: [PATCH] Add initial API tests Test the basic assumptions of the API like response codes and types. --- tests/__init__.py | 0 tests/test_api.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/test_api.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_api.py b/tests/test_api.py new file mode 100644 index 0000000..ee6f22d --- /dev/null +++ b/tests/test_api.py @@ -0,0 +1,67 @@ +from falcon import testing +import json +import pytest + +from dspace_statistics_api.app import api + + +@pytest.fixture +def client(): + return testing.TestClient(api) + + +def test_get_docs(client): + '''Test requesting the documentation at the root.''' + + response = client.simulate_get('/') + + assert isinstance(response.content, bytes) + assert response.status_code == 200 + + +def test_get_item(client): + '''Test requesting a single item.''' + + response = client.simulate_get('/item/17') + response_doc = json.loads(response.content) + + assert isinstance(response_doc['downloads'], int) + assert isinstance(response_doc['id'], int) + assert isinstance(response_doc['views'], int) + assert response.status_code == 200 + + +def test_get_missing_item(client): + '''Test requesting a single non-existing item.''' + + response = client.simulate_get('/item/1') + + assert response.status_code == 404 + + +def test_get_items(client): + '''Test requesting 100 items.''' + + response = client.simulate_get('/items', query_string='limit=100') + response_doc = json.loads(response.content) + + assert isinstance(response_doc['currentPage'], int) + assert isinstance(response_doc['totalPages'], int) + assert isinstance(response_doc['statistics'], list) + assert response.status_code == 200 + + +def test_get_items_invalid_limit(client): + '''Test requesting 100 items with an invalid limit parameter.''' + + response = client.simulate_get('/items', query_string='limit=101') + + assert response.status_code == 400 + + +def test_get_items_invalid_page(client): + '''Test requesting 100 items with an invalid page parameter.''' + + response = client.simulate_get('/items', query_string='page=-1') + + assert response.status_code == 400