server/test/unittest_repository.py
branchstable
changeset 5115 2e43ef618d14
parent 5106 782ff24a7e46
child 5126 c869d8e7d166
equal deleted inserted replaced
5114:55b8b7a5bc4a 5115:2e43ef618d14
     6 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     7 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     7 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     8 """
     8 """
     9 from __future__ import with_statement
     9 from __future__ import with_statement
    10 
    10 
       
    11 from __future__ import with_statement
       
    12 
    11 import os
    13 import os
    12 import sys
    14 import sys
    13 import threading
    15 import threading
    14 import time
    16 import time
    15 from copy import deepcopy
    17 from copy import deepcopy
    19 
    21 
    20 from yams.constraints import UniqueConstraint
    22 from yams.constraints import UniqueConstraint
    21 
    23 
    22 from cubicweb import (BadConnectionId, RepositoryError, ValidationError,
    24 from cubicweb import (BadConnectionId, RepositoryError, ValidationError,
    23                       UnknownEid, AuthenticationError)
    25                       UnknownEid, AuthenticationError)
       
    26 from cubicweb.selectors import implements
    24 from cubicweb.schema import CubicWebSchema, RQLConstraint
    27 from cubicweb.schema import CubicWebSchema, RQLConstraint
    25 from cubicweb.dbapi import connect, multiple_connections_unfix
    28 from cubicweb.dbapi import connect, multiple_connections_unfix
    26 from cubicweb.devtools.testlib import CubicWebTC
    29 from cubicweb.devtools.testlib import CubicWebTC
    27 from cubicweb.devtools.repotest import tuplify
    30 from cubicweb.devtools.repotest import tuplify
    28 from cubicweb.server import repository, hook
    31 from cubicweb.server import repository, hook
    29 from cubicweb.server.sqlutils import SQL_PREFIX
    32 from cubicweb.server.sqlutils import SQL_PREFIX
       
    33 from cubicweb.server.hook import Hook
    30 from cubicweb.server.sources import native
    34 from cubicweb.server.sources import native
    31 
    35 
    32 # start name server anyway, process will fail if already running
    36 # start name server anyway, process will fail if already running
    33 os.system('pyro-ns >/dev/null 2>/dev/null &')
    37 os.system('pyro-ns >/dev/null 2>/dev/null &')
    34 
    38 
   271         finally:
   275         finally:
   272             repository.pyro_unregister(self.repo.config)
   276             repository.pyro_unregister(self.repo.config)
   273             from logilab.common import pyro_ext
   277             from logilab.common import pyro_ext
   274             pyro_ext._DAEMONS.clear()
   278             pyro_ext._DAEMONS.clear()
   275 
   279 
       
   280 
   276     def _pyro_client(self, done):
   281     def _pyro_client(self, done):
   277         cnx = connect(self.repo.config.appid, u'admin', password='gingkow')
   282         cnx = connect(self.repo.config.appid, u'admin', password='gingkow',
       
   283                       initlog=False) # don't reset logging configuration
   278         try:
   284         try:
   279             # check we can get the schema
   285             # check we can get the schema
   280             schema = cnx.get_schema()
   286             schema = cnx.get_schema()
   281             self.assertEquals(schema.__hashmode__, None)
   287             self.assertEquals(schema.__hashmode__, None)
   282             cu = cnx.cursor()
   288             cu = cnx.cursor()
   283             rset = cu.execute('Any U,G WHERE U in_group G')
   289             rset = cu.execute('Any U,G WHERE U in_group G')
   284             cnx.close()
   290             cnx.close()
   285             done.append(True)
   291             done.append(True)
   286         finally:
   292         finally:
   287             # connect monkey path some method by default, remove them
   293             # connect monkey patch some method by default, remove them
   288             multiple_connections_unfix()
   294             multiple_connections_unfix()
   289 
   295 
   290     def test_internal_api(self):
   296     def test_internal_api(self):
   291         repo = self.repo
   297         repo = self.repo
   292         cnxid = repo.connect(self.admlogin, password=self.admpassword)
   298         cnxid = repo.connect(self.admlogin, password=self.admpassword)
   356                             {'x': note.eid})
   362                             {'x': note.eid})
   357         self.assertEquals(len(rset), 1)
   363         self.assertEquals(len(rset), 1)
   358         self.assertEquals(rset.rows[0][0], p2.eid)
   364         self.assertEquals(rset.rows[0][0], p2.eid)
   359 
   365 
   360 
   366 
       
   367     def test_set_attributes_in_before_update(self):
       
   368         # local hook
       
   369         class DummyBeforeHook(Hook):
       
   370             __regid__ = 'dummy-before-hook'
       
   371             __select__ = Hook.__select__ & implements('EmailAddress')
       
   372             events = ('before_update_entity',)
       
   373             def __call__(self):
       
   374                 # safety belt: avoid potential infinite recursion if the test
       
   375                 #              fails (i.e. RuntimeError not raised)
       
   376                 pendings = self._cw.transaction_data.setdefault('pending', set())
       
   377                 if self.entity.eid not in pendings:
       
   378                     pendings.add(self.entity.eid)
       
   379                     self.entity.set_attributes(alias=u'foo')
       
   380         with self.temporary_appobjects(DummyBeforeHook):
       
   381             req = self.request()
       
   382             addr = req.create_entity('EmailAddress', address=u'a@b.fr')
       
   383             addr.set_attributes(address=u'a@b.com')
       
   384             rset = self.execute('Any A,AA WHERE X eid %(x)s, X address A, X alias AA',
       
   385                                 {'x': addr.eid})
       
   386             self.assertEquals(rset.rows, [[u'a@b.com', u'foo']])
       
   387 
       
   388     def test_set_attributes_in_before_add(self):
       
   389         # local hook
       
   390         class DummyBeforeHook(Hook):
       
   391             __regid__ = 'dummy-before-hook'
       
   392             __select__ = Hook.__select__ & implements('EmailAddress')
       
   393             events = ('before_add_entity',)
       
   394             def __call__(self):
       
   395                 # set_attributes is forbidden within before_add_entity()
       
   396                 self.entity.set_attributes(alias=u'foo')
       
   397         with self.temporary_appobjects(DummyBeforeHook):
       
   398             req = self.request()
       
   399             self.assertRaises(RepositoryError, req.create_entity,
       
   400                               'EmailAddress', address=u'a@b.fr')
       
   401 
       
   402 
   361 class DataHelpersTC(CubicWebTC):
   403 class DataHelpersTC(CubicWebTC):
   362 
   404 
   363     def test_create_eid(self):
   405     def test_create_eid(self):
   364         self.session.set_pool()
   406         self.session.set_pool()
   365         self.assert_(self.repo.system_source.create_eid(self.session))
   407         self.assert_(self.repo.system_source.create_eid(self.session))