cubicweb/devtools/testlib.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Mon, 24 Oct 2016 09:31:55 +0200
changeset 11744 a6dc650bc230
parent 11725 904ee9cd0cf9
child 11748 c6d37774f4d0
permissions -rw-r--r--
[test] Replace logilab-common's with_tempdir by tempfile.TemporaryDirectory logilab-common's with_tempdir decorator, which is considered harmful (see https://www.logilab.org/ticket/8267966). Rely on backports.tempfile to provide TemporaryDirectory on Python 2. Re-export it in testlib module for convenience.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11266
ea25d87a8537 [devtools] show DeprecationWarning during test execution
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
     1
# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
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
     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"""
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10569
diff changeset
    19
from __future__ import print_function
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10569
diff changeset
    20
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
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
    22
import re
11266
ea25d87a8537 [devtools] show DeprecationWarning during test execution
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
    23
import warnings
6631
26c303c3f1aa [test] make test independant from the working directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6590
diff changeset
    24
from os.path import dirname, join, abspath
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
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
    26
from contextlib import contextmanager
11182
66c44d3d97af [devtools/testlib] Issue a RuntimeError when a generative tests is detected
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11181
diff changeset
    27
from inspect import isgeneratorfunction
8460
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
    28
from itertools import chain
11744
a6dc650bc230 [test] Replace logilab-common's with_tempdir by tempfile.TemporaryDirectory
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11725
diff changeset
    29
import tempfile
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
10675
e0db937f5add [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10663
diff changeset
    31
from six import text_type, string_types
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10603
diff changeset
    32
from six.moves import range
10603
65ad6980976e [py3k] import URL mangling functions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10594
diff changeset
    33
from six.moves.urllib.parse import urlparse, parse_qs, unquote as urlunquote
65ad6980976e [py3k] import URL mangling functions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10594
diff changeset
    34
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    35
import yams.schema
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
11184
2a7b98d91736 [testlib] Import nocoverage from logilab.common.testlib
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11182
diff changeset
    37
from logilab.common.testlib import Tags, nocoverage
2773
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
7222
fcb8932082a5 [testlib] refactor create_user and grant_permission to make them usable from pre_setup_database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7163
diff changeset
    40
from logilab.common.decorators import cached, classproperty, clear_cache, iclassmethod
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
9580
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9570
diff changeset
    44
from cubicweb import (ValidationError, NoSelectableObject, AuthenticationError,
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
    45
                      BadConnectionId)
9580
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9570
diff changeset
    46
from cubicweb import cwconfig, devtools, web, server, repoapi
8652
7812093e21f7 [testlib] test_view now parse json data, closes #2557467
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8628
diff changeset
    47
from cubicweb.utils import json
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    48
from cubicweb.sobjects import notification
10219
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
    49
from cubicweb.web import Redirect, application, eid_param
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
9117
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
    51
from cubicweb.server.session import 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
    52
from cubicweb.devtools import SYSTEM_ENTITIES, SYSTEM_RELATIONS, VIEW_VALIDATORS
8930
6a02be304486 remove unused import
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8725
diff changeset
    53
from cubicweb.devtools import fake, htmlparser, DEFAULT_EMPTY_DB_ID
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
    54
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
11076
403a901b6b1e [devtools] Re-implement generative tests using subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11069
diff changeset
    56
if sys.version_info[:2] < (3, 4):
11181
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    57
    from unittest2 import TestCase
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    58
    if not hasattr(TestCase, 'subTest'):
11076
403a901b6b1e [devtools] Re-implement generative tests using subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11069
diff changeset
    59
        raise ImportError('no subTest support in available unittest2')
11744
a6dc650bc230 [test] Replace logilab-common's with_tempdir by tempfile.TemporaryDirectory
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11725
diff changeset
    60
    from backports.tempfile import TemporaryDirectory  # noqa
11076
403a901b6b1e [devtools] Re-implement generative tests using subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11069
diff changeset
    61
else:
11181
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    62
    from unittest import TestCase
11744
a6dc650bc230 [test] Replace logilab-common's with_tempdir by tempfile.TemporaryDirectory
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11725
diff changeset
    63
    from tempfile import TemporaryDirectory  # noqa
11181
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    64
11266
ea25d87a8537 [devtools] show DeprecationWarning during test execution
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
    65
# in python 2.7, DeprecationWarning are not shown anymore by default
ea25d87a8537 [devtools] show DeprecationWarning during test execution
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
    66
warnings.filterwarnings('default', category=DeprecationWarning)
ea25d87a8537 [devtools] show DeprecationWarning during test execution
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11195
diff changeset
    67
11181
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    68
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    69
# provide a data directory for the test class ##################################
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    70
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    71
class BaseTestCase(TestCase):
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    72
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    73
    @classproperty
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    74
    @cached
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11270
diff changeset
    75
    def datadir(cls):  # pylint: disable=E0213
11181
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    76
        """helper attribute holding the standard test's data directory
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    77
        """
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    78
        mod = sys.modules[cls.__module__]
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    79
        return join(dirname(abspath(mod.__file__)), 'data')
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    80
    # cache it (use a class method to cache on class since TestCase is
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    81
    # instantiated for each test run)
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    82
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    83
    @classmethod
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    84
    def datapath(cls, *fname):
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    85
        """joins the object's datadir and `fname`"""
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    86
        return join(cls.datadir, *fname)
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    87
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    88
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    89
if hasattr(BaseTestCase, 'assertItemsEqual'):
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
    90
    BaseTestCase.assertCountEqual = BaseTestCase.assertItemsEqual
11076
403a901b6b1e [devtools] Re-implement generative tests using subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11069
diff changeset
    91
403a901b6b1e [devtools] Re-implement generative tests using subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11069
diff changeset
    92
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
    93
# low-level utilities ##########################################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
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
    96
    """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
    97
    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
    98
    """
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
    def do_view(self, arg):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
        import webbrowser
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        data = self._getval(arg)
10614
57dfde80df11 [py3k] file → open
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10612
diff changeset
   102
        with open('/tmp/toto.html', 'w') as toto:
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   103
            toto.write(data)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
        webbrowser.open('file:///tmp/toto.html')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   106
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
def line_context_filter(line_no, center, before=3, after=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
    """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
   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
    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
   111
    """
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
    if after is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
        after = before
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
    return center - before <= line_no <= center + after
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   116
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   117
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
   118
    """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
   119
    (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
   120
    """
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   121
    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
   122
        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
   123
    else:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   124
        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
   125
    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
   126
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   127
8652
7812093e21f7 [testlib] test_view now parse json data, closes #2557467
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8628
diff changeset
   128
class JsonValidator(object):
7812093e21f7 [testlib] test_view now parse json data, closes #2557467
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8628
diff changeset
   129
    def parse_string(self, data):
10700
a6d9d27f4253 [web/views] port JSON views to py3k
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10689
diff changeset
   130
        return json.loads(data.decode('ascii'))
8652
7812093e21f7 [testlib] test_view now parse json data, closes #2557467
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8628
diff changeset
   131
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   132
9427
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   133
@contextmanager
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   134
def real_error_handling(app):
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   135
    """By default, CubicWebTC `app` attribute (ie the publisher) is monkey
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   136
    patched so that unexpected error are raised rather than going through the
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   137
    `error_handler` method.
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   138
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   139
    By using this context manager you disable this monkey-patching temporarily.
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   140
    Hence when publishihng a request no error will be raised, you'll get
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   141
    req.status_out set to an HTTP error status code and the generated page will
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   142
    usually hold a traceback as HTML.
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   143
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   144
    >>> with real_error_handling(app):
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   145
    >>>     page = app.handle_request(req)
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   146
    """
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   147
    # remove the monkey patched error handler
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   148
    fake_error_handler = app.error_handler
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   149
    del app.error_handler
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   150
    # return the app
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   151
    yield app
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   152
    # restore
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   153
    app.error_handler = fake_error_handler
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   154
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   155
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   156
# 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
   157
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   158
MAILBOX = []
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   159
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   160
9363
d773589b6d46 [mail] allow to specify SMTP's MAIL FROM address to config.sendmails(). Closes #3373620
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9255
diff changeset
   161
class Email(object):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   162
    """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
   163
    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
   164
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   165
    * `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
   166
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   167
    * `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
   168
      message
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   169
    """
9363
d773589b6d46 [mail] allow to specify SMTP's MAIL FROM address to config.sendmails(). Closes #3373620
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9255
diff changeset
   170
    def __init__(self, fromaddr, recipients, msg):
d773589b6d46 [mail] allow to specify SMTP's MAIL FROM address to config.sendmails(). Closes #3373620
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9255
diff changeset
   171
        self.fromaddr = fromaddr
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   172
        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
   173
        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
   174
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   175
    @property
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   176
    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
   177
        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
   178
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   179
    @property
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   180
    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
   181
        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
   182
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   183
    @property
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   184
    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
   185
        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
   186
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   187
    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
   188
        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
   189
                                                  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
   190
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   191
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   192
# 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
   193
# 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
   194
class MockSMTP:
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   195
2773
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 __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
   197
        pass
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   198
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   199
    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
   200
        pass
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   201
9363
d773589b6d46 [mail] allow to specify SMTP's MAIL FROM address to config.sendmails(). Closes #3373620
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9255
diff changeset
   202
    def sendmail(self, fromaddr, recipients, msg):
d773589b6d46 [mail] allow to specify SMTP's MAIL FROM address to config.sendmails(). Closes #3373620
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9255
diff changeset
   203
        MAILBOX.append(Email(fromaddr, recipients, msg))
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   204
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   205
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
   206
6424
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6410
diff changeset
   207
9117
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   208
# Repoaccess utility ###############################################3###########
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   209
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   210
class RepoAccess(object):
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   211
    """An helper to easily create object to access the repo as a specific user
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   212
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   213
    Each RepoAccess have it own session.
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   214
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   215
    A repo access can create three type of object:
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   216
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10353
diff changeset
   217
    .. automethod:: cubicweb.testlib.RepoAccess.cnx
9117
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   218
    .. automethod:: cubicweb.testlib.RepoAccess.web_request
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   219
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   220
    The RepoAccess need to be closed to destroy the associated Session.
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   221
    TestCase usually take care of this aspect for the user.
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   222
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   223
    .. automethod:: cubicweb.testlib.RepoAccess.close
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   224
    """
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   225
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   226
    def __init__(self, repo, login, requestcls):
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   227
        self._repo = repo
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   228
        self._login = login
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   229
        self.requestcls = requestcls
9757
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   230
        self._session = self._unsafe_connect(login)
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   231
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   232
    def _unsafe_connect(self, login, **kwargs):
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   233
        """ a completely unsafe connect method for the tests """
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   234
        # use an internal connection
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   235
        with self._repo.internal_cnx() as cnx:
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   236
            # try to get a user object
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   237
            user = cnx.find('CWUser', login=login).one()
9117
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   238
            user.groups
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   239
            user.properties
9757
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   240
            user.login
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   241
            session = Session(user, self._repo)
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   242
            self._repo._sessions[session.sessionid] = session
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   243
            user._cw = user.cw_rset.req = session
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   244
        with session.new_cnx() as cnx:
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   245
            self._repo.hm.call_hooks('session_open', cnx)
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   246
            # commit connection at this point in case write operation has been
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   247
            # done during `session_open` hooks
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   248
            cnx.commit()
6eb7f361fba0 [testlib] complete the RepoAccess object (closes #3843614)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9667
diff changeset
   249
        return session
9117
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   250
9630
e7dbc4f06a48 minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9628
diff changeset
   251
    @contextmanager
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10353
diff changeset
   252
    def cnx(self):
9117
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   253
        """Context manager returning a server side connection for the user"""
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   254
        with self._session.new_cnx() as cnx:
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   255
            yield cnx
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   256
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10353
diff changeset
   257
    # aliases for bw compat
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10353
diff changeset
   258
    client_cnx = repo_cnx = cnx
9117
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   259
9630
e7dbc4f06a48 minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9628
diff changeset
   260
    @contextmanager
9570
14452b344d19 [devtools] add a 'method' argument to RepoAccess.web_request
David Douard <david.douard@logilab.fr>
parents: 9501
diff changeset
   261
    def web_request(self, url=None, headers={}, method='GET', **kwargs):
9117
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   262
        """Context manager returning a web request pre-linked to a client cnx
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   263
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   264
        To commit and rollback use::
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   265
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   266
            req.cnx.commit()
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   267
            req.cnx.rolback()
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   268
        """
9570
14452b344d19 [devtools] add a 'method' argument to RepoAccess.web_request
David Douard <david.douard@logilab.fr>
parents: 9501
diff changeset
   269
        req = self.requestcls(self._repo.vreg, url=url, headers=headers,
14452b344d19 [devtools] add a 'method' argument to RepoAccess.web_request
David Douard <david.douard@logilab.fr>
parents: 9501
diff changeset
   270
                              method=method, form=kwargs)
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10353
diff changeset
   271
        with self._session.new_cnx() as cnx:
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10353
diff changeset
   272
            req.set_cnx(cnx)
9117
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   273
            yield req
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   274
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   275
    def close(self):
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   276
        """Close the session associated to the RepoAccess"""
11195
5de859b95988 [session, repository] deprecate repo.connect and move .close reponsibility to session object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 11184
diff changeset
   277
        self._session.close()
9117
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   278
9667
4714d4afd4ea [devtools] add 'shell' method to RepoAccess, deprecate CubicWebTC's
Julien Cristau <julien.cristau@logilab.fr>
parents: 9635
diff changeset
   279
    @contextmanager
4714d4afd4ea [devtools] add 'shell' method to RepoAccess, deprecate CubicWebTC's
Julien Cristau <julien.cristau@logilab.fr>
parents: 9635
diff changeset
   280
    def shell(self):
4714d4afd4ea [devtools] add 'shell' method to RepoAccess, deprecate CubicWebTC's
Julien Cristau <julien.cristau@logilab.fr>
parents: 9635
diff changeset
   281
        from cubicweb.server.migractions import ServerMigrationHelper
10353
d9a1e7939ee6 [migractions] remove any session related leftovers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   282
        with self._session.new_cnx() as cnx:
9667
4714d4afd4ea [devtools] add 'shell' method to RepoAccess, deprecate CubicWebTC's
Julien Cristau <julien.cristau@logilab.fr>
parents: 9635
diff changeset
   283
            mih = ServerMigrationHelper(None, repo=self._repo, cnx=cnx,
4714d4afd4ea [devtools] add 'shell' method to RepoAccess, deprecate CubicWebTC's
Julien Cristau <julien.cristau@logilab.fr>
parents: 9635
diff changeset
   284
                                        interactive=False,
4714d4afd4ea [devtools] add 'shell' method to RepoAccess, deprecate CubicWebTC's
Julien Cristau <julien.cristau@logilab.fr>
parents: 9635
diff changeset
   285
                                        # hack so it don't try to load fs schema
4714d4afd4ea [devtools] add 'shell' method to RepoAccess, deprecate CubicWebTC's
Julien Cristau <julien.cristau@logilab.fr>
parents: 9635
diff changeset
   286
                                        schema=1)
4714d4afd4ea [devtools] add 'shell' method to RepoAccess, deprecate CubicWebTC's
Julien Cristau <julien.cristau@logilab.fr>
parents: 9635
diff changeset
   287
            yield mih
4714d4afd4ea [devtools] add 'shell' method to RepoAccess, deprecate CubicWebTC's
Julien Cristau <julien.cristau@logilab.fr>
parents: 9635
diff changeset
   288
            cnx.commit()
4714d4afd4ea [devtools] add 'shell' method to RepoAccess, deprecate CubicWebTC's
Julien Cristau <julien.cristau@logilab.fr>
parents: 9635
diff changeset
   289
9117
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   290
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   291
# 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
   292
11076
403a901b6b1e [devtools] Re-implement generative tests using subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11069
diff changeset
   293
class CubicWebTC(BaseTestCase):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   294
    """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
   295
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   296
    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
   297
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   298
    * `vreg`, the vregistry
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   299
    * `schema`, self.vreg.schema
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   300
    * `config`, cubicweb configuration
9580
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9570
diff changeset
   301
    * `cnx`, repoapi connection to the repository using an admin user
5229
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   302
    * `session`, server side session associated to `cnx`
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   303
    * `app`, the cubicweb publisher (for web testing)
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   304
    * `repo`, the repository object
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   305
    * `admlogin`, login of the admin user
67dbd07a05f3 [doc/book] expand tesing material
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5175
diff changeset
   306
    * `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
   307
    * `shell`, create and use shell environment
11014
9c9f5e913f9c [devtools] Move attribute docstring to correct location
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10997
diff changeset
   308
    * `anonymous_allowed`: flag telling if anonymous browsing should be allowed
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   309
    """
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   310
    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
   311
    configcls = devtools.ApptestConfiguration
9570
14452b344d19 [devtools] add a 'method' argument to RepoAccess.web_request
David Douard <david.douard@logilab.fr>
parents: 9501
diff changeset
   312
    requestcls = fake.FakeRequest
11181
2a7fb3422ae1 [devtools] stop using lgc.testlib.TestCase
Julien Cristau <julien.cristau@logilab.fr>
parents: 11165
diff changeset
   313
    tags = Tags('cubicweb', 'cw_repo')
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   314
    test_db_id = DEFAULT_EMPTY_DB_ID
9043
97c3bb9a7c99 [testlib] move repo and related attribute back on Instance instead of Class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9042
diff changeset
   315
9929
16163ee1cdf9 [tests] Move 'anonymous_allowed' property from CubicWebServerTC to CubicWebTC
Christophe de Vienne <christophe@unlish.com>
parents: 9845
diff changeset
   316
    # anonymous is logged by default in cubicweb test cases
16163ee1cdf9 [tests] Move 'anonymous_allowed' property from CubicWebServerTC to CubicWebTC
Christophe de Vienne <christophe@unlish.com>
parents: 9845
diff changeset
   317
    anonymous_allowed = True
16163ee1cdf9 [tests] Move 'anonymous_allowed' property from CubicWebServerTC to CubicWebTC
Christophe de Vienne <christophe@unlish.com>
parents: 9845
diff changeset
   318
11270
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11269
diff changeset
   319
    @classmethod
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11269
diff changeset
   320
    def setUpClass(cls):
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11269
diff changeset
   321
        test_module_file = sys.modules[cls.__module__].__file__
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11269
diff changeset
   322
        assert 'config' not in cls.__dict__, (
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11269
diff changeset
   323
            '%s has a config class attribute before entering setUpClass. '
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11269
diff changeset
   324
            'Let CubicWebTC.setUpClass instantiate it and modify it afterwards.' % cls)
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11269
diff changeset
   325
        cls.config = cls.configcls(cls.appid, test_module_file)
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11269
diff changeset
   326
        cls.config.mode = 'test'
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11269
diff changeset
   327
9043
97c3bb9a7c99 [testlib] move repo and related attribute back on Instance instead of Class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9042
diff changeset
   328
    def __init__(self, *args, **kwargs):
9070
4a803380f718 PARTIAL: Using the repoapi in test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9044
diff changeset
   329
        self._admin_session = None
9043
97c3bb9a7c99 [testlib] move repo and related attribute back on Instance instead of Class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9042
diff changeset
   330
        self.repo = None
9117
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   331
        self._open_access = set()
9043
97c3bb9a7c99 [testlib] move repo and related attribute back on Instance instead of Class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9042
diff changeset
   332
        super(CubicWebTC, self).__init__(*args, **kwargs)
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   333
11182
66c44d3d97af [devtools/testlib] Issue a RuntimeError when a generative tests is detected
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11181
diff changeset
   334
    def run(self, *args, **kwds):
66c44d3d97af [devtools/testlib] Issue a RuntimeError when a generative tests is detected
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11181
diff changeset
   335
        testMethod = getattr(self, self._testMethodName)
66c44d3d97af [devtools/testlib] Issue a RuntimeError when a generative tests is detected
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11181
diff changeset
   336
        if isgeneratorfunction(testMethod):
66c44d3d97af [devtools/testlib] Issue a RuntimeError when a generative tests is detected
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11181
diff changeset
   337
            raise RuntimeError(
66c44d3d97af [devtools/testlib] Issue a RuntimeError when a generative tests is detected
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11181
diff changeset
   338
                '%s appears to be a generative test. This is not handled '
66c44d3d97af [devtools/testlib] Issue a RuntimeError when a generative tests is detected
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11181
diff changeset
   339
                'anymore, use subTest API instead.' % self)
66c44d3d97af [devtools/testlib] Issue a RuntimeError when a generative tests is detected
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11181
diff changeset
   340
        return super(CubicWebTC, self).run(*args, **kwds)
66c44d3d97af [devtools/testlib] Issue a RuntimeError when a generative tests is detected
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11181
diff changeset
   341
9044
cfec5cc46008 [testlib] gather all repository access logic in one place
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9043
diff changeset
   342
    # repository connection handling ###########################################
9630
e7dbc4f06a48 minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9628
diff changeset
   343
9117
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   344
    def new_access(self, login):
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   345
        """provide a new RepoAccess object for a given user
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   346
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   347
        The access is automatically closed at the end of the test."""
10687
d394bfcd8c25 [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10675
diff changeset
   348
        login = text_type(login)
9117
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   349
        access = RepoAccess(self.repo, login, self.requestcls)
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   350
        self._open_access.add(access)
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   351
        return access
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   352
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   353
    def _close_access(self):
e25c5abc667c [testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9114
diff changeset
   354
        while self._open_access:
9628
2997f81cde19 [devtools] properly close open access on tearDown
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9627
diff changeset
   355
            try:
2997f81cde19 [devtools] properly close open access on tearDown
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9627
diff changeset
   356
                self._open_access.pop().close()
2997f81cde19 [devtools] properly close open access on tearDown
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9627
diff changeset
   357
            except BadConnectionId:
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   358
                continue  # already closed
9044
cfec5cc46008 [testlib] gather all repository access logic in one place
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9043
diff changeset
   359
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   360
    @property
9044
cfec5cc46008 [testlib] gather all repository access logic in one place
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9043
diff changeset
   361
    def session(self):
10363
e1ebf3d12098 [devtools] remove the remaining bw compat for old-style tests
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10356
diff changeset
   362
        """return admin session"""
9070
4a803380f718 PARTIAL: Using the repoapi in test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9044
diff changeset
   363
        return self._admin_session
4a803380f718 PARTIAL: Using the repoapi in test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9044
diff changeset
   364
9044
cfec5cc46008 [testlib] gather all repository access logic in one place
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9043
diff changeset
   365
    def _init_repo(self):
cfec5cc46008 [testlib] gather all repository access logic in one place
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9043
diff changeset
   366
        """init the repository and connection to it.
cfec5cc46008 [testlib] gather all repository access logic in one place
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9043
diff changeset
   367
        """
cfec5cc46008 [testlib] gather all repository access logic in one place
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9043
diff changeset
   368
        # get or restore and working db.
9790
0872ac2a1db0 [testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9757
diff changeset
   369
        db_handler = devtools.get_test_db_handler(self.config, self.init_config)
9044
cfec5cc46008 [testlib] gather all repository access logic in one place
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9043
diff changeset
   370
        db_handler.build_db_cache(self.test_db_id, self.pre_setup_database)
9070
4a803380f718 PARTIAL: Using the repoapi in test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9044
diff changeset
   371
        db_handler.restore_database(self.test_db_id)
4a803380f718 PARTIAL: Using the repoapi in test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9044
diff changeset
   372
        self.repo = db_handler.get_repo(startup=True)
4a803380f718 PARTIAL: Using the repoapi in test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9044
diff changeset
   373
        # get an admin session (without actual login)
10687
d394bfcd8c25 [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10675
diff changeset
   374
        login = text_type(db_handler.config.default_admin_config['login'])
9118
bb9e19df9a05 [testlib] add an default testcase.adminaccess (and use it for default session)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9117
diff changeset
   375
        self.admin_access = self.new_access(login)
bb9e19df9a05 [testlib] add an default testcase.adminaccess (and use it for default session)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9117
diff changeset
   376
        self._admin_session = self.admin_access._session
9044
cfec5cc46008 [testlib] gather all repository access logic in one place
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9043
diff changeset
   377
cfec5cc46008 [testlib] gather all repository access logic in one place
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9043
diff changeset
   378
    # config management ########################################################
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   379
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   380
    @classmethod  # XXX could be turned into a regular method
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   381
    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
   382
        """configuration initialization hooks.
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6410
diff changeset
   383
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6410
diff changeset
   384
        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
   385
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6410
diff changeset
   386
        Otherwise, consider to use a different :class:`ApptestConfiguration`
9790
0872ac2a1db0 [testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9757
diff changeset
   387
        defined in the `configcls` class attribute.
0872ac2a1db0 [testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9757
diff changeset
   388
0872ac2a1db0 [testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9757
diff changeset
   389
        This method will be called by the database handler once the config has
0872ac2a1db0 [testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9757
diff changeset
   390
        been properly bootstrapped.
0872ac2a1db0 [testlib] call init_config once the config has been properly bootstraped
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9757
diff changeset
   391
        """
10017
58c7a075c793 [devtools/testlib] Use actual 'admin' user configuration values
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9933
diff changeset
   392
        admincfg = config.default_admin_config
10675
e0db937f5add [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10663
diff changeset
   393
        cls.admlogin = text_type(admincfg['login'])
10017
58c7a075c793 [devtools/testlib] Use actual 'admin' user configuration values
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9933
diff changeset
   394
        cls.admpassword = admincfg['password']
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   395
        # uncomment the line below if you want rql queries to be logged
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   396
        # config.global_set_option('query-log-file',
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   397
        #                          '/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
   398
        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
   399
        # 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
   400
        # 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
   401
        config.global_set_option('default-dest-addrs', ['whatever'])
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   402
        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
   403
        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
   404
        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
   405
        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
   406
        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
   407
        # 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
   408
        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
   409
        # 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
   410
        try:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   411
            config.global_set_option('embed-allowed', re.compile('.*'))
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   412
        except Exception:  # not in server only configuration
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   413
            pass
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   414
9043
97c3bb9a7c99 [testlib] move repo and related attribute back on Instance instead of Class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9042
diff changeset
   415
    @property
97c3bb9a7c99 [testlib] move repo and related attribute back on Instance instead of Class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9042
diff changeset
   416
    def vreg(self):
97c3bb9a7c99 [testlib] move repo and related attribute back on Instance instead of Class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9042
diff changeset
   417
        return self.repo.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
   418
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   419
    # 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
   420
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   421
    @property
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   422
    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
   423
        """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
   424
        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
   425
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   426
    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
   427
        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
   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 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
   430
        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
   431
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6425
diff changeset
   432
    def debugged(self, debugmode):
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6425
diff changeset
   433
        return server.debugged(debugmode)
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6425
diff changeset
   434
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
    # 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
   436
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   437
    def setUp(self):
11270
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11269
diff changeset
   438
        assert hasattr(self, 'config'), (
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11269
diff changeset
   439
            'It seems that CubicWebTC.setUpClass has not been called. '
a9dc97b87ced [devtools] set .config in setUpClass instead of using a cache in a classproperty
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11269
diff changeset
   440
            'Missing super() call in %s?' % self.setUpClass)
6398
ea26eb5fd388 [test] send mail synchronously during tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6369
diff changeset
   441
        # monkey patch send mail operation so emails are sent synchronously
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   442
        self._patch_SendMailOp()
11069
020de2d09c0f [devtools/testlib] Disable pause trace in setUp
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   443
        previous_failure = self.__class__.__dict__.get('_repo_init_failed')
020de2d09c0f [devtools/testlib] Disable pause trace in setUp
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   444
        if previous_failure is not None:
020de2d09c0f [devtools/testlib] Disable pause trace in setUp
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   445
            self.skipTest('repository is not initialised: %r' % previous_failure)
020de2d09c0f [devtools/testlib] Disable pause trace in setUp
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   446
        try:
020de2d09c0f [devtools/testlib] Disable pause trace in setUp
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   447
            self._init_repo()
020de2d09c0f [devtools/testlib] Disable pause trace in setUp
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   448
        except Exception as ex:
020de2d09c0f [devtools/testlib] Disable pause trace in setUp
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   449
            self.__class__._repo_init_failed = ex
020de2d09c0f [devtools/testlib] Disable pause trace in setUp
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   450
            raise
020de2d09c0f [devtools/testlib] Disable pause trace in setUp
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   451
        self.addCleanup(self._close_access)
10937
eb05348b0e2d [devtools] Disable anonymous login right before executing tests
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10837
diff changeset
   452
        self.config.set_anonymous_allowed(self.anonymous_allowed)
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
        self.setup_database()
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   454
        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
   455
6efb7a7ae570 [testlib] properly close connections opened during test in tearDown
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5128
diff changeset
   456
    def tearDown(self):
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   457
        # XXX hack until logilab.common.testlib is fixed
9070
4a803380f718 PARTIAL: Using the repoapi in test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9044
diff changeset
   458
        if self._admin_session is not None:
11195
5de859b95988 [session, repository] deprecate repo.connect and move .close reponsibility to session object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 11184
diff changeset
   459
            self._admin_session.close()
9070
4a803380f718 PARTIAL: Using the repoapi in test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9044
diff changeset
   460
            self._admin_session = None
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   461
        while self._cleanups:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   462
            cleanup, args, kwargs = self._cleanups.pop(-1)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   463
            cleanup(*args, **kwargs)
10112
ff7f86d8393d [devtools] call turn_repo_off in tearDown (closes #4673491)
Julien Cristau <julien.cristau@logilab.fr>
parents: 10108
diff changeset
   464
        self.repo.turn_repo_off()
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   465
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   466
    def _patch_SendMailOp(self):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   467
        # monkey patch send mail operation so emails are sent synchronously
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   468
        _old_mail_postcommit_event = SendMailOp.postcommit_event
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   469
        SendMailOp.postcommit_event = SendMailOp.sendmails
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   470
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   471
        def reverse_SendMailOp_monkey_patch():
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   472
            SendMailOp.postcommit_event = _old_mail_postcommit_event
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   473
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   474
        self.addCleanup(reverse_SendMailOp_monkey_patch)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   475
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   476
    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
   477
        """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
   478
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   479
    @classmethod
9933
3674f249ab1d [devtools] pre_setup_database takes a Connection, not a Session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9931
diff changeset
   480
    def pre_setup_database(cls, cnx, config):
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   481
        """add your pre database setup code by overriding this method
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   482
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   483
        Do not forget to set the cls.test_db_id value to enable caching of the
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   484
        result.
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   485
        """
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   486
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   487
    # 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
   488
9631
c51a05b5dcb8 [devtools] deprecate CWTC.user()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9630
diff changeset
   489
    @deprecated('[3.19] explicitly use RepoAccess object in test instead')
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   490
    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
   491
        """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
   492
        if req is None:
9070
4a803380f718 PARTIAL: Using the repoapi in test
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9044
diff changeset
   493
            return self.request().user
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   494
        else:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   495
            return req.user
563
a690996639ca [testlib] fix pb. related to class scoped variables
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 562
diff changeset
   496
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   497
    @iclassmethod  # XXX turn into a class method
7222
fcb8932082a5 [testlib] refactor create_user and grant_permission to make them usable from pre_setup_database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7163
diff changeset
   498
    def create_user(self, req, login=None, groups=('users',), password=None,
7337
67cd9d5b4c2e [testlib] create_user may now create user's email address
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7331
diff changeset
   499
                    email=None, 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
   500
        """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
   501
        if password is None:
10363
e1ebf3d12098 [devtools] remove the remaining bw compat for old-style tests
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10356
diff changeset
   502
            password = login
10259
2be01bb6f9de [devtools] accept str objects in CubicWebTC.new_access and .create_user
Julien Cristau <julien.cristau@logilab.fr>
parents: 10112
diff changeset
   503
        if login is not None:
10687
d394bfcd8c25 [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10675
diff changeset
   504
            login = text_type(login)
10259
2be01bb6f9de [devtools] accept str objects in CubicWebTC.new_access and .create_user
Julien Cristau <julien.cristau@logilab.fr>
parents: 10112
diff changeset
   505
        user = req.create_entity('CWUser', login=login,
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
   506
                                 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
   507
        req.execute('SET X in_group G WHERE X eid %%(x)s, G name IN(%s)'
7038
fe0afc4e8ebb [testlib] fix rql generation bug w/ groups given as unicode string
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6979
diff changeset
   508
                    % ','.join(repr(str(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
   509
                    {'x': user.eid})
7337
67cd9d5b4c2e [testlib] create_user may now create user's email address
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7331
diff changeset
   510
        if email is not None:
10687
d394bfcd8c25 [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10675
diff changeset
   511
            req.create_entity('EmailAddress', address=text_type(email),
7337
67cd9d5b4c2e [testlib] create_user may now create user's email address
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7331
diff changeset
   512
                              reverse_primary_email=user)
5557
1a534c596bff [entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   513
        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
   514
        if commit:
7222
fcb8932082a5 [testlib] refactor create_user and grant_permission to make them usable from pre_setup_database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7163
diff changeset
   515
            try:
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   516
                req.commit()  # req is a session
7222
fcb8932082a5 [testlib] refactor create_user and grant_permission to make them usable from pre_setup_database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7163
diff changeset
   517
            except AttributeError:
fcb8932082a5 [testlib] refactor create_user and grant_permission to make them usable from pre_setup_database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7163
diff changeset
   518
                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
   519
        return user
563
a690996639ca [testlib] fix pb. related to class scoped variables
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 562
diff changeset
   520
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   521
    # 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
   522
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
   523
    @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
   524
    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
   525
        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
   526
        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
   527
            self.vreg.register(obj)
9212
0d346a0a1451 [testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8978
diff changeset
   528
            registered = getattr(obj, '__registered__', None)
0d346a0a1451 [testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8978
diff changeset
   529
            if registered:
0d346a0a1451 [testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8978
diff changeset
   530
                for registry in obj.__registries__:
0d346a0a1451 [testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8978
diff changeset
   531
                    registered(self.vreg[registry])
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
   532
        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
   533
            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
   534
        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
   535
            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
   536
                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
   537
8460
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   538
    @contextmanager
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   539
    def temporary_permissions(self, *perm_overrides, **perm_kwoverrides):
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   540
        """Set custom schema permissions within context.
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   541
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   542
        There are two ways to call this method, which may be used together :
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   543
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   544
        * using positional argument(s):
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   545
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   546
          .. sourcecode:: python
8480
086cff6a306a [book] fix build warnings/errors. Closes #2430042
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8463
diff changeset
   547
8460
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   548
                rdef = self.schema['CWUser'].rdef('login')
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   549
                with self.temporary_permissions((rdef, {'read': ()})):
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   550
                    ...
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   551
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   552
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   553
        * using named argument(s):
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   554
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   555
          .. sourcecode:: python
8480
086cff6a306a [book] fix build warnings/errors. Closes #2430042
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8463
diff changeset
   556
8460
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   557
                with self.temporary_permissions(CWUser={'read': ()}):
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   558
                    ...
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   559
9580
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9570
diff changeset
   560
        Usually the former will be preferred to override permissions on a
8460
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   561
        relation definition, while the latter is well suited for entity types.
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   562
9580
abaae1496ba4 [book] Update documentation for new repoapi
Julien Cristau <julien.cristau@logilab.fr>
parents: 9570
diff changeset
   563
        The allowed keys in the permission dictionary depend on the schema type
8460
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   564
        (entity type / relation definition). Resulting permissions will be
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   565
        similar to `orig_permissions.update(partial_perms)`.
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   566
        """
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   567
        torestore = []
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10651
diff changeset
   568
        for erschema, etypeperms in chain(perm_overrides, perm_kwoverrides.items()):
10612
84468b90e9c1 [py3k] basestring → six.string_types
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10609
diff changeset
   569
            if isinstance(erschema, string_types):
8460
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   570
                erschema = self.schema[erschema]
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10651
diff changeset
   571
            for action, actionperms in etypeperms.items():
8460
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   572
                origperms = erschema.permissions[action]
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   573
                erschema.set_action_permissions(action, actionperms)
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   574
                torestore.append([erschema, action, origperms])
10639
1ed205146ee2 [devtools] Fix CubicWebTC.temporary_permissions
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10638
diff changeset
   575
        try:
1ed205146ee2 [devtools] Fix CubicWebTC.temporary_permissions
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10638
diff changeset
   576
            yield
1ed205146ee2 [devtools] Fix CubicWebTC.temporary_permissions
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10638
diff changeset
   577
        finally:
1ed205146ee2 [devtools] Fix CubicWebTC.temporary_permissions
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10638
diff changeset
   578
            for erschema, action, permissions in torestore:
1ed205146ee2 [devtools] Fix CubicWebTC.temporary_permissions
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10638
diff changeset
   579
                if action is None:
1ed205146ee2 [devtools] Fix CubicWebTC.temporary_permissions
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10638
diff changeset
   580
                    erschema.permissions = permissions
1ed205146ee2 [devtools] Fix CubicWebTC.temporary_permissions
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10638
diff changeset
   581
                else:
1ed205146ee2 [devtools] Fix CubicWebTC.temporary_permissions
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10638
diff changeset
   582
                    erschema.set_action_permissions(action, permissions)
8460
b1f6777fc839 [testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8004
diff changeset
   583
7039
df0e8581b06f [testlib] backport some useful testing utilities from tracker's testutils module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7038
diff changeset
   584
    def assertModificationDateGreater(self, entity, olddate):
df0e8581b06f [testlib] backport some useful testing utilities from tracker's testutils module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7038
diff changeset
   585
        entity.cw_attr_cache.pop('modification_date', None)
10638
243e96db0004 [devtools] Use TestCase.assertGreater
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10219
diff changeset
   586
        self.assertGreater(entity.modification_date, olddate)
7039
df0e8581b06f [testlib] backport some useful testing utilities from tracker's testutils module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7038
diff changeset
   587
9424
5027afeb5739 [testlib] Fix assertMessageEqual
Julien Cristau <julien.cristau@logilab.fr>
parents: 9212
diff changeset
   588
    def assertMessageEqual(self, req, params, expected_msg):
7441
b70f4f4c8620 [testlib] add convenience assertion method to check __message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7439
diff changeset
   589
        msg = req.session.data[params['_cwmsgid']]
9424
5027afeb5739 [testlib] Fix assertMessageEqual
Julien Cristau <julien.cristau@logilab.fr>
parents: 9212
diff changeset
   590
        self.assertEqual(expected_msg, msg)
7441
b70f4f4c8620 [testlib] add convenience assertion method to check __message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7439
diff changeset
   591
7039
df0e8581b06f [testlib] backport some useful testing utilities from tracker's testutils module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7038
diff changeset
   592
    # workflow utilities #######################################################
df0e8581b06f [testlib] backport some useful testing utilities from tracker's testutils module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7038
diff changeset
   593
df0e8581b06f [testlib] backport some useful testing utilities from tracker's testutils module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7038
diff changeset
   594
    def assertPossibleTransitions(self, entity, expected):
df0e8581b06f [testlib] backport some useful testing utilities from tracker's testutils module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7038
diff changeset
   595
        transitions = entity.cw_adapt_to('IWorkflowable').possible_transitions()
df0e8581b06f [testlib] backport some useful testing utilities from tracker's testutils module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7038
diff changeset
   596
        self.assertListEqual(sorted(tr.name for tr in transitions),
df0e8581b06f [testlib] backport some useful testing utilities from tracker's testutils module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7038
diff changeset
   597
                             sorted(expected))
df0e8581b06f [testlib] backport some useful testing utilities from tracker's testutils module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7038
diff changeset
   598
df0e8581b06f [testlib] backport some useful testing utilities from tracker's testutils module
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7038
diff changeset
   599
    # views and actions registries inspection ##################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   600
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   601
    def pviews(self, req, rset):
3468
b02fa4db2868 [tests] make unittest_viewselectors pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3454
diff changeset
   602
        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
   603
                      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
   604
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   605
    def pactions(self, req, rset,
6967
07d889e3f35d [testlib] by default skip new 'manage' action's category
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6848
diff changeset
   606
                 skipcategories=('addrelated', 'siteactions', 'useractions',
07d889e3f35d [testlib] by default skip new 'manage' action's category
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6848
diff changeset
   607
                                 'footer', 'manage')):
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3230
diff changeset
   608
        return [(a.__regid__, a.__class__)
2813
0cf6c8005bf6 R propagate deprecation of CWRegistry.possible_vobjects
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2793
diff changeset
   609
                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
   610
                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
   611
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   612
    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
   613
        return [(a.__regid__, a.__class__)
2813
0cf6c8005bf6 R propagate deprecation of CWRegistry.possible_vobjects
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2793
diff changeset
   614
                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
   615
                if a.category in categories]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   616
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   617
    def pactionsdict(self, req, rset,
6967
07d889e3f35d [testlib] by default skip new 'manage' action's category
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6848
diff changeset
   618
                     skipcategories=('addrelated', 'siteactions', 'useractions',
07d889e3f35d [testlib] by default skip new 'manage' action's category
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6848
diff changeset
   619
                                     'footer', 'manage')):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   620
        res = {}
2813
0cf6c8005bf6 R propagate deprecation of CWRegistry.possible_vobjects
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2793
diff changeset
   621
        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
   622
            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
   623
                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
   624
        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
   625
3230
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   626
    def action_submenu(self, req, rset, id):
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   627
        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
   628
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   629
    def _test_action(self, action):
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   630
        class fake_menu(list):
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   631
            @property
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   632
            def items(self):
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   633
                return self
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   634
3230
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   635
        class fake_box(object):
6800
3f3d576b87d9 [web action] refactor box menu handling, fixing #1401943 on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6774
diff changeset
   636
            def action_link(self, action, **kwargs):
3230
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   637
                return (action.title, action.url())
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   638
        submenu = fake_menu()
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   639
        action.fill_menu(fake_box(), submenu)
1d25e928c299 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163 3226
diff changeset
   640
        return submenu
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   641
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   642
    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
   643
        """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
   644
        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
   645
        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
   646
        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
   647
        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
   648
        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
   649
            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
   650
                continue
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   651
            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
   652
                continue
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   653
            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
   654
                     if view.category != 'startupview'
6454
97203d0af4cb [test/possible views] skip deprecated views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6442
diff changeset
   655
                     and not issubclass(view, notification.NotificationView)
97203d0af4cb [test/possible views] skip deprecated views
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6442
diff changeset
   656
                     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
   657
            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
   658
                try:
2774
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2770 2773
diff changeset
   659
                    view = viewsvreg._select_best(views, req, rset=rset)
7272
771f594c12a2 [vreg] vregistry._select_best was needlessly instanciating NoSelectableObject (closes #1626708)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7222
diff changeset
   660
                    if view is None:
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   661
                        raise NoSelectableObject((req,), {'rset': rset}, views)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   662
                    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
   663
                        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
   664
                    else:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   665
                        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
   666
                    # 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
   667
                    # 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
   668
                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
   669
                    continue
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   670
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   671
    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
   672
        """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
   673
        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
   674
        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
   675
            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
   676
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   677
    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
   678
        """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
   679
        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
   680
        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
   681
            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
   682
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   683
    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
   684
        """returns the list of startup views"""
9836
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
   685
        with self.admin_access.web_request() as req:
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
   686
            for view in self.vreg['views'].possible_views(req, None):
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
   687
                if view.category == 'startupview':
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
   688
                    yield view.__regid__
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
   689
                else:
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
   690
                    not_selected(self.vreg, view)
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
   691
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   692
    # 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
   693
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   694
    @property
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   695
    @cached
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   696
    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
   697
        """return a cubicweb publisher"""
9031
6ff29f2879da web/application: instantiate the repository outside of CubicWebPublisher
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9030
diff changeset
   698
        publisher = application.CubicWebPublisher(self.repo, self.config)
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   699
4137
3a2d6c38bcd9 override publisher's error handler to ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4083
diff changeset
   700
        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
   701
            raise
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   702
4137
3a2d6c38bcd9 override publisher's error handler to ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4083
diff changeset
   703
        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
   704
        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
   705
9845
9c3ec404534f [testlib] deprecate .remote_call and provide new connection api friendly .remote_calling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9837
diff changeset
   706
    @deprecated('[3.19] use the .remote_calling method')
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   707
    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
   708
        """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
   709
        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
   710
        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
   711
        req = self.request(fname=fname, pageid='123', arg=args)
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8004
diff changeset
   712
        ctrl = self.vreg['controllers'].select('ajax', req)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   713
        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
   714
9845
9c3ec404534f [testlib] deprecate .remote_call and provide new connection api friendly .remote_calling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9837
diff changeset
   715
    @contextmanager
9c3ec404534f [testlib] deprecate .remote_call and provide new connection api friendly .remote_calling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9837
diff changeset
   716
    def remote_calling(self, fname, *args):
9c3ec404534f [testlib] deprecate .remote_call and provide new connection api friendly .remote_calling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9837
diff changeset
   717
        """remote json call simulation"""
9c3ec404534f [testlib] deprecate .remote_call and provide new connection api friendly .remote_calling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9837
diff changeset
   718
        args = [json.dumps(arg) for arg in args]
9c3ec404534f [testlib] deprecate .remote_call and provide new connection api friendly .remote_calling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9837
diff changeset
   719
        with self.admin_access.web_request(fname=fname, pageid='123', arg=args) as req:
9c3ec404534f [testlib] deprecate .remote_call and provide new connection api friendly .remote_calling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9837
diff changeset
   720
            ctrl = self.vreg['controllers'].select('ajax', req)
9c3ec404534f [testlib] deprecate .remote_call and provide new connection api friendly .remote_calling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9837
diff changeset
   721
            yield ctrl.publish(), req
9c3ec404534f [testlib] deprecate .remote_call and provide new connection api friendly .remote_calling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9837
diff changeset
   722
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   723
    @application._deprecated_path_arg
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   724
    def app_handle_request(self, req):
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   725
        return self.app.core_handle(req)
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   726
8363
3f3b4e4c63f5 [devtool-testlib] add deprecated entry point for publish method
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8318
diff changeset
   727
    @deprecated("[3.15] app_handle_request is the new and better way"
3f3b4e4c63f5 [devtool-testlib] add deprecated entry point for publish method
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8318
diff changeset
   728
                " (beware of small semantic changes)")
3f3b4e4c63f5 [devtool-testlib] add deprecated entry point for publish method
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8318
diff changeset
   729
    def app_publish(self, *args, **kwargs):
3f3b4e4c63f5 [devtool-testlib] add deprecated entry point for publish method
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8318
diff changeset
   730
        return self.app_handle_request(*args, **kwargs)
3f3b4e4c63f5 [devtool-testlib] add deprecated entry point for publish method
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8318
diff changeset
   731
8720
d2f01aaae8c0 [testlib] url_publish should give url to the request and the rset returned by the path evaluator to ctrl_publish. Closes #2557468
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
   732
    def ctrl_publish(self, req, ctrl='edit', rset=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
   733
        """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
   734
        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
   735
        try:
8720
d2f01aaae8c0 [testlib] url_publish should give url to the request and the rset returned by the path evaluator to ctrl_publish. Closes #2557468
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
   736
            result = ctrl.publish(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
   737
            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
   738
        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
   739
            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
   740
            raise
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   741
        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
   742
10219
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   743
    @staticmethod
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   744
    def fake_form(formid, field_dict=None, entity_field_dicts=()):
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   745
        """Build _cw.form dictionnary to fake posting of some standard cubicweb form
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   746
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   747
        * `formid`, the form id, usually form's __regid__
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   748
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   749
        * `field_dict`, dictionary of name:value for fields that are not tied to an entity
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   750
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   751
        * `entity_field_dicts`, list of (entity, dictionary) where dictionary contains name:value
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   752
          for fields that are not tied to the given entity
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   753
        """
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   754
        assert field_dict or entity_field_dicts, \
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   755
            'field_dict and entity_field_dicts arguments must not be both unspecified'
10219
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   756
        if field_dict is None:
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   757
            field_dict = {}
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   758
        form = {'__form_id': formid}
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   759
        fields = []
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   760
        for field, value in field_dict.items():
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   761
            fields.append(field)
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   762
            form[field] = value
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   763
10219
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   764
        def _add_entity_field(entity, field, value):
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   765
            entity_fields.append(field)
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   766
            form[eid_param(field, entity.eid)] = value
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   767
10219
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   768
        for entity, field_dict in entity_field_dicts:
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   769
            if '__maineid' not in form:
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   770
                form['__maineid'] = entity.eid
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   771
            entity_fields = []
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   772
            form.setdefault('eid', []).append(entity.eid)
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   773
            _add_entity_field(entity, '__type', entity.cw_etype)
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   774
            for field, value in field_dict.items():
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   775
                _add_entity_field(entity, field, value)
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   776
            if entity_fields:
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   777
                form[eid_param('_cw_entity_fields', entity.eid)] = ','.join(entity_fields)
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   778
        if fields:
10468
3f27669634d2 [devtools] Sort _cw_fields in fake_form
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10363
diff changeset
   779
            form['_cw_fields'] = ','.join(sorted(fields))
10219
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   780
        return form
eacb8ea38bf5 [devtools] add a method on CubicWebTC to fake data posted by standard cw forms. Closes #4985805
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10112
diff changeset
   781
9931
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   782
    @deprecated('[3.19] use .admin_request_from_url instead')
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
   783
    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
   784
        """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
   785
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
   786
        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
   787
        """
8720
d2f01aaae8c0 [testlib] url_publish should give url to the request and the rset returned by the path evaluator to ctrl_publish. Closes #2557468
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
   788
        req = self.request(url=url)
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
   789
        if isinstance(url, unicode):
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   790
            url = url.encode(req.encoding)  # req.setup_params() expects encoded strings
10603
65ad6980976e [py3k] import URL mangling functions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10594
diff changeset
   791
        querystring = urlparse(url)[-2]
65ad6980976e [py3k] import URL mangling functions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10594
diff changeset
   792
        params = parse_qs(querystring)
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
   793
        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
   794
        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
   795
9931
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   796
    @contextmanager
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   797
    def admin_request_from_url(self, url):
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   798
        """parses `url` and builds the corresponding CW-web request
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   799
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   800
        req.form will be setup using the url's query string
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   801
        """
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   802
        with self.admin_access.web_request(url=url) as req:
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   803
            if isinstance(url, unicode):
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   804
                url = url.encode(req.encoding)  # req.setup_params() expects encoded strings
10603
65ad6980976e [py3k] import URL mangling functions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10594
diff changeset
   805
            querystring = urlparse(url)[-2]
65ad6980976e [py3k] import URL mangling functions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10594
diff changeset
   806
            params = parse_qs(querystring)
9931
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   807
            req.setup_params(params)
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   808
            yield req
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   809
8720
d2f01aaae8c0 [testlib] url_publish should give url to the request and the rset returned by the path evaluator to ctrl_publish. Closes #2557468
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
   810
    def url_publish(self, url, data=None):
d2f01aaae8c0 [testlib] url_publish should give url to the request and the rset returned by the path evaluator to ctrl_publish. Closes #2557468
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
   811
        """takes `url`, uses application's app_resolver to find the appropriate
d2f01aaae8c0 [testlib] url_publish should give url to the request and the rset returned by the path evaluator to ctrl_publish. Closes #2557468
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
   812
        controller and result set, then publishes the result.
d2f01aaae8c0 [testlib] url_publish should give url to the request and the rset returned by the path evaluator to ctrl_publish. Closes #2557468
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
   813
d2f01aaae8c0 [testlib] url_publish should give url to the request and the rset returned by the path evaluator to ctrl_publish. Closes #2557468
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
   814
        To simulate post of www-form-encoded data, give a `data` dictionary
d2f01aaae8c0 [testlib] url_publish should give url to the request and the rset returned by the path evaluator to ctrl_publish. Closes #2557468
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8696
diff changeset
   815
        containing desired key/value associations.
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
   816
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
   817
        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
   818
        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
   819
        """
9931
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   820
        with self.admin_request_from_url(url) as req:
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   821
            if data is not None:
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   822
                req.form.update(data)
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   823
            ctrlid, rset = self.app.url_resolver.process(req, req.relative_path(False))
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   824
            return self.ctrl_publish(req, ctrlid, rset)
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
   825
8721
b399c87df63c [devtools] add http_publish to CubicWebTC (closes #2565882)
Julien Cristau <julien.cristau@logilab.fr>
parents: 8720
diff changeset
   826
    def http_publish(self, url, data=None):
9427
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   827
        """like `url_publish`, except this returns a http response, even in case
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   828
        of errors. You may give form parameters using the `data` argument.
9c13ebd45cb6 [test api] extract a context manager to temporarily disable app.error_handler monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9424
diff changeset
   829
        """
9931
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   830
        with self.admin_request_from_url(url) as req:
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   831
            if data is not None:
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   832
                req.form.update(data)
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   833
            with real_error_handling(self.app):
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   834
                result = self.app_handle_request(req, req.relative_path(False))
013dd868e6da devtools: deprecate .req_from_url
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9929
diff changeset
   835
            return result, req
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
   836
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   837
    @staticmethod
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   838
    def _parse_location(req, location):
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   839
        try:
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   840
            path, params = location.split('?', 1)
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   841
        except ValueError:
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   842
            path = location
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   843
            params = {}
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   844
        else:
10603
65ad6980976e [py3k] import URL mangling functions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10594
diff changeset
   845
            cleanup = lambda p: (p[0], urlunquote(p[1]))
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   846
            params = dict(cleanup(p.split('=', 1)) for p in params.split('&') if p)
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   847
        if path.startswith(req.base_url()):  # may be relative
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   848
            path = path[len(req.base_url()):]
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   849
        return path, params
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   850
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   851
    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
   852
        """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
   853
        Redirect exception
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   854
        """
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   855
        try:
4719
aaed3f813ef8 kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4690
diff changeset
   856
            callback(req)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   857
        except Redirect as ex:
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   858
            return self._parse_location(req, ex.location)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   859
        else:
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   860
            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
   861
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   862
    def expect_redirect_handle_request(self, req, path='edit'):
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   863
        """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
   864
        get a Redirect exception
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   865
        """
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   866
        if req.relative_path(False) != path:
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   867
            req._url = path
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   868
        self.app_handle_request(req)
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   869
        self.assertTrue(300 <= req.status_out < 400, req.status_out)
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   870
        location = req.get_response_header('location')
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   871
        return self._parse_location(req, location)
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   872
8363
3f3b4e4c63f5 [devtool-testlib] add deprecated entry point for publish method
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8318
diff changeset
   873
    @deprecated("[3.15] expect_redirect_handle_request is the new and better way"
3f3b4e4c63f5 [devtool-testlib] add deprecated entry point for publish method
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8318
diff changeset
   874
                " (beware of small semantic changes)")
3f3b4e4c63f5 [devtool-testlib] add deprecated entry point for publish method
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8318
diff changeset
   875
    def expect_redirect_publish(self, *args, **kwargs):
3f3b4e4c63f5 [devtool-testlib] add deprecated entry point for publish method
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8318
diff changeset
   876
        return self.expect_redirect_handle_request(*args, **kwargs)
3f3b4e4c63f5 [devtool-testlib] add deprecated entry point for publish method
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8318
diff changeset
   877
7163
d6d905d0344f [web test] extract a set_auth_mode method from init_authentication and use it in test_fb_login_concept to avoid bad test interaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7088
diff changeset
   878
    def set_auth_mode(self, authmode, anonuser=None):
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   879
        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
   880
        self.set_option('anonymous-user', anonuser)
7071
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7060
diff changeset
   881
        if anonuser is None:
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7060
diff changeset
   882
            self.config.anonymous_credential = None
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7060
diff changeset
   883
        else:
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7060
diff changeset
   884
            self.config.anonymous_credential = (anonuser, anonuser)
7163
d6d905d0344f [web test] extract a set_auth_mode method from init_authentication and use it in test_fb_login_concept to avoid bad test interaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7088
diff changeset
   885
d6d905d0344f [web test] extract a set_auth_mode method from init_authentication and use it in test_fb_login_concept to avoid bad test interaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7088
diff changeset
   886
    def init_authentication(self, authmode, anonuser=None):
d6d905d0344f [web test] extract a set_auth_mode method from init_authentication and use it in test_fb_login_concept to avoid bad test interaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7088
diff changeset
   887
        self.set_auth_mode(authmode, anonuser)
9039
488255d1cf3b [testlib] rework request building in init_authentication
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9031
diff changeset
   888
        req = self.requestcls(self.vreg, url='login')
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   889
        sh = self.app.session_handler
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   890
        authm = sh.session_manager.authmanager
4916
4b8cdda342ae fix tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   891
        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
   892
        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
   893
        # not properly cleaned between tests
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   894
        self.open_sessions = sh.session_manager._sessions = {}
9626
90fb0623bb70 [devtools] avoid an internal deprecation warning
Julien Cristau <julien.cristau@logilab.fr>
parents: 9606
diff changeset
   895
        return req, self.session
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   896
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
   897
    def assertAuthSuccess(self, req, origsession, nbsessions=1):
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9017
diff changeset
   898
        session = self.app.get_session(req)
11699
b48020a80dc3 Store user groups and properties as session data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11462
diff changeset
   899
        cnx = session.new_cnx()
b48020a80dc3 Store user groups and properties as session data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11462
diff changeset
   900
        with cnx:
b48020a80dc3 Store user groups and properties as session data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11462
diff changeset
   901
            req.set_cnx(cnx)
6369
a151453dc564 [test] more update to unittest2 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6346
diff changeset
   902
        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
   903
        self.assertEqual(session.login, origsession.login)
a151453dc564 [test] more update to unittest2 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6346
diff changeset
   904
        self.assertEqual(session.anonymous_session, False)
3657
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   905
3676
fa170564e1a0 optional nbsessions arg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3657
diff changeset
   906
    def assertAuthFailure(self, req, nbsessions=0):
9017
aa709bc6b6c1 [application/connect] simplify connection logic
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8978
diff changeset
   907
        with self.assertRaises(AuthenticationError):
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9017
diff changeset
   908
            self.app.get_session(req)
9017
aa709bc6b6c1 [application/connect] simplify connection logic
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8978
diff changeset
   909
        # +0 since we do not track the opened session
aa709bc6b6c1 [application/connect] simplify connection logic
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8978
diff changeset
   910
        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
   911
        clear_cache(req, 'get_authorization')
706d7bf0ae3d factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3647
diff changeset
   912
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
   913
    # content validation #######################################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   914
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   915
    # validators are used to validate (XML, DTD, whatever) view's content
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   916
    # validators availables are :
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   917
    #  DTDValidator : validates XML + declared DTD
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   918
    #  SaxOnlyValidator : guarantees XML is well formed
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   919
    #  None : do not try to validate anything
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   920
    # 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
   921
    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
   922
        # 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
   923
        #
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
   924
        # 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
   925
        # snippets
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   926
        # 'text/html': DTDValidator,
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   927
        # 'application/xhtml+xml': DTDValidator,
8971
062e40f41b57 [testlib] SaxOnlyValidator is deprecated, use XMLValidator instead
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8930
diff changeset
   928
        'application/xml': htmlparser.XMLValidator,
062e40f41b57 [testlib] SaxOnlyValidator is deprecated, use XMLValidator instead
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8930
diff changeset
   929
        'text/xml': htmlparser.XMLValidator,
8652
7812093e21f7 [testlib] test_view now parse json data, closes #2557467
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8628
diff changeset
   930
        'application/json': JsonValidator,
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
   931
        '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
   932
        '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
   933
        '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
   934
        '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
   935
        'image/png': None,
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11270
diff changeset
   936
    }
562
bdadb26c4a3c old .validators attribute is now .vid_validators
sylvain.thenault@logilab.fr
parents: 549
diff changeset
   937
    # 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
   938
    vid_validators = dict((vid, htmlparser.VALMAP[valkey])
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10651
diff changeset
   939
                          for vid, valkey in VIEW_VALIDATORS.items())
1605
b239744627c7 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1152
diff changeset
   940
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
   941
    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
   942
             **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   943
        """This method tests the view `vid` on `rset` using `template`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   944
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
   945
        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
   946
        and parsed.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   947
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   948
        :returns: an instance of `cubicweb.devtools.htmlparser.PageInfo`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   949
                  encapsulation the generated HTML
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   950
        """
8628
b236b91836c0 [test] fix req default value handling in CubicWebTC.view (closes #2544376)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8497
diff changeset
   951
        if req is None:
11462
2046b3a4da5d [devtools] CWTC.request is gone, don't attempt to use it in CWTC.view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11348
diff changeset
   952
            assert rset is not None, 'you must supply at least one of rset or req'
2046b3a4da5d [devtools] CWTC.request is gone, don't attempt to use it in CWTC.view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11348
diff changeset
   953
            req = rset.req
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   954
        req.form['vid'] = vid
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
   955
        viewsreg = self.vreg['views']
8497
7f71e366cb5e [testlib] consider rset for template / view selection (closes #2447183)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8480
diff changeset
   956
        view = viewsreg.select(vid, req, rset=rset, **kwargs)
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
   957
        if template is None:  # raw view testing, no template
1773
789ee507a8e0 use render
sylvain.thenault@logilab.fr
parents: 1605
diff changeset
   958
            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
   959
        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
   960
            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
   961
            viewfunc = lambda **k: viewsreg.main_template(req, template,
8497
7f71e366cb5e [testlib] consider rset for template / view selection (closes #2447183)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8480
diff changeset
   962
                                                          rset=rset, **kwargs)
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
   963
        return self._test_view(viewfunc, view, template, kwargs)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   964
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
   965
    def _test_view(self, viewfunc, view, template='main-template', kwargs={}):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   966
        """this method does the actual call to the view
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   967
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
   968
        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
   969
        and parsed.
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
        :returns: an instance of `cubicweb.devtools.htmlparser.PageInfo`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   972
                  encapsulation the generated HTML
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   973
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   974
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   975
            output = viewfunc(**kwargs)
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7782
diff changeset
   976
        except Exception:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   977
            # hijack exception: generative tests stop when the exception
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   978
            # is not an AssertionError
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   979
            klass, exc, tcbk = sys.exc_info()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   980
            try:
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3230
diff changeset
   981
                msg = '[%s in %s] %s' % (klass, view.__regid__, exc)
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7782
diff changeset
   982
            except Exception:
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3230
diff changeset
   983
                msg = '[%s in %s] undisplayable exception' % (klass, view.__regid__)
10590
7629902e7554 [py3k] Fix raise with embedded traceback
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10589
diff changeset
   984
            exc = AssertionError(msg)
7629902e7554 [py3k] Fix raise with embedded traceback
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10589
diff changeset
   985
            exc.__traceback__ = tcbk
7629902e7554 [py3k] Fix raise with embedded traceback
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10589
diff changeset
   986
            raise exc
6806
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
   987
        return self._check_html(output, view, template)
427
e894eec21a1b move selection of entity types to test in a method to ease overriding
sylvain.thenault@logilab.fr
parents: 0
diff changeset
   988
6978
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
   989
    def get_validator(self, view=None, content_type=None, output=None):
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
   990
        if view is not None:
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
   991
            try:
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
   992
                return self.vid_validators[view.__regid__]()
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
   993
            except KeyError:
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
   994
                if content_type is None:
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
   995
                    content_type = view.content_type
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
   996
        if content_type is None:
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
   997
            content_type = 'text/html'
8978
269548f2306e [testlib] fix page validator selection. Closes #2869456
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8975
diff changeset
   998
        if content_type in ('text/html', 'application/xhtml+xml') and output:
10689
49a62b8f6d43 [py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10687
diff changeset
   999
            if output.startswith(b'<!DOCTYPE html>'):
8978
269548f2306e [testlib] fix page validator selection. Closes #2869456
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8975
diff changeset
  1000
                # only check XML well-formness since HTMLValidator isn't html5
269548f2306e [testlib] fix page validator selection. Closes #2869456
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8975
diff changeset
  1001
                # compatible and won't like various other extensions
269548f2306e [testlib] fix page validator selection. Closes #2869456
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8975
diff changeset
  1002
                default_validator = htmlparser.XMLSyntaxValidator
10689
49a62b8f6d43 [py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10687
diff changeset
  1003
            elif output.startswith(b'<?xml'):
6978
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1004
                default_validator = htmlparser.DTDValidator
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1005
            else:
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1006
                default_validator = htmlparser.HTMLValidator
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1007
        else:
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1008
            default_validator = None
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1009
        validatorclass = self.content_type_validators.get(content_type,
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1010
                                                          default_validator)
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1011
        if validatorclass is None:
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1012
            return
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1013
        return validatorclass()
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1014
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1015
    @nocoverage
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1016
    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
  1017
        """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
  1018
        output = output.strip()
10755
4c48d3bab38c [devtools] CubicWebTC._check_html may have to deal with unicode or bytes
Julien Cristau <julien.cristau@logilab.fr>
parents: 10700
diff changeset
  1019
        if isinstance(output, text_type):
4c48d3bab38c [devtools] CubicWebTC._check_html may have to deal with unicode or bytes
Julien Cristau <julien.cristau@logilab.fr>
parents: 10700
diff changeset
  1020
            # XXX
4c48d3bab38c [devtools] CubicWebTC._check_html may have to deal with unicode or bytes
Julien Cristau <julien.cristau@logilab.fr>
parents: 10700
diff changeset
  1021
            output = output.encode('utf-8')
6978
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1022
        validator = self.get_validator(view, output=output)
6979
a8fbcf9b6572 [testlib] take care, validator may be None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6978
diff changeset
  1023
        if validator is None:
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
  1024
            return output  # return raw output if no validator is defined
4346
02f12f39bae5 XXX quick fix html validation by removing <canvas>
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
  1025
        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
  1026
            # 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
  1027
            output = re.sub('<canvas.*?></canvas>', '', output)
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
  1028
        return self.assertWellFormed(validator, output.strip(), context=view.__regid__)
6806
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1029
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1030
    def assertWellFormed(self, validator, content, context=None):
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1031
        try:
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1032
            return validator.parse_string(content)
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7782
diff changeset
  1033
        except Exception:
6806
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1034
            # hijack exception: generative tests stop when the exception
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1035
            # is not an AssertionError
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1036
            klass, exc, tcbk = sys.exc_info()
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1037
            if context is None:
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1038
                msg = u'[%s]' % (klass,)
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1039
            else:
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1040
                msg = u'[%s in %s]' % (klass, context)
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1041
            msg = msg.encode(sys.getdefaultencoding(), 'replace')
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1042
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1043
            try:
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1044
                str_exc = str(exc)
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7782
diff changeset
  1045
            except Exception:
6806
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1046
                str_exc = 'undisplayable exception'
10689
49a62b8f6d43 [py3k] unicode vs str vs bytes vs the world
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10687
diff changeset
  1047
            msg += str_exc.encode(sys.getdefaultencoding(), 'replace')
6806
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1048
            if content is not None:
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1049
                position = getattr(exc, "position", (0,))[0]
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1050
                if position:
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1051
                    # define filter
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1052
                    if isinstance(content, str):
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1053
                        content = unicode(content, sys.getdefaultencoding(), 'replace')
8975
045e449617ad [testlib] repreprocess content so contextual display has a chance to show the proper line. Closes #2869481
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8971
diff changeset
  1054
                    content = validator.preprocess_data(content)
6806
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1055
                    content = content.splitlines()
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1056
                    width = int(log(len(content), 10)) + 1
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1057
                    line_template = " %" + ("%i" % width) + "i: %s"
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1058
                    # XXX no need to iterate the whole file except to get
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1059
                    # the line number
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1060
                    content = u'\n'.join(line_template % (idx + 1, line)
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1061
                                         for idx, line in enumerate(content)
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11270
diff changeset
  1062
                                         if line_context_filter(idx + 1, position))
6806
8291f5e0c9f6 [testlib] extract validation logic into a ``assertWellFormed`` method.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6800
diff changeset
  1063
                    msg += u'\nfor content:\n%s' % content
10590
7629902e7554 [py3k] Fix raise with embedded traceback
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10589
diff changeset
  1064
            exc = AssertionError(msg)
7629902e7554 [py3k] Fix raise with embedded traceback
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10589
diff changeset
  1065
            exc.__traceback__ = tcbk
7629902e7554 [py3k] Fix raise with embedded traceback
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10589
diff changeset
  1066
            raise exc
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1067
6978
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1068
    def assertDocTestFile(self, testfile):
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1069
        # doctest returns tuple (failure_count, test_count)
10562
0c492a2720f1 [devtools] fix CubicWebTC.assertDocTestFile (closes #5930028)
Julien Cristau <julien.cristau@logilab.fr>
parents: 10468
diff changeset
  1070
        with self.admin_access.shell() as mih:
0c492a2720f1 [devtools] fix CubicWebTC.assertDocTestFile (closes #5930028)
Julien Cristau <julien.cristau@logilab.fr>
parents: 10468
diff changeset
  1071
            result = mih.process_script(testfile)
6978
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1072
        if result[0] and result[1]:
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1073
            raise self.failureException("doctest file '%s' failed"
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1074
                                        % testfile)
cac71b8a8711 [testlib] extract get_validator method from _check_html, move assertDocTestFile
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6806
diff changeset
  1075
7060
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1076
    # notifications ############################################################
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1077
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1078
    def assertSentEmail(self, subject, recipients=None, nb_msgs=None):
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1079
        """test recipients in system mailbox for given email subject
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1080
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1081
        :param subject: email subject to find in mailbox
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1082
        :param recipients: list of email recipients
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1083
        :param nb_msgs: expected number of entries
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1084
        :returns: list of matched emails
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1085
        """
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1086
        messages = [email for email in MAILBOX
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1087
                    if email.message.get('Subject') == subject]
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1088
        if recipients is not None:
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1089
            sent_to = set()
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1090
            for msg in messages:
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1091
                sent_to.update(msg.recipients)
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1092
            self.assertSetEqual(set(recipients), sent_to)
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1093
        if nb_msgs is not None:
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1094
            self.assertEqual(len(MAILBOX), nb_msgs)
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1095
        return messages
f26a1cbddc91 [testlib] New assertion method assertSentEmail() to test presence of emails in system mailbox
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7039
diff changeset
  1096
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1097
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1098
# 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
  1099
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1100
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
  1101
4513
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
  1102
# 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
  1103
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
  1104
9837
64c8ee99baf7 [devtools/testlib,fill] use the new connection api (for auto_populate)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
  1105
def how_many_dict(schema, cnx, how_many, skip):
4513
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
  1106
    """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
  1107
    satisfy relations cardinality.
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
  1108
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
  1109
    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
  1110
    least.
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
  1111
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
  1112
    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
  1113
    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
  1114
    """
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1115
    relmap = {}
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1116
    for rschema in schema.relations():
3720
5376aaadd16b backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3676 3689
diff changeset
  1117
        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
  1118
            continue
4053
7cc66b1d9183 more api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 4036
diff changeset
  1119
        for subj, obj in rschema.rdefs:
7cc66b1d9183 more api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 4036
diff changeset
  1120
            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
  1121
            # 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
  1122
            # obj to satisfy it
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
  1123
            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
  1124
                # 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
  1125
                # 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
  1126
                # -> 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
  1127
                #    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
  1128
                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
  1129
            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
  1130
                # 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
  1131
                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
  1132
    unprotected = unprotected_entities(schema)
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
  1133
    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
  1134
        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
  1135
    howmanydict = {}
4513
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
  1136
    # 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
  1137
    # 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
  1138
    for etype in unprotected_entities(schema, strict=True):
9837
64c8ee99baf7 [devtools/testlib,fill] use the new connection api (for auto_populate)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
  1139
        howmanydict[str(etype)] = cnx.execute('Any COUNT(X) WHERE X is %s' % etype)[0][0]
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1140
        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
  1141
            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
  1142
    # 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
  1143
    # 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
  1144
    #
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
  1145
    # 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
  1146
    #
8abf464d2ffe fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4367
diff changeset
  1147
    # XXX we should first check there is no cycle then propagate changes
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10651
diff changeset
  1148
    for (rschema, etype), targets in relmap.items():
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1149
        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
  1150
        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
  1151
    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
  1152
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1153
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1154
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
  1155
    """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
  1156
    __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
  1157
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
  1158
    test_db_id = 'autopopulate'
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
  1159
5913
85240b3f9ee4 [test] add default generic test Tags to CubicWeb TestCase class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5875
diff changeset
  1160
    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
  1161
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1162
    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
  1163
    # 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
  1164
    # 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
  1165
    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
  1166
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1167
    no_auto_populate = ()
3734
b2107f68176c ignored_relations should be a set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3720
diff changeset
  1168
    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
  1169
427
e894eec21a1b move selection of entity types to test in a method to ease overriding
sylvain.thenault@logilab.fr
parents: 0
diff changeset
  1170
    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
  1171
        return unprotected_entities(self.schema, strict=True)
1605
b239744627c7 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1152
diff changeset
  1172
9837
64c8ee99baf7 [devtools/testlib,fill] use the new connection api (for auto_populate)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
  1173
    def custom_populate(self, how_many, 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
  1174
        pass
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1175
9837
64c8ee99baf7 [devtools/testlib,fill] use the new connection api (for auto_populate)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
  1176
    def post_populate(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
  1177
        pass
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1178
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1179
    @nocoverage
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1180
    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
  1181
        """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
  1182
        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
  1183
        """
10354
635cfac73d28 [repoapi] fold ClientConnection into Connection
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10353
diff changeset
  1184
        with self.admin_access.cnx() as cnx:
9837
64c8ee99baf7 [devtools/testlib,fill] use the new connection api (for auto_populate)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
  1185
            with cnx.security_enabled(read=False, write=False):
64c8ee99baf7 [devtools/testlib,fill] use the new connection api (for auto_populate)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
  1186
                self._auto_populate(cnx, how_many)
64c8ee99baf7 [devtools/testlib,fill] use the new connection api (for auto_populate)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
  1187
                cnx.commit()
5004
4cc020ee70e2 le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4958
diff changeset
  1188
9837
64c8ee99baf7 [devtools/testlib,fill] use the new connection api (for auto_populate)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
  1189
    def _auto_populate(self, cnx, how_many):
64c8ee99baf7 [devtools/testlib,fill] use the new connection api (for auto_populate)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
  1190
        self.custom_populate(how_many, 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
  1191
        vreg = self.vreg
9837
64c8ee99baf7 [devtools/testlib,fill] use the new connection api (for auto_populate)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
  1192
        howmanydict = how_many_dict(self.schema, cnx, how_many, self.no_auto_populate)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1193
        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
  1194
            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
  1195
                continue
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1196
            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
  1197
            for rql, args in insert_entity_queries(etype, self.schema, vreg, nb):
9837
64c8ee99baf7 [devtools/testlib,fill] use the new connection api (for auto_populate)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
  1198
                cnx.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
  1199
        edict = {}
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1200
        for etype in unprotected_entities(self.schema, strict=True):
9837
64c8ee99baf7 [devtools/testlib,fill] use the new connection api (for auto_populate)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
  1201
            rset = cnx.execute('%s X' % etype)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1202
            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
  1203
        existingrels = {}
4036
137be4878127 [mq]: fix_set_addition
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 4016
diff changeset
  1204
        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
  1205
        for rschema in self.schema.relations():
11086
c9641ee0b652 [devtools] don't auto-populate virtual relations
Julien Cristau <julien.cristau@logilab.fr>
parents: 11076
diff changeset
  1206
            if rschema.final or rschema in ignored_relations or rschema.rule:
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1207
                continue
9837
64c8ee99baf7 [devtools/testlib,fill] use the new connection api (for auto_populate)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
  1208
            rset = cnx.execute('DISTINCT Any X,Y WHERE X %s Y' % rschema)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1209
            existingrels.setdefault(rschema.type, set()).update((x, y) for x, y in rset)
9837
64c8ee99baf7 [devtools/testlib,fill] use the new connection api (for auto_populate)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
  1210
        q = make_relations_queries(self.schema, edict, cnx, 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
  1211
                                   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
  1212
        for rql, args in q:
3998
94cc7cad3d2d backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777 3956
diff changeset
  1213
            try:
9837
64c8ee99baf7 [devtools/testlib,fill] use the new connection api (for auto_populate)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
  1214
                cnx.execute(rql, args)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
  1215
            except ValidationError as ex:
3998
94cc7cad3d2d backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777 3956
diff changeset
  1216
                # failed to satisfy some constraint
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10569
diff changeset
  1217
                print('error in automatic db population', ex)
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
  1218
                cnx.commit_state = None  # reset uncommitable flag
9837
64c8ee99baf7 [devtools/testlib,fill] use the new connection api (for auto_populate)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9836
diff changeset
  1219
        self.post_populate(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
  1220
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1221
    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
  1222
        etypes = etypes or self.to_test_etypes()
9836
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1223
        with self.admin_access.web_request() as req:
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1224
            for etype in etypes:
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1225
                if limit:
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1226
                    rql = 'Any X LIMIT %s WHERE X is %s' % (limit, etype)
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1227
                else:
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1228
                    rql = 'Any X WHERE X is %s' % etype
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1229
                rset = req.execute(rql)
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10603
diff changeset
  1230
                for row in range(len(rset)):
9836
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1231
                    if limit and row > limit:
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1232
                        break
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1233
                    # XXX iirk
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1234
                    rset2 = rset.limit(limit=1, offset=row)
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1235
                    yield rset2
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1236
1004
625e59773119 fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 563
diff changeset
  1237
    def iter_automatic_rsets(self, limit=10):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1238
        """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
  1239
        etypes = self.to_test_etypes()
2219
bb5098e74b82 protect against empty etypes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2070
diff changeset
  1240
        if not etypes:
bb5098e74b82 protect against empty etypes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2070
diff changeset
  1241
            return
9836
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1242
        with self.admin_access.web_request() as req:
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1243
            for etype in etypes:
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1244
                yield req.execute('Any X LIMIT %s WHERE X is %s' % (limit, etype))
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1245
            etype1 = etypes.pop()
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1246
            try:
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1247
                etype2 = etypes.pop()
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1248
            except KeyError:
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1249
                etype2 = etype1
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1250
            # test a mixed query (DISTINCT/GROUP to avoid getting duplicate
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1251
            # X which make muledit view failing for instance (html validation fails
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1252
            # because of some duplicate "id" attributes)
10974
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
  1253
            yield req.execute('DISTINCT Any X, MAX(Y) GROUPBY X WHERE X is %s, Y is %s' %
6557833657d6 a bit of pep8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10937
diff changeset
  1254
                              (etype1, etype2))
9836
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1255
            # test some application-specific queries if defined
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1256
            for rql in self.application_rql:
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1257
                yield req.execute(rql)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1258
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1259
    def _test_everything_for(self, rset):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1260
        """this method tries to find everything that can be tested
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1261
        for `rset` and yields a callable test (as needed in generative tests)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1262
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1263
        propdefs = self.vreg['propertydefs']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1264
        # make all components visible
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1265
        for k, v in propdefs.items():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1266
            if k.endswith('visible') and not v['default']:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1267
                propdefs[k]['default'] = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1268
        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
  1269
            backup_rset = rset.copy(rset.rows, rset.description)
11076
403a901b6b1e [devtools] Re-implement generative tests using subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11069
diff changeset
  1270
            with self.subTest(name=self._testname(rset, view.__regid__, 'view')):
403a901b6b1e [devtools] Re-implement generative tests using subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11069
diff changeset
  1271
                self.view(view.__regid__, rset,
403a901b6b1e [devtools] Re-implement generative tests using subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11069
diff changeset
  1272
                          rset.req.reset_headers(), 'main-template')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1273
            # We have to do this because some views modify the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1274
            # resultset's syntax tree
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1275
            rset = backup_rset
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1276
        for action in self.list_actions_for(rset):
11076
403a901b6b1e [devtools] Re-implement generative tests using subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11069
diff changeset
  1277
            with self.subTest(name=self._testname(rset, action.__regid__, 'action')):
403a901b6b1e [devtools] Re-implement generative tests using subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11069
diff changeset
  1278
                self._test_action(action)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1279
        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
  1280
            w = [].append
11076
403a901b6b1e [devtools] Re-implement generative tests using subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11069
diff changeset
  1281
            with self.subTest(self._testname(rset, box.__regid__, 'box')):
403a901b6b1e [devtools] Re-implement generative tests using subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11069
diff changeset
  1282
                box.render(w)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1283
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1284
    @staticmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1285
    def _testname(rset, objid, objtype):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1286
        return '%s_%s_%s' % ('_'.join(rset.column_types(0)), objid, objtype)
1605
b239744627c7 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1152
diff changeset
  1287
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1288
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1289
# 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
  1290
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1291
class AutomaticWebTest(AutoPopulateTest):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1292
    """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
  1293
85240b3f9ee4 [test] add default generic test Tags to CubicWeb TestCase class
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 5875
diff changeset
  1294
    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
  1295
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1296
    def setUp(self):
10837
b71511460a4f [devtools/testlib] Remove assertions about direct usage of CubicWebTC/AutomaticWebTest
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10755
diff changeset
  1297
        if self.__class__ is AutomaticWebTest:
b71511460a4f [devtools/testlib] Remove assertions about direct usage of CubicWebTC/AutomaticWebTest
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10755
diff changeset
  1298
            # Prevent direct use of AutomaticWebTest to avoid database caching
b71511460a4f [devtools/testlib] Remove assertions about direct usage of CubicWebTC/AutomaticWebTest
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10755
diff changeset
  1299
            # issues.
b71511460a4f [devtools/testlib] Remove assertions about direct usage of CubicWebTC/AutomaticWebTest
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 10755
diff changeset
  1300
            return
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
  1301
        super(AutomaticWebTest, self).setUp()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
  1302
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1303
        # 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
  1304
        # 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
  1305
        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
  1306
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1307
    def test_one_each_config(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1308
        self.auto_populate(1)
1004
625e59773119 fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 563
diff changeset
  1309
        for rset in self.iter_automatic_rsets(limit=1):
11076
403a901b6b1e [devtools] Re-implement generative tests using subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11069
diff changeset
  1310
            self._test_everything_for(rset)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1311
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1312
    def test_ten_each_config(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1313
        self.auto_populate(10)
1004
625e59773119 fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 563
diff changeset
  1314
        for rset in self.iter_automatic_rsets(limit=10):
11076
403a901b6b1e [devtools] Re-implement generative tests using subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11069
diff changeset
  1315
            self._test_everything_for(rset)
1605
b239744627c7 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1152
diff changeset
  1316
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1317
    def test_startup_views(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1318
        for vid in self.list_startup_views():
9836
71045bb09136 [webtests/automatic views tests] use the new connection api
Julien Cristau <julien.cristau@logilab.fr>
parents: 9790
diff changeset
  1319
            with self.admin_access.web_request() as req:
11076
403a901b6b1e [devtools] Re-implement generative tests using subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11069
diff changeset
  1320
                with self.subTest(vid=vid):
403a901b6b1e [devtools] Re-implement generative tests using subtests
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11069
diff changeset
  1321
                    self.view(vid, None, req)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1322
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1323
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1324
# registry instrumentization ###################################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1325
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
  1326
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
  1327
    try:
2657
de974465d381 [appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
  1328
        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
  1329
    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
  1330
        pass
1605
b239744627c7 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1152
diff changeset
  1331
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1332
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1333
# def vreg_instrumentize(testclass):
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1334
#     # XXX broken
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1335
#     from cubicweb.devtools.apptest import TestEnvironment
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1336
#     env = testclass._env = TestEnvironment('data', configcls=testclass.configcls)
10663
54b8a1f249fb [py3k] dict.itervalues → dict.values
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
  1337
#     for reg in env.vreg.values():
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1338
#         reg._selected = {}
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1339
#         try:
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1340
#             orig_select_best = reg.__class__.__orig_select_best
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1341
#         except Exception:
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1342
#             orig_select_best = reg.__class__._select_best
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1343
#         def instr_select_best(self, *args, **kwargs):
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1344
#             selected = orig_select_best(self, *args, **kwargs)
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1345
#             try:
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1346
#                 self._selected[selected.__class__] += 1
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1347
#             except KeyError:
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1348
#                 self._selected[selected.__class__] = 1
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1349
#             except AttributeError:
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1350
#                 pass # occurs on reg used to restore database
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1351
#             return selected
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1352
#         reg.__class__._select_best = instr_select_best
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1353
#         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
  1354
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2668
diff changeset
  1355
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1356
# def print_untested_objects(testclass, skipregs=('hooks', 'etypes')):
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10651
diff changeset
  1357
#     for regname, reg in testclass._env.vreg.items():
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1358
#         if regname in skipregs:
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1359
#             continue
10663
54b8a1f249fb [py3k] dict.itervalues → dict.values
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
  1360
#         for appobjects in reg.values():
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1361
#             for appobject in appobjects:
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1362
#                 if not reg._selected.get(appobject):
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
  1363
#                     print 'not tested', regname, appobject