dump-things-service/dump_things_service/tests/test_roundtrip_flatsocial.py
Christian Monch ba4a8d952c
Some checks failed
Codespell / Check for spelling errors (push) Successful in 25s
Ruff / Code linting (push) Failing after 28s
Test execution / Test-all (push) Successful in 1m2s
chore: reformat according to hatch check fmt
2026-11-13 15:13:36 +01:00

196 lines
7.4 KiB
Python

import freezegun
import pytest
from .. import HTTP_200_OK
from ..utils import cleaned_json
json_record = {
'pid': 'dlflatsocial:test_john_json',
'given_name': 'Johnöüß',
}
json_record_out = {
'schema_type': 'dlflatsocial:Person',
**json_record,
}
new_ttl_pid = 'dlflatsocial:another_john_json'
ttl_record = """@prefix dlflatsocial: <https://concepts.datalad.org/s/flat-social/unreleased/> .
@prefix dlsocialmx: <https://concepts.datalad.org/s/social-mixin/unreleased/> .
@prefix dlthings: <https://concepts.datalad.org/s/things/v1/> .
dlflatsocial:test_john_ttl a dlflatsocial:Person ;
dlsocialmx:given_name "Johnöüß" ;
dlthings:annotations [ a dlflat:Annotation ;
dlthings:annotation_tag <http://purl.obolibrary.org/obo/NCIT_C54269> ;
dlthings:annotation_value "test_user_1" ] .
"""
ttl_input_record = """@prefix dlflatsocial: <https://concepts.datalad.org/s/flat-social/unreleased/> .
@prefix dlsocialmx: <https://concepts.datalad.org/s/social-mixin/unreleased/> .
@prefix dlthings: <https://concepts.datalad.org/s/things/v1/> .
dlflatsocial:test_john_ttl a dlflatsocial:Person ;
dlsocialmx:given_name "Johnöüß" .
"""
ttl_output_record_a = """@prefix dlflatsocial: <https://concepts.datalad.org/s/flat-social/unreleased/> .
@prefix dlsocialmx: <https://concepts.datalad.org/s/social-mixin/unreleased/> .
dlflatsocial:another_john_ttl a dlflatsocial:Person ;
dlsocialmx:given_name "Johnöüß" .
"""
ttl_output_record_b = """@prefix dlflatsocial: <https://concepts.datalad.org/s/flat-social/unreleased/> .
@prefix dlsocialmx: <https://concepts.datalad.org/s/social-mixin/unreleased/> .
dlflatsocial:another_john_ttl a dlflatsocial:Person ;
dlsocialmx:given_name "Johnöüß" .
"""
ttl_output_record_with_annotation_a = """@prefix dlflat: <https://concepts.datalad.org/s/flat/unreleased/> .
@prefix dlflatsocial: <https://concepts.datalad.org/s/flat-social/unreleased/> .
@prefix dlsocialmx: <https://concepts.datalad.org/s/social-mixin/unreleased/> .
@prefix dlthings: <https://concepts.datalad.org/s/things/v1/> .
@prefix obo: <http://purl.obolibrary.org/obo/> .
dlflatsocial:another_john_ttl a dlflatsocial:Person ;
dlsocialmx:given_name "Johnöüß" ;
dlthings:annotations [ a dlflat:FlatAnnotation ;
dlthings:annotation_tag <http://semanticscience.org/resource/SIO_001083> ;
dlthings:annotation_value "1970-01-01T00:00:00" ],
[ a dlflat:FlatAnnotation ;
dlthings:annotation_tag obo:NCIT_C54269 ;
dlthings:annotation_value "user_all" ] .
"""
ttl_output_record_with_annotation_b = """@prefix dlflat: <https://concepts.datalad.org/s/flat/unreleased/> .
@prefix dlflatsocial: <https://concepts.datalad.org/s/flat-social/unreleased/> .
@prefix dlsocialmx: <https://concepts.datalad.org/s/social-mixin/unreleased/> .
@prefix dlthings: <https://concepts.datalad.org/s/things/v1/> .
@prefix obo: <http://purl.obolibrary.org/obo/> .
dlflatsocial:another_john_ttl a dlflatsocial:Person ;
dlsocialmx:given_name "Johnöüß" ;
dlthings:annotations [ a dlflat:FlatAnnotation ;
dlthings:annotation_tag obo:NCIT_C54269 ;
dlthings:annotation_value "user_all" ],
[ a dlflat:FlatAnnotation ;
dlthings:annotation_tag <http://semanticscience.org/resource/SIO_001083> ;
dlthings:annotation_value "1970-01-01T00:00:00" ] .
"""
new_json_pid = 'dlflatsocial:another_john_ttl'
# It seems that this test fails because of updates in either linkml or in the
# referenced dlflatsocial-schema. The test should be adapted to new behavior of
# linkml and made more robust, e.g., by checking for specific triples in the
# ttl output rather than comparing the whole string.
@pytest.mark.xfail
def test_json_ttl_json_dlflatsocial(fastapi_client_simple):
test_client, _, _ = fastapi_client_simple
for i in range(1, 3):
# Deposit JSON records
response = test_client.post(
f'/collection_dlflatsocial-{i}/record/Person',
headers={'x-dumpthings-token': 'token-all'},
json=json_record,
)
assert response.status_code == HTTP_200_OK
# Retrieve TTL records
response = test_client.get(
f'/collection_dlflatsocial-{i}/record?pid={json_record["pid"]}&format=ttl',
headers={'x-dumpthings-token': 'token-all'},
)
assert response.status_code == HTTP_200_OK
ttl = response.text
# modify the pid
ttl = ttl.replace(json_record['pid'], new_ttl_pid)
response = test_client.post(
f'/collection_dlflatsocial-{i}/record/Person?format=ttl',
headers={'content-type': 'text/turtle', 'x-dumpthings-token': 'token-all'},
data=ttl,
)
assert response.status_code == HTTP_200_OK
# Retrieve JSON record
response = test_client.get(
f'/collection_dlflatsocial-{i}/record?pid={new_ttl_pid}&format=json',
headers={'x-dumpthings-token': 'token-all'},
)
assert response.status_code == HTTP_200_OK
json_object = response.json()
assert cleaned_json(json_object, remove_keys=('schema_type',)) != json_record
json_object['pid'] = json_record['pid']
assert json_object == json_record_out
# It seems that this test fails because of updates in either linkml or in the
# referenced dlflatsocial-schema. The test should be adapted to new behavior of
# linkml and made more robust, e.g., by checking for specific triples in the
# ttl output rather than comparing the whole string.
@pytest.mark.xfail
@freezegun.freeze_time('1970-01-01')
def test_ttl_json_ttl_dlflatsocial(fastapi_client_simple):
test_client, _, _ = fastapi_client_simple
for i in range(1, 3):
# Deposit a ttl record
response = test_client.post(
f'/collection_dlflatsocial-{i}/record/Person?format=ttl',
headers={
'x-dumpthings-token': 'token-all',
'content-type': 'text/turtle',
},
data=ttl_input_record,
)
assert response.status_code == HTTP_200_OK, (
'Response content: ' + response.content.decode()
)
# Retrieve JSON records
response = test_client.get(
f'/collection_dlflatsocial-{i}/record?pid=dlflatsocial:test_john_ttl&format=json',
headers={'x-dumpthings-token': 'token-all'},
)
assert response.status_code == HTTP_200_OK
json_object = response.json()
# modify the pid
json_object['pid'] = new_json_pid
response = test_client.post(
f'/collection_dlflatsocial-{i}/record/Person?format=json',
headers={'x-dumpthings-token': 'token-all'},
json=json_object,
)
assert response.status_code == HTTP_200_OK
# Retrieve ttl record
response = test_client.get(
f'/collection_dlflatsocial-{i}/record?pid={new_json_pid}&format=ttl',
headers={'x-dumpthings-token': 'token-all'},
)
assert response.status_code == HTTP_200_OK
assert (
response.text.strip()
== ttl_output_record_a.replace(
'dlflatsocial:test_john_ttl',
new_json_pid,
).strip()
) or (
response.text.strip()
== ttl_output_record_b.replace(
'dlflatsocial:test_john_ttl',
new_json_pid,
).strip()
)