瀏覽代碼

Merge pull request #60 from scossu/fix_22

Fix flaw in creating LDP-IC members; add tests for LDP-DC & LDP-IC.
Stefano Cossu 7 年之前
父節點
當前提交
1ad625152b
共有 2 個文件被更改,包括 102 次插入6 次删除
  1. 2 6
      lakesuperior/model/ldpr.py
  2. 100 0
      tests/test_resource_api.py

+ 2 - 6
lakesuperior/model/ldpr.py

@@ -883,16 +883,12 @@ class Ldpr(metaclass=ABCMeta):
             s = cont_rsrc.metadata.value(cont_rsrc.uri, self.MBR_RSRC_URI)
             p = cont_rsrc.metadata.value(cont_rsrc.uri, self.MBR_REL_URI)
 
-            if cont_rsrc.metadata[RDF.type: nsc['ldp'].DirectContainer]:
+            if nsc['ldp'].DirectContainer in cont_rsrc.ldp_types:
                 logger.info('Parent is a direct container.')
-
                 logger.debug('Creating DC triples.')
                 o = self.uri
 
-            elif (
-                    cont_rsrc.metadata[
-                        RDF.type: nsc['ldp'].IndirectContainer] and
-                    self.INS_CNT_REL_URI in cont_p):
+            elif nsc['ldp'].IndirectContainer in cont_rsrc.ldp_types:
                 logger.info('Parent is an indirect container.')
                 cont_rel_uri = cont_rsrc.metadata.value(
                     cont_rsrc.uri, self.INS_CNT_REL_URI)

+ 100 - 0
tests/test_resource_api.py

@@ -19,6 +19,31 @@ from lakesuperior.model.ldpr import Ldpr
 def random_uuid():
     return str(uuid.uuid4())
 
+@pytest.fixture
+def dc_rdf():
+    return b'''
+    PREFIX dcterms: <http://purl.org/dc/terms/>
+    PREFIX ldp: <http://www.w3.org/ns/ldp#>
+
+    <> dcterms:title "Direct Container" ;
+        ldp:membershipResource <info:fcres/member> ;
+        ldp:hasMemberRelation dcterms:relation .
+    '''
+
+
+@pytest.fixture
+def ic_rdf():
+    return b'''
+    PREFIX dcterms: <http://purl.org/dc/terms/>
+    PREFIX ldp: <http://www.w3.org/ns/ldp#>
+    PREFIX ore: <http://www.openarchives.org/ore/terms/>
+
+    <> dcterms:title "Indirect Container" ;
+        ldp:membershipResource <info:fcres/top_container> ;
+        ldp:hasMemberRelation dcterms:relation ;
+        ldp:insertedContentRelation ore:proxyFor .
+    '''
+
 
 @pytest.mark.usefixtures('db')
 class TestResourceApi:
@@ -225,3 +250,78 @@ class TestResourceApi:
             rsrc.uri : nsc['foaf'].name : Literal('Joe 12oz Bob')]
 
 
+    def test_create_ldp_dc_post(self, dc_rdf):
+        """
+        Create an LDP Direct Container via POST.
+        """
+        rsrc_api.create_or_replace('/member')
+        dc_uid = rsrc_api.create(
+                '/', 'test_dc_post', rdf_data=dc_rdf, rdf_fmt='turtle')
+
+        dc_rsrc = rsrc_api.get(dc_uid)
+        member_rsrc = rsrc_api.get('/member')
+
+        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):
+        """
+        Create an LDP Direct Container via PUT.
+        """
+        dc_uid = '/test_dc_put01'
+        rsrc_api.create_or_replace(
+                dc_uid, rdf_data=dc_rdf, rdf_fmt='turtle')
+
+        dc_rsrc = rsrc_api.get(dc_uid)
+        member_rsrc = rsrc_api.get('/member')
+
+        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):
+        """
+        Add members to a direct container and verify special properties.
+        """
+        dc_uid = '/test_dc_put02'
+        rsrc_api.create_or_replace(
+                dc_uid, rdf_data=dc_rdf, rdf_fmt='turtle')
+
+        dc_rsrc = rsrc_api.get(dc_uid)
+        child_uid = rsrc_api.create(dc_uid, None)
+        member_rsrc = rsrc_api.get('/member')
+
+        assert member_rsrc.imr[
+            member_rsrc.uri: nsc['dcterms'].relation: nsc['fcres'][child_uid]]
+
+
+    def test_indirect_container(self, ic_rdf):
+        """
+        Create an indirect container verify special properties.
+        """
+        cont_uid = '/top_container'
+        ic_uid = '{}/test_ic'.format(cont_uid)
+        member_uid = '{}/ic_member'.format(ic_uid)
+        target_uid = '/ic_target'
+        ic_member_rdf = b'''
+        PREFIX ore: <http://www.openarchives.org/ore/terms/>
+        <> ore:proxyFor <info:fcres/ic_target> .'''
+
+        rsrc_api.create_or_replace(cont_uid)
+        rsrc_api.create_or_replace(target_uid)
+        rsrc_api.create_or_replace(ic_uid, rdf_data=ic_rdf, rdf_fmt='turtle')
+        rsrc_api.create_or_replace(
+                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
+
+        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]]
+