[PATCH 3 of 8 sherpa] [test] Introduce testutils module providing some factory function

Sylvain Thenault sylvain.thenault at logilab.fr
Wed Mar 29 11:59:58 CEST 2017


# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1490205278 -3600
#      Wed Mar 22 18:54:38 2017 +0100
# Node ID ffdaa181f9e71a770ca4d5dd6a62c952bd65e0dc
# Parent  d851c4686604f7d5b3bfbcee08643fe096b681c9
[test] Introduce testutils module providing some factory function

easing creation of complex objects in tests.

diff --git a/test/test_security.py b/test/test_security.py
--- a/test/test_security.py
+++ b/test/test_security.py
@@ -18,10 +18,12 @@
 from contextlib import contextmanager
 
 from cubicweb import Unauthorized
 from cubicweb.devtools.testlib import CubicWebTC
 
+import testutils
+
 
 class SecurityTC(CubicWebTC):
 
     @contextmanager
     def assertUnauthorized(self, cnx):
@@ -58,14 +60,11 @@ class SecurityTC(CubicWebTC):
             entity.cw_delete()
             cnx.commit()
 
     def test_add_read_update_delete_record(self):
         with self.new_access('bob').cnx() as cnx:
-            agent_kind = cnx.find('AgentKind', name=u'person').one()
-            record = cnx.create_entity('AuthorityRecord', agent_kind=agent_kind)
-            cnx.create_entity('NameEntry', parts=u'bob notice', form_variant=u'authorized',
-                              name_entry_for=record)
+            testutils.create_authority_record(cnx, u'bob notice')
             cnx.commit()
 
         self.assertOwnershipBasedAccess('AuthorityRecord', 'isni')
 
     def test_add_read_update_delete_transfer(self):
@@ -75,17 +74,11 @@ class SecurityTC(CubicWebTC):
 
         self.assertOwnershipBasedAccess('SEDAArchiveTransfer', 'title')
 
     def test_add_read_update_delete_unit(self):
         with self.new_access('bob').cnx() as cnx:
-            au = cnx.create_entity('SEDAArchiveUnit', user_annotation=u'title')
-            alt = cnx.create_entity('SEDAAltArchiveUnitArchiveUnitRefId',
-                                    reverse_seda_alt_archive_unit_archive_unit_ref_id=au)
-            last = cnx.create_entity(
-                'SEDASeqAltArchiveUnitArchiveUnitRefIdManagement',
-                reverse_seda_seq_alt_archive_unit_archive_unit_ref_id_management=alt)
-            cnx.create_entity('SEDATitle', seda_title=last)
+            au, alt, last = testutils.create_archive_unit(None, cnx=cnx)
             cnx.commit()
 
         self.assertOwnershipBasedAccess('SEDAArchiveUnit', 'user_annotation')
 
     def test_add_read_update_delete_schemes(self):
diff --git a/test/testutils.py b/test/testutils.py
new file mode 100644
--- /dev/null
+++ b/test/testutils.py
@@ -0,0 +1,43 @@
+# copyright 2016-2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# contact http://www.logilab.fr -- mailto:contact at logilab.fr
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 2.1 of the License, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License along
+# with this program. If not, see <http://www.gnu.org/licenses/>.
+"""Miscellaneous test utilities."""
+
+
+def create_archive_unit(parent, **kwargs):
+    """Create an archive unit and its mandatory children, return a 3-uple
+
+         (archive unit, alternative, sequence)
+    """
+    cnx = kwargs.pop('cnx', getattr(parent, '_cw', None))
+    kwargs.setdefault('user_annotation', u'archive unit title')
+    au = cnx.create_entity('SEDAArchiveUnit', seda_archive_unit=parent, **kwargs)
+    alt = cnx.create_entity('SEDAAltArchiveUnitArchiveUnitRefId',
+                            reverse_seda_alt_archive_unit_archive_unit_ref_id=au)
+    last = cnx.create_entity(
+        'SEDASeqAltArchiveUnitArchiveUnitRefIdManagement',
+        reverse_seda_seq_alt_archive_unit_archive_unit_ref_id_management=alt)
+    cnx.create_entity('SEDATitle', seda_title=last)
+
+    return au, alt, last
+
+
+def create_authority_record(cnx, name, kind=u'person', **kwargs):
+    """Return an AuthorityRecord with specified kind and name."""
+    kind_eid = cnx.find('AgentKind', name=kind)[0][0]
+    record = cnx.create_entity('AuthorityRecord', agent_kind=kind_eid, **kwargs)
+    cnx.create_entity('NameEntry', parts=name, form_variant=u'authorized',
+                      name_entry_for=record)
+    return record


More information about the saem-devel mailing list