[server/test] use server config in migration
authorPierre-Yves David <pierre-yves.david@logilab.fr>
Fri, 26 Apr 2013 11:08:06 +0200
changeset 8935 a8a9ba6f4cdd
parent 8934 48a6e6b88b16
child 8936 010adc548b97
[server/test] use server config in migration This allows a lighter initialisation (no rtag needed). Execution of those test move from 300 secondes to 220 seconds here. A 25% speedup
server/test/unittest_migractions.py
--- a/server/test/unittest_migractions.py	Mon Apr 22 17:57:46 2013 +0200
+++ b/server/test/unittest_migractions.py	Fri Apr 26 11:08:06 2013 +0200
@@ -31,6 +31,8 @@
 from cubicweb.server.sqlutils import SQL_PREFIX
 from cubicweb.server.migractions import *
 
+import cubicweb.devtools
+
 migrschema = None
 def tearDownModule(*args):
     global migrschema
@@ -40,6 +42,8 @@
 
 class MigrationCommandsTC(CubicWebTC):
 
+    configcls = cubicweb.devtools.TestServerConfiguration
+
     tags = CubicWebTC.tags | Tags(('server', 'migration', 'migractions'))
 
     @classmethod
@@ -73,8 +77,8 @@
 
     def test_add_attribute_int(self):
         self.assertFalse('whatever' in self.schema)
-        self.request().create_entity('Note')
-        self.commit()
+        self.session.create_entity('Note')
+        self.session.commit(free_cnxset=False)
         orderdict = dict(self.mh.rqlexec('Any RTN, O WHERE X name "Note", RDEF from_entity X, '
                                          'RDEF relation_type RT, RDEF ordernum O, RT name RTN'))
         self.mh.cmd_add_attribute('Note', 'whatever')
@@ -83,10 +87,10 @@
         self.assertEqual(self.schema['whatever'].objects(), ('Int',))
         self.assertEqual(self.schema['Note'].default('whatever'), 2)
         # test default value set on existing entities
-        note = self.execute('Note X').get_entity(0, 0)
+        note = self.session.execute('Note X').get_entity(0, 0)
         self.assertEqual(note.whatever, 2)
         # test default value set for next entities
-        self.assertEqual(self.request().create_entity('Note').whatever, 2)
+        self.assertEqual(self.session.create_entity('Note').whatever, 2)
         # test attribute order
         orderdict2 = dict(self.mh.rqlexec('Any RTN, O WHERE X name "Note", RDEF from_entity X, '
                                           'RDEF relation_type RT, RDEF ordernum O, RT name RTN'))
@@ -107,8 +111,8 @@
 
     def test_add_attribute_varchar(self):
         self.assertFalse('whatever' in self.schema)
-        self.request().create_entity('Note')
-        self.commit()
+        self.session.create_entity('Note')
+        self.session.commit(free_cnxset=False)
         self.assertFalse('shortpara' in self.schema)
         self.mh.cmd_add_attribute('Note', 'shortpara')
         self.assertTrue('shortpara' in self.schema)
@@ -118,11 +122,10 @@
         notesql = self.mh.sqlexec("SELECT sql FROM sqlite_master WHERE type='table' and name='%sNote'" % SQL_PREFIX)[0][0]
         fields = dict(x.strip().split()[:2] for x in notesql.split('(', 1)[1].rsplit(')', 1)[0].split(','))
         self.assertEqual(fields['%sshortpara' % SQL_PREFIX], 'varchar(64)')
-        req = self.request()
         # test default value set on existing entities
-        self.assertEqual(req.execute('Note X').get_entity(0, 0).shortpara, 'hop')
+        self.assertEqual(self.session.execute('Note X').get_entity(0, 0).shortpara, 'hop')
         # test default value set for next entities
-        self.assertEqual(req.create_entity('Note').shortpara, 'hop')
+        self.assertEqual(self.session.create_entity('Note').shortpara, 'hop')
         self.mh.rollback()
 
     def test_add_datetime_with_default_value_attribute(self):
@@ -195,9 +198,9 @@
         self.mh.cmd_add_entity_type('Folder2')
         self.assertTrue('Folder2' in self.schema)
         self.assertTrue('Old' in self.schema)
-        self.assertTrue(self.execute('CWEType X WHERE X name "Folder2"'))
+        self.assertTrue(self.session.execute('CWEType X WHERE X name "Folder2"'))
         self.assertTrue('filed_under2' in self.schema)
