cubicweb/dataimport/test/test_massive_store.py
changeset 11328 9f2d7da47526
parent 11324 4b530be87d36
child 11329 a8cab8fb54ba
equal deleted inserted replaced
11327:901243e41152 11328:9f2d7da47526
    16 # with this program. If not, see <http://www.gnu.org/licenses/>.
    16 # with this program. If not, see <http://www.gnu.org/licenses/>.
    17 """Massive store test case"""
    17 """Massive store test case"""
    18 
    18 
    19 import itertools
    19 import itertools
    20 
    20 
    21 from cubicweb.dataimport import ucsvreader
       
    22 from cubicweb.devtools import testlib, PostgresApptestConfiguration
    21 from cubicweb.devtools import testlib, PostgresApptestConfiguration
    23 from cubicweb.devtools import startpgcluster, stoppgcluster
    22 from cubicweb.devtools import startpgcluster, stoppgcluster
       
    23 from cubicweb.dataimport import ucsvreader, stores
    24 from cubicweb.dataimport.massive_store import MassiveObjectStore, PGHelper
    24 from cubicweb.dataimport.massive_store import MassiveObjectStore, PGHelper
       
    25 
       
    26 from test_stores import NoHookRQLObjectStoreWithCustomMDGenStoreTC
    25 
    27 
    26 
    28 
    27 def setUpModule():
    29 def setUpModule():
    28     startpgcluster(__file__)
    30     startpgcluster(__file__)
    29 
    31 
    30 
    32 
    31 def tearDownModule(*args):
    33 def tearDownModule(*args):
    32     stoppgcluster(__file__)
    34     stoppgcluster(__file__)
       
    35 
       
    36 
       
    37 class MassiveObjectStoreWithCustomMDGenStoreTC(NoHookRQLObjectStoreWithCustomMDGenStoreTC):
       
    38     configcls = PostgresApptestConfiguration
       
    39 
       
    40     def store_impl(self, cnx):
       
    41         source = cnx.create_entity('CWSource', type=u'datafeed', name=u'test', url=u'test')
       
    42         cnx.commit()
       
    43         metagen = stores.MetadataGenerator(cnx, source=cnx.repo.sources_by_eid[source.eid])
       
    44         return MassiveObjectStore(cnx, metagen=metagen)
    33 
    45 
    34 
    46 
    35 class MassImportSimpleTC(testlib.CubicWebTC):
    47 class MassImportSimpleTC(testlib.CubicWebTC):
    36     configcls = PostgresApptestConfiguration
    48     configcls = PostgresApptestConfiguration
    37     appid = 'data-massimport'
    49     appid = 'data-massimport'
   181         self.assertEqual(init_descr, final_descr)
   193         self.assertEqual(init_descr, final_descr)
   182 
   194 
   183     def test_on_commit_callback(self):
   195     def test_on_commit_callback(self):
   184         counter = itertools.count()
   196         counter = itertools.count()
   185         with self.admin_access.repo_cnx() as cnx:
   197         with self.admin_access.repo_cnx() as cnx:
   186             store = MassiveObjectStore(cnx, on_commit_callback=lambda:next(counter))
   198             store = MassiveObjectStore(cnx, on_commit_callback=lambda: next(counter))
   187             store.prepare_insert_entity('Location', name=u'toto')
   199             store.prepare_insert_entity('Location', name=u'toto')
   188             store.flush()
   200             store.flush()
   189             store.commit()
   201             store.commit()
   190         self.assertGreaterEqual(next(counter), 1)
   202         self.assertEqual(next(counter), 1)
   191 
   203 
   192     def test_on_rollback_callback(self):
   204     def test_on_rollback_callback(self):
   193         counter = itertools.count()
   205         counter = itertools.count()
   194         with self.admin_access.repo_cnx() as cnx:
   206         with self.admin_access.repo_cnx() as cnx:
   195             store = MassiveObjectStore(cnx, on_rollback_callback=lambda *_: next(counter))
   207             store = MassiveObjectStore(cnx, on_rollback_callback=lambda *_: next(counter))
   196             store.prepare_insert_entity('Location', nm='toto')
   208             store.prepare_insert_entity('Location', nm='toto')
   197             store.flush()
   209             store.commit()  # commit modification to the database before flush
   198             store.commit()
   210             store.flush()
   199         self.assertGreaterEqual(next(counter), 1)
   211         self.assertEqual(next(counter), 1)
   200 
   212 
   201     def test_slave_mode_indexes(self):
   213     def test_slave_mode_indexes(self):
   202         with self.admin_access.repo_cnx() as cnx:
   214         with self.admin_access.repo_cnx() as cnx:
   203             slave_store = MassiveObjectStore(cnx, slave_mode=True)
   215             slave_store = MassiveObjectStore(cnx, slave_mode=True)
   204         with self.admin_access.repo_cnx() as cnx:
   216         with self.admin_access.repo_cnx() as cnx: