Skip to content

Commit

Permalink
Add GraphDB fixture for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
julian-risch committed Apr 7, 2021
1 parent 0edf288 commit cd26ab3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
1 change: 0 additions & 1 deletion haystack/knowledge_graph/graphdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def delete_index(self):
if response.status_code > 299:
raise Exception(response.text)


def import_from_ttl_file(self, index: str, path: Path):
url = f"{self.url}/repositories/{index}/statements"
headers = {"Content-type": "application/x-turtle"}
Expand Down
26 changes: 26 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest
import requests
from elasticsearch import Elasticsearch
from haystack.knowledge_graph.graphdb import GraphDBKnowledgeGraph
from milvus import Milvus

from haystack.document_store.milvus import MilvusDocumentStore
Expand Down Expand Up @@ -54,6 +55,8 @@ def pytest_collection_modifyitems(items):
item.add_marker(pytest.mark.tika)
elif "elasticsearch" in item.nodeid:
item.add_marker(pytest.mark.elasticsearch)
elif "graphdb" in item.nodeid:
item.add_marker(pytest.mark.graphdb)
elif "pipeline" in item.nodeid:
item.add_marker(pytest.mark.pipeline)
elif "slow" in item.nodeid:
Expand Down Expand Up @@ -96,6 +99,29 @@ def milvus_fixture():
time.sleep(40)


@pytest.fixture(scope="session")
def graphdb_fixture():
# test if a GraphDB instance is already running. If not, download and start a GraphDB instance locally.
try:
kg = GraphDBKnowledgeGraph()
# TODO fail if not running GraphDB
raise RuntimeError
except:
print("Starting GraphDB ...")
status = subprocess.run(
['docker rm haystack_test_graphdb'],
shell=True
)
status = subprocess.run(
['docker run -d -p 7200:7200 --name haystack_test_graphdb docker-registry.ontotext.com/graphdb-free:9.4.1-adoptopenjdk11'],
shell=True
)
if status.returncode:
raise Exception(
"Failed to launch GraphDB. Please check docker container logs.")
time.sleep(30)


@pytest.fixture(scope="session")
def tika_fixture():
try:
Expand Down
1 change: 1 addition & 0 deletions test/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ markers =
slow: marks tests as slow (deselect with '-m "not slow"')
tika: marks tests which require tika container (deselect with '-m "not tika"')
elasticsearch: marks tests which require elasticsearch container (deselect with '-m "not elasticsearch"')
graphdb: marks tests which require graphdb container (deselect with '-m "not graphdb"')
generator: marks generator tests (deselect with '-m "not generator"')
pipeline: marks tests with pipeline
summarizer: marks summarizer tests
27 changes: 12 additions & 15 deletions test/test_knowledge_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@

import pytest

from haystack.graph_retriever.text_to_sparql import Text2SparqlRetriever
from haystack.graph_retriever import Text2SparqlRetriever
from haystack.knowledge_graph.graphdb import GraphDBKnowledgeGraph
from haystack.preprocessor.utils import fetch_archive_from_http


@pytest.mark.graph
@pytest.mark.graphdb
def test_graph_retrieval(retriever_with_docs, document_store_with_docs):
# doc_dir = "../data/tutorial10_knowledge_graph/"
# s3_url = "https://fandom-qa.s3-eu-west-1.amazonaws.com/triples_and_config.zip"
# fetch_archive_from_http(url=s3_url, output_dir=doc_dir)
#
# # Fetch a pre-trained BART model that translates natural language questions to SPARQL queries
# doc_dir = "../saved_models/tutorial10_knowledge_graph/"
# s3_url = "https://fandom-qa.s3-eu-west-1.amazonaws.com/saved_models/hp_v3.4.zip"
# fetch_archive_from_http(url=s3_url, output_dir=doc_dir)

# status = subprocess.run(
# ['docker run -d -p 7200:7200 --name graphdb-instance-tutorial docker-registry.ontotext.com/graphdb-free:9.4.1-adoptopenjdk11'], shell=True
# )
# TODO rename doc_dir
doc_dir = "../data/tutorial10_knowledge_graph/"
s3_url = "https://fandom-qa.s3-eu-west-1.amazonaws.com/triples_and_config.zip"
fetch_archive_from_http(url=s3_url, output_dir=doc_dir)

# Fetch a pre-trained BART model that translates natural language questions to SPARQL queries
doc_dir = "../saved_models/tutorial10_knowledge_graph/"
s3_url = "https://fandom-qa.s3-eu-west-1.amazonaws.com/saved_models/hp_v3.4.zip"
fetch_archive_from_http(url=s3_url, output_dir=doc_dir)

kg = GraphDBKnowledgeGraph(index="tutorial_10_index")
kg.create_index(config_path=Path("../data/tutorial10_knowledge_graph/repo-config.ttl"))
Expand Down

0 comments on commit cd26ab3

Please sign in to comment.