# HG changeset patch # User Sylvain Thénault # Date 1422825284 -3600 # Node ID 277074659cad46f1d118b21f96bb78dbd68950a0 # Parent 04266443a6a97abf42680adc510572152065e2fe [dataimport] don't generate metadata which are explicitly specified Related to #4891552 diff -r 04266443a6a9 -r 277074659cad dataimport.py --- a/dataimport.py Fri Mar 27 17:23:06 2015 +0100 +++ b/dataimport.py Sun Feb 01 22:14:44 2015 +0100 @@ -883,6 +883,9 @@ def init_entity(self, entity): entity.eid = self.source.create_eid(self._cnx) for attr in self.entity_attrs: + if attr in entity.cw_edited: + # already set, skip this attribute + continue genfunc = self.generate(attr) if genfunc: entity.cw_edited.edited_attribute(attr, genfunc(entity)) @@ -891,6 +894,7 @@ return getattr(self, 'gen_%s' % rtype, None) def gen_cwuri(self, entity): + assert self.baseurl, 'baseurl is None while generating cwuri' return u'%s%s' % (self.baseurl, entity.eid) def gen_creation_date(self, entity): diff -r 04266443a6a9 -r 277074659cad test/unittest_dataimport.py --- a/test/unittest_dataimport.py Fri Mar 27 17:23:06 2015 +0100 +++ b/test/unittest_dataimport.py Sun Feb 01 22:14:44 2015 +0100 @@ -1,7 +1,10 @@ # -*- coding: utf-8 -*- + import datetime as DT from StringIO import StringIO + from logilab.common.testlib import TestCase, unittest_main + from cubicweb import dataimport from cubicweb.devtools.testlib import CubicWebTC @@ -24,6 +27,7 @@ self.assertEqual(1, len(users)) self.assertEqual(group_eid, groups.one().eid) + class CreateCopyFromBufferTC(TestCase): # test converters @@ -135,8 +139,8 @@ class MetaGeneratorTC(CubicWebTC): + def test_dont_generate_relation_to_internal_manager(self): - from cubicweb.server.edition import EditedEntity with self.admin_access.repo_cnx() as cnx: metagen = dataimport.MetaGenerator(cnx) self.assertIn('created_by', metagen.etype_rels) @@ -146,5 +150,18 @@ self.assertNotIn('created_by', metagen.etype_rels) self.assertNotIn('owned_by', metagen.etype_rels) + def test_dont_generate_specified_values(self): + with self.admin_access.repo_cnx() as cnx: + metagen = dataimport.MetaGenerator(cnx) + # hijack gen_modification_date to ensure we don't go through it + metagen.gen_modification_date = None + md = DT.datetime.now() - DT.timedelta(days=1) + entity, rels = metagen.base_etype_dicts('CWUser') + entity.cw_edited.update(dict(modification_date=md)) + with cnx.ensure_cnx_set: + metagen.init_entity(entity) + self.assertEqual(entity.cw_edited['modification_date'], md) + + if __name__ == '__main__': unittest_main()