Skip to content

Commit

Permalink
Merge pull request #25 from fair-workflows/use-test-endpoint
Browse files Browse the repository at this point in the history
Use test endpoint
  • Loading branch information
raar1 authored Oct 30, 2020
2 parents e425bb9 + ebd12b1 commit 7b7f82e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 27 deletions.
11 changes: 11 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import pytest
import requests

from nanopub.nanopub import NANOPUB_TEST_GRLC_URL


def pytest_addoption(parser):
parser.addoption('--no_rsa_key', action='store_true', default=False,
help="enable no_rsa_key decorated tests")
Expand All @@ -6,3 +12,8 @@ def pytest_addoption(parser):
def pytest_configure(config):
if not config.option.no_rsa_key:
setattr(config.option, 'markexpr', 'not no_rsa_key')


skip_if_nanopub_server_unavailable = (
pytest.mark.skipif(requests.get(NANOPUB_TEST_GRLC_URL).status_code != 200,
reason='Nanopub server is unavailable'))
26 changes: 24 additions & 2 deletions nanopub/java_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@
from typing import Union

import rdflib
import requests

from nanopub.definitions import PKG_FILEPATH

# Location of nanopub tool (currently shipped along with the lib)
NANOPUB_SCRIPT = str(PKG_FILEPATH / 'np')
NANOPUB_TEST_SERVER = 'http://test-server.nanopubs.lod.labs.vu.nl/'


class JavaWrapper:
"""
Wrapper around 'np' java tool that is used to sign and publish nanopublications to
a nanopub server.
"""
def __init__(self, use_test_server=False):
"""Construct JavaWrapper.
Args:
use_test_server: Toggle using the test nanopub server.
"""
self.use_test_server = use_test_server

@staticmethod
def _run_command(command):
result = subprocess.run(command, shell=True, stderr=subprocess.PIPE)
Expand All @@ -33,7 +43,20 @@ def sign(self, unsigned_file: Union[str, Path]) -> str:
return self._get_signed_file(unsigned_file)

def publish(self, signed: str):
self._run_command(f'{NANOPUB_SCRIPT} publish ' + signed)
""" Publish.
Publish the signed nanopub to the nanopub server. Publishing to the real server depends
on nanopub-java, for the test server we do a simple POST request.
TODO: Use nanopub-java for publishing to test once it supports it.
"""
if self.use_test_server:
headers = {'content-type': 'application/x-www-form-urlencoded'}
with open(signed, 'rb') as data:
r = requests.post(NANOPUB_TEST_SERVER, headers=headers, data=data)
r.raise_for_status()
else:
self._run_command(f'{NANOPUB_SCRIPT} publish ' + signed)
return self.extract_nanopub_url(signed)

@staticmethod
Expand All @@ -47,5 +70,4 @@ def extract_nanopub_url(signed: Union[str, Path]):
@staticmethod
def _get_signed_file(unsigned_file: str):
unsigned_file = Path(unsigned_file)

return str(unsigned_file.parent / f'signed.{unsigned_file.name}')
16 changes: 13 additions & 3 deletions nanopub/nanopub.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"https://grlc.nanopubs.knows.idlab.ugent.be/api/local/local/",
"http://grlc.np.scify.org/api/local/local/",
"http://grlc.np.dumontierlab.com/api/local/local/"]
NANOPUB_TEST_GRLC_URL = 'http://test-grlc.nanopubs.lod.labs.vu.nl/api/local/local/'
DEFAULT_URI = 'http://purl.org/nanopub/temp/mynanopub'


Expand Down Expand Up @@ -206,9 +207,18 @@ class NanopubClient:
Provides utility functions for searching, creating and publishing RDF graphs
as assertions in a nanopublication.
"""
def __init__(self):
self.java_wrapper = JavaWrapper()
self.grlc_urls = NANOPUB_GRLC_URLS
def __init__(self, use_test_server=False):
"""Construct NanopubClient.
Args:
use_test_server: Toggle using the test nanopub server.
"""
self.use_test_server = use_test_server
self.java_wrapper = JavaWrapper(use_test_server=use_test_server)
if use_test_server:
self.grlc_urls = [NANOPUB_TEST_GRLC_URL]
else:
self.grlc_urls = NANOPUB_GRLC_URLS

def find_nanopubs_with_text(self, text, max_num_results=1000):
"""
Expand Down
9 changes: 9 additions & 0 deletions tests/test_java_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest

