[PATCH 09 of 14 eac] Add tests on the relationships

Guillaume Vandevelde guillaume.vandevelde at logilab.fr
Tue Jul 2 14:53:58 CEST 2019


# HG changeset patch
# User Guillaume Vandevelde <gvandevelde at logilab.fr>
# Date 1561464350 -7200
#      Tue Jun 25 14:05:50 2019 +0200
# Node ID 045bcf5cd1aa7bbdf0821fad2099c344288db05c
# Parent  f79eb89597e6db8a371861a1895453323607a9b7
# Available At http://hg.logilab.org/review/cubes/eac
#              hg pull http://hg.logilab.org/review/cubes/eac -r 045bcf5cd1aa
Add tests on the relationships

This changeset include tests on the previously imported tags.

I saw later that the tests was bad implemented, and changed them.

Differential Revision: https://phab.logilab.fr/D3590

diff -r f79eb89597e6 -r 045bcf5cd1aa cubicweb_eac/dataimport.py
--- a/cubicweb_eac/dataimport.py	Mon Jun 24 17:37:24 2019 +0200
+++ b/cubicweb_eac/dataimport.py	Tue Jun 25 14:05:50 2019 +0200
@@ -75,6 +75,12 @@
         return dct
 
 
+def removekey(d, key):
+    r = dict(d)
+    del r[key]
+    return r
+
+
 def external_uri(uri):
     values = [text_type(uri)]
     return ExtEntity('ExternalUri', uri, {'uri': set(values), 'cwuri': set(values)})
@@ -306,20 +312,11 @@
             return trace_extentity(self)(attr)
         return attr
 
-    def check_maintenance_status(self, status):
-        if status and status in self.valid_maintenance_status:
-            return True
-        else:
-            return False
-
-    def check_publication_status(self, status):
-        if status and status in self.valid_publication_status:
-            return True
-        else:
-            return False
-
     def values_from_xpaths(self, elem, name_path_tuples, values={}):
-        """build a `values` dict from xpath requests"""
+        """build a `values` dict from xpath requests
+        partial is used for building a function with the signature
+        append_func :: dict -> (varname, xpath) -> dict
+        and use it in the reduce"""
         finder = partial(self._elem_find, elem)
         append_func = partial(safe_append, finder)
         return reduce(append_func, name_path_tuples, values)
@@ -647,7 +644,7 @@
                 event = self._elem_find(citem, 'eac:event')
                 if event is not None and event.text:
                     values['event'] = set([text_type(event.text)])
-                    if date is None and date_range:
+                    if date is None and date_range is not None:
                         date_range = self.parse_daterange(date_range)
                         values.update(date_range)
                     elif date is not None:
@@ -862,7 +859,10 @@
 
     @add_xml_wrap_for('EACFunctionRelation')
     def build_function_relation(self, elem):
-        """Build a relation between function entities"""
+        """Build a relation between function entities
+        yield an ExternalUri object, and an EACFunctionRelation
+        object that make the link between the ExternalUri and
+        the AuthorityRecord object"""
         relationship = elem.attrib.get('functionRelationType')
         if relationship is None:
             self.import_log.record_warning(self._(
@@ -876,6 +876,7 @@
                 'found a functionRelation without any object (no xlink:href '
                 'attribute), skipping'), line=elem.sourceline)
             return
+        # Yield the ExternalUri object
         yield external_uri(obj_uri)
         values = self.parse_tag_description(elem)
         dates = self.parse_daterange(
@@ -887,13 +888,17 @@
             'function_relation_function': set([text_type(obj_uri)]),
             'function_relation_agent': set([text_type(self.record.extid)]),
         })
-        new_values = self.values_from_xpaths(
+        values.update(self.values_from_xpaths(
             elem,
             (('place_entry', 'eac:placeEntry'),
-             ('relation_entry', 'eac:relationEntry')),
-            values)
-        #import pdb; pdb.set_trace()
-        yield ExtEntity('EACFunctionRelation', self._gen_extid(), new_values)
+             ('relation_entry', 'eac:relationEntry'))))
+        attrib = {k: v for (k, v) in elem.attrib.items()}
+        attrib = reduce(removekey, ('functionRelationType',
+                                    '{%(xlink)s}href' % self.namespaces),
+                        attrib)
+        if attrib:
+            values.update({'attributes': set([text_type(attrib)])})
+        yield ExtEntity('EACFunctionRelation', self._gen_extid(), values)
 
     @add_xml_wrap_for('EACResourceRelation')
     def build_resource_relation(self, elem):
@@ -1006,9 +1011,9 @@
              ('agency_name', 'eac:agencyName'),
              ('other_agency_code', 'eac:otherAgencyCode')),
             desc_value)
-        if self.check_maintenance_status(m_status):
+        if m_status in self.valid_maintenance_status:
             values.update({'maintenance_status': set([text_type(m_status)])})
