From e5190b7c0e8f0c8388c8b0831cb1b528ed42d65f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20Lindstr=C3=B6m?= Date: Mon, 28 Mar 2022 08:57:22 +0000 Subject: [PATCH 1/4] Use f-strings --- tests/test_restapi.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_restapi.py b/tests/test_restapi.py index 6beda94..29bf5a2 100644 --- a/tests/test_restapi.py +++ b/tests/test_restapi.py @@ -32,9 +32,9 @@ 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 From dfcbc8f6641ed14cb595fc7ebc883e3d0e25b196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20Lindstr=C3=B6m?= Date: Mon, 28 Mar 2022 12:42:02 +0000 Subject: [PATCH 2/4] Use a pre-selected record id instead of find_id --- tests/test_restapi.py | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/tests/test_restapi.py b/tests/test_restapi.py index 29bf5a2..0ebe481 100644 --- a/tests/test_restapi.py +++ b/tests/test_restapi.py @@ -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) @@ -42,6 +44,10 @@ def test_get_bib(session): 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'] @@ -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 @@ -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) @@ -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: @@ -352,25 +361,6 @@ 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] - - url = ROOT_URL + '/find?q=' + q - - result = session.get(url) - assert result.status_code == 200 - json = result.json() - assert len(json['items']) == 1 - - the_id = json['items'][0]['@id'] - the_id = re.sub('#.*', '', the_id) - cached_ids[q] = the_id - - return the_id - - def test_search(session): search_endpoint = "/find" limit = 1 From 232efc1d6edb83850d82d6ba25550d1f154b612c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20Lindstr=C3=B6m?= Date: Mon, 28 Mar 2022 12:44:02 +0000 Subject: [PATCH 3/4] Add tests for content and profile negotiation --- tests/test_restapi.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/test_restapi.py b/tests/test_restapi.py index 0ebe481..0917994 100644 --- a/tests/test_restapi.py +++ b/tests/test_restapi.py @@ -361,6 +361,33 @@ def check_lens(json): assert check_lens(json) == lens +def test_content_negotiation(session): + bib_id = UNIQUE_INSTANCE_RECORD_ID + + result = session.get(bib_id, headers={'Accept': "text/turtle"}) + + assert result.status_code == 200 + assert result.headers['Content-Type'] == 'text/turtle;charset=UTF-8' + + assert b'prefix : <' in result.content + + +def test_profile_negotiation(session): + bib_id = UNIQUE_INSTANCE_RECORD_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): search_endpoint = "/find" limit = 1 From 88705798ab8c79dcff4d6e72d108a24e6f009e14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20Lindstr=C3=B6m?= Date: Mon, 28 Mar 2022 13:16:08 +0000 Subject: [PATCH 4/4] Test that catalogue start page is correctly served --- tests/test_restapi.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_restapi.py b/tests/test_restapi.py index 0917994..78c830c 100644 --- a/tests/test_restapi.py +++ b/tests/test_restapi.py @@ -930,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])