-        self.assertTrue(self.execute('CWRType X WHERE X name "filed_under2"'))
+        self.assertTrue(self.session.execute('CWRType X WHERE X name "filed_under2"'))
         self.schema.rebuild_infered_relations()
         self.assertEqual(sorted(str(rs) for rs in self.schema['Folder2'].subject_relations()),
                           ['created_by', 'creation_date', 'cw_source', 'cwuri',
@@ -225,15 +228,15 @@
         done = wf.add_state(u'done')
         wf.add_transition(u'redoit', done, todo)
         wf.add_transition(u'markasdone', todo, done)
-        self.commit()
+        self.session.commit(free_cnxset=False)
         eschema = self.schema.eschema('Folder2')
         self.mh.cmd_drop_entity_type('Folder2')
         self.assertFalse('Folder2' in self.schema)
-        self.assertFalse(self.execute('CWEType X WHERE X name "Folder2"'))
+        self.assertFalse(self.session.execute('CWEType X WHERE X name "Folder2"'))
         # test automatic workflow deletion
-        self.assertFalse(self.execute('Workflow X WHERE NOT X workflow_of ET'))
-        self.assertFalse(self.execute('State X WHERE NOT X state_of WF'))
-        self.assertFalse(self.execute('Transition X WHERE NOT X transition_of WF'))
+        self.assertFalse(self.session.execute('Workflow X WHERE NOT X workflow_of ET'))
+        self.assertFalse(self.session.execute('State X WHERE NOT X state_of WF'))
+        self.assertFalse(self.session.execute('Transition X WHERE NOT X transition_of WF'))
 
     def test_rename_entity_type(self):
         entity = self.mh.create_entity('Old', name=u'old')
@@ -268,7 +271,7 @@
         self.mh.create_entity('Personne', nom=u'tot')
         self.mh.create_entity('Affaire')
         self.mh.rqlexec('SET X concerne2 Y WHERE X is Personne, Y is Affaire')
-        self.commit()
+        self.session.commit(free_cnxset=False)
         self.mh.cmd_drop_relation_definition('Personne', 'concerne2', 'Affaire')
         self.assertTrue('concerne2' in self.schema)
         self.mh.cmd_drop_relation_definition('Personne', 'concerne2', 'Note')
@@ -290,7 +293,7 @@
         self.assertEqual(sorted(str(e) for e in self.schema['concerne'].objects()),
                           ['Affaire', 'Division', 'Note', 'Societe', 'SubDivision'])
         # trick: overwrite self.maxeid to avoid deletion of just reintroduced types
-        self.maxeid = self.execute('Any MAX(X)')[0][0]
+        self.maxeid = self.session.execute('Any MAX(X)')[0][0]
 
     def test_drop_relation_definition_with_specialization(self):
         self.assertEqual(sorted(str(e) for e in self.schema['concerne'].subjects()),
@@ -314,7 +317,7 @@
         self.assertEqual(sorted(str(e) for e in self.schema['concerne'].objects()),
                           ['Affaire', 'Division', 'Note', 'Societe', 'SubDivision'])
         # trick: overwrite self.maxeid to avoid deletion of just reintroduced types
-        self.maxeid = self.execute('Any MAX(X)')[0][0]
+        self.maxeid = self.session.execute('Any MAX(X)')[0][0]
 
     def test_rename_relation(self):
         self.skipTest('implement me')
@@ -495,8 +498,8 @@
                                           ('Note', 'Bookmark')]))
                 self.assertEqual(sorted(schema['see_also'].subjects()), ['Bookmark', 'Folder', 'Note'])
                 self.assertEqual(sorted(schema['see_also'].objects()), ['Bookmark', 'Folder', 'Note'])
-                self.assertEqual(self.execute('Any X WHERE X pkey "system.version.email"').rowcount, 0)
-                self.assertEqual(self.execute('Any X WHERE X pkey "system.version.file"').rowcount, 0)
+                self.assertEqual(self.session.execute('Any X WHERE X pkey "system.version.email"').rowcount, 0)
+                self.assertEqual(self.session.execute('Any X WHERE X pkey "system.version.file"').rowcount, 0)
             except :
                 import traceback
                 traceback.print_exc()
@@ -520,16 +523,16 @@
             self.assertEqual(sorted(schema['see_also'].objects()), ['Bookmark', 'EmailThread', 'Folder', 'Note'])
             from cubes.email.__pkginfo__ import version as email_version
             from cubes.file.__pkginfo__ import version as file_version
