devtools/repotest.py
changeset 5768 1e73a466aa69
parent 5426 0d4853a6e5ee
child 6369 a151453dc564
equal deleted inserted replaced
5766:c397819f2482 5768:1e73a466aa69
    16 # You should have received a copy of the GNU Lesser General Public License along
    16 # You should have received a copy of the GNU Lesser General Public License along
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """some utilities to ease repository testing
    18 """some utilities to ease repository testing
    19 
    19 
    20 This module contains functions to initialize a new repository.
    20 This module contains functions to initialize a new repository.
    21 
       
    22 """
    21 """
       
    22 
    23 __docformat__ = "restructuredtext en"
    23 __docformat__ = "restructuredtext en"
    24 
    24 
    25 from pprint import pprint
    25 from pprint import pprint
    26 
    26 
    27 from logilab.common.decorators import clear_cache
    27 from logilab.common.decorators import clear_cache
   132         for rdef in x.rdefs.itervalues():
   132         for rdef in x.rdefs.itervalues():
   133             rdef.eid = schema_eids[(rdef.subject, rdef.rtype, rdef.object)]
   133             rdef.eid = schema_eids[(rdef.subject, rdef.rtype, rdef.object)]
   134             schema._eid_index[rdef.eid] = rdef
   134             schema._eid_index[rdef.eid] = rdef
   135 
   135 
   136 
   136 
   137 from logilab.common.testlib import TestCase
   137 from logilab.common.testlib import TestCase, mock_object
       
   138 from logilab.database import get_db_helper
       
   139 
   138 from rql import RQLHelper
   140 from rql import RQLHelper
       
   141 
   139 from cubicweb.devtools.fake import FakeRepo, FakeSession
   142 from cubicweb.devtools.fake import FakeRepo, FakeSession
   140 from cubicweb.server import set_debug
   143 from cubicweb.server import set_debug
   141 from cubicweb.server.querier import QuerierHelper
   144 from cubicweb.server.querier import QuerierHelper
   142 from cubicweb.server.session import Session
   145 from cubicweb.server.session import Session
   143 from cubicweb.server.sources.rql2sql import remove_unused_solutions
   146 from cubicweb.server.sources.rql2sql import SQLGenerator, remove_unused_solutions
   144 
   147 
   145 class RQLGeneratorTC(TestCase):
   148 class RQLGeneratorTC(TestCase):
   146     schema = None # set this in concret test
   149     schema = backend = None # set this in concret test
   147 
   150 
   148     def setUp(self):
   151     def setUp(self):
   149         self.repo = FakeRepo(self.schema)
   152         self.repo = FakeRepo(self.schema)
       
   153         self.repo.system_source = mock_object(dbdriver=self.backend)
   150         self.rqlhelper = RQLHelper(self.schema, special_relations={'eid': 'uid',
   154         self.rqlhelper = RQLHelper(self.schema, special_relations={'eid': 'uid',
   151                                                                    'has_text': 'fti'})
   155                                                                    'has_text': 'fti'},
       
   156                                    backend=self.backend)
   152         self.qhelper = QuerierHelper(self.repo, self.schema)
   157         self.qhelper = QuerierHelper(self.repo, self.schema)
   153         ExecutionPlan._check_permissions = _dummy_check_permissions
   158         ExecutionPlan._check_permissions = _dummy_check_permissions
   154         rqlannotation._select_principal = _select_principal
   159         rqlannotation._select_principal = _select_principal
       
   160         if self.backend is not None:
       
   161             dbhelper = get_db_helper(self.backend)
       
   162             self.o = SQLGenerator(self.schema, dbhelper)
   155 
   163 
   156     def tearDown(self):
   164     def tearDown(self):
   157         ExecutionPlan._check_permissions = _orig_check_permissions
   165         ExecutionPlan._check_permissions = _orig_check_permissions
   158         rqlannotation._select_principal = _orig_select_principal
   166         rqlannotation._select_principal = _orig_select_principal
   159 
   167 
   268         self.schema = self.o.schema
   276         self.schema = self.o.schema
   269         self.sources = self.o._repo.sources
   277         self.sources = self.o._repo.sources
   270         self.system = self.sources[-1]
   278         self.system = self.sources[-1]
   271         do_monkey_patch()
   279         do_monkey_patch()
   272         self._dumb_sessions = [] # by hi-jacked parent setup
   280         self._dumb_sessions = [] # by hi-jacked parent setup
       
   281         self.repo.vreg.rqlhelper.backend = 'postgres' # so FTIRANK is considered
   273 
   282 
   274     def add_source(self, sourcecls, uri):
   283     def add_source(self, sourcecls, uri):
   275         self.sources.append(sourcecls(self.repo, self.o.schema,
   284         self.sources.append(sourcecls(self.repo, self.o.schema,
   276                                       {'uri': uri}))
   285                                       {'uri': uri}))
   277         self.repo.sources_by_uri[uri] = self.sources[-1]
   286         self.repo.sources_by_uri[uri] = self.sources[-1]