-        if self.check_publication_status(p_status):
+        if p_status in self.valid_publication_status:
             values.update({'publication_status': set([text_type(p_status)])})
         yield ExtEntity('MaintenanceAg', self._gen_extid(), values)
 
diff -r f79eb89597e6 -r 045bcf5cd1aa test/data/FRAD033_EAC_00001_simplified.xml
--- a/test/data/FRAD033_EAC_00001_simplified.xml	Mon Jun 24 17:37:24 2019 +0200
+++ b/test/data/FRAD033_EAC_00001_simplified.xml	Tue Jun 25 14:05:50 2019 +0200
@@ -272,7 +272,10 @@
       </resourceRelation>
       <functionRelation
 	  functionRelationType="performs"
-	  xlink:href="http://gael.gironde.fr/ead.html?id=FRAD033_IR_N">
+	  xlink:href="http://gael.gironde.fr/ead.html?id=FRAD033_IR_N"
+	  xlink:actuate="onLoad"
+	  xlink:arcrole="http://test_arcrole.lol.com"
+	  xlink:role="http://test_role.lmao.com">
 	<relationEntry>Alumni communication
 	management, University of
 	Glasgow
diff -r f79eb89597e6 -r 045bcf5cd1aa test/data/no_maintenance_agency.xml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/data/no_maintenance_agency.xml	Tue Jun 25 14:05:50 2019 +0200
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE ead PUBLIC "+//ISBN 1-931666-00-8//DTD ead.dtd (Encoded Archival Description (EAD) Version 2002)//EN" "ead.dtd">
+<eac-cpf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="urn:isbn:1-931666-33-4 http://eac.staatsbibliothek-berlin.de/schema/cpf.xsd"
+         xmlns="urn:isbn:1-931666-33-4">
+  <control>
+    <maintenanceStatus>new</maintenanceStatus>
+
+    <publicationStatus>approved</publicationStatus>
+
+    <recordId>FRAD033_EAC_00001</recordId>
+  </control>
+  <cpfDescription>
+    <identity>
+      <entityId>22330001300016</entityId>
+      <entityType>corporateBody</entityType>
+    </identity>
+  </cpfDescription>
+
+</eac-cpf>
diff -r f79eb89597e6 -r 045bcf5cd1aa test/data/no_maintenance_status.xml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/data/no_maintenance_status.xml	Tue Jun 25 14:05:50 2019 +0200
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE ead PUBLIC "+//ISBN 1-931666-00-8//DTD ead.dtd (Encoded Archival Description (EAD) Version 2002)//EN" "ead.dtd">
+<eac-cpf xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="urn:isbn:1-931666-33-4 http://eac.staatsbibliothek-berlin.de/schema/cpf.xsd"
+         xmlns="urn:isbn:1-931666-33-4">
+  <control>
+    <publicationStatus>approved</publicationStatus>
+
+    <maintenanceAgency>
+      <agencyCode>FR-AD033</agencyCode>
+      <!-- Problème dans les identifiants des services d'archives : l'EAC-CPF ne prend que la forme FR-ADXXX -->
+      <agencyName>Gironde. Archives départementales</agencyName>
+    </maintenanceAgency>
+    <recordId>FRAD033_EAC_00001</recordId>
+  </control>
+  <cpfDescription>
+    <identity>
+      <entityId>22330001300016</entityId>
+      <entityType>corporateBody</entityType>
+    </identity>
+  </cpfDescription>
+
+</eac-cpf>
diff -r f79eb89597e6 -r 045bcf5cd1aa test/test_dataimport.py
--- a/test/test_dataimport.py	Mon Jun 24 17:37:24 2019 +0200
+++ b/test/test_dataimport.py	Tue Jun 25 14:05:50 2019 +0200
@@ -368,7 +368,8 @@
                                                  'id=FRAD033_IR_N']),
               'relation_entry': set([u'Alumni communication\n\tmanagement, '
                                      'University of\n\tGlasgow\n\t']),