-            self.assertEqual(self.execute('Any V WHERE X value V, X pkey "system.version.email"')[0][0],
+            self.assertEqual(self.session.execute('Any V WHERE X value V, X pkey "system.version.email"')[0][0],
                               email_version)
-            self.assertEqual(self.execute('Any V WHERE X value V, X pkey "system.version.file"')[0][0],
+            self.assertEqual(self.session.execute('Any V WHERE X value V, X pkey "system.version.file"')[0][0],
                               file_version)
             # trick: overwrite self.maxeid to avoid deletion of just reintroduced
             #        types (and their associated tables!)
-            self.maxeid = self.execute('Any MAX(X)')[0][0]
+            self.maxeid = self.session.execute('Any MAX(X)')[0][0]
             # why this commit is necessary is unclear to me (though without it
             # next test may fail complaining of missing tables
-            self.commit()
+            self.session.commit(free_cnxset=False)
 
 
     @tag('longrun')
@@ -554,10 +557,10 @@
             self.assertTrue('email' in self.config.cubes())
             # trick: overwrite self.maxeid to avoid deletion of just reintroduced
             #        types (and their associated tables!)
-            self.maxeid = self.execute('Any MAX(X)')[0][0]
+            self.maxeid = self.session.execute('Any MAX(X)')[0][0]
             # why this commit is necessary is unclear to me (though without it
             # next test may fail complaining of missing tables
-            self.commit()
+            self.session.commit(free_cnxset=False)
 
     def test_remove_dep_cube(self):
         with self.assertRaises(ConfigurationError) as cm:
@@ -577,16 +580,16 @@
                           ['Note', 'Text'])
         self.assertEqual(self.schema['Text'].specializes().type, 'Para')
         # test columns have been actually added
-        text = self.execute('INSERT Text X: X para "hip", X summary "hop", X newattr "momo"').get_entity(0, 0)
-        note = self.execute('INSERT Note X: X para "hip", X shortpara "hop", X newattr "momo", X unique_id "x"').get_entity(0, 0)
-        aff = self.execute('INSERT Affaire X').get_entity(0, 0)
-        self.assertTrue(self.execute('SET X newnotinlined Y WHERE X eid %(x)s, Y eid %(y)s',
+        text = self.session.execute('INSERT Text X: X para "hip", X summary "hop", X newattr "momo"').get_entity(0, 0)
+        note = self.session.execute('INSERT Note X: X para "hip", X shortpara "hop", X newattr "momo", X unique_id "x"').get_entity(0, 0)
+        aff = self.session.execute('INSERT Affaire X').get_entity(0, 0)
+        self.assertTrue(self.session.execute('SET X newnotinlined Y WHERE X eid %(x)s, Y eid %(y)s',
                                      {'x': text.eid, 'y': aff.eid}))
-        self.assertTrue(self.execute('SET X newnotinlined Y WHERE X eid %(x)s, Y eid %(y)s',
+        self.assertTrue(self.session.execute('SET X newnotinlined Y WHERE X eid %(x)s, Y eid %(y)s',
                                      {'x': note.eid, 'y': aff.eid}))
-        self.assertTrue(self.execute('SET X newinlined Y WHERE X eid %(x)s, Y eid %(y)s',
+        self.assertTrue(self.session.execute('SET X newinlined Y WHERE X eid %(x)s, Y eid %(y)s',
                                      {'x': text.eid, 'y': aff.eid}))
-        self.assertTrue(self.execute('SET X newinlined Y WHERE X eid %(x)s, Y eid %(y)s',
+        self.assertTrue(self.session.execute('SET X newinlined Y WHERE X eid %(x)s, Y eid %(y)s',
                                      {'x': note.eid, 'y': aff.eid}))
         # XXX remove specializes by ourselves, else tearDown fails when removing
         # Para because of Note inheritance. This could be fixed by putting the
@@ -598,8 +601,8 @@
         # specialization relationship...
         self.session.data['rebuild-infered'] = True
         try:
-            self.execute('DELETE X specializes Y WHERE Y name "Para"')
-            self.commit()
+            self.session.execute('DELETE X specializes Y WHERE Y name "Para"')
+            self.session.commit(free_cnxset=False)
         finally:
             self.session.data['rebuild-infered'] = False
         self.assertEqual(sorted(et.type for et in self.schema['Para'].specialized_by()),