devtools/testlib.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 05 Jan 2011 10:30:52 +0100
branchstable
changeset 6774 bd50a9c1514e
parent 6772 68bb0943d192
child 6800 3f3d576b87d9
permissions -rw-r--r--
d-t-w
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5401
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5401
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5401
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5401
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5401
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5401
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5401
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5401
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5401
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5401
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5401
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5401
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5401
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5401
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5401
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5401
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5813
0b250d72fcfa [transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5786
diff changeset
    18
"""this module contains base classes and utilities for cubicweb tests"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
5004
4cc020ee70e2 le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4958
diff changeset
    20
from __future__ import with_statement
4cc020ee70e2 le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4958
diff changeset
    21
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    24
import os
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
import sys
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    26
import re
6585
9af22f2c0c4c [test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6469
diff changeset
    27
import urlparse
6631
26c303c3f1aa [test] make test independant from the working directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6590
diff changeset
    28
from os.path import dirname, join, abspath
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    29
from urllib import unquote
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
from math import log
5102
04c84959cd85 [testlib] simple helper method to register temporary appobjects (hooks for instance) with a context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5055
diff changeset
    31
from contextlib import contextmanager
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5159
diff changeset
    32
from warnings import warn
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    34
import yams.schema
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
5913
85240b3f9ee4 [test] add default generic test Tags to CubicWeb TestCase class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5875
diff changeset
    36
from logilab.common.testlib import TestCase, InnerTest, Tags
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    37
from logilab.common.pytest import nocoverage, pause_tracing, resume_tracing
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    38
from logilab.common.debugger import Debugger
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    39
from logilab.common.umessage import message_from_string
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
    40
from logilab.common.decorators import cached, classproperty, clear_cache
6454
97203d0af4cb [test/possible views] skip deprecated views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6442
diff changeset
    41
from logilab.common.deprecation import deprecated, class_deprecated
6720
43a38c093f6f [testlib] Use logilab.common.shellutils.getlogin instead of local hack.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6589
diff changeset
    42
from logilab.common.shellutils import getlogin
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
3998
94cc7cad3d2d backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777 3956
diff changeset
    44
from cubicweb import ValidationError, NoSelectableObject, AuthenticationError
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
    45
from cubicweb import cwconfig, devtools, web, server
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
    46
from cubicweb.dbapi import ProgrammingError, DBAPISession, repo_connect
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    47
from cubicweb.sobjects import notification
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
    48
from cubicweb.web import Redirect, application
5004
4cc020ee70e2 le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4958
diff changeset
    49
from cubicweb.server.session import security_enabled
6398
ea26eb5fd388 [test] send mail synchronously during tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6369
diff changeset
    50
from cubicweb.server.hook import SendMailOp
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    51
from cubicweb.devtools import SYSTEM_ENTITIES, SYSTEM_RELATIONS, VIEW_VALIDATORS
6316
51aa84c40a71 [test] we actually still need the set_option on base-url for multi-sources (eg repo) configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6281
diff changeset
    52
from cubicweb.devtools import BASE_URL, fake, htmlparser
5389
809d3b5b3d31 [python2.6] careful: json-py provides a json package. We don't want that.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5377
diff changeset
    53
from cubicweb.utils import json
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    55
# low-level utilities ##########################################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
class CubicWebDebugger(Debugger):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    58
    """special debugger class providing a 'view' function which saves some
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    59
    html into a temporary file and open a web browser to examinate it.
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    60
    """
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
    def do_view(self, arg):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
        import webbrowser
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
        data = self._getval(arg)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
        file('/tmp/toto.html', 'w').write(data)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
        webbrowser.open('file:///tmp/toto.html')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
def line_context_filter(line_no, center, before=3, after=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
    """return true if line are in context
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    69
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    70
    if after is None: after = before
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    71
    """
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
    if after is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
        after = before
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
    return center - before <= line_no <= center + after
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    76
def unprotected_entities(schema, strict=False):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    77
    """returned a set of each non final entity type, excluding "system" entities
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    78
    (eg CWGroup, CWUser...)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    79
    """
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    80
    if strict:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    81
        protected_entities = yams.schema.BASE_TYPES
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    82
    else:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    83
        protected_entities = yams.schema.BASE_TYPES.union(SYSTEM_ENTITIES)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    84
    return set(schema.entities()) - protected_entities
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    85
4689
4eb1f4490538 [test] skipping versions checking during test is enough, no need for monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4681
diff changeset
    86
def refresh_repo(repo, resetschema=False, resetvreg=False):
5751
1e5ef464cade [test] properly close connections during test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    87
    for pool in repo.pools:
1e5ef464cade [test] properly close connections during test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    88
        pool.close(True)
1e5ef464cade [test] properly close connections during test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    89
    repo.system_source.shutdown()
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    90
    devtools.reset_test_database(repo.config)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    91
    for pool in repo.pools:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    92
        pool.reconnect()
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    93
    repo._type_source_cache = {}
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    94
    repo._extid_cache = {}
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    95
    repo.querier._rql_cache = {}
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    96
    for source in repo.sources:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    97
        source.reset_caches()
4690
0cfb6b63f90b [testlib] provide ability to control schema/vreg reset using reset_schema/reset_vreg test class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4689
diff changeset
    98
    if resetschema:
0cfb6b63f90b [testlib] provide ability to control schema/vreg reset using reset_schema/reset_vreg test class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4689
diff changeset
    99
        repo.set_schema(repo.config.load_schema(), resetvreg=resetvreg)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   100
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   101
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   102
# email handling, to test emails sent by an application ########################
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   103
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   104
MAILBOX = []
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   105
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   106
class Email:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   107
    """you'll get instances of Email into MAILBOX during tests that trigger
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   108
    some notification.
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   109
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   110
    * `msg` is the original message object
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   111
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   112
    * `recipients` is a list of email address which are the recipients of this
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   113
      message
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   114
    """
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   115
    def __init__(self, recipients, msg):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   116
        self.recipients = recipients
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   117
        self.msg = msg
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   118
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   119
    @property
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   120
    def message(self):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   121
        return message_from_string(self.msg)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   122
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   123
    @property
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   124
    def subject(self):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   125
        return self.message.get('Subject')
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   126
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   127
    @property
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   128
    def content(self):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   129
        return self.message.get_payload(decode=True)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   130
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   131
    def __repr__(self):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   132
        return '<Email to %s with subject %s>' % (','.join(self.recipients),
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   133
                                                  self.message.get('Subject'))
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   134
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   135
# the trick to get email into MAILBOX instead of actually sent: monkey patch
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   136
# cwconfig.SMTP object
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   137
class MockSMTP:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   138
    def __init__(self, server, port):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   139
        pass
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   140
    def close(self):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   141
        pass
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   142
    def sendmail(self, helo_addr, recipients, msg):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   143
        MAILBOX.append(Email(recipients, msg))
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   144
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   145
cwconfig.SMTP = MockSMTP
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   146
6424
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6410
diff changeset
   147
6165
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   148
class TestCaseConnectionProxy(object):
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   149
    """thin wrapper around `cubicweb.dbapi.Connection` context-manager
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   150
    used in CubicWebTC (cf. `cubicweb.devtools.testlib.CubicWebTC.login` method)
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   151
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   152
    It just proxies to the default connection context manager but
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   153
    restores the original connection on exit.
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   154
    """
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   155
    def __init__(self, testcase, cnx):
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   156
        self.testcase = testcase
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   157
        self.cnx = cnx
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   158
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   159
    def __getattr__(self, attrname):
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   160
        return getattr(self.cnx, attrname)
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   161
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   162
    def __enter__(self):
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   163
        return self.cnx.__enter__()
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   164
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   165
    def __exit__(self, exctype, exc, tb):
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   166
        try:
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   167
            return self.cnx.__exit__(exctype, exc, tb)
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   168
        finally:
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   169
            self.cnx.close()
7da7c270bbbd [devtools] make CubiwebTC.login() context-manager restore the testcase connection on exit
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6109
diff changeset
   170
            self.testcase.restore_connection()
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   171
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   172
# base class for cubicweb tests requiring a full cw environments ###############
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   173
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   174
class CubicWebTC(TestCase):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   175
    """abstract class for test using an apptest environment
5267
7bac6791bbc2 [devtools] do not forbid use of postgres as a source for tests (but it still needs proper dump/restore support)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5229
diff changeset
   176
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   177
    attributes:
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   178
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   179
    * `vreg`, the vregistry
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   180
    * `schema`, self.vreg.schema
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   181
    * `config`, cubicweb configuration
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   182
    * `cnx`, dbapi connection to the repository using an admin user
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   183
    * `session`, server side session associated to `cnx`
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   184
    * `app`, the cubicweb publisher (for web testing)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   185
    * `repo`, the repository object
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   186
    * `admlogin`, login of the admin user
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   187
    * `admpassword`, password of the admin user
6694
d9eac4fd57dc [devtools] make migration handler easily available from CubicWebTC through .shell() method
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6687
diff changeset
   188
    * `shell`, create and use shell environment
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   189
    """
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   190
    appid = 'data'
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   191
    configcls = devtools.ApptestConfiguration
4690
0cfb6b63f90b [testlib] provide ability to control schema/vreg reset using reset_schema/reset_vreg test class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4689
diff changeset
   192
    reset_schema = reset_vreg = False # reset schema / vreg between tests
6441
adbce42ff1b5 TestServerConfig needs default_base_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6439
diff changeset
   193
    tags = TestCase.tags | Tags('cubicweb', 'cw_repo')
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   194
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   195
    @classproperty
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   196
    def config(cls):
6424
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6410
diff changeset
   197
        """return the configuration object
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6410
diff changeset
   198
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6410
diff changeset
   199
        Configuration is cached on the test class.
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   200
        """
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   201
        try:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   202
            return cls.__dict__['_config']
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   203
        except KeyError:
6631
26c303c3f1aa [test] make test independant from the working directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6590
diff changeset
   204
            home = abspath(join(dirname(sys.modules[cls.__module__].__file__), cls.appid))
6585
9af22f2c0c4c [test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6469
diff changeset
   205
            config = cls._config = cls.configcls(cls.appid, apphome=home)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   206
            config.mode = 'test'
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   207
            return config
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   208
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   209
    @classmethod
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   210
    def init_config(cls, config):
6424
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6410
diff changeset
   211
        """configuration initialization hooks.
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6410
diff changeset
   212
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6410
diff changeset
   213
        You may only want to override here the configuraton logic.
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6410
diff changeset
   214
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6410
diff changeset
   215
        Otherwise, consider to use a different :class:`ApptestConfiguration`
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6410
diff changeset
   216
        defined in the `configcls` class attribute"""
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   217
        source = config.sources()['system']
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   218
        cls.admlogin = unicode(source['db-user'])
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   219
        cls.admpassword = source['db-password']
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   220
        # uncomment the line below if you want rql queries to be logged
3163
edfe43ceaa35 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
   221
        #config.global_set_option('query-log-file',
edfe43ceaa35 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
   222
        #                         '/tmp/test_rql_log.' + `os.getpid()`)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   223
        config.global_set_option('log-file', None)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   224
        # set default-dest-addrs to a dumb email address to avoid mailbox or
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   225
        # mail queue pollution
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   226
        config.global_set_option('default-dest-addrs', ['whatever'])
6720
43a38c093f6f [testlib] Use logilab.common.shellutils.getlogin instead of local hack.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6589
diff changeset
   227
        send_to =  '%s@logilab.fr' % getlogin()
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   228
        config.global_set_option('sender-addr', send_to)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   229
        config.global_set_option('default-dest-addrs', send_to)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   230
        config.global_set_option('sender-name', 'cubicweb-test')
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   231
        config.global_set_option('sender-addr', 'cubicweb-test@logilab.fr')
6432
30bde2b7fea2 restore line dropped in 6424:f443a2b8a5c7 which is necessary to have proper base-url on test server config (used for ms tests at least)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6424
diff changeset
   232
        # default_base_url on config class isn't enough for TestServerConfiguration
6439
fe0fb6f95ff0 [test] we must consider default_base_url()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6432
diff changeset
   233
        config.global_set_option('base-url', config.default_base_url())
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   234
        # web resources
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   235
        try:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   236
            config.global_set_option('embed-allowed', re.compile('.*'))
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   237
        except: # not in server only configuration
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   238
            pass
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   239
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   240
    @classmethod
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   241
    def _init_repo(cls):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   242
        """init the repository and connection to it.
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   243
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   244
        Repository and connection are cached on the test class. Once
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   245
        initialized, we simply reset connections and repository caches.
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   246
        """
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   247
        if not 'repo' in cls.__dict__:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   248
            cls._build_repo()
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   249
        else:
5778
1a4ac9e2ce63 [test] workaround connection leak making tests fail under windows environment (though we've still probably some open connection leak to resolve...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5751
diff changeset
   250
            try:
1a4ac9e2ce63 [test] workaround connection leak making tests fail under windows environment (though we've still probably some open connection leak to resolve...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5751
diff changeset
   251
                cls.cnx.rollback()
5783
c5ff8cd74758 [devtools/teslib] be more defensive on rollbacks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5778
diff changeset
   252
            except ProgrammingError:
5778
1a4ac9e2ce63 [test] workaround connection leak making tests fail under windows environment (though we've still probably some open connection leak to resolve...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5751
diff changeset
   253
                pass
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   254
            cls._refresh_repo()
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   255
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   256
    @classmethod
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   257
    def _build_repo(cls):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   258
        cls.repo, cls.cnx = devtools.init_test_database(config=cls.config)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   259
        cls.init_config(cls.config)
4958
665eacdd8c50 [repo] call server_startup event in start_looping_task the repo is fully started (registered in pyro for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4917
diff changeset
   260
        cls.repo.hm.call_hooks('server_startup', repo=cls.repo)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   261
        cls.vreg = cls.repo.vreg
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
   262
        cls.websession = DBAPISession(cls.cnx, cls.admlogin,
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
   263
                                      {'password': cls.admpassword})
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
   264
        cls._orig_cnx = (cls.cnx, cls.websession)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   265
        cls.config.repository = lambda x=None: cls.repo
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   266
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   267
    @classmethod
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   268
    def _refresh_repo(cls):
4690
0cfb6b63f90b [testlib] provide ability to control schema/vreg reset using reset_schema/reset_vreg test class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4689
diff changeset
   269
        refresh_repo(cls.repo, cls.reset_schema, cls.reset_vreg)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   270
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   271
    # global resources accessors ###############################################
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   272
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   273
    @property
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   274
    def schema(self):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   275
        """return the application schema"""
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   276
        return self.vreg.schema
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   277
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   278
    @property
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   279
    def session(self):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   280
        """return current server side session (using default manager account)"""
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   281
        session = self.repo._sessions[self.cnx.sessionid]
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   282
        session.set_pool()
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   283
        return session
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   284
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   285
    @property
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   286
    def adminsession(self):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   287
        """return current server side session (using default manager account)"""
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
   288
        return self.repo._sessions[self._orig_cnx[0].sessionid]
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   289
6694
d9eac4fd57dc [devtools] make migration handler easily available from CubicWebTC through .shell() method
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6687
diff changeset
   290
    def shell(self):
d9eac4fd57dc [devtools] make migration handler easily available from CubicWebTC through .shell() method
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6687
diff changeset
   291
        """return a shell session object"""
d9eac4fd57dc [devtools] make migration handler easily available from CubicWebTC through .shell() method
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6687
diff changeset
   292
        from cubicweb.server.migractions import ServerMigrationHelper
d9eac4fd57dc [devtools] make migration handler easily available from CubicWebTC through .shell() method
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6687
diff changeset
   293
        return ServerMigrationHelper(None, repo=self.repo, cnx=self.cnx,
d9eac4fd57dc [devtools] make migration handler easily available from CubicWebTC through .shell() method
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6687
diff changeset
   294
                                     interactive=False,
d9eac4fd57dc [devtools] make migration handler easily available from CubicWebTC through .shell() method
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6687
diff changeset
   295
                                     # hack so it don't try to load fs schema
d9eac4fd57dc [devtools] make migration handler easily available from CubicWebTC through .shell() method
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6687
diff changeset
   296
                                     schema=1)
d9eac4fd57dc [devtools] make migration handler easily available from CubicWebTC through .shell() method
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6687
diff changeset
   297
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   298
    def set_option(self, optname, value):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   299
        self.config.global_set_option(optname, value)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   300
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   301
    def set_debug(self, debugmode):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   302
        server.set_debug(debugmode)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   303
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6425
diff changeset
   304
    def debugged(self, debugmode):
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6425
diff changeset
   305
        return server.debugged(debugmode)
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6425
diff changeset
   306
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   307
    # default test setup and teardown #########################################
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   308
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   309
    def setUp(self):
6398
ea26eb5fd388 [test] send mail synchronously during tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6369
diff changeset
   310
        # monkey patch send mail operation so emails are sent synchronously
6403
b7e24d49639b [testlib] Fix mail monkeypatch in testlib
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6398
diff changeset
   311
        self._old_mail_postcommit_event = SendMailOp.postcommit_event
b7e24d49639b [testlib] Fix mail monkeypatch in testlib
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6398
diff changeset
   312
        SendMailOp.postcommit_event = SendMailOp.sendmails
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   313
        pause_tracing()
5020
30a539c2074d When a TC failed to initiliase the database, the following test are SKIPPED
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4958
diff changeset
   314
        previous_failure = self.__class__.__dict__.get('_repo_init_failed')
30a539c2074d When a TC failed to initiliase the database, the following test are SKIPPED
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4958
diff changeset
   315
        if previous_failure is not None:
6369
a151453dc564 [test] more update to unittest2 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6346
diff changeset
   316
            self.skipTest('repository is not initialised: %r' % previous_failure)
5020
30a539c2074d When a TC failed to initiliase the database, the following test are SKIPPED
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4958
diff changeset
   317
        try:
30a539c2074d When a TC failed to initiliase the database, the following test are SKIPPED
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4958
diff changeset
   318
            self._init_repo()
30a539c2074d When a TC failed to initiliase the database, the following test are SKIPPED
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4958
diff changeset
   319
        except Exception, ex:
30a539c2074d When a TC failed to initiliase the database, the following test are SKIPPED
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4958
diff changeset
   320
            self.__class__._repo_init_failed = ex
30a539c2074d When a TC failed to initiliase the database, the following test are SKIPPED
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 4958
diff changeset
   321
            raise
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   322
        resume_tracing()
5401
547911327475 [testlib] move _cnxs initialization before setup_database so one can login in it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5389
diff changeset
   323
        self._cnxs = []
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   324
        self.setup_database()
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   325
        self.commit()
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   326
        MAILBOX[:] = [] # reset mailbox
5175
6efb7a7ae570 [testlib] properly close connections opened during test in tearDown
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5128
diff changeset
   327
6efb7a7ae570 [testlib] properly close connections opened during test in tearDown
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5128
diff changeset
   328
    def tearDown(self):
5785
c0661b963ced [test] fix test w/ closed connection pb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5783
diff changeset
   329
        if not self.cnx._closed:
5783
c5ff8cd74758 [devtools/teslib] be more defensive on rollbacks
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5778
diff changeset
   330
            self.cnx.rollback()
5175
6efb7a7ae570 [testlib] properly close connections opened during test in tearDown
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5128
diff changeset
   331
        for cnx in self._cnxs:
6efb7a7ae570 [testlib] properly close connections opened during test in tearDown
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5128
diff changeset
   332
            if not cnx._closed:
6efb7a7ae570 [testlib] properly close connections opened during test in tearDown
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5128
diff changeset
   333
                cnx.close()
6403
b7e24d49639b [testlib] Fix mail monkeypatch in testlib
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6398
diff changeset
   334
        SendMailOp.postcommit_event = self._old_mail_postcommit_event
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   335
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   336
    def setup_database(self):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   337
        """add your database setup code by overriding this method"""
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   338
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   339
    # user / session management ###############################################
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   340
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   341
    def user(self, req=None):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   342
        """return the application schema"""
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   343
        if req is None:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   344
            req = self.request()
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   345
            return self.cnx.user(req)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   346
        else:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   347
            return req.user
563
a690996639ca [testlib] fix pb. related to class scoped variables
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 562
diff changeset
   348
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   349
    def create_user(self, login, groups=('users',), password=None, req=None,
5055
14906474c55d [testlib] create_user support extra kwargs to give to create_entity(CWUser)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5036
diff changeset
   350
                    commit=True, **kwargs):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   351
        """create and return a new user entity"""
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   352
        if password is None:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   353
            password = login.encode('utf8')
5055
14906474c55d [testlib] create_user support extra kwargs to give to create_entity(CWUser)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5036
diff changeset
   354
        if req is None:
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
   355
            req = self._orig_cnx[0].request()
5055
14906474c55d [testlib] create_user support extra kwargs to give to create_entity(CWUser)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5036
diff changeset
   356
        user = req.create_entity('CWUser', login=unicode(login),
14906474c55d [testlib] create_user support extra kwargs to give to create_entity(CWUser)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5036
diff changeset
   357
                                 upassword=password, **kwargs)
14906474c55d [testlib] create_user support extra kwargs to give to create_entity(CWUser)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5036
diff changeset
   358
        req.execute('SET X in_group G WHERE X eid %%(x)s, G name IN(%s)'
14906474c55d [testlib] create_user support extra kwargs to give to create_entity(CWUser)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5036
diff changeset
   359
                    % ','.join(repr(g) for g in groups),
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5159
diff changeset
   360
                    {'x': user.eid})
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   361
        user.cw_clear_relation_cache('in_group', 'subject')
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   362
        if commit:
5055
14906474c55d [testlib] create_user support extra kwargs to give to create_entity(CWUser)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5036
diff changeset
   363
            req.cnx.commit()
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   364
        return user
563
a690996639ca [testlib] fix pb. related to class scoped variables
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 562
diff changeset
   365
3647
2941f4a0aab9 refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3589
diff changeset
   366
    def login(self, login, **kwargs):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   367
        """return a connection for the given login/password"""
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   368
        if login == self.admlogin:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   369
            self.restore_connection()
6346
1a968e545e4e [testlib] don't wrap main test connection into TestCaseConnectionProxy, it should not be closed afterwards
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6316
diff changeset
   370
            # definitly don't want autoclose when used as a context manager
1a968e545e4e [testlib] don't wrap main test connection into TestCaseConnectionProxy, it should not be closed afterwards
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6316
diff changeset
   371
            return self.cnx
1a968e545e4e [testlib] don't wrap main test connection into TestCaseConnectionProxy, it should not be closed afterwards
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6316
diff changeset
   372
        autoclose = kwargs.pop('autoclose', True)
1a968e545e4e [testlib] don't wrap main test connection into TestCaseConnectionProxy, it should not be closed afterwards
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6316
diff changeset
   373
        if not kwargs:
1a968e545e4e [testlib] don't wrap main test connection into TestCaseConnectionProxy, it should not be closed afterwards
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6316
diff changeset
   374
            kwargs['password'] = str(login)
1a968e545e4e [testlib] don't wrap main test connection into TestCaseConnectionProxy, it should not be closed afterwards
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6316
diff changeset
   375
        self.cnx = repo_connect(self.repo, unicode(login), **kwargs)
1a968e545e4e [testlib] don't wrap main test connection into TestCaseConnectionProxy, it should not be closed afterwards
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6316
diff changeset
   376
        self.websession = DBAPISession(self.cnx)
1a968e545e4e [testlib] don't wrap main test connection into TestCaseConnectionProxy, it should not be closed afterwards
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6316
diff changeset
   377
        self._cnxs.append(self.cnx)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   378
        if login == self.vreg.config.anonymous_user()[0]:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   379
            self.cnx.anonymous_connection = True
6346
1a968e545e4e [testlib] don't wrap main test connection into TestCaseConnectionProxy, it should not be closed afterwards
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6316
diff changeset
   380
        if autoclose:
1a968e545e4e [testlib] don't wrap main test connection into TestCaseConnectionProxy, it should not be closed afterwards
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6316
diff changeset
   381
            return TestCaseConnectionProxy(self, self.cnx)
1a968e545e4e [testlib] don't wrap main test connection into TestCaseConnectionProxy, it should not be closed afterwards
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6316
diff changeset
   382
        return self.cnx
563
a690996639ca [testlib] fix pb. related to class scoped variables
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 562
diff changeset
   383
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   384
    def restore_connection(self):
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
   385
        if not self.cnx is self._orig_cnx[0]:
5786
7c16a4e7a592 [test] fix stupd name error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5785
diff changeset
   386
            if not self.cnx._closed:
5785
c0661b963ced [test] fix test w/ closed connection pb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5783
diff changeset
   387
                self.cnx.close()
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   388
            try:
5175
6efb7a7ae570 [testlib] properly close connections opened during test in tearDown
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5128
diff changeset
   389
                self._cnxs.remove(self.cnx)
5785
c0661b963ced [test] fix test w/ closed connection pb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5783
diff changeset
   390
            except ValueError:
c0661b963ced [test] fix test w/ closed connection pb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5783
diff changeset
   391
                pass
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
   392
        self.cnx, self.websession = self._orig_cnx
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   393
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   394
    # db api ##################################################################
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   395
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   396
    @nocoverage
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   397
    def cursor(self, req=None):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   398
        return self.cnx.cursor(req or self.request())
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   399
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   400
    @nocoverage
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   401
    def execute(self, rql, args=None, eidkey=None, req=None):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   402
        """executes <rql>, builds a resultset, and returns a couple (rset, req)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   403
        where req is a FakeRequest
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   404
        """
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5159
diff changeset
   405
        if eidkey is not None:
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5159
diff changeset
   406
            warn('[3.8] eidkey is deprecated, you can safely remove this argument',
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5159
diff changeset
   407
                 DeprecationWarning, stacklevel=2)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   408
        req = req or self.request(rql=rql)
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5159
diff changeset
   409
        return req.execute(unicode(rql), args)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   410
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   411
    @nocoverage
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   412
    def commit(self):
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4835
diff changeset
   413
        try:
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4835
diff changeset
   414
            return self.cnx.commit()
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4835
diff changeset
   415
        finally:
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4835
diff changeset
   416
            self.session.set_pool() # ensure pool still set after commit
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   417
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   418
    @nocoverage
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   419
    def rollback(self):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   420
        try:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   421
            self.cnx.rollback()
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   422
        except ProgrammingError:
5785
c0661b963ced [test] fix test w/ closed connection pb
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5783
diff changeset
   423
            pass # connection closed
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4835
diff changeset
   424
        finally:
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4835
diff changeset
   425
            self.session.set_pool() # ensure pool still set after commit
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   426
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   427
    # # server side db api #######################################################
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   428
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   429
    def sexecute(self, rql, args=None, eid_key=None):
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5159
diff changeset
   430
        if eid_key is not None:
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5159
diff changeset
   431
            warn('[3.8] eid_key is deprecated, you can safely remove this argument',
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5159
diff changeset
   432
                 DeprecationWarning, stacklevel=2)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   433
        self.session.set_pool()
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5159
diff changeset
   434
        return self.session.execute(rql, args)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   435
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   436
    # other utilities #########################################################
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   437
5102
04c84959cd85 [testlib] simple helper method to register temporary appobjects (hooks for instance) with a context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5055
diff changeset
   438
    @contextmanager
04c84959cd85 [testlib] simple helper method to register temporary appobjects (hooks for instance) with a context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5055
diff changeset
   439
    def temporary_appobjects(self, *appobjects):
04c84959cd85 [testlib] simple helper method to register temporary appobjects (hooks for instance) with a context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5055
diff changeset
   440
        self.vreg._loadedmods.setdefault(self.__module__, {})
04c84959cd85 [testlib] simple helper method to register temporary appobjects (hooks for instance) with a context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5055
diff changeset
   441
        for obj in appobjects:
04c84959cd85 [testlib] simple helper method to register temporary appobjects (hooks for instance) with a context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5055
diff changeset
   442
            self.vreg.register(obj)
5128
e5d300d75519 [python] take care to this detail of @contextmanager: if an unhandled exception occurs in the block, it is reraised inside the generator at the point where the yield occurred
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5102
diff changeset
   443
        try:
e5d300d75519 [python] take care to this detail of @contextmanager: if an unhandled exception occurs in the block, it is reraised inside the generator at the point where the yield occurred
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5102
diff changeset
   444
            yield
e5d300d75519 [python] take care to this detail of @contextmanager: if an unhandled exception occurs in the block, it is reraised inside the generator at the point where the yield occurred
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5102
diff changeset
   445
        finally:
e5d300d75519 [python] take care to this detail of @contextmanager: if an unhandled exception occurs in the block, it is reraised inside the generator at the point where the yield occurred
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5102
diff changeset
   446
            for obj in appobjects:
e5d300d75519 [python] take care to this detail of @contextmanager: if an unhandled exception occurs in the block, it is reraised inside the generator at the point where the yield occurred
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5102
diff changeset
   447
                self.vreg.unregister(obj)
5102
04c84959cd85 [testlib] simple helper method to register temporary appobjects (hooks for instance) with a context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5055
diff changeset
   448
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   449
    # vregistry inspection utilities ###########################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   450
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   451
    def pviews(self, req, rset):
3468
b02fa4db2868 [tests] make unittest_viewselectors pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3454
diff changeset
   452
        return sorted((a.__regid__, a.__class__)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   453
                      for a in self.vreg['views'].possible_views(req, rset=rset))
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   454
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   455
    def pactions(self, req, rset,
3589
a5432f99f2d9 backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3468
diff changeset
   456
                 skipcategories=('addrelated', 'siteactions', 'useractions', 'footer')):
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3230
diff changeset
   457
        return [(a.__regid__, a.__class__)
2813
0cf6c8005bf6 R propagate deprecation of CWRegistry.possible_vobjects
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2793
diff changeset
   458
                for a in self.vreg['actions'].poss_visible_objects(req, rset=rset)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   459
                if a.category not in skipcategories]
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   460
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   461
    def pactions_by_cats(self, req, rset, categories=('addrelated',)):
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3230
diff changeset
   462
        return [(a.__regid__, a.__class__)
2813
0cf6c8005bf6 R propagate deprecation of CWRegistry.possible_vobjects
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2793
diff changeset
   463
                for a in self.vreg['actions'].poss_visible_objects(req, rset=rset)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   464
                if a.category in categories]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   465
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   466
    def pactionsdict(self, req, rset,
3589
a5432f99f2d9 backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3468
diff changeset
   467
                     skipcategories=('addrelated', 'siteactions', 'useractions', 'footer')):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   468
        res = {}
2813
0cf6c8005bf6 R propagate deprecation of CWRegistry.possible_vobjects
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2793
diff changeset
   469
        for a in self.vreg['actions'].poss_visible_objects(req, rset=rset):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   470
            if a.category not in skipcategories:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   471
                res.setdefault(a.category, []).append(a.__class__)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   472
        return res
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   473
3230
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   474
    def action_submenu(self, req, rset, id):
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   475
        return self._test_action(self.vreg['actions'].select(id, req, rset=rset))
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   476
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   477
    def _test_action(self, action):
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   478
        class fake_menu(list):
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   479
            @property
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   480
            def items(self):
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   481
                return self
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   482
        class fake_box(object):
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   483
            def mk_action(self, label, url, **kwargs):
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   484
                return (label, url)
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   485
            def box_action(self, action, **kwargs):
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   486
                return (action.title, action.url())
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   487
        submenu = fake_menu()
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   488
        action.fill_menu(fake_box(), submenu)
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   489
        return submenu
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   490
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   491
    def list_views_for(self, rset):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   492
        """returns the list of views that can be applied on `rset`"""
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   493
        req = rset.req
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   494
        only_once_vids = ('primary', 'secondary', 'text')
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   495
        req.data['ex'] = ValueError("whatever")
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   496
        viewsvreg = self.vreg['views']
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   497
        for vid, views in viewsvreg.items():
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   498
            if vid[0] == '_':
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   499
                continue
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   500
            if rset.rowcount > 1 and vid in only_once_vids:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   501
                continue
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   502
            views = [view for view in views
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   503
                     if view.category != 'startupview'
6454
97203d0af4cb [test/possible views] skip deprecated views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6442
diff changeset
   504
                     and not issubclass(view, notification.NotificationView)
97203d0af4cb [test/possible views] skip deprecated views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6442
diff changeset
   505
                     and not isinstance(view, class_deprecated)]
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   506
            if views:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   507
                try:
2774
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2770 2773
diff changeset
   508
                    view = viewsvreg._select_best(views, req, rset=rset)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   509
                    if view.linkable():
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   510
                        yield view
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   511
                    else:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   512
                        not_selected(self.vreg, view)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   513
                    # else the view is expected to be used as subview and should
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   514
                    # not be tested directly
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   515
                except NoSelectableObject:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   516
                    continue
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   517
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   518
    def list_actions_for(self, rset):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   519
        """returns the list of actions that can be applied on `rset`"""
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   520
        req = rset.req
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   521
        for action in self.vreg['actions'].possible_objects(req, rset=rset):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   522
            yield action
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   523
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   524
    def list_boxes_for(self, rset):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   525
        """returns the list of boxes that can be applied on `rset`"""
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   526
        req = rset.req
6141
b8287e54b528 [web api] unify 'contentnav' (VComponent) and 'boxes' registries as 'ctxcomponents' (CtxComponent)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6140
diff changeset
   527
        for box in self.vreg['ctxcomponents'].possible_objects(req, rset=rset):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   528
            yield box
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   529
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   530
    def list_startup_views(self):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   531
        """returns the list of startup views"""
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   532
        req = self.request()
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   533
        for view in self.vreg['views'].possible_views(req, None):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   534
            if view.category == 'startupview':
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3230
diff changeset
   535
                yield view.__regid__
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   536
            else:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   537
                not_selected(self.vreg, view)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   538
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   539
    # web ui testing utilities #################################################
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   540
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   541
    @property
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   542
    @cached
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   543
    def app(self):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   544
        """return a cubicweb publisher"""
4137
3a2d6c38bcd9 override publisher's error handler to ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4083
diff changeset
   545
        publisher = application.CubicWebPublisher(self.config, vreg=self.vreg)
3a2d6c38bcd9 override publisher's error handler to ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4083
diff changeset
   546
        def raise_error_handler(*args, **kwargs):
3a2d6c38bcd9 override publisher's error handler to ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4083
diff changeset
   547
            raise
3a2d6c38bcd9 override publisher's error handler to ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4083
diff changeset
   548
        publisher.error_handler = raise_error_handler
3a2d6c38bcd9 override publisher's error handler to ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4083
diff changeset
   549
        return publisher
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   550
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   551
    requestcls = fake.FakeRequest
6410
2e7a7b0829ed [test] fix tests broken by transaction behaviour on Unauthorized/ValidationError (no rollback but connection marked as non-commitable)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6408
diff changeset
   552
    def request(self, rollbackfirst=False, **kwargs):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   553
        """return a web ui request"""
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   554
        req = self.requestcls(self.vreg, form=kwargs)
6410
2e7a7b0829ed [test] fix tests broken by transaction behaviour on Unauthorized/ValidationError (no rollback but connection marked as non-commitable)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6408
diff changeset
   555
        if rollbackfirst:
2e7a7b0829ed [test] fix tests broken by transaction behaviour on Unauthorized/ValidationError (no rollback but connection marked as non-commitable)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6408
diff changeset
   556
            self.websession.cnx.rollback()
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
   557
        req.set_session(self.websession)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   558
        return req
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   559
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   560
    def remote_call(self, fname, *args):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   561
        """remote json call simulation"""
5377
84d14ddfae13 [python2.6] prefer python2.6's builtin json module over simplejson
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5268
diff changeset
   562
        dump = json.dumps
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   563
        args = [dump(arg) for arg in args]
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   564
        req = self.request(fname=fname, pageid='123', arg=args)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   565
        ctrl = self.vreg['controllers'].select('json', req)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   566
        return ctrl.publish(), req
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   567
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   568
    def app_publish(self, req, path='view'):
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   569
        return self.app.publish(path, req)
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   570
5247
3c8f6e6014f4 [testlib] ctrl_publish now allow to specify the controller id as argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5229
diff changeset
   571
    def ctrl_publish(self, req, ctrl='edit'):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   572
        """call the publish method of the edit controller"""
5942
f7768f44b4ac [tests] make sure controllers created by ctrl_publish() have a valid appli attribute
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5813
diff changeset
   573
        ctrl = self.vreg['controllers'].select(ctrl, req, appli=self.app)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   574
        try:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   575
            result = ctrl.publish()
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   576
            req.cnx.commit()
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   577
        except web.Redirect:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   578
            req.cnx.commit()
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   579
            raise
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   580
        return result
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   581
6166
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   582
    def req_from_url(self, url):
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   583
        """parses `url` and builds the corresponding CW-web request
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   584
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   585
        req.form will be setup using the url's query string
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   586
        """
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   587
        req = self.request()
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   588
        if isinstance(url, unicode):
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   589
            url = url.encode(req.encoding) # req.setup_params() expects encoded strings
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   590
        querystring = urlparse.urlparse(url)[-2]
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   591
        params = urlparse.parse_qs(querystring)
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   592
        req.setup_params(params)
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   593
        return req
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   594
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   595
    def url_publish(self, url):
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   596
        """takes `url`, uses application's app_resolver to find the
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   597
        appropriate controller, and publishes the result.
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   598
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   599
        This should pretty much correspond to what occurs in a real CW server
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   600
        except the apache-rewriter component is not called.
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   601
        """
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   602
        req = self.req_from_url(url)
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   603
        ctrlid, rset = self.app.url_resolver.process(req, req.relative_path(False))
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   604
        return self.ctrl_publish(req, ctrlid)
fc47b4e06d94 [devtools] add url_publish() method on CubicWebTC to test a full publishing process from the url_resolver to the html generation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6165
diff changeset
   605
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   606
    def expect_redirect(self, callback, req):
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   607
        """call the given callback with req as argument, expecting to get a
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   608
        Redirect exception
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   609
        """
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   610
        try:
4719
aaed3f813ef8 kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4690
diff changeset
   611
            callback(req)
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   612
        except Redirect, ex:
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   613
            try:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   614
                path, params = ex.location.split('?', 1)
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   615
            except ValueError:
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   616
                path = ex.location
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   617
                params = {}
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   618
            else:
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   619
                cleanup = lambda p: (p[0], unquote(p[1]))
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   620
                params = dict(cleanup(p.split('=', 1)) for p in params.split('&') if p)
5036
3e436a494ce3 [testlib] __errorurl may be a relative path, check we're actually removing base url first
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5020
diff changeset
   621
            if path.startswith(req.base_url()): # may be relative
3e436a494ce3 [testlib] __errorurl may be a relative path, check we're actually removing base url first
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5020
diff changeset
   622
                path = path[len(req.base_url()):]
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   623
            return path, params
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   624
        else:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   625
            self.fail('expected a Redirect exception')
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   626
4523
d8127c2bd6b5 use 'edit' as defaut path for expect_redirect_publish
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4513
diff changeset
   627
    def expect_redirect_publish(self, req, path='edit'):
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   628
        """call the publish method of the application publisher, expecting to
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   629
        get a Redirect exception
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   630
        """
4083
3b285889b8e9 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4053
diff changeset
   631
        return self.expect_redirect(lambda x: self.app_publish(x, path), req)
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   632
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   633
    def init_authentication(self, authmode, anonuser=None):
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   634
        self.set_option('auth-mode', authmode)
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   635
        self.set_option('anonymous-user', anonuser)
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   636
        req = self.request()
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
   637
        origsession = req.session
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
   638
        req.session = req.cnx = None
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
   639
        del req.execute # get back to class implementation
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   640
        sh = self.app.session_handler
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   641
        authm = sh.session_manager.authmanager
4916
4b8cdda342ae fix tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   642
        authm.anoninfo = self.vreg.config.anonymous_user()
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
   643
        authm.anoninfo = authm.anoninfo[0], {'password': authm.anoninfo[1]}
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   644
        # not properly cleaned between tests
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   645
        self.open_sessions = sh.session_manager._sessions = {}
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
   646
        return req, origsession
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   647
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
   648
    def assertAuthSuccess(self, req, origsession, nbsessions=1):
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   649
        sh = self.app.session_handler
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   650
        path, params = self.expect_redirect(lambda x: self.app.connect(x), req)
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
   651
        session = req.session
6369
a151453dc564 [test] more update to unittest2 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6346
diff changeset
   652
        self.assertEqual(len(self.open_sessions), nbsessions, self.open_sessions)
a151453dc564 [test] more update to unittest2 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6346
diff changeset
   653
        self.assertEqual(session.login, origsession.login)
a151453dc564 [test] more update to unittest2 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6346
diff changeset
   654
        self.assertEqual(session.anonymous_session, False)
a151453dc564 [test] more update to unittest2 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6346
diff changeset
   655
        self.assertEqual(path, 'view')
a151453dc564 [test] more update to unittest2 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6346
diff changeset
   656
        self.assertEqual(params, {'__message': 'welcome %s !' % req.user.login})
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   657
3676
fa170564e1a0 optional nbsessions arg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3657
diff changeset
   658
    def assertAuthFailure(self, req, nbsessions=0):
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
   659
        self.app.connect(req)
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5177
diff changeset
   660
        self.assertIsInstance(req.session, DBAPISession)
6369
a151453dc564 [test] more update to unittest2 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6346
diff changeset
   661
        self.assertEqual(req.session.cnx, None)
a151453dc564 [test] more update to unittest2 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6346
diff changeset
   662
        self.assertEqual(req.cnx, None)
a151453dc564 [test] more update to unittest2 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6346
diff changeset
   663
        self.assertEqual(len(self.open_sessions), nbsessions)
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   664
        clear_cache(req, 'get_authorization')
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   665
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   666
    # content validation #######################################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   667
6687
0b1f5c14646e [shell, testlib] return doc test results in process_script and new assertDocTestFile method on CubicWebTC
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6631
diff changeset
   668
    def assertDocTestFile(self, testfile):
0b1f5c14646e [shell, testlib] return doc test results in process_script and new assertDocTestFile method on CubicWebTC
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6631
diff changeset
   669
        # doctest returns tuple (failure_count, test_count)
6694
d9eac4fd57dc [devtools] make migration handler easily available from CubicWebTC through .shell() method
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6687
diff changeset
   670
        result = self.shell().process_script(testfile)
6687
0b1f5c14646e [shell, testlib] return doc test results in process_script and new assertDocTestFile method on CubicWebTC
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6631
diff changeset
   671
        if result[0] and result[1]:
0b1f5c14646e [shell, testlib] return doc test results in process_script and new assertDocTestFile method on CubicWebTC
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6631
diff changeset
   672
            raise self.failureException("doctest file '%s' failed"
0b1f5c14646e [shell, testlib] return doc test results in process_script and new assertDocTestFile method on CubicWebTC
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6631
diff changeset
   673
                                        % testfile)
0b1f5c14646e [shell, testlib] return doc test results in process_script and new assertDocTestFile method on CubicWebTC
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6631
diff changeset
   674
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   675
    # validators are used to validate (XML, DTD, whatever) view's content
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   676
    # validators availables are :
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   677
    #  DTDValidator : validates XML + declared DTD
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   678
    #  SaxOnlyValidator : guarantees XML is well formed
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   679
    #  None : do not try to validate anything
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   680
    # validators used must be imported from from.devtools.htmlparser
534
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
   681
    content_type_validators = {
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
   682
        # maps MIME type : validator name
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
   683
        #
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
   684
        # do not set html validators here, we need HTMLValidator for html
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
   685
        # snippets
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
   686
        #'text/html': DTDValidator,
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
   687
        #'application/xhtml+xml': DTDValidator,
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   688
        'application/xml': htmlparser.SaxOnlyValidator,
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   689
        'text/xml': htmlparser.SaxOnlyValidator,
534
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
   690
        'text/plain': None,
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
   691
        'text/comma-separated-values': None,
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
   692
        'text/x-vcard': None,
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
   693
        'text/calendar': None,
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
   694
        'application/json': None,
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
   695
        'image/png': None,
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
   696
        }
562
bdadb26c4a3c old .validators attribute is now .vid_validators
sylvain.thenault@logilab.fr
parents: 549
diff changeset
   697
    # maps vid : validator name (override content_type_validators)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   698
    vid_validators = dict((vid, htmlparser.VALMAP[valkey])
562
bdadb26c4a3c old .validators attribute is now .vid_validators
sylvain.thenault@logilab.fr
parents: 549
diff changeset
   699
                          for vid, valkey in VIEW_VALIDATORS.iteritems())
1605
b239744627c7 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1152
diff changeset
   700
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   701
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   702
    def view(self, vid, rset=None, req=None, template='main-template',
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   703
             **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   704
        """This method tests the view `vid` on `rset` using `template`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   705
6109
47d9c0e0f7b7 integrate Celso's work on translation file: proper/complete spanish translation, fixed some typos in french translation, occured -> occurred fix in various places
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5974
diff changeset
   706
        If no error occurred while rendering the view, the HTML is analyzed
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   707
        and parsed.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   708
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   709
        :returns: an instance of `cubicweb.devtools.htmlparser.PageInfo`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   710
                  encapsulation the generated HTML
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   711
        """
1142
03a9acf3c299 rset may be None
sylvain.thenault@logilab.fr
parents: 1133
diff changeset
   712
        req = req or rset and rset.req or self.request()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   713
        req.form['vid'] = vid
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   714
        kwargs['rset'] = rset
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
   715
        viewsreg = self.vreg['views']
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
   716
        view = viewsreg.select(vid, req, **kwargs)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   717
        # set explicit test description
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   718
        if rset is not None:
5875
e707d481219e [testlib] improve test description messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5815
diff changeset
   719
            self.set_description("testing vid=%s defined in %s with (%s)" % (
2070
d98f5be5e371 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   720
                vid, view.__module__, rset.printable_rql()))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   721
        else:
5875
e707d481219e [testlib] improve test description messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5815
diff changeset
   722
            self.set_description("testing vid=%s defined in %s without rset" % (
2070
d98f5be5e371 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   723
                vid, view.__module__))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   724
        if template is None: # raw view testing, no template
1773
789ee507a8e0 use render
sylvain.thenault@logilab.fr
parents: 1605
diff changeset
   725
            viewfunc = view.render
829
ea092805d8f8 The main template is now a simple view, there's no need to monkey patch TheMainTemplate._select_view_and_rset
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 823
diff changeset
   726
        else:
ea092805d8f8 The main template is now a simple view, there's no need to monkey patch TheMainTemplate._select_view_and_rset
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 823
diff changeset
   727
            kwargs['view'] = view
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
   728
            templateview = viewsreg.select(template, req, **kwargs)
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
   729
            viewfunc = lambda **k: viewsreg.main_template(req, template,
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
   730
                                                          **kwargs)
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   731
        kwargs.pop('rset')
829
ea092805d8f8 The main template is now a simple view, there's no need to monkey patch TheMainTemplate._select_view_and_rset
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 823
diff changeset
   732
        return self._test_view(viewfunc, view, template, kwargs)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   733
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   734
829
ea092805d8f8 The main template is now a simple view, there's no need to monkey patch TheMainTemplate._select_view_and_rset
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 823
diff changeset
   735
    def _test_view(self, viewfunc, view, template='main-template', kwargs={}):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   736
        """this method does the actual call to the view
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   737
6109
47d9c0e0f7b7 integrate Celso's work on translation file: proper/complete spanish translation, fixed some typos in french translation, occured -> occurred fix in various places
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5974
diff changeset
   738
        If no error occurred while rendering the view, the HTML is analyzed
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   739
        and parsed.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   740
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   741
        :returns: an instance of `cubicweb.devtools.htmlparser.PageInfo`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   742
                  encapsulation the generated HTML
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   743
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   744
        output = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   745
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   746
            output = viewfunc(**kwargs)
534
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
   747
            return self._check_html(output, view, template)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   748
        except (SystemExit, KeyboardInterrupt):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   749
            raise
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   750
        except:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   751
            # hijack exception: generative tests stop when the exception
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   752
            # is not an AssertionError
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   753
            klass, exc, tcbk = sys.exc_info()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   754
            try:
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3230
diff changeset
   755
                msg = '[%s in %s] %s' % (klass, view.__regid__, exc)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   756
            except:
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3230
diff changeset
   757
                msg = '[%s in %s] undisplayable exception' % (klass, view.__regid__)
6524
eca20b012104 [test] take care to unicode __regid__ (usually when using _()), may lead to later unicode decode error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6470
diff changeset
   758
            msg = str(msg) # ensure no unicode
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   759
            if output is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   760
                position = getattr(exc, "position", (0,))[0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   761
                if position:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   762
                    # define filter
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   763
                    output = output.splitlines()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   764
                    width = int(log(len(output), 10)) + 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   765
                    line_template = " %" + ("%i" % width) + "i: %s"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   766
                    # XXX no need to iterate the whole file except to get
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   767
                    # the line number
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   768
                    output = '\n'.join(line_template % (idx + 1, line)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   769
                                for idx, line in enumerate(output)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   770
                                if line_context_filter(idx+1, position))
1133
8a409ea0c9ec more linting
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   771
                    msg += '\nfor output:\n%s' % output
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   772
            raise AssertionError, msg, tcbk
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   773
427
e894eec21a1b move selection of entity types to test in a method to ease overriding
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   774
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   775
    @nocoverage
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   776
    def _check_html(self, output, view, template='main-template'):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   777
        """raises an exception if the HTML is invalid"""
6772
68bb0943d192 [test, html validation] make validator selection somewhat smarter (at least it works properly when content is demoted from xhtml to html, making the XMLDemotingValidator class useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6721
diff changeset
   778
        output = output.strip()
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   779
        try:
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3230
diff changeset
   780
            validatorclass = self.vid_validators[view.__regid__]
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   781
        except KeyError:
3998
94cc7cad3d2d backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777 3956
diff changeset
   782
            if view.content_type in ('text/html', 'application/xhtml+xml'):
6772
68bb0943d192 [test, html validation] make validator selection somewhat smarter (at least it works properly when content is demoted from xhtml to html, making the XMLDemotingValidator class useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6721
diff changeset
   783
                if output.startswith('<?xml'):
68bb0943d192 [test, html validation] make validator selection somewhat smarter (at least it works properly when content is demoted from xhtml to html, making the XMLDemotingValidator class useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6721
diff changeset
   784
                    default_validator = htmlparser.DTDValidator
68bb0943d192 [test, html validation] make validator selection somewhat smarter (at least it works properly when content is demoted from xhtml to html, making the XMLDemotingValidator class useless
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6721
diff changeset
   785
                else:
3998
94cc7cad3d2d backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777 3956
diff changeset
   786
                    default_validator = htmlparser.HTMLValidator
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   787
            else:
3998
94cc7cad3d2d backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777 3956
diff changeset
   788
                default_validator = None
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   789
            validatorclass = self.content_type_validators.get(view.content_type,
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   790
                                                              default_validator)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   791
        if validatorclass is None:
6774
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6772
diff changeset
   792
            return
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   793
        validator = validatorclass()
4346
02f12f39bae5 XXX quick fix html validation by removing <canvas>
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   794
        if isinstance(validator, htmlparser.DTDValidator):
02f12f39bae5 XXX quick fix html validation by removing <canvas>
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   795
            # XXX remove <canvas> used in progress widget, unknown in html dtd
02f12f39bae5 XXX quick fix html validation by removing <canvas>
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   796
            output = re.sub('<canvas.*?></canvas>', '', output)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   797
        return validator.parse_string(output.strip())
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   798
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   799
    # deprecated ###############################################################
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   800
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5159
diff changeset
   801
    @deprecated('[3.8] use self.execute(...).get_entity(0, 0)')
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5159
diff changeset
   802
    def entity(self, rql, args=None, eidkey=None, req=None):
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5159
diff changeset
   803
        if eidkey is not None:
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5159
diff changeset
   804
            warn('[3.8] eidkey is deprecated, you can safely remove this argument',
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5159
diff changeset
   805
                 DeprecationWarning, stacklevel=2)
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5159
diff changeset
   806
        return self.execute(rql, args, req=req).get_entity(0, 0)
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5159
diff changeset
   807
4016
250784a2d105 deprecated one more custom add_entity in favor of req.create_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   808
    @deprecated('[3.6] use self.request().create_entity(...)')
250784a2d105 deprecated one more custom add_entity in favor of req.create_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   809
    def add_entity(self, etype, req=None, **kwargs):
250784a2d105 deprecated one more custom add_entity in favor of req.create_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   810
        if req is None:
250784a2d105 deprecated one more custom add_entity in favor of req.create_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   811
            req = self.request()
250784a2d105 deprecated one more custom add_entity in favor of req.create_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   812
        return req.create_entity(etype, **kwargs)
250784a2d105 deprecated one more custom add_entity in favor of req.create_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3998
diff changeset
   813
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   814
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   815
# auto-populating test classes and utilities ###################################
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   816
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   817
from cubicweb.devtools.fill import insert_entity_queries, make_relations_queries
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   818
4513
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   819
# XXX cleanup unprotected_entities & all mess
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   820
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   821
def how_many_dict(schema, cursor, how_many, skip):
4513
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   822
    """given a schema, compute how many entities by type we need to be able to
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   823
    satisfy relations cardinality.
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   824
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   825
    The `how_many` argument tells how many entities of which type we want at
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   826
    least.
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   827
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   828
    Return a dictionary with entity types as key, and the number of entities for
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   829
    this type as value.
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   830
    """
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   831
    relmap = {}
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   832
    for rschema in schema.relations():
3720
5376aaadd16b backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3676 3689
diff changeset
   833
        if rschema.final:
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   834
            continue
4053
7cc66b1d9183 more api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 4036
diff changeset
   835
        for subj, obj in rschema.rdefs:
7cc66b1d9183 more api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 4036
diff changeset
   836
            card = rschema.rdef(subj, obj).cardinality
4513
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   837
            # if the relation is mandatory, we'll need at least as many subj and
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   838
            # obj to satisfy it
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   839
            if card[0] in '1+' and card[1] in '1?':
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   840
                # subj has to be linked to at least one obj,
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   841
                # but obj can be linked to only one subj
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   842
                # -> we need at least as many subj as obj to satisfy
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   843
                #    cardinalities for this relation
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   844
                relmap.setdefault((rschema, subj), []).append(str(obj))
4513
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   845
            if card[1] in '1+' and card[0] in '1?':
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   846
                # reverse subj and obj in the above explanation
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   847
                relmap.setdefault((rschema, obj), []).append(str(subj))
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   848
    unprotected = unprotected_entities(schema)
4513
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   849
    for etype in skip: # XXX (syt) duh? explain or kill
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   850
        unprotected.add(etype)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   851
    howmanydict = {}
4513
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   852
    # step 1, compute a base number of each entity types: number of already
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   853
    # existing entities of this type + `how_many`
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   854
    for etype in unprotected_entities(schema, strict=True):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   855
        howmanydict[str(etype)] = cursor.execute('Any COUNT(X) WHERE X is %s' % etype)[0][0]
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   856
        if etype in unprotected:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   857
            howmanydict[str(etype)] += how_many
4513
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   858
    # step 2, augment nb entity per types to satisfy cardinality constraints,
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   859
    # by recomputing for each relation that constrained an entity type:
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   860
    #
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   861
    # new num for etype = max(current num, sum(num for possible target etypes))
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   862
    #
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
   863
    # XXX we should first check there is no cycle then propagate changes
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   864
    for (rschema, etype), targets in relmap.iteritems():
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   865
        relfactor = sum(howmanydict[e] for e in targets)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   866
        howmanydict[str(etype)] = max(relfactor, howmanydict[etype])
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   867
    return howmanydict
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   868
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   869
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   870
class AutoPopulateTest(CubicWebTC):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   871
    """base class for test with auto-populating of the database"""
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   872
    __abstract__ = True
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   873
5913
85240b3f9ee4 [test] add default generic test Tags to CubicWeb TestCase class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5875
diff changeset
   874
    tags = CubicWebTC.tags | Tags('autopopulated')
85240b3f9ee4 [test] add default generic test Tags to CubicWeb TestCase class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5875
diff changeset
   875
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   876
    pdbclass = CubicWebDebugger
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   877
    # this is a hook to be able to define a list of rql queries
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   878
    # that are application dependent and cannot be guessed automatically
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   879
    application_rql = []
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   880
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   881
    no_auto_populate = ()
3734
b2107f68176c ignored_relations should be a set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3720
diff changeset
   882
    ignored_relations = set()
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   883
427
e894eec21a1b move selection of entity types to test in a method to ease overriding
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   884
    def to_test_etypes(self):
e894eec21a1b move selection of entity types to test in a method to ease overriding
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   885
        return unprotected_entities(self.schema, strict=True)
1605
b239744627c7 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1152
diff changeset
   886
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   887
    def custom_populate(self, how_many, cursor):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   888
        pass
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   889
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   890
    def post_populate(self, cursor):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   891
        pass
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   892
3720
5376aaadd16b backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3676 3689
diff changeset
   893
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   894
    @nocoverage
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   895
    def auto_populate(self, how_many):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   896
        """this method populates the database with `how_many` entities
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   897
        of each possible type. It also inserts random relations between them
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   898
        """
5004
4cc020ee70e2 le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4958
diff changeset
   899
        with security_enabled(self.session, read=False, write=False):
4cc020ee70e2 le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4958
diff changeset
   900
            self._auto_populate(how_many)
4cc020ee70e2 le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4958
diff changeset
   901
4cc020ee70e2 le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4958
diff changeset
   902
    def _auto_populate(self, how_many):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   903
        cu = self.cursor()
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   904
        self.custom_populate(how_many, cu)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   905
        vreg = self.vreg
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   906
        howmanydict = how_many_dict(self.schema, cu, how_many, self.no_auto_populate)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   907
        for etype in unprotected_entities(self.schema):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   908
            if etype in self.no_auto_populate:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   909
                continue
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   910
            nb = howmanydict.get(etype, how_many)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   911
            for rql, args in insert_entity_queries(etype, self.schema, vreg, nb):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   912
                cu.execute(rql, args)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   913
        edict = {}
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   914
        for etype in unprotected_entities(self.schema, strict=True):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   915
            rset = cu.execute('%s X' % etype)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   916
            edict[str(etype)] = set(row[0] for row in rset.rows)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   917
        existingrels = {}
4036
137be4878127 [mq]: fix_set_addition
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 4016
diff changeset
   918
        ignored_relations = SYSTEM_RELATIONS | self.ignored_relations
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   919
        for rschema in self.schema.relations():
3720
5376aaadd16b backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3676 3689
diff changeset
   920
            if rschema.final or rschema in ignored_relations:
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   921
                continue
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   922
            rset = cu.execute('DISTINCT Any X,Y WHERE X %s Y' % rschema)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   923
            existingrels.setdefault(rschema.type, set()).update((x, y) for x, y in rset)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   924
        q = make_relations_queries(self.schema, edict, cu, ignored_relations,
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   925
                                   existingrels=existingrels)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   926
        for rql, args in q:
3998
94cc7cad3d2d backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777 3956
diff changeset
   927
            try:
94cc7cad3d2d backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777 3956
diff changeset
   928
                cu.execute(rql, args)
94cc7cad3d2d backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777 3956
diff changeset
   929
            except ValidationError, ex:
94cc7cad3d2d backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777 3956
diff changeset
   930
                # failed to satisfy some constraint
94cc7cad3d2d backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777 3956
diff changeset
   931
                print 'error in automatic db population', ex
6469
82cf41ae8b89 [test] reset uncommitable flag on ValidationError raised during automatic db population
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6441
diff changeset
   932
                self.session.commit_state = None # reset uncommitable flag
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   933
        self.post_populate(cu)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   934
        self.commit()
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   935
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   936
    def iter_individual_rsets(self, etypes=None, limit=None):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   937
        etypes = etypes or self.to_test_etypes()
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   938
        for etype in etypes:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   939
            if limit:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   940
                rql = 'Any X LIMIT %s WHERE X is %s' % (limit, etype)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   941
            else:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   942
                rql = 'Any X WHERE X is %s' % etype
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   943
            rset = self.execute(rql)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   944
            for row in xrange(len(rset)):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   945
                if limit and row > limit:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   946
                    break
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   947
                # XXX iirk
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   948
                rset2 = rset.limit(limit=1, offset=row)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   949
                yield rset2
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   950
1004
625e59773119 fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 563
diff changeset
   951
    def iter_automatic_rsets(self, limit=10):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   952
        """generates basic resultsets for each entity type"""
427
e894eec21a1b move selection of entity types to test in a method to ease overriding
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   953
        etypes = self.to_test_etypes()
2219
bb5098e74b82 protect against empty etypes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2070
diff changeset
   954
        if not etypes:
bb5098e74b82 protect against empty etypes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2070
diff changeset
   955
            return
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   956
        for etype in etypes:
1004
625e59773119 fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 563
diff changeset
   957
            yield self.execute('Any X LIMIT %s WHERE X is %s' % (limit, etype))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   958
        etype1 = etypes.pop()
1775
f450f1594992 fix in case only one type is tested
sylvain.thenault@logilab.fr
parents: 1773
diff changeset
   959
        try:
f450f1594992 fix in case only one type is tested
sylvain.thenault@logilab.fr
parents: 1773
diff changeset
   960
            etype2 = etypes.pop()
f450f1594992 fix in case only one type is tested
sylvain.thenault@logilab.fr
parents: 1773
diff changeset
   961
        except KeyError:
f450f1594992 fix in case only one type is tested
sylvain.thenault@logilab.fr
parents: 1773
diff changeset
   962
            etype2 = etype1
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   963
        # test a mixed query (DISTINCT/GROUP to avoid getting duplicate
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   964
        # X which make muledit view failing for instance (html validation fails
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   965
        # because of some duplicate "id" attributes)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   966
        yield self.execute('DISTINCT Any X, MAX(Y) GROUPBY X WHERE X is %s, Y is %s' % (etype1, etype2))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   967
        # test some application-specific queries if defined
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   968
        for rql in self.application_rql:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   969
            yield self.execute(rql)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   970
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   971
    def _test_everything_for(self, rset):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   972
        """this method tries to find everything that can be tested
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   973
        for `rset` and yields a callable test (as needed in generative tests)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   974
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   975
        propdefs = self.vreg['propertydefs']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   976
        # make all components visible
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   977
        for k, v in propdefs.items():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   978
            if k.endswith('visible') and not v['default']:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   979
                propdefs[k]['default'] = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   980
        for view in self.list_views_for(rset):
3764
034aa14b740a drop _prepare_copy method from rset in favor of a more generic copy method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   981
            backup_rset = rset.copy(rset.rows, rset.description)
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3230
diff changeset
   982
            yield InnerTest(self._testname(rset, view.__regid__, 'view'),
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3230
diff changeset
   983
                            self.view, view.__regid__, rset,
823
cb8ccbef8fa5 main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 563
diff changeset
   984
                            rset.req.reset_headers(), 'main-template')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   985
            # We have to do this because some views modify the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   986
            # resultset's syntax tree
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   987
            rset = backup_rset
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   988
        for action in self.list_actions_for(rset):
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3230
diff changeset
   989
            yield InnerTest(self._testname(rset, action.__regid__, 'action'), self._test_action, action)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   990
        for box in self.list_boxes_for(rset):
6140
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6109
diff changeset
   991
            w = [].append
65a619eb31c4 [boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6109
diff changeset
   992
            yield InnerTest(self._testname(rset, box.__regid__, 'box'), box.render, w)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   993
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   994
    @staticmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   995
    def _testname(rset, objid, objtype):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   996
        return '%s_%s_%s' % ('_'.join(rset.column_types(0)), objid, objtype)
1605
b239744627c7 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1152
diff changeset
   997
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   998
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   999
# concrete class for automated application testing  ############################
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1000
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1001
class AutomaticWebTest(AutoPopulateTest):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1002
    """import this if you wan automatic tests to be ran"""
5913
85240b3f9ee4 [test] add default generic test Tags to CubicWeb TestCase class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5875
diff changeset
  1003
85240b3f9ee4 [test] add default generic test Tags to CubicWeb TestCase class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5875
diff changeset
  1004
    tags = AutoPopulateTest.tags | Tags('web', 'generated')
85240b3f9ee4 [test] add default generic test Tags to CubicWeb TestCase class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5875
diff changeset
  1005
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1006
    def setUp(self):
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1007
        AutoPopulateTest.setUp(self)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1008
        # access to self.app for proper initialization of the authentication
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1009
        # machinery (else some views may fail)
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1010
        self.app
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1011
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1012
    ## one each
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1013
    def test_one_each_config(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1014
        self.auto_populate(1)
1004
625e59773119 fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 563
diff changeset
  1015
        for rset in self.iter_automatic_rsets(limit=1):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1016
            for testargs in self._test_everything_for(rset):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1017
                yield testargs
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1018
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1019
    ## ten each
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1020
    def test_ten_each_config(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1021
        self.auto_populate(10)
1004
625e59773119 fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 563
diff changeset
  1022
        for rset in self.iter_automatic_rsets(limit=10):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1023
            for testargs in self._test_everything_for(rset):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1024
                yield testargs
1605
b239744627c7 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1152
diff changeset
  1025
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1026
    ## startup views
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1027
    def test_startup_views(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1028
        for vid in self.list_startup_views():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1029
            req = self.request()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1030
            yield self.view, vid, None, req
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1031
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1032
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1033
# registry instrumentization ###################################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1034
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
  1035
def not_selected(vreg, appobject):
534
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
  1036
    try:
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
  1037
        vreg._selected[appobject.__class__] -= 1
534
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
  1038
    except (KeyError, AttributeError):
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
  1039
        pass
1605
b239744627c7 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1152
diff changeset
  1040
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1041
534
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
  1042
def vreg_instrumentize(testclass):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1043
    # XXX broken
534
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
  1044
    from cubicweb.devtools.apptest import TestEnvironment
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1045
    env = testclass._env = TestEnvironment('data', configcls=testclass.configcls)
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
  1046
    for reg in env.vreg.values():
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
  1047
        reg._selected = {}
2668
979c7ccb4a86 [testlib] take care of re-monkeypatching which'll cause infinite recursion error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
  1048
        try:
979c7ccb4a86 [testlib] take care of re-monkeypatching which'll cause infinite recursion error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
  1049
            orig_select_best = reg.__class__.__orig_select_best
979c7ccb4a86 [testlib] take care of re-monkeypatching which'll cause infinite recursion error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
  1050
        except:
2770
356e9d7c356d R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2668
diff changeset
  1051
            orig_select_best = reg.__class__._select_best
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
  1052
        def instr_select_best(self, *args, **kwargs):
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
  1053
            selected = orig_select_best(self, *args, **kwargs)
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
  1054
            try:
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
  1055
                self._selected[selected.__class__] += 1
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
  1056
            except KeyError:
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
  1057
                self._selected[selected.__class__] = 1
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
  1058
            except AttributeError:
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
  1059
                pass # occurs on reg used to restore database
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
  1060
            return selected
2770
356e9d7c356d R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2668
diff changeset
  1061
        reg.__class__._select_best = instr_select_best
2668
979c7ccb4a86 [testlib] take care of re-monkeypatching which'll cause infinite recursion error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
  1062
        reg.__class__.__orig_select_best = orig_select_best
534
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
  1063
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1064
534
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
  1065
def print_untested_objects(testclass, skipregs=('hooks', 'etypes')):
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
  1066
    for regname, reg in testclass._env.vreg.iteritems():
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2234
diff changeset
  1067
        if regname in skipregs:
534
1368c80276bc refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents: 514
diff changeset
  1068
            continue
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
  1069
        for appobjects in reg.itervalues():
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
  1070
            for appobject in appobjects:
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
  1071
                if not reg._selected.get(appobject):
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
  1072
                    print 'not tested', regname, appobject