mirror of
https://github.com/ilri/dspace-statistics-api.git
synced 2024-11-16 03:17:04 +01:00
Alan Orth
d94134f80a
In Python 3.5 it seems that json.loads() cannot decode a bytes, but it works in Python 3.6 and 3.7. Let's try a workaround to see if we can get it working on both Python 3.5 and 3.6+. See: https://docs.python.org/3.5/library/json.html#json.loads See: https://docs.python.org/3.6/library/json.html#json.loads
73 lines
1.9 KiB
Python
73 lines
1.9 KiB
Python
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.content is a bytes in Python 3.5
|
|
if isinstance(response.content, bytes):
|
|
response_doc = json.loads(response.content.decode('utf-8'))
|
|
else:
|
|
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
|