diff options
-rw-r--r-- | tests/fixtures/__init__.py | 0 | ||||
-rw-r--r-- | tests/fixtures/rdf.py | 41 | ||||
-rw-r--r-- | tests/unit/db/rdf/test_wiki.py | 11 |
3 files changed, 47 insertions, 5 deletions
diff --git a/tests/fixtures/__init__.py b/tests/fixtures/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/fixtures/__init__.py diff --git a/tests/fixtures/rdf.py b/tests/fixtures/rdf.py new file mode 100644 index 0000000..a5fd711 --- /dev/null +++ b/tests/fixtures/rdf.py @@ -0,0 +1,41 @@ +"""Test fixtures to set up a test named graph for loading RDF data.""" +from requests.auth import HTTPDigestAuth +from flask import config +import os +import requests +import pytest + + +def get_sparql_auth_conf() -> dict: + """Fetch SPARQL auth configuration for the GN3_SECRETS file.""" + sparql_conf = config.Config("") + sparql_conf.from_envvar("GN3_SECRETS") + sparql_conf.from_envvar("GN3_CONF") + return { + "sparql_user": sparql_conf["SPARQL_USER"], + "sparql_auth_uri": sparql_conf["SPARQL_AUTH_URI"], + "sparql_crud_auth_uri": sparql_conf["SPARQL_CRUD_AUTH_URI"], + "sparql_endpoint": sparql_conf["SPARQL_ENDPOINT"], + "sparql_password": sparql_conf["SPARQL_PASSWORD"], + } + + +@pytest.fixture(scope="module") +def rdf_setup(): + """Upload RDF to a Virtuoso named graph""" + # Define the URL and file + sparql_conf = get_sparql_auth_conf() + url = sparql_conf["sparql_crud_auth_uri"] + file_path = os.path.join( + os.path.dirname(__file__).split("fixtures")[0], + "test_data/ttl-files/test-data.ttl", + ) + # Define the query parameters and authentication + params = {"graph": "http://cd-test.genenetwork.org"} + auth = HTTPDigestAuth( + sparql_conf["sparql_user"], sparql_conf["sparql_password"]) + # Open the file in binary mode and send the request + with open(file_path, "rb") as file: + response = requests.put(url, params=params, auth=auth, data=file) + yield response + requests.delete(url, params=params, auth=auth) diff --git a/tests/unit/db/rdf/test_wiki.py b/tests/unit/db/rdf/test_wiki.py index 99ecab0..a1500e9 100644 --- a/tests/unit/db/rdf/test_wiki.py +++ b/tests/unit/db/rdf/test_wiki.py @@ -1,6 +1,6 @@ """Tests for gn3/db/rdf/wiki.py""" from unittest import TestCase - +from tests.fixtures.rdf import rdf_setup, get_sparql_auth_conf import pytest import os @@ -12,6 +12,7 @@ from gn3.db.rdf.wiki import ( ) +SPARQL_CONF = get_sparql_auth_conf() GRAPH = "<http://cd-test.genenetwork.org>" @@ -168,11 +169,11 @@ def test_sanitize_result(result, expected): @pytest.mark.rdf -def test_get_wiki_entries_by_symbol(): +def test_get_wiki_entries_by_symbol(rdf_setup): """Test that wiki entries are fetched correctly by symbol""" result = get_wiki_entries_by_symbol( symbol="ckb", - sparql_uri=os.environ.get("SPARQL_ENDPOINT", "http://localhost:9082/sparql"), + sparql_uri=SPARQL_CONF["sparql_endpoint"], graph=GRAPH, ) expected = { @@ -239,10 +240,10 @@ def test_get_wiki_entries_by_symbol(): @pytest.mark.rdf -def test_get_comment_history(): +def test_get_comment_history(rdf_setup): result = get_comment_history( comment_id=1158, - sparql_uri=os.environ.get("SPARQL_ENDPOINT", "http://localhost:9082/sparql"), + sparql_uri=SPARQL_CONF["sparql_endpoint"], graph=GRAPH, ) expected = { |