devtools/fake.py
branchstable
changeset 3315 59220b704562
parent 3255 a1679842bd52
child 3293 69c0ba095536
child 4212 ab6573088b4a
equal deleted inserted replaced
3298:caef98aa4a98 3315:59220b704562
    11 from logilab.common.adbh import get_adv_func_helper
    11 from logilab.common.adbh import get_adv_func_helper
    12 
    12 
    13 from indexer import get_indexer
    13 from indexer import get_indexer
    14 
    14 
    15 from cubicweb import RequestSessionMixIn
    15 from cubicweb import RequestSessionMixIn
       
    16 from cubicweb.cwvreg import CubicWebVRegistry
    16 from cubicweb.web.request import CubicWebRequestBase
    17 from cubicweb.web.request import CubicWebRequestBase
    17 from cubicweb.devtools import BASE_URL, BaseApptestConfiguration
    18 from cubicweb.devtools import BASE_URL, BaseApptestConfiguration
    18 
    19 
    19 
    20 
    20 class FakeConfig(dict, BaseApptestConfiguration):
    21 class FakeConfig(dict, BaseApptestConfiguration):
    33         return self._cubes
    34         return self._cubes
    34 
    35 
    35     def sources(self):
    36     def sources(self):
    36         return {}
    37         return {}
    37 
    38 
    38 class FakeVReg(dict):
       
    39     def __init__(self, schema=None, config=None):
       
    40         self.schema = schema
       
    41         self.config = config or FakeConfig()
       
    42         self.properties = {'ui.encoding': 'UTF8',
       
    43                            'ui.language': 'en',
       
    44                            }
       
    45         self.update({
       
    46             'controllers' : {'login': []},
       
    47             'views' : {},
       
    48             })
       
    49 
       
    50     def property_value(self, key):
       
    51         return self.properties[key]
       
    52 
       
    53     def etype_class(self, etype):
       
    54         class Entity(dict):
       
    55             e_schema = self.schema[etype]
       
    56             def __init__(self, session, eid, row=0, col=0):
       
    57                 self.req = session
       
    58                 self.eid = eid
       
    59                 self.row, self.col = row, col
       
    60             def set_eid(self, eid):
       
    61                 self.eid = self['eid'] = eid
       
    62         return Entity
       
    63 
       
    64 
    39 
    65 class FakeRequest(CubicWebRequestBase):
    40 class FakeRequest(CubicWebRequestBase):
    66     """test implementation of an cubicweb request object"""
    41     """test implementation of an cubicweb request object"""
    67 
    42 
    68     def __init__(self, *args, **kwargs):
    43     def __init__(self, *args, **kwargs):
    69         if not (args or 'vreg' in kwargs):
    44         if not (args or 'vreg' in kwargs):
    70             kwargs['vreg'] = FakeVReg()
    45             kwargs['vreg'] = CubicWebVRegistry(FakeConfig(), initlog=False)
    71         kwargs['https'] = False
    46         kwargs['https'] = False
    72         self._url = kwargs.pop('url', 'view?rql=Blop&vid=blop')
    47         self._url = kwargs.pop('url', 'view?rql=Blop&vid=blop')
    73         super(FakeRequest, self).__init__(*args, **kwargs)
    48         super(FakeRequest, self).__init__(*args, **kwargs)
    74         self._session_data = {}
    49         self._session_data = {}
    75         self._headers = {}
    50         self._headers = {}
   146         """return the original parent session if any, else self"""
   121         """return the original parent session if any, else self"""
   147         kwargs.pop('propagate', None)
   122         kwargs.pop('propagate', None)
   148         return self.execute(*args, **kwargs)
   123         return self.execute(*args, **kwargs)
   149 
   124 
   150 
   125 
   151 # class FakeRequestNoCnx(FakeRequest):
       
   152 #     def get_session_data(self, key, default=None, pop=False):
       
   153 #         """return value associated to `key` in session data"""
       
   154 #         if pop:
       
   155 #             return self._session_data.pop(key, default)
       
   156 #         else:
       
   157 #             return self._session_data.get(key, default)
       
   158 
       
   159 #     def set_session_data(self, key, value):
       
   160 #         """set value associated to `key` in session data"""
       
   161 #         self._session_data[key] = value
       
   162 
       
   163 #     def del_session_data(self, key):
       
   164 #         try:
       
   165 #             del self._session_data[key]
       
   166 #         except KeyError:
       
   167 #             pass
       
   168 
       
   169 
       
   170 class FakeUser(object):
   126 class FakeUser(object):
   171     login = 'toto'
   127     login = 'toto'
   172     eid = 0
   128     eid = 0
   173     def in_groups(self, groups):
   129     def in_groups(self, groups):
   174         return True
   130         return True
   175 
   131 
   176 
   132 
   177 class FakeSession(RequestSessionMixIn):
   133 class FakeSession(RequestSessionMixIn):
   178     def __init__(self, repo=None, user=None):
   134     def __init__(self, repo=None, user=None):
   179         self.repo = repo
   135         self.repo = repo
   180         self.vreg = getattr(self.repo, 'vreg', FakeVReg())
   136         self.vreg = getattr(self.repo, 'vreg', CubicWebVRegistry(FakeConfig(), initlog=False))
   181         self.pool = FakePool()
   137         self.pool = FakePool()
   182         self.user = user or FakeUser()
   138         self.user = user or FakeUser()
   183         self.is_internal_session = False
   139         self.is_internal_session = False
   184         self.is_super_session = self.user.eid == -1
   140         self.is_super_session = self.user.eid == -1
   185         self.transaction_data = {}
   141         self.transaction_data = {}
   208     def __init__(self, schema, vreg=None, config=None):
   164     def __init__(self, schema, vreg=None, config=None):
   209         self.extids = {}
   165         self.extids = {}
   210         self.eids = {}
   166         self.eids = {}
   211         self._count = 0
   167         self._count = 0
   212         self.schema = schema
   168         self.schema = schema
   213         self.vreg = vreg or FakeVReg()
       
   214         self.config = config or FakeConfig()
   169         self.config = config or FakeConfig()
       
   170         self.vreg = vreg or CubicWebVRegistry(self.config, initlog=False)
       
   171         self.vreg.schema = schema
   215 
   172 
   216     def internal_session(self):
   173     def internal_session(self):
   217         return FakeSession(self)
   174         return FakeSession(self)
   218 
   175 
   219     def extid2eid(self, source, extid, etype, session, insert=True,
   176     def extid2eid(self, source, extid, etype, session, insert=True,
   247 
   204 
   248 
   205 
   249 class FakePool(object):
   206 class FakePool(object):
   250     def source(self, uri):
   207     def source(self, uri):
   251         return FakeSource(uri)
   208         return FakeSource(uri)
   252 
       
   253 # commented until proven to be useful
       
   254 ## from logging import getLogger
       
   255 ## from cubicweb import set_log_methods
       
   256 ## for cls in (FakeConfig, FakeVReg, FakeRequest, FakeSession, FakeRepo,
       
   257 ##             FakeSource, FakePool):
       
   258 ##     set_log_methods(cls, getLogger('fake'))