cubicweb/devtools/testlib.py
branch3.24
changeset 11811 f09efeead7f9
parent 11801 7a7ce3da28fb
child 11821 7534b32c45e3
equal deleted inserted replaced
11810:2e03bb4372b2 11811:f09efeead7f9
    26 from math import log
    26 from math import log
    27 from contextlib import contextmanager
    27 from contextlib import contextmanager
    28 from inspect import isgeneratorfunction
    28 from inspect import isgeneratorfunction
    29 from itertools import chain
    29 from itertools import chain
    30 from warnings import warn
    30 from warnings import warn
    31 import tempfile
       
    32 
    31 
    33 from six import text_type, string_types, reraise
    32 from six import text_type, string_types, reraise
    34 from six.moves import range
    33 from six.moves import range
    35 from six.moves.urllib.parse import urlparse, parse_qs, unquote as urlunquote
    34 from six.moves.urllib.parse import urlparse, parse_qs, unquote as urlunquote
    36 
    35 
    43 from logilab.common.deprecation import deprecated, class_deprecated
    42 from logilab.common.deprecation import deprecated, class_deprecated
    44 from logilab.common.shellutils import getlogin
    43 from logilab.common.shellutils import getlogin
    45 
    44 
    46 from cubicweb import (ValidationError, NoSelectableObject, AuthenticationError,
    45 from cubicweb import (ValidationError, NoSelectableObject, AuthenticationError,
    47                       BadConnectionId)
    46                       BadConnectionId)
    48 from cubicweb import cwconfig, devtools, web, server, repoapi
    47 from cubicweb import cwconfig, devtools, web, server
    49 from cubicweb.utils import json
    48 from cubicweb.utils import json
    50 from cubicweb.sobjects import notification
    49 from cubicweb.sobjects import notification
    51 from cubicweb.web import Redirect, application, eid_param
    50 from cubicweb.web import Redirect, application, eid_param
    52 from cubicweb.server.hook import SendMailOp
    51 from cubicweb.server.hook import SendMailOp
    53 from cubicweb.server.session import Session
    52 from cubicweb.server.session import Session
    54 from cubicweb.devtools import SYSTEM_ENTITIES, SYSTEM_RELATIONS, VIEW_VALIDATORS
    53 from cubicweb.devtools import SYSTEM_ENTITIES, SYSTEM_RELATIONS, VIEW_VALIDATORS
    55 from cubicweb.devtools import fake, htmlparser, DEFAULT_EMPTY_DB_ID
    54 from cubicweb.devtools import fake, htmlparser, DEFAULT_EMPTY_DB_ID
       
    55 from cubicweb.devtools.fill import insert_entity_queries, make_relations_queries
    56 
    56 
    57 
    57 
    58 if sys.version_info[:2] < (3, 4):
    58 if sys.version_info[:2] < (3, 4):
    59     from unittest2 import TestCase
    59     from unittest2 import TestCase
    60     if not hasattr(TestCase, 'subTest'):
    60     if not hasattr(TestCase, 'subTest'):
   850             path, params = location.split('?', 1)
   850             path, params = location.split('?', 1)
   851         except ValueError:
   851         except ValueError:
   852             path = location
   852             path = location
   853             params = {}
   853             params = {}
   854         else:
   854         else:
   855             cleanup = lambda p: (p[0], urlunquote(p[1]))
   855             def cleanup(p):
       
   856                 return (p[0], urlunquote(p[1]))
       
   857 
   856             params = dict(cleanup(p.split('=', 1)) for p in params.split('&') if p)
   858             params = dict(cleanup(p.split('=', 1)) for p in params.split('&') if p)
   857         if path.startswith(req.base_url()):  # may be relative
   859         if path.startswith(req.base_url()):  # may be relative
   858             path = path[len(req.base_url()):]
   860             path = path[len(req.base_url()):]
   859         return path, params
   861         return path, params
   860 
   862 
   966         view = viewsreg.select(vid, req, rset=rset, **kwargs)
   968         view = viewsreg.select(vid, req, rset=rset, **kwargs)
   967         if template is None:  # raw view testing, no template
   969         if template is None:  # raw view testing, no template
   968             viewfunc = view.render
   970             viewfunc = view.render
   969         else:
   971         else:
   970             kwargs['view'] = view
   972             kwargs['view'] = view
   971             viewfunc = lambda **k: viewsreg.main_template(req, template,
   973 
   972                                                           rset=rset, **kwargs)
   974             def viewfunc(**k):
       
   975                 return viewsreg.main_template(req, template,
       
   976                                               rset=rset, **kwargs)
       
   977 
   973         return self._test_view(viewfunc, view, template, kwargs)
   978         return self._test_view(viewfunc, view, template, kwargs)
   974 
   979 
   975     def _test_view(self, viewfunc, view, template='main-template', kwargs={}):
   980     def _test_view(self, viewfunc, view, template='main-template', kwargs={}):
   976         """this method does the actual call to the view
   981         """this method does the actual call to the view
   977 
   982 
  1103         return messages
  1108         return messages
  1104 
  1109 
  1105 
  1110 
  1106 # auto-populating test classes and utilities ###################################
  1111 # auto-populating test classes and utilities ###################################
  1107 
  1112 
  1108 from cubicweb.devtools.fill import insert_entity_queries, make_relations_queries
       
  1109 
       
  1110 # XXX cleanup unprotected_entities & all mess
  1113 # XXX cleanup unprotected_entities & all mess
  1111 
       
  1112 
  1114 
  1113 def how_many_dict(schema, cnx, how_many, skip):
  1115 def how_many_dict(schema, cnx, how_many, skip):
  1114     """given a schema, compute how many entities by type we need to be able to
  1116     """given a schema, compute how many entities by type we need to be able to
  1115     satisfy relations cardinality.
  1117     satisfy relations cardinality.
  1116 
  1118