[PATCH 3 of 7 saem_ref V2] [test] Reuse functions from cubicweb_seda.testutils

Sylvain Thenault sylvain.thenault at logilab.fr
Mon Apr 10 14:50:43 CEST 2017

# HG changeset patch
# User Sylvain Thénault <sylvain.thenault at logilab.fr>
# Date 1490967297 -7200
#      Fri Mar 31 15:34:57 2017 +0200
# Node ID b4a62c6a8964ae02ac99774051844eeb6f8495fb
# Parent  eacf1752ed3d04c6f017a24544140060a455814a
[test] Reuse functions from cubicweb_seda.testutils

Avoid duplication and ease to follow changes from the seda cube, such as recent
cardinality change of the seda_description_level cardinality.

Because of this, there is still a test failure that will be fixed in a

diff --git a/test/testutils.py b/test/testutils.py
--- a/test/testutils.py
+++ b/test/testutils.py
@@ -15,39 +15,30 @@
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 """cubicweb-saem_ref common test tools"""
 from __future__ import print_function
-from contextlib import contextmanager
 from os.path import join, dirname, exists
 import sys
 from doctest import Example
 from lxml import etree
 from lxml.doctestcompare import LXMLOutputChecker
 from logilab.common.decorators import monkeypatch
-from cubicweb import NoResultError, ValidationError, Unauthorized, devtools
+from cubicweb import NoResultError, devtools
 from cubicweb.devtools import testlib
+from cubicweb_seda import testutils as seda_testutils
- at contextmanager
-def assertValidationError(self, cnx):
-    with self.assertRaises(ValidationError) as cm:
-        yield cm
-        cnx.commit()
-    cnx.rollback()
- at contextmanager
-def assertUnauthorized(self, cnx):
-    with self.assertRaises(Unauthorized) as cm:
-        yield cm
-        cnx.commit()
-    cnx.rollback()
+# make some names them appear in this module namespace to ease test API
+assertValidationError = seda_testutils.assertValidationError
+assertUnauthorized = seda_testutils.assertUnauthorized
+create_archive_unit = seda_testutils.create_archive_unit
+create_data_object = seda_testutils.create_data_object
 def _authority(func):
     """Decorator binding an Organization with an NAA configured in
@@ -135,56 +126,23 @@ def setup_profile(cnx, **kwargs):
     """Return a minimal SEDA profile."""
     kwargs.setdefault('title', u'Test profile')
     return cnx.create_entity('SEDAArchiveTransfer', ark_naa=naa(cnx), **kwargs)
-def create_archive_unit(parent, title=None, **kwargs):
-    cnx = kwargs.pop('cnx', getattr(parent, '_cw', None))
-    kwargs.setdefault('user_annotation', u'au1')
-    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)
-    alt_seq = cnx.create_entity(
-        'SEDASeqAltArchiveUnitArchiveUnitRefIdManagement',
-        reverse_seda_seq_alt_archive_unit_archive_unit_ref_id_management=alt)
-    cnx.create_entity('SEDATitle', seda_title=alt_seq, title=title)
-    return au, alt, alt_seq
-def create_data_object(transfer, **kwargs):
-    cnx = kwargs.pop('cnx', getattr(transfer, '_cw', None))
-    create = cnx.create_entity
-    kwargs.setdefault('user_annotation', u'bdo1')
-    bdo = create('SEDABinaryDataObject', seda_binary_data_object=transfer, **kwargs)
-    choice = create('SEDAAltBinaryDataObjectAttachment',
-                    reverse_seda_alt_binary_data_object_attachment=bdo)
-    create('SEDAAttachment', seda_attachment=choice)  # Choice cannot be empty
-    return bdo
 def concept(cnx, label):
     """Return concept entity with the given preferred label (expected to be unique)."""
     return cnx.execute('Concept X WHERE X preferred_label L, L label %(label)s',
                        {'label': label}).one()
-def map_cs_to_type(scheme, rtype, etype=None):
-    cnx = scheme._cw
-    cnx.execute('SET CS scheme_relation_type RT WHERE CS eid %(cs)s, RT name %(rt)s',
-                {'cs': scheme.eid, 'rt': rtype})
-    if etype is not None:
-        cnx.execute('SET CS scheme_entity_type ET WHERE CS eid %(cs)s, ET name %(et)s',
-                    {'cs': scheme.eid, 'et': etype})
+# monkey patch scheme_for_rtype to ensure every scheme is created with a naa set
+orig_scheme_for_type = seda_testutils.scheme_for_type
+ at monkeypatch(seda_testutils)
 def scheme_for_type(cnx, rtype, etype, *concept_labels):
-    scheme = cnx.create_entity('ConceptScheme', title=u'{0}/{1} vocabulary'.format(rtype, etype),
-                               ark_naa=naa(cnx))
-    map_cs_to_type(scheme, rtype, etype)
-    for label in concept_labels:
-        scheme.add_concept(label)
-    return scheme
+    return orig_scheme_for_type(cnx, rtype, etype, *concept_labels, ark_naa=naa(cnx))
 class XmlTestMixin(object):
     """Mixin class provinding additional assertion methods for checking XML data."""

More information about the saem-devel mailing list