2018-11-11 10:51:22 +01:00
|
|
|
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):
|
2019-12-14 11:39:58 +01:00
|
|
|
"""Test requesting the documentation at the root."""
|
2018-11-11 10:51:22 +01:00
|
|
|
|
2019-12-14 11:39:58 +01:00
|
|
|
response = client.simulate_get("/")
|
2018-11-11 10:51:22 +01:00
|
|
|
|
|
|
|
assert isinstance(response.content, bytes)
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_item(client):
|
2019-12-14 11:39:58 +01:00
|
|
|
"""Test requesting a single item."""
|
2018-11-11 10:51:22 +01:00
|
|
|
|
2020-03-02 10:10:41 +01:00
|
|
|
response = client.simulate_get("/item/c3910974-c3a5-4053-9dce-104aa7bb1621")
|
2018-11-11 16:01:17 +01:00
|
|
|
response_doc = json.loads(response.text)
|
2018-11-11 10:51:22 +01:00
|
|
|
|
2019-12-14 11:39:58 +01:00
|
|
|
assert isinstance(response_doc["downloads"], int)
|
2020-03-02 10:10:41 +01:00
|
|
|
assert isinstance(response_doc["id"], str)
|
2019-12-14 11:39:58 +01:00
|
|
|
assert isinstance(response_doc["views"], int)
|
2018-11-11 10:51:22 +01:00
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_missing_item(client):
|
2019-12-14 11:39:58 +01:00
|
|
|
"""Test requesting a single non-existing item."""
|
2018-11-11 10:51:22 +01:00
|
|
|
|
2020-03-02 10:10:41 +01:00
|
|
|
response = client.simulate_get("/item/c3910974-c3a5-4053-9dce-104aa7bb1620")
|
2018-11-11 10:51:22 +01:00
|
|
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_items(client):
|
2019-12-14 11:39:58 +01:00
|
|
|
"""Test requesting 100 items."""
|
2018-11-11 10:51:22 +01:00
|
|
|
|
2019-12-14 11:39:58 +01:00
|
|
|
response = client.simulate_get("/items", query_string="limit=100")
|
2018-11-11 16:05:31 +01:00
|
|
|
response_doc = json.loads(response.text)
|
2018-11-11 10:51:22 +01:00
|
|
|
|
2019-12-14 11:39:58 +01:00
|
|
|
assert isinstance(response_doc["currentPage"], int)
|
|
|
|
assert isinstance(response_doc["totalPages"], int)
|
|
|
|
assert isinstance(response_doc["statistics"], list)
|
2018-11-11 10:51:22 +01:00
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_items_invalid_limit(client):
|
2019-12-14 11:39:58 +01:00
|
|
|
"""Test requesting 100 items with an invalid limit parameter."""
|
2018-11-11 10:51:22 +01:00
|
|
|
|
2019-12-14 11:39:58 +01:00
|
|
|
response = client.simulate_get("/items", query_string="limit=101")
|
2018-11-11 10:51:22 +01:00
|
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_items_invalid_page(client):
|
2019-12-14 11:39:58 +01:00
|
|
|
"""Test requesting 100 items with an invalid page parameter."""
|
2018-11-11 10:51:22 +01:00
|
|
|
|
2019-12-14 11:39:58 +01:00
|
|
|
response = client.simulate_get("/items", query_string="page=-1")
|
2018-11-11 10:51:22 +01:00
|
|
|
|
|
|
|
assert response.status_code == 400
|