-              'xml_wrap': ['<mods xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:isbn:1-931666-33-4" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.loc.gov/mods/v3 http:         //www.loc.gov/mods/v3/mods-3-3.xsd">\n\t    <titleInfo>\n\t      <title>Artisti trentini tra le due\n\t      guerre\n\t      </title>\n\t    </titleInfo>\n\t    <name>\n\t      <namePart type="given">Nicoletta\n\t      </namePart>\n\t      <namePart type="family">Boschiero\n\t      </namePart>\n\t      <role>\n\t\t<roleTerm type="text">autore\n\t\t</roleTerm>\n\t      </role>\n\t    </name>\n\t  </mods>\n\t'], # noqa
+              'xml_wrap': set(['<mods xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:isbn:1-931666-33-4" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.loc.gov/mods/v3 http:         //www.loc.gov/mods/v3/mods-3-3.xsd">\n\t    <titleInfo>\n\t      <title>Artisti trentini tra le due\n\t      guerre\n\t      </title>\n\t    </titleInfo>\n\t    <name>\n\t      <namePart type="given">Nicoletta\n\t      </namePart>\n\t      <namePart type="family">Boschiero\n\t      </namePart>\n\t      <role>\n\t\t<roleTerm type="text">autore\n\t\t</roleTerm>\n\t      </role>\n\t    </name>\n\t  </mods>\n\t']), # noqa
+              'attributes': set([u"{'{http://www.w3.org/1999/xlink}actuate': 'onLoad', '{http://www.w3.org/1999/xlink}arcrole': 'http://test_arcrole.lol.com', '{http://www.w3.org/1999/xlink}role': 'http://test_role.lmao.com'}"]) # noqa
              },
             ),
             ('EACFunctionRelation', _gen_extid(),
@@ -588,6 +589,12 @@
         self.assertEqual(rset.one().printable_value('agency_code',
                                                     format=u'text/plain').strip(),
                          set([u'FR-AD033']))
+        self.assertEqual(rset.one().printable_value('maintenance_status',
+                                                    format=u'text/plain').strip(),
+                         set([u'new']))
+        self.assertEqual(rset.one().printable_value('publication_status',
+                                                    format=u'text/plain').strip(),
+                         set([u'approved']))
 
     def _check_convention(self, cnx, record):
         rset = cnx.find('History', convention_of=record)
@@ -682,6 +689,29 @@
                          u'http://gael.gironde.fr/ead.html?id=FRAD033_IR_N')
         self.assertEqual(rrelation.xml_wrap.getvalue(),
                          '<he xmlns="urn:isbn:1-931666-33-4" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">joe</he>')  # noqa
+        rset = cnx.find('EACFunctionRelation', r_type=u'performs')
+        self.assertEqual(len(rset), 1)
+        func_relation = rset.one()
+        self.assertEqual(func_relation.attributes,
+                         u"{'{http://www.w3.org/1999/xlink}actuate': 'onLoad', '{http://www.w3.org/1999/xlink}arcrole': 'http://test_arcrole.lol.com', '{http://www.w3.org/1999/xlink}role': 'http://test_role.lmao.com'}") # noqa
+        self.assertEqual(func_relation.relation_entry,
+                         u'Alumni communication\n\tmanagement, '
+                         'University of\n\tGlasgow\n\t')
+        self.assertEqual(func_relation.xml_wrap.getvalue(),
+                         u'<mods xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:isbn:1-931666-33-4" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.loc.gov/mods/v3 http:         //www.loc.gov/mods/v3/mods-3-3.xsd">\n\t    <titleInfo>\n\t      <title>Artisti trentini tra le due\n\t      guerre\n\t      </title>\n\t    </titleInfo>\n\t    <name>\n\t      <namePart type="given">Nicoletta\n\t      </namePart>\n\t      <namePart type="family">Boschiero\n\t      </namePart>\n\t      <role>\n\t\t<roleTerm type="text">autore\n\t\t</roleTerm>\n\t      </role>\n\t    </name>\n\t  </mods>\n\t') # noqa
+        self.assertEqual(func_relation.function_relation_agent[0], record)
+        self.assertEqual(func_relation.function_relation_function[0].uri,
+                         u'http://gael.gironde.fr/ead.html?id=FRAD033_IR_N')
+        rset = cnx.find('EACFunctionRelation', r_type=u'controls')
+        self.assertEqual(len(rset), 1)
+        func_relation = rset.one()
+        self.assertEqual(func_relation.function_relation_agent[0], record)
+        self.assertEqual(func_relation.function_relation_function[0].uri,
+                         u'FRAD033_IR_N')
+        self.assertEqual(func_relation.start_date,
+                         datetime.date(1922, 1, 1))
+        self.assertEqual(func_relation.end_date,
+                         datetime.date(2001, 1, 1))
 
     def _check_equivalent_concept(self, cnx, record):
         functions = dict((f.name, f) for f in record.reverse_function_agent)
@@ -727,6 +757,17 @@
             self.assertEqual(cnx.find('AuthorityRecord').one().agent_kind[0].name,
                              'unknown-agent-kind')
 
+    def test_no_maintenance_tags(self):
+        with self.admin_access.repo_cnx() as cnx:
+            with self.assertRaises(dataimport.MissingTag) as cm:
+                testutils.eac_import(cnx, self.datapath('no_maintenance_agency.xml'))
+            self.assertEqual(cm.exception.tag, 'maintenanceAgency')
+            self.assertEqual(cm.exception.tag_parent, 'control')
+            with self.assertRaises(dataimport.MissingTag) as cm:
+                testutils.eac_import(cnx, self.datapath('no_maintenance_status.xml'))
+            self.assertEqual(cm.exception.tag, 'maintenanceStatus')
+            self.assertEqual(cm.exception.tag_parent, 'control')
+
     def test_no_name_entry(self):
         with self.admin_access.repo_cnx() as cnx:
             with self.assertRaises(dataimport.MissingTag) as cm:


More information about the saem-devel mailing list