Skip to content

Commit

Permalink
Merge pull request #157 from tetherless-world/unrewrite_bnodes_on_nan…
Browse files Browse the repository at this point in the history
…opub_load

Unrewrite bnodes on nanopub load
  • Loading branch information
jpmccu authored Nov 26, 2019
2 parents 3c8a221 + 066ba0d commit 89cf0d1
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 46 deletions.
27 changes: 18 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,24 @@ def run_importer(self, entity_name):
self,
update_listener=self.nanopub_update_listener)

_file_depot = None
@property
def file_depot(self):
if self._file_depot is None:
if DepotManager.get('files') is None:
DepotManager.configure('files', self.config['file_archive'])
self._file_depot = DepotManager.get('files')
return self._file_depot

_nanopub_depot = None
@property
def nanopub_depot(self):
if self._nanopub_depot is None:
if DepotManager.get('nanopublications') is None:
DepotManager.configure('nanopublications', self.config['nanopub_archive'])
self._nanopub_depot = DepotManager.get('nanopublications')
return self._nanopub_depot

def configure_database(self):
"""
Database configuration should be set here
Expand All @@ -244,15 +262,6 @@ def configure_database(self):
self.security = Security(self, self.datastore,
register_form=ExtendedRegisterForm)

self.file_depot = DepotManager.get('files')
if self.file_depot is None:
DepotManager.configure('files', self.config['file_archive'])
self.file_depot = DepotManager.get('files')
self.nanopub_depot = DepotManager.get('nanopublications')
if self.nanopub_depot is None:
DepotManager.configure('nanopublications', self.config['nanopub_archive'])
self.nanopub_depot = DepotManager.get('nanopublications')

def __weighted_route(self, *args, **kwargs):
"""
Override the match_compare_key function of the Rule created by invoking Flask.route.
Expand Down
12 changes: 10 additions & 2 deletions tests/api/test_nanopub_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,24 @@ def test_create(self):

def test_bnode_rewrite(self):
self.login_new_user()
self.app.config['BNODE_REWRITE'] = True
response = self.post_nanopub(data=PERSON_INSTANCE_BNODE_TURTLE,
content_type="text/turtle",
expected_headers=["Location"])

rewritten_bnode_subjects = [s for s,p,o,context in self.app.db.quads()
if isinstance(s, URIRef) and s.startswith('bnode:')]
bnode_subjects = [s for s,p,o,context in self.app.db.quads() if isinstance(s, BNode)]
self.assertEquals(len(rewritten_bnode_subjects),1)
self.assertEquals(len(bnode_subjects), 0)

# Nanopublication bnodes should, when retrieved, get turned back into bnodes
nanopub = self.app.nanopub_manager.get(URIRef(response.headers['Location']))
rewritten_bnode_subjects = [s for s,p,o,context in nanopub.quads()
if isinstance(s, URIRef) and s.startswith('bnode:')]
bnode_subjects = [s for s,p,o,context in nanopub.quads() if isinstance(s, BNode)]
self.assertEquals(len(rewritten_bnode_subjects),1)
self.assertEquals(len(bnode_subjects), 0)
self.assertEquals(len(rewritten_bnode_subjects),0)
self.assertEquals(len(bnode_subjects), 1)

def test_no_bnode_rewrite(self):
self.app.config['BNODE_REWRITE'] = False
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/whyis_test/autonomic/test_frir_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def test_basic_file_storage(self):

content = self.client.get("/about",query_string={"uri":manifestation},
follow_redirects=True)
# This isn't going to work until we untangle the app
# configured by manage.py from the app that's configured for
# running the tests. The get_entity blueprint seems to be
# using the manage.py app.

