Browse Source

All internal API tests pass.

Stefano Cossu 5 years ago
parent
commit
4abca97ef4

+ 1 - 1
lakesuperior/model/ldp/ldp_factory.py

@@ -15,7 +15,7 @@ from lakesuperior.dictionaries.namespaces import ns_collection as nsc
 from lakesuperior.exceptions import (
         IncompatibleLdpTypeError, InvalidResourceError, ResourceExistsError,
         ResourceNotExistsError, TombstoneError)
-from lakesuperior.model.rdf.graph import Graph
+from lakesuperior.model.rdf.graph import Graph, from_rdf
 
 
 LDP_NR_TYPE = nsc['ldp'].NonRDFSource

+ 6 - 5
lakesuperior/model/ldp/ldpr.py

@@ -12,8 +12,9 @@ from urllib.parse import urldefrag
 from uuid import uuid4
 
 import arrow
+import rdflib
 
-from rdflib import Graph, URIRef, Literal
+from rdflib import URIRef, Literal
 from rdflib.compare import to_isomorphic
 from rdflib.namespace import RDF
 
@@ -675,15 +676,15 @@ class Ldpr(metaclass=ABCMeta):
         qry_str = (
                 re.sub('<#([^>]+)>', '<{}#\\1>'.format(self.uri), qry_str)
                 .replace('<>', '<{}>'.format(self.uri)))
-        pre_gr = self.imr.as_rdflib().graph
-        post_gr = Graph(identifier=self.uri)
+        pre_gr = self.imr.as_rdflib()
+        post_gr = rdflib.Graph(identifier=self.uri)
         post_gr |= pre_gr
 
         post_gr.update(qry_str)
 
         # FIXME Fix and  use SimpleGraph's native subtraction operation.
-        remove_gr = self.check_mgd_terms(SimpleGraph(set(pre_gr - post_gr)))
-        add_gr = self.check_mgd_terms(SimpleGraph(set(post_gr - pre_gr)))
+        remove_gr = self.check_mgd_terms(Graph(data=set(pre_gr - post_gr)))
+        add_gr = self.check_mgd_terms(Graph(data=set(post_gr - pre_gr)))
 
         return remove_gr, add_gr
 

+ 2 - 2
lakesuperior/model/rdf/graph.pyx

@@ -292,7 +292,7 @@ cdef class Graph:
         if isinstance(item, slice):
             s, p, o = item.start, item.stop, item.step
             return self._slice(s, p, o)
-        elif self.uri and isinstance(item, rdflib.Node):
+        elif self.uri and isinstance(item, rdflib.term.Identifier):
             # If a Node is given, return all values for that predicate.
             return self._slice(self.uri, item, None)
         else:
