hooks/test/unittest_syncschema.py
changeset 4766 162b2b127b15
parent 4691 ae468fae9965
child 4829 3b79a0fc91db
equal deleted inserted replaced
4764:ec9c20c6b9f7 4766:162b2b127b15
     1 from logilab.common.testlib import TestCase, unittest_main
     1 from logilab.common.testlib import TestCase, unittest_main
     2 
     2 
     3 from cubicweb import ValidationError
     3 from cubicweb import ValidationError
     4 from cubicweb.devtools.testlib import CubicWebTC
     4 from cubicweb.devtools.testlib import CubicWebTC
     5 from cubicweb.server.sqlutils import SQL_PREFIX
     5 from cubicweb.server.sqlutils import SQL_PREFIX
     6 
     6 from cubicweb.devtools.repotest import schema_eids_idx, restore_schema_eids_idx
     7 
     7 
     8 SCHEMA_EIDS = {}
     8 def teardown_module(*args):
       
     9     del SchemaModificationHooksTC.schema_eids
       
    10 
     9 class SchemaModificationHooksTC(CubicWebTC):
    11 class SchemaModificationHooksTC(CubicWebTC):
    10     reset_schema = True
    12     reset_schema = True
    11 
    13 
    12     @classmethod
    14     @classmethod
    13     def init_config(cls, config):
    15     def init_config(cls, config):
    14         super(SchemaModificationHooksTC, cls).init_config(config)
    16         super(SchemaModificationHooksTC, cls).init_config(config)
    15         # we have to read schema from the database to get eid for schema entities
    17         # we have to read schema from the database to get eid for schema entities
    16         config._cubes = None
    18         config._cubes = None
    17         cls.repo.fill_schema()
    19         cls.repo.fill_schema()
    18         # remember them so we can reread it from the fs instead of the db (too
    20         cls.schema_eids = schema_eids_idx(cls.repo.schema)
    19         # costly) between tests
       
    20         for x in cls.repo.schema.entities():
       
    21             SCHEMA_EIDS[x] = x.eid
       
    22         for x in cls.repo.schema.relations():
       
    23             SCHEMA_EIDS[x] = x.eid
       
    24             for rdef in x.rdefs.itervalues():
       
    25                 SCHEMA_EIDS[(rdef.subject, rdef.rtype, rdef.object)] = rdef.eid
       
    26 
    21 
    27     @classmethod
    22     @classmethod
    28     def _refresh_repo(cls):
    23     def _refresh_repo(cls):
    29         super(SchemaModificationHooksTC, cls)._refresh_repo()
    24         super(SchemaModificationHooksTC, cls)._refresh_repo()
    30         # rebuild schema eid index
    25         restore_schema_eids_idx(cls.repo.schema, cls.schema_eids)
    31         schema = cls.repo.schema
       
    32         for x in schema.entities():
       
    33             x.eid = SCHEMA_EIDS[x]
       
    34             schema._eid_index[x.eid] = x
       
    35         for x in cls.repo.schema.relations():
       
    36             x.eid = SCHEMA_EIDS[x]
       
    37             schema._eid_index[x.eid] = x
       
    38             for rdef in x.rdefs.itervalues():
       
    39                 rdef.eid = SCHEMA_EIDS[(rdef.subject, rdef.rtype, rdef.object)]
       
    40                 schema._eid_index[rdef.eid] = rdef
       
    41 
    26 
    42     def index_exists(self, etype, attr, unique=False):
    27     def index_exists(self, etype, attr, unique=False):
    43         self.session.set_pool()
    28         self.session.set_pool()
    44         dbhelper = self.session.pool.source('system').dbhelper
    29         dbhelper = self.session.pool.source('system').dbhelper
    45         sqlcursor = self.session.pool['system']
    30         sqlcursor = self.session.pool['system']