-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tests for federated SPARQL queries between the curies mapping service and popular triplestores #53
Open
vemonet
wants to merge
32
commits into
biopragmatics:main
Choose a base branch
from
vemonet:add-federated-queries-test-with-docker
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add tests for federated SPARQL queries between the curies mapping service and popular triplestores #53
Changes from 7 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
843541f
Add tests to check if federated queries between the curies mapping se…
vemonet 899d586
Update MANIFEST.in
cthoyt 6863cc3
Merge branch 'main' into pr/53
cthoyt e77a846
Update test_sparql.py
cthoyt 67d009e
Remove redundant code
cthoyt 79dd180
Merge branch 'main' into pr/53
cthoyt 7a61e43
Update test_sparql.py
cthoyt 423e9f6
Code cleanup
cthoyt e4443c0
Update test_sparql.py
cthoyt 418fd74
Update test_sparql.py
cthoyt 41dfdb7
Merge branch 'main' into pr/53
cthoyt eaf2906
Update test_sparql.py
cthoyt 5f555bf
Update test_sparql.py
cthoyt 62a1f49
try to fix a bit the URLs that have been changed without checking
vemonet e32c6da
fix the blazegraph local URL in test
vemonet 984f10c
fix federated queries test, only test_from_virtuoso_to_mapping_servic…
vemonet 0e709cf
fix CSV parsing, which fixes all tests
vemonet 85f8652
Use the same query for test from the mapping service to external trip…
vemonet 432fc06
improve how triples are defined in init script
vemonet 477fe35
Add externally configurable tests
cthoyt 5e5e1e9
Add second generic test
cthoyt 78fb063
Better configure queries
cthoyt 3561a57
Update src/curies/mapping_service/utils.py
cthoyt 58e6021
Cleanup code
cthoyt 18cb2d7
pass flake8
cthoyt f2b9f74
add federated queries tests for fuseki
vemonet dac165c
merge
vemonet efbfa00
Remove non-generic tests
cthoyt 3657376
Update test_sparql.py
cthoyt 7e6d339
Make tests generic and not rely on docker bioregistry
cthoyt 7b66804
Switch to cases
cthoyt 4dd192c
Merge branch 'main' into add-federated-queries-test-with-docker
cthoyt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
version: "3" | ||
services: | ||
|
||
mapping-service: | ||
build: | ||
context: . | ||
dockerfile: tests/resources/Dockerfile | ||
ports: | ||
- 8888:8888 | ||
volumes: | ||
- ./src:/app/src | ||
- ./tests:/app/tests | ||
|
||
blazegraph: | ||
image: metaphacts/blazegraph-basic:2.2.0-20160908.003514-6-jetty9.4.44-jre8-45dbfff | ||
ports: | ||
- 8889:8080 | ||
|
||
virtuoso: | ||
image: openlink/virtuoso-opensource-7:latest | ||
ports: | ||
- 8890:8890 | ||
environment: | ||
- DBA_PASSWORD=${VIRTUOSO_PASSWORD:-dba} | ||
- SPARQL_UPDATE=true | ||
- VIRT_Database_ErrorLogLevel=7 # 7 is maximum logs | ||
- VIRT_HTTPServer_HTTPLogFile=/http.log | ||
# https://docs.openlinksw.com/virtuoso/loggingandrecording/ | ||
|
||
# TODO: add Apache Fuseki |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM python:3.10 | ||
|
||
# Dockerfile used to spawn a mapping service SPARQL endpoint for testing | ||
|
||
WORKDIR /app | ||
|
||
ADD . . | ||
|
||
RUN pip install -e ".[fastapi,rdflib,bioregistry]" | ||
|
||
CMD [ "bioregistry", "web", "--port", "8888", "--host", "0.0.0.0" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
## Script to initialize the triplestores started with docker | ||
# Run it from the root of the repo: ./tests/resources/init_triplestores.sh | ||
|
||
# Enable federated query for Virtuoso and load a triple for testing | ||
docker compose exec virtuoso isql -U dba -P dba exec='GRANT "SPARQL_SELECT_FED" TO "SPARQL";' | ||
docker compose exec virtuoso isql -U dba -P dba exec='SPARQL INSERT IN <https://purl.uniprot.org> { <https://purl.uniprot.org/uniprot/P07862> <https://w3id.org/biolink/vocab/category> <https://w3id.org/biolink/vocab/GeneProduct> };' | ||
|
||
# Load a triple to local blazegraph for testing | ||
docker compose exec blazegraph curl -X POST http://localhost:8080/blazegraph/namespace/kb/sparql -d 'update=insert data {<http://identifiers.org/ensembl/ENSG00000006453> <https://w3id.org/biolink/vocab/category> <https://w3id.org/biolink/vocab/Gene> . }' | ||
cthoyt marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
"""Tests federated SPARQL queries between the curies mapping service and popular triplestores.""" | ||
|
||
import unittest | ||
from typing import Set, Tuple | ||
|
||
from curies.mapping_service.utils import ( | ||
get_sparql_record_so_tuples, | ||
get_sparql_records, | ||
sparql_service_available, | ||
) | ||
from tests.test_mapping_service import VALID_CONTENT_TYPES | ||
|
||
# NOTE: federated queries need to use docker internal URL | ||
DOCKER_BIOREGISTRY = "http://mapping-service:8888/sparql" | ||
LOCAL_BIOREGISTRY = "http://localhost:8888/sparql" | ||
LOCAL_BLAZEGRAPH = "http://localhost:8889/blazegraph/namespace/kb/sparql" | ||
DOCKER_BLAZEGRAPH = "http://blazegraph:8080/blazegraph/namespace/kb/sparql" | ||
LOCAL_VIRTUOSO = "http://localhost:8890/sparql" | ||
DOCKER_VIRTUOSO = "http://virtuoso:8890/sparql" | ||
|
||
|
||
def get(endpoint: str, sparql: str, accept: str) -> Set[Tuple[str, str]]: | ||
"""Get a response from a given SPARQL query.""" | ||
records = get_sparql_records(endpoint=endpoint, sparql=sparql, accept=accept) | ||
return get_sparql_record_so_tuples(records) | ||
|
||
|
||
SPARQL_VALUES = f"""\ | ||
PREFIX owl: <http://www.w3.org/2002/07/owl#> | ||
SELECT DISTINCT ?s ?o WHERE {{ | ||
SERVICE <{DOCKER_BIOREGISTRY}> {{ | ||
VALUES ?s {{ <http://purl.obolibrary.org/obo/CHEBI_24867> <http://purl.obolibrary.org/obo/CHEBI_24868> }} . | ||
?s owl:sameAs ?o . | ||
}} | ||
}} | ||
""".rstrip() | ||
|
||
SPARQL_SIMPLE = f"""\ | ||
PREFIX owl: <http://www.w3.org/2002/07/owl#> | ||
SELECT DISTINCT ?s ?o WHERE {{ | ||
SERVICE <{DOCKER_BIOREGISTRY}> {{ | ||
<http://purl.obolibrary.org/obo/CHEBI_24867> owl:sameAs ?o . | ||
?s owl:sameAs ?o . | ||
}} | ||
}} | ||
""".rstrip() | ||
|
||
|
||
@unittest.skipUnless( | ||
sparql_service_available(LOCAL_BIOREGISTRY), reason="No local Bioregistry is running" | ||
) | ||
class TestSPARQL(unittest.TestCase): | ||
"""Tests federated SPARQL queries between the curies mapping service and blazegraph/virtuoso triplestores. | ||
|
||
Run and init the required triplestores locally: | ||
1. docker compose up | ||
2. ./tests/resources/init_triplestores.sh | ||
""" | ||
|
||
def assert_endpoint(self, endpoint: str, query: str, *, accept: str): | ||
"""Assert the endpoint returns favorable results.""" | ||
records = get(endpoint, query, accept=accept) | ||
self.assertIn( | ||
("http://purl.obolibrary.org/obo/CHEBI_24867", "https://bioregistry.io/chebi:24867"), | ||
records, | ||
) | ||
|
||
@unittest.skipUnless( | ||
sparql_service_available(LOCAL_BLAZEGRAPH), reason="No local BlazeGraph is running" | ||
) | ||
def test_from_blazegraph_to_bioregistry(self): | ||
"""Test a federated query from a Blazegraph triplestore to the curies service.""" | ||
for mimetype in VALID_CONTENT_TYPES: | ||
with self.subTest(mimetype=mimetype): | ||
self.assert_endpoint(LOCAL_BLAZEGRAPH, SPARQL_SIMPLE, accept=mimetype) | ||
self.assert_endpoint(LOCAL_BLAZEGRAPH, SPARQL_VALUES, accept=mimetype) | ||
|
||
@unittest.skipUnless( | ||
sparql_service_available(LOCAL_VIRTUOSO), reason="No local Virtuoso is running" | ||
) | ||
def test_from_virtuoso_to_bioregistry(self): | ||
"""Test a federated query from a OpenLink Virtuoso triplestore to the curies service.""" | ||
for mimetype in VALID_CONTENT_TYPES: | ||
with self.subTest(mimetype=mimetype): | ||
self.assert_endpoint(LOCAL_VIRTUOSO, SPARQL_SIMPLE, accept=mimetype) | ||
# TODO: Virtuoso fails to resolves VALUES in federated query | ||
# self.assert_endpoint(LOCAL_VIRTUOSO, SPARQL_VALUES, accept=mimetype) | ||
|
||
@unittest.skipUnless( | ||
sparql_service_available(LOCAL_BIOREGISTRY), reason="No local Bioregistry is running" | ||
) | ||
def test_from_bioregistry_to_virtuoso(self): | ||
"""Test a federated query from the curies service to a OpenLink Virtuoso triplestore.""" | ||
query = f"""\ | ||
SELECT ?s ?o WHERE {{ | ||
<https://identifiers.org/uniprot/P07862> <http://www.w3.org/2002/07/owl#sameAs> ?s . | ||
SERVICE <{DOCKER_VIRTUOSO}> {{ | ||
?s ?p ?o . | ||
}} | ||
}} | ||
""".rstrip() | ||
for mimetype in VALID_CONTENT_TYPES: | ||
with self.subTest(mimetype=mimetype): | ||
records = get(LOCAL_BIOREGISTRY, query, accept=mimetype) | ||
self.assertGreater(len(records), 0) | ||
|
||
@unittest.skipUnless( | ||
sparql_service_available(LOCAL_BIOREGISTRY), reason="No local Bioregistry is running" | ||
) | ||
def test_from_bioregistry_to_blazegraph(self): | ||
"""Test a federated query from the curies service to a OpenLink Virtuoso triplestore.""" | ||
query = f"""\ | ||
PREFIX owl: <http://www.w3.org/2002/07/owl#> | ||
PREFIX bl: <https://w3id.org/biolink/vocab/> | ||
SELECT ?s ?o WHERE {{ | ||
<https://www.ensembl.org/id/ENSG00000006453> owl:sameAs ?s . | ||
|
||
SERVICE <{DOCKER_BLAZEGRAPH}> {{ | ||
?s bl:category ?o . | ||
}} | ||
}} | ||
""".rstrip() | ||
for mimetype in VALID_CONTENT_TYPES: | ||
with self.subTest(mimetype=mimetype): | ||
records = get(LOCAL_BIOREGISTRY, query, accept=mimetype) | ||
self.assertGreater(len(records), 0) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason we can't just use the default ports for each service as we expose outside of docker?