devtools/fake.py
changeset 1808 aa09e20dd8c0
parent 1802 d628defebc17
child 1856 b0a6e34ba11b
equal deleted inserted replaced
1693:49075f57cf2c 1808:aa09e20dd8c0
     1 """Fake objects to ease testing of cubicweb without a fully working environment
     1 """Fake objects to ease testing of cubicweb without a fully working environment
     2 
     2 
     3 :organization: Logilab
     3 :organization: Logilab
     4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 """
     6 """
     7 __docformat__ = "restructuredtext en"
     7 __docformat__ = "restructuredtext en"
     8 
     8 
     9 from logilab.common.testlib import mock_object as Mock
     9 from logilab.common.testlib import mock_object as Mock
    22     def __init__(self, appid='data', apphome=None, cubes=()):
    22     def __init__(self, appid='data', apphome=None, cubes=()):
    23         self.appid = appid
    23         self.appid = appid
    24         self.apphome = apphome
    24         self.apphome = apphome
    25         self._cubes = cubes
    25         self._cubes = cubes
    26         self['auth-mode'] = 'cookie'
    26         self['auth-mode'] = 'cookie'
    27         self['uid'] = None 
    27         self['uid'] = None
    28         self['base-url'] = BASE_URL
    28         self['base-url'] = BASE_URL
    29         self['rql-cache-size'] = 100
    29         self['rql-cache-size'] = 100
    30        
    30 
    31     def cubes(self, expand=False):
    31     def cubes(self, expand=False):
    32         return self._cubes
    32         return self._cubes
    33     
    33 
    34     def sources(self):
    34     def sources(self):
    35         return {}
    35         return {}
    36 
    36 
    37 class FakeVReg(object):
    37 class FakeVReg(object):
    38     def __init__(self, schema=None, config=None):
    38     def __init__(self, schema=None, config=None):
    39         self.schema = schema
    39         self.schema = schema
    40         self.config = config or FakeConfig()
    40         self.config = config or FakeConfig()
    41         self.properties = {'ui.encoding': 'UTF8',
    41         self.properties = {'ui.encoding': 'UTF8',
    42                            'ui.language': 'en',
    42                            'ui.language': 'en',
    43                            }
    43                            }
    44         
    44 
    45     def property_value(self, key):
    45     def property_value(self, key):
    46         return self.properties[key]
    46         return self.properties[key]
    47 
    47 
    48     _registries = {
    48     _registries = {
    49         'controllers' : [Mock(id='view'), Mock(id='login'),
    49         'controllers' : [Mock(id='view'), Mock(id='login'),
    50                          Mock(id='logout'), Mock(id='edit')],
    50                          Mock(id='logout'), Mock(id='edit')],
    51         'views' : [Mock(id='primary'), Mock(id='secondary'),
    51         'views' : [Mock(id='primary'), Mock(id='secondary'),
    52                          Mock(id='oneline'), Mock(id='list')],
    52                          Mock(id='oneline'), Mock(id='list')],
    53         }
    53         }
    54     
    54 
    55     def registry_objects(self, name, oid=None):
    55     def registry_objects(self, name, oid=None):
    56         return self._registries[name]
    56         return self._registries[name]
    57     
    57 
    58     def etype_class(self, etype):
    58     def etype_class(self, etype):
    59         class Entity(dict):
    59         class Entity(dict):
    60             e_schema = self.schema[etype]
    60             e_schema = self.schema[etype]
    61             def __init__(self, session, eid, row=0, col=0):
    61             def __init__(self, session, eid, row=0, col=0):
    62                 self.req = session
    62                 self.req = session
   110         pass
   110         pass
   111 
   111 
   112     def set_header(self, header, value):
   112     def set_header(self, header, value):
   113         """set an output HTTP header"""
   113         """set an output HTTP header"""
   114         pass
   114         pass
   115     
   115 
   116     def add_header(self, header, value):
   116     def add_header(self, header, value):
   117         """set an output HTTP header"""
   117         """set an output HTTP header"""
   118         pass
   118         pass
   119     
   119 
   120     def remove_header(self, header):
   120     def remove_header(self, header):
   121         """remove an output HTTP header"""
   121         """remove an output HTTP header"""
   122         pass
   122         pass
   123     
   123 
   124     def get_header(self, header, default=None):
   124     def get_header(self, header, default=None):
   125         """return the value associated with the given input header,
   125         """return the value associated with the given input header,
   126         raise KeyError if the header is not set
   126         raise KeyError if the header is not set
   127         """
   127         """
   128         return self._headers.get(header, default)
   128         return self._headers.get(header, default)
   167         self.pool = FakePool()
   167         self.pool = FakePool()
   168         self.user = user or FakeUser()
   168         self.user = user or FakeUser()
   169         self.is_internal_session = False
   169         self.is_internal_session = False
   170         self.is_super_session = self.user.eid == -1
   170         self.is_super_session = self.user.eid == -1
   171         self._query_data = {}
   171         self._query_data = {}
   172         
   172 
   173     def execute(self, *args):
   173     def execute(self, *args):
   174         pass
   174         pass
   175     def commit(self, *args):
   175     def commit(self, *args):
   176         self._query_data.clear()
   176         self._query_data.clear()
   177     def close(self, *args):
   177     def close(self, *args):
   184         rset.req = self
   184         rset.req = self
   185         return rset
   185         return rset
   186 
   186 
   187     def set_entity_cache(self, entity):
   187     def set_entity_cache(self, entity):
   188         pass
   188         pass
   189     
   189 
   190 class FakeRepo(object):
   190 class FakeRepo(object):
   191     querier = None
   191     querier = None
   192     def __init__(self, schema, vreg=None, config=None):
   192     def __init__(self, schema, vreg=None, config=None):
   193         self.extids = {}
   193         self.extids = {}
   194         self.eids = {}
   194         self.eids = {}
   197         self.vreg = vreg or FakeVReg()
   197         self.vreg = vreg or FakeVReg()
   198         self.config = config or FakeConfig()
   198         self.config = config or FakeConfig()
   199 
   199 
   200     def internal_session(self):
   200     def internal_session(self):
   201         return FakeSession(self)
   201         return FakeSession(self)
   202     
   202 
   203     def extid2eid(self, source, extid, etype, session, insert=True):
   203     def extid2eid(self, source, extid, etype, session, insert=True,
       
   204                   recreate=False):
   204         try:
   205         try:
   205             return self.extids[extid]
   206             return self.extids[extid]
   206         except KeyError:
   207         except KeyError:
   207             if not insert:
   208             if not insert:
   208                 return None
   209                 return None
   211             entity = source.before_entity_insertion(session, extid, etype, eid)
   212             entity = source.before_entity_insertion(session, extid, etype, eid)
   212             self.extids[extid] = eid
   213             self.extids[extid] = eid
   213             self.eids[eid] = extid
   214             self.eids[eid] = extid
   214             source.after_entity_insertion(session, extid, entity)
   215             source.after_entity_insertion(session, extid, entity)
   215             return eid
   216             return eid
   216         
   217 
   217     def eid2extid(self, source, eid, session=None):
   218     def eid2extid(self, source, eid, session=None):
   218         return self.eids[eid]
   219         return self.eids[eid]
   219 
   220 
   220 
   221 
   221 class FakeSource(object):
   222 class FakeSource(object):
   226     dbhelper.fti_restriction_sql = indexer.restriction_sql
   227     dbhelper.fti_restriction_sql = indexer.restriction_sql
   227     dbhelper.fti_need_distinct_query = indexer.need_distinct
   228     dbhelper.fti_need_distinct_query = indexer.need_distinct
   228     def __init__(self, uri):
   229     def __init__(self, uri):
   229         self.uri = uri
   230         self.uri = uri
   230 
   231 
   231         
   232 
   232 class FakePool(object):
   233 class FakePool(object):
   233     def source(self, uri):
   234     def source(self, uri):
   234         return FakeSource(uri)
   235         return FakeSource(uri)
   235 
   236 
   236 # commented until proven to be useful
   237 # commented until proven to be useful