Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 44 additions & 19 deletions tests/test_restapi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from conf_util import *
import re

UNIQUE_INSTANCE_RECORD_ID = ROOT_URL + '/fxql7jqr38b1dkf'


def test_update_and_delete_holding(session, load_holding):
holding_id = load_holding(session)
Expand Down Expand Up @@ -32,16 +34,20 @@ def test_get_bib(session):
bib_id = create_bib(session)

expected_record_location = bib_id
expected_content_location = "{0}/data.jsonld".format(bib_id)
expected_document_header = "{0}".format(bib_id)
expected_link_header = "<{0}>; rel=describedby".format(bib_id)
expected_content_location = f"{bib_id}/data.jsonld"
expected_document_header = f"{bib_id}"
expected_link_header = f"<{bib_id}>; rel=describedby"

result = session.get(bib_id)
assert result.status_code == 200

json_body = result.json()
graph = json_body['@graph']
record = graph[0]

# Record
assert record['@id'] == expected_record_location

thing = graph[1]
record_sameas = record['sameAs'][0]['@id']
thing_id = thing['@id']
Expand Down Expand Up @@ -188,10 +194,13 @@ def test_update_bib(session):
json_body = result.json()
graph = json_body['@graph']
record = graph[0]
thing = graph[1]
record_id = record['@id']
record_sameas = record['sameAs'][0]['@id']

thing = graph[1]
thing_id = thing['@id']
thing_sameas = thing['sameAs'][0]['@id']

expected_thing_location = thing_id

# Update value in document
Expand Down Expand Up @@ -308,11 +317,11 @@ def is_framed(json):

def is_embellished(json):
if is_framed(json):
return len(json['mainEntity']['instanceOf']['illustrativeContent']) > 1
return len(json['mainEntity']['instanceOf']['language'][0]) > 1
else:
return len(json['@graph']) > 3

bib_id = find_id(session, '9789187745317+nya+konditionstest+cykel')
bib_id = UNIQUE_INSTANCE_RECORD_ID
url = ROOT_URL + '/' + bib_id + view

query = '?framed=%s&embellished=%s' % (framed, embellished)
Expand All @@ -337,7 +346,7 @@ def check_lens(json):
return 'chip'
raise Exception('could not identify lens')

bib_id = find_id(session, '9789187745317+nya+konditionstest+cykel')
bib_id = UNIQUE_INSTANCE_RECORD_ID
url = ROOT_URL + '/' + bib_id + view

if lens:
Expand All @@ -352,23 +361,31 @@ def check_lens(json):
assert check_lens(json) == lens


cached_ids = {}
def find_id(session, q):
if q in cached_ids:
return cached_ids[q]
def test_content_negotiation(session):
bib_id = UNIQUE_INSTANCE_RECORD_ID

url = ROOT_URL + '/find?q=' + q
result = session.get(bib_id, headers={'Accept': "text/turtle"})

result = session.get(url)
assert result.status_code == 200
json = result.json()
assert len(json['items']) == 1
assert result.headers['Content-Type'] == 'text/turtle;charset=UTF-8'

assert b'prefix : <' in result.content


the_id = json['items'][0]['@id']
the_id = re.sub('#.*', '', the_id)
cached_ids[q] = the_id
def test_profile_negotiation(session):
bib_id = UNIQUE_INSTANCE_RECORD_ID

return the_id
profile_uri = 'https://id.kb.se/sys/context/target/loc-w3c-sdo'
result = session.get(bib_id, headers={
'Accept': "text/turtle",
'Accept-Profile': f"<{profile_uri}>"
})

assert result.status_code == 200
assert result.headers['Content-Type'] == 'text/turtle;charset=UTF-8'
assert profile_uri in result.headers['Content-Profile']

assert b'prefix bf2: <' in result.content


def test_search(session):
Expand Down Expand Up @@ -913,6 +930,14 @@ def test_search_or_prefix(session):
assert (len(deduplicated_a_and_b) == len(es_result_a_or_b['items']))


def test_get_catalogue_start_page(session):
result = requests.get(ROOT_URL + "/katalogisering", headers={
'Accept': "text/html, */*;q=0.9",
})
assert result.status_code == 200
assert result.headers['Content-Type'].startswith('text/html')


def has_reference(x, ref, key=None):
if isinstance(x, dict):
return any([has_reference(x[key], ref, key=key) for key in x])
Expand Down