@@ -427,7 +427,7 @@ cdef class Graph:
 
         :rtype: rdflib.Graph
         """
-        gr = Graph(identifier=self.uri)
+        gr = rdflib.Graph(identifier=self.uri)
         for trp in self.data:
             gr.add(trp)
 

+ 93 - 74
tests/2_api/test_resource_api.py

@@ -130,7 +130,8 @@ class TestResourceCRUD:
                 uid, stream=BytesIO(data), mimetype='text/plain')
 
         rsrc = rsrc_api.get(uid)
-        assert rsrc.content.read() == data
+        with rsrc.imr.store.txn_ctx():
+            assert rsrc.content.read() == data
 
 
     def test_replace_rsrc(self):
@@ -179,10 +180,11 @@ class TestResourceCRUD:
         uid_rs = '/test_incomp_rs'
         uid_nr = '/test_incomp_nr'
         data = b'mock binary content'
-        gr = from_rdf(
-            data='<> a <http://ex.org/type#A> .', format='turtle',
-            publicID=nsc['fcres'][uid_rs]
-        )
+        with env.app_globals.rdf_store.txn_ctx():
+            gr = from_rdf(
+                data='<> a <http://ex.org/type#A> .', format='turtle',
+                publicID=nsc['fcres'][uid_rs]
+            )
 
         rsrc_api.create_or_replace(uid_rs, graph=gr)
         rsrc_api.create_or_replace(
@@ -216,17 +218,18 @@ class TestResourceCRUD:
             (URIRef(uri), nsc['rdf'].type, nsc['foaf'].Organization),
         }
 
-        gr = Graph()
-        gr += init_trp
+        with env.app_globals.rdf_store.txn_ctx():
+            gr = Graph(data=init_trp)
         rsrc_api.create_or_replace(uid, graph=gr)
         rsrc_api.update_delta(uid, remove_trp, add_trp)
         rsrc = rsrc_api.get(uid)
 
-        assert rsrc.imr[
-                rsrc.uri : nsc['rdf'].type : nsc['foaf'].Organization]
-        assert rsrc.imr[rsrc.uri : nsc['foaf'].name : Literal('Joe Bob')]
-        assert not rsrc.imr[
-                rsrc.uri : nsc['rdf'].type : nsc['foaf'].Person]
+        with env.app_globals.rdf_store.txn_ctx():
+            assert rsrc.imr[
+                    rsrc.uri : nsc['rdf'].type : nsc['foaf'].Organization]
+            assert rsrc.imr[rsrc.uri : nsc['foaf'].name : Literal('Joe Bob')]
+            assert not rsrc.imr[
+                    rsrc.uri : nsc['rdf'].type : nsc['foaf'].Person]
 
 
     def test_delta_update_wildcard(self):
@@ -248,20 +251,21 @@ class TestResourceCRUD:
             (URIRef(uri), nsc['foaf'].name, Literal('Joan Knob')),
         }
 
-        gr = Graph()
-        gr += init_trp
+        with env.app_globals.rdf_store.txn_ctx():
+            gr = Graph(data=init_trp)
         rsrc_api.create_or_replace(uid, graph=gr)
         rsrc_api.update_delta(uid, remove_trp, add_trp)
         rsrc = rsrc_api.get(uid)
 
-        assert rsrc.imr[
-                rsrc.uri : nsc['rdf'].type : nsc['foaf'].Person]
-        assert rsrc.imr[rsrc.uri : nsc['foaf'].name : Literal('Joan Knob')]
-        assert not rsrc.imr[rsrc.uri : nsc['foaf'].name : Literal('Joe Bob')]
-        assert not rsrc.imr[
-            rsrc.uri : nsc['foaf'].name : Literal('Joe Average Bob')]
-        assert not rsrc.imr[
-            rsrc.uri : nsc['foaf'].name : Literal('Joe 12oz Bob')]
+        with env.app_globals.rdf_store.txn_ctx():
+            assert rsrc.imr[
+                    rsrc.uri : nsc['rdf'].type : nsc['foaf'].Person]
+            assert rsrc.imr[rsrc.uri : nsc['foaf'].name : Literal('Joan Knob')]
+            assert not rsrc.imr[rsrc.uri : nsc['foaf'].name : Literal('Joe Bob')]
+            assert not rsrc.imr[
+                rsrc.uri : nsc['foaf'].name : Literal('Joe Average Bob')]
+            assert not rsrc.imr[
+                rsrc.uri : nsc['foaf'].name : Literal('Joe 12oz Bob')]
 
 
     def test_sparql_update(self):
@@ -285,19 +289,20 @@ class TestResourceCRUD:
         ver_uid = rsrc_api.create_version(uid, 'v1').split('fcr:versions/')[-1]
 
         rsrc = rsrc_api.update(uid, update_str)
-        assert (
-            (rsrc.uri, nsc['dcterms'].title, Literal('Original title.'))
-            not in set(rsrc.imr))
-        assert (
-            (rsrc.uri, nsc['dcterms'].title, Literal('Title #2.'))
-            in set(rsrc.imr))
-        assert (
-            (rsrc.uri, nsc['dcterms'].title, Literal('Title #3.'))
-            in set(rsrc.imr))
-        assert ((
-                URIRef(str(rsrc.uri) + '#h1'),
-                nsc['dcterms'].title, Literal('This is a hash.'))
-            in set(rsrc.imr))
+        with env.app_globals.rdf_store.txn_ctx():
+            assert (
+                (rsrc.uri, nsc['dcterms'].title, Literal('Original title.'))
+                not in set(rsrc.imr))
+            assert (
+                (rsrc.uri, nsc['dcterms'].title, Literal('Title #2.'))
+                in set(rsrc.imr))
+            assert (
+                (rsrc.uri, nsc['dcterms'].title, Literal('Title #3.'))
+                in set(rsrc.imr))
+            assert ((
+                    URIRef(str(rsrc.uri) + '#h1'),
+                    nsc['dcterms'].title, Literal('This is a hash.'))
+                in set(rsrc.imr))
 
 
     def test_create_ldp_dc_post(self, dc_rdf):
@@ -310,8 +315,9 @@ class TestResourceCRUD:
 
         member_rsrc = rsrc_api.get('/member')
 
-        assert nsc['ldp'].Container in dc_rsrc.ldp_types
-        assert nsc['ldp'].DirectContainer in dc_rsrc.ldp_types
+        with env.app_globals.rdf_store.txn_ctx():
+            assert nsc['ldp'].Container in dc_rsrc.ldp_types
+            assert nsc['ldp'].DirectContainer in dc_rsrc.ldp_types
 
 
     def test_create_ldp_dc_put(self, dc_rdf):
@@ -324,8 +330,9 @@ class TestResourceCRUD:
 
         member_rsrc = rsrc_api.get('/member')
 
-        assert nsc['ldp'].Container in dc_rsrc.ldp_types
-        assert nsc['ldp'].DirectContainer in dc_rsrc.ldp_types
+        with env.app_globals.rdf_store.txn_ctx():
+            assert nsc['ldp'].Container in dc_rsrc.ldp_types
+            assert nsc['ldp'].DirectContainer in dc_rsrc.ldp_types
 
 
     def test_add_dc_member(self, dc_rdf):
@@ -339,8 +346,9 @@ class TestResourceCRUD:
         child_uid = rsrc_api.create(dc_uid, None).uid
         member_rsrc = rsrc_api.get('/member')
 
-        assert member_rsrc.imr[
-            member_rsrc.uri: nsc['dcterms'].relation: nsc['fcres'][child_uid]]
+        with env.app_globals.rdf_store.txn_ctx():
+            assert member_rsrc.imr[
+                member_rsrc.uri: nsc['dcterms'].relation: nsc['fcres'][child_uid]]
 
 
     def test_indirect_container(self, ic_rdf):
@@ -362,15 +370,17 @@ class TestResourceCRUD:
                 member_uid, rdf_data=ic_member_rdf, rdf_fmt='turtle')
 
         ic_rsrc = rsrc_api.get(ic_uid)
-        assert nsc['ldp'].Container in ic_rsrc.ldp_types
-        assert nsc['ldp'].IndirectContainer in ic_rsrc.ldp_types
-        assert nsc['ldp'].DirectContainer not in ic_rsrc.ldp_types
+        with env.app_globals.rdf_store.txn_ctx():
+            assert nsc['ldp'].Container in ic_rsrc.ldp_types
+            assert nsc['ldp'].IndirectContainer in ic_rsrc.ldp_types
+            assert nsc['ldp'].DirectContainer not in ic_rsrc.ldp_types
 
         member_rsrc = rsrc_api.get(member_uid)
         top_cont_rsrc = rsrc_api.get(cont_uid)
-        assert top_cont_rsrc.imr[
-            top_cont_rsrc.uri: nsc['dcterms'].relation:
-            nsc['fcres'][target_uid]]
+        with env.app_globals.rdf_store.txn_ctx():
+            assert top_cont_rsrc.imr[
+                top_cont_rsrc.uri: nsc['dcterms'].relation:
+                nsc['fcres'][target_uid]]
 
 
 
@@ -400,7 +410,8 @@ class TestAdvancedDelete:
         rsrc_api.resurrect(uid)
 
         rsrc = rsrc_api.get(uid)
-        assert nsc['ldp'].Resource in rsrc.ldp_types
+        with env.app_globals.rdf_store.txn_ctx():
+            assert nsc['ldp'].Resource in rsrc.ldp_types
 
 
     def test_hard_delete(self):
@@ -444,10 +455,12 @@ class TestAdvancedDelete:
         uid = '/test_soft_delete_children01'
         rsrc_api.resurrect(uid)
         parent_rsrc = rsrc_api.get(uid)
-        assert nsc['ldp'].Resource in parent_rsrc.ldp_types
+        with env.app_globals.rdf_store.txn_ctx():
+            assert nsc['ldp'].Resource in parent_rsrc.ldp_types
         for i in range(3):
             child_rsrc = rsrc_api.get('{}/child{}'.format(uid, i))
-            assert nsc['ldp'].Resource in child_rsrc.ldp_types
+            with env.app_globals.rdf_store.txn_ctx():
+                assert nsc['ldp'].Resource in child_rsrc.ldp_types
 
 
     def test_hard_delete_children(self):
@@ -526,24 +539,26 @@ class TestResourceVersioning:
         rsrc_api.create_or_replace(uid, rdf_data=rdf_data, rdf_fmt='turtle')
         ver_uid = rsrc_api.create_version(uid, 'v1').split('fcr:versions/')[-1]
         #FIXME Without this, the test fails.
-        set(rsrc_api.get_version(uid, ver_uid))
+        #set(rsrc_api.get_version(uid, ver_uid))
 
         rsrc_api.update(uid, update_str)
         current = rsrc_api.get(uid)
-        assert (
-            (current.uri, nsc['dcterms'].title, Literal('Title #2.'))
-            in current.imr)
-        assert (
-            (current.uri, nsc['dcterms'].title, Literal('Original title.'))
-            not in current.imr)
+        with env.app_globals.rdf_store.txn_ctx():
+            assert (
+                (current.uri, nsc['dcterms'].title, Literal('Title #2.'))
+                in current.imr)
+            assert (
+                (current.uri, nsc['dcterms'].title, Literal('Original title.'))
+                not in current.imr)
 
         v1 = rsrc_api.get_version(uid, ver_uid)
-        assert (
-            (v1.uri, nsc['dcterms'].title, Literal('Original title.'))
-            in set(v1))
-        assert (
-            (v1.uri, nsc['dcterms'].title, Literal('Title #2.'))
-            not in set(v1))
+        with env.app_globals.rdf_store.txn_ctx():
+            assert (
+                (v1.uri, nsc['dcterms'].title, Literal('Original title.'))
+                in set(v1))
+            assert (
+                (v1.uri, nsc['dcterms'].title, Literal('Title #2.'))
+                    not in set(v1))
 
 
     def test_revert_to_version(self):
@@ -556,9 +571,10 @@ class TestResourceVersioning:
         ver_uid = 'v1'
         rsrc_api.revert_to_version(uid, ver_uid)
         rev = rsrc_api.get(uid)
-        assert (
-            (rev.uri, nsc['dcterms'].title, Literal('Original title.'))
-            in rev.imr)
+        with env.app_globals.rdf_store.txn_ctx():
+            assert (
+                (rev.uri, nsc['dcterms'].title, Literal('Original title.'))
+                in rev.imr)
 
 
     def test_versioning_children(self):
@@ -580,18 +596,21 @@ class TestResourceVersioning:
         rsrc_api.create_or_replace(ch1_uid)
         ver_uid = rsrc_api.create_version(uid, ver_uid).split('fcr:versions/')[-1]
         rsrc = rsrc_api.get(uid)
-        assert nsc['fcres'][ch1_uid] in rsrc.imr[
-                rsrc.uri : nsc['ldp'].contains]
+        with env.app_globals.rdf_store.txn_ctx():
+            assert nsc['fcres'][ch1_uid] in rsrc.imr[
+                    rsrc.uri : nsc['ldp'].contains]
 
         rsrc_api.create_or_replace(ch2_uid)
         rsrc = rsrc_api.get(uid)
-        assert nsc['fcres'][ch2_uid] in rsrc.imr[
-                rsrc.uri : nsc['ldp'].contains]
+        with env.app_globals.rdf_store.txn_ctx():
+            assert nsc['fcres'][ch2_uid] in rsrc.imr[
+                    rsrc.uri : nsc['ldp'].contains]
 
         rsrc_api.revert_to_version(uid, ver_uid)
         rsrc = rsrc_api.get(uid)
-        assert nsc['fcres'][ch1_uid] in rsrc.imr[
-                rsrc.uri : nsc['ldp'].contains]
-        assert nsc['fcres'][ch2_uid] in rsrc.imr[
-                rsrc.uri : nsc['ldp'].contains]
+        with env.app_globals.rdf_store.txn_ctx():
+            assert nsc['fcres'][ch1_uid] in rsrc.imr[
+                    rsrc.uri : nsc['ldp'].contains]
+            assert nsc['fcres'][ch2_uid] in rsrc.imr[
+                    rsrc.uri : nsc['ldp'].contains]