self.assertEquals(content.mimetype, "application/n-quads")
digest = hashlib.sha256(content.data).hexdigest().lstrip('0')
self.assertEquals(pmanif[digest], manifestation)
Expand Down
8 changes: 4 additions & 4 deletions whyis/blueprint/entity/get_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ def view(name=None, format=None, view=None):
# 'view' is the default view
fileid = resource.value(current_app.NS.whyis.hasFileID)
if fileid is not None and 'view' not in request.args:
print (resource.identifier, fileid)
fileid = fileid.value
f = None
if current_app.file_depot.exists(fileid):
f = current_app.file_depot.get(fileid)
elif current_app.nanopub_depot.exists(fileid):
if current_app.nanopub_depot.exists(fileid):
f = current_app.nanopub_depot.get(fileid)
elif current_app.file_depot.exists(fileid):
f = current_app.file_depot.get(fileid)
if f is not None:
fsa = FileServeApp(f, current_app.config["file_archive"].get("cache_max_age",3600*24*7))
return fsa
Expand Down
10 changes: 8 additions & 2 deletions whyis/nanopub/nanopublication_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def skolemize(x):
return x
for store in stores:
for s, p, o, c in rdflib.ConjunctiveGraph(store).quads():
if 'BNODE_REWRITE' not in self.app.config or self.app.config['BNODE_REWRITE']:
if self.app.config.get('BNODE_REWRITE', False):
s = skolemize(s)
o = skolemize(o)
# predicates can't be bnodes, and contexts have already been rewritten.
Expand Down Expand Up @@ -244,6 +244,12 @@ def get(self, nanopub_uri, graph=None):
?np np:hasAssertion?|np:hasProvenance?|np:hasPublicationInfo? ?g.
graph ?g { ?s ?p ?o}
}''', initNs={'np':np}, initBindings={'np':nanopub_uri})
graph.addN(quads)
for s, p, o, g in quads:
if self.app.config.get('BNODE_REWRITE', False):
if isinstance(s, rdflib.URIRef) and s.startswith('bnode:'):
s = rdflib.BNode(s.replace('bnode:','',1))
if isinstance(o, rdflib.URIRef) and o.startswith('bnode:'):
o = rdflib.BNode(o.replace('bnode:','',1))
graph.add((s,p,o,g))
nanopub = Nanopublication(store=graph.store, identifier=nanopub_uri)
return nanopub
24 changes: 1 addition & 23 deletions whyis/test/integration_test_case.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,12 @@
import urllib.request, urllib.error, urllib.parse
from flask import Flask, g
from flask_testing import TestCase
from .test_case import TestCase
from flask_login import login_user, current_user, current_app
import requests


class IntegrationTestCase(TestCase):

def create_app(self):
from whyis.app_factory import app_factory
from depot.manager import DepotManager
import config_defaults

if 'admin_queryEndpoint' in config_defaults.Test:
del config_defaults.Test['admin_queryEndpoint']
del config_defaults.Test['admin_updateEndpoint']
del config_defaults.Test['knowledge_queryEndpoint']
del config_defaults.Test['knowledge_updateEndpoint']

# Default port is 5000
config_defaults.Test['LIVESERVER_PORT'] = 8943
# Default timeout is 5 seconds
config_defaults.Test['LIVESERVER_TIMEOUT'] = 10

application = app_factory(config_defaults.Test, config_defaults.project_name)
application.config['TESTING'] = True
application.config['WTF_CSRF_ENABLED'] = False

return application

def create_user(self, email, password, username="identifier", fn="First", ln="Last", roles='Admin'):
import commands
from uuid import uuid4
Expand Down
20 changes: 14 additions & 6 deletions whyis/test/test_case.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import flask_testing

from flask import current_app
from flask import Response
from rdflib import URIRef
from typing import Optional, Dict
from depot.manager import DepotManager

class TestCase(flask_testing.TestCase):

Expand Down Expand Up @@ -35,23 +37,29 @@ def create_app(self):
import config
except:
from whyis import config_defaults as config

if 'admin_queryEndpoint' in config.Test:
del config.Test['admin_queryEndpoint']
del config.Test['admin_updateEndpoint']
del config.Test['knowledge_queryEndpoint']
del config.Test['knowledge_updateEndpoint']

config.Test['TESTING'] = True
config.Test['WTF_CSRF_ENABLED'] = False
config.Test['nanopub_archive'] = {
'depot.backend' : 'depot.io.memory.MemoryFileStorage'
}
config.Test['DEFAULT_ANONYMOUS_READ'] = False
config.Test['file_archive'] = {
'depot.backend' : 'depot.io.memory.MemoryFileStorage'
}
# Default port is 5000
config.Test['LIVESERVER_PORT'] = 8943
# Default timeout is 5 seconds
config.Test['LIVESERVER_TIMEOUT'] = 10

from whyis.app_factory import app_factory
application = app_factory(config.Test, config.project_name)
application.config['TESTING'] = True
application.config['WTF_CSRF_ENABLED'] = False


return application

def create_user(self, email, password, username="identifier", fn="First", ln="Last", roles='Admin'):
Expand Down

0 comments on commit 89cf0d1

Please sign in to comment.