[dataimport] don't generate metadata which are explicitly specified
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Sun, 01 Feb 2015 22:14:44 +0100
changeset 10294 277074659cad
parent 10293 04266443a6a9
child 10295 080ac14df6fa
[dataimport] don't generate metadata which are explicitly specified Related to #4891552
dataimport.py
test/unittest_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):
--- 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()