from conftest import skip_if_nanopub_server_unavailable
from nanopub.definitions import TEST_RESOURCES_FILEPATH
from nanopub.java_wrapper import JavaWrapper

Expand Down Expand Up @@ -49,3 +50,11 @@ def test_sign_nanopub_no_rsa_key(tmp_path):
with pytest.raises(RuntimeError) as e:
java_wrapper.sign(unsigned_file=temp_unsigned_file)
assert 'RSA key appears to be missing' in str(e.value)


@pytest.mark.flaky(max_runs=10)
@skip_if_nanopub_server_unavailable
def test_publish():
java_wrapper = JavaWrapper(use_test_server=True)
pubinfo = java_wrapper.publish(NANOPUB_SAMPLE_SIGNED)
assert pubinfo
46 changes: 24 additions & 22 deletions tests/test_nanopub.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
from unittest import mock
from unittest.mock import patch

import pytest
import rdflib
import requests
from rdflib.namespace import RDF

from nanopub import NanopubClient, namespaces
from nanopub.nanopub import Nanopub
from conftest import skip_if_nanopub_server_unavailable
from nanopub import NanopubClient, namespaces, Nanopub

skip_if_nanopub_server_unavailable = (
pytest.mark.skipif(requests.get('http://grlc.nanopubs.lod.labs.vu.nl/').status_code != 200,
reason='Nanopub server is unavailable'))

client = NanopubClient()
client = NanopubClient(use_test_server=True)

def test_nanopub_construction_with_bnode_introduced_concept():
"""
Expand All @@ -34,13 +28,14 @@ def test_nanopub_construction_with_bnode_introduced_concept():
)
assert str(nanopub.introduces_concept) == test_concept_uri


@pytest.mark.flaky(max_runs=10)
@skip_if_nanopub_server_unavailable
def test_find_nanopubs_with_text():
"""
Check that Nanopub text search is returning results for a few common search terms
"""
searches = ['fair', 'heart']
searches = ['test', 'US']

for search in searches:
results = client.find_nanopubs_with_text(search)
Expand All @@ -49,15 +44,28 @@ def test_find_nanopubs_with_text():
assert len(client.find_nanopubs_with_text('')) == 0


@pytest.mark.flaky(max_runs=10)
def test_find_nanopubs_with_text_prod():
"""
Check that Nanopub text search is returning results for a few common search terms on the
production nanopub server
"""
prod_client = NanopubClient()
searches = ['test', 'US']
for search in searches:
results = prod_client.find_nanopubs_with_text(search)
assert len(results) > 0


@pytest.mark.flaky(max_runs=10)
@skip_if_nanopub_server_unavailable
def test_find_nanopubs_with_pattern():
"""
Check that Nanopub pattern search is returning results
"""
searches = [
('', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'https://www.omg.org/spec/BPMN/scriptTask'),
('http://purl.org/np/RANhYfdZCVDQr8ItxDYCZWhvBhzjJTs9Cq-vPnmSBDd5g', '', '')
('', 'http://example.org/transmits', 'http://example.org/malaria'),
('http://purl.org/np/RA8ui7ddvV25m1qdyxR4lC8q8-G0yb3SN8AC0Bu5q8Yeg', '', '')
]

for subj, pred, obj in searches:
Expand All @@ -68,18 +76,12 @@ def test_find_nanopubs_with_pattern():

@pytest.mark.flaky(max_runs=10)
@skip_if_nanopub_server_unavailable
def test_nanopub_search_things():
def test_nanopub_find_things():
"""
Check that Nanopub 'find_things' search is returning results
Check that Nanopub 'find_things' search is returning results
"""
searches = [
'http://dkm.fbk.eu/index.php/BPMN2_Ontology#ManualTask',
'http://purl.org/net/p-plan#Plan'
]

for thing_type in searches:
results = client.find_things(type=thing_type)
assert len(results) > 0
results = client.find_things(type='http://purl.org/net/p-plan#Plan')
assert len(results) > 0

with pytest.raises(Exception):
client.find_things()
Expand Down

0 comments on commit 7b7f82e

Please sign in to comment.