diff --git a/main.py b/main.py index 48c57ceb..1fed69c7 100644 --- a/main.py +++ b/main.py @@ -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 @@ -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. diff --git a/tests/api/test_nanopub_crud.py b/tests/api/test_nanopub_crud.py index 0f3c09a5..bf671e60 100644 --- a/tests/api/test_nanopub_crud.py +++ b/tests/api/test_nanopub_crud.py @@ -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 diff --git a/tests/unit/whyis_test/autonomic/test_frir_agent.py b/tests/unit/whyis_test/autonomic/test_frir_agent.py index 4b60a896..77694ac8 100644 --- a/tests/unit/whyis_test/autonomic/test_frir_agent.py +++ b/tests/unit/whyis_test/autonomic/test_frir_agent.py @@ -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) diff --git a/whyis/blueprint/entity/get_entity.py b/whyis/blueprint/entity/get_entity.py index 00228171..1ecc2274 100644 --- a/whyis/blueprint/entity/get_entity.py +++ b/whyis/blueprint/entity/get_entity.py @@ -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 diff --git a/whyis/nanopub/nanopublication_manager.py b/whyis/nanopub/nanopublication_manager.py index bf1888b5..39cf126e 100644 --- a/whyis/nanopub/nanopublication_manager.py +++ b/whyis/nanopub/nanopublication_manager.py @@ -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. @@ -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 diff --git a/whyis/test/integration_test_case.py b/whyis/test/integration_test_case.py index 868a4eb4..1a68c6de 100644 --- a/whyis/test/integration_test_case.py +++ b/whyis/test/integration_test_case.py @@ -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 diff --git a/whyis/test/test_case.py b/whyis/test/test_case.py index 9e4712ac..0064b81a 100644 --- a/whyis/test/test_case.py +++ b/whyis/test/test_case.py @@ -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): @@ -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'):