author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 06 Apr 2010 10:29:41 +0200 | |
branch | stable |
changeset 5150 | 7a9e71ee5671 |
parent 5128 | e5d300d75519 |
child 5159 | 2543cfa5d54a |
child 5175 | 6efb7a7ae570 |
permissions | -rw-r--r-- |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
1 |
"""this module contains base classes and utilities for cubicweb tests |
0 | 2 |
|
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3956
diff
changeset
|
4 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1775
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
10 |
import os |
0 | 11 |
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
|
12 |
import re |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
13 |
from urllib import unquote |
0 | 14 |
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
|
15 |
from contextlib import contextmanager |
0 | 16 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
17 |
import simplejson |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
18 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
19 |
import yams.schema |
0 | 20 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
21 |
from logilab.common.testlib import TestCase, InnerTest |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
22 |
from logilab.common.pytest import nocoverage, pause_tracing, resume_tracing |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
23 |
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
|
24 |
from logilab.common.umessage import message_from_string |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
25 |
from logilab.common.decorators import cached, classproperty, clear_cache |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
26 |
from logilab.common.deprecation import deprecated |
0 | 27 |
|
3998
94cc7cad3d2d
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
28 |
from cubicweb import ValidationError, NoSelectableObject, AuthenticationError |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
29 |
from cubicweb import cwconfig, devtools, web, server |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
30 |
from cubicweb.dbapi import repo_connect, ConnectionProperties, ProgrammingError |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
31 |
from cubicweb.sobjects import notification |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
32 |
from cubicweb.web import Redirect, application |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
33 |
from cubicweb.devtools import SYSTEM_ENTITIES, SYSTEM_RELATIONS, VIEW_VALIDATORS |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
34 |
from cubicweb.devtools import fake, htmlparser |
0 | 35 |
|
36 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
37 |
# low-level utilities ########################################################## |
0 | 38 |
|
39 |
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
|
40 |
"""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
|
41 |
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
|
42 |
""" |
0 | 43 |
def do_view(self, arg): |
44 |
import webbrowser |
|
45 |
data = self._getval(arg) |
|
46 |
file('/tmp/toto.html', 'w').write(data) |
|
47 |
webbrowser.open('file:///tmp/toto.html') |
|
48 |
||
49 |
def line_context_filter(line_no, center, before=3, after=None): |
|
50 |
"""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
|
51 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
52 |
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
|
53 |
""" |
0 | 54 |
if after is None: |
55 |
after = before |
|
56 |
return center - before <= line_no <= center + after |
|
57 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
58 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
59 |
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
|
60 |
"""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
|
61 |
(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
|
62 |
""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
63 |
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
|
64 |
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
|
65 |
else: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
66 |
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
|
67 |
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
|
68 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
69 |
|
4689
4eb1f4490538
[test] skipping versions checking during test is enough, no need for monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4681
diff
changeset
|
70 |
def refresh_repo(repo, resetschema=False, resetvreg=False): |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
71 |
devtools.reset_test_database(repo.config) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
72 |
for pool in repo.pools: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
73 |
pool.reconnect() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
74 |
repo._type_source_cache = {} |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
75 |
repo._extid_cache = {} |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
76 |
repo.querier._rql_cache = {} |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
77 |
for source in repo.sources: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
78 |
source.reset_caches() |
4690
0cfb6b63f90b
[testlib] provide ability to control schema/vreg reset using reset_schema/reset_vreg test class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4689
diff
changeset
|
79 |
if resetschema: |
0cfb6b63f90b
[testlib] provide ability to control schema/vreg reset using reset_schema/reset_vreg test class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4689
diff
changeset
|
80 |
repo.set_schema(repo.config.load_schema(), resetvreg=resetvreg) |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
81 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
82 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
83 |
# 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
|
84 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
85 |
MAILBOX = [] |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
86 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
87 |
class Email: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
88 |
"""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
|
89 |
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
|
90 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
91 |
* `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
|
92 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
93 |
* `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
|
94 |
message |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
95 |
""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
96 |
def __init__(self, recipients, msg): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
97 |
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
|
98 |
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
|
99 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
100 |
@property |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
101 |
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
|
102 |
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
|
103 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
104 |
@property |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
105 |
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
|
106 |
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
|
107 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
108 |
@property |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
109 |
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
|
110 |
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
|
111 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
112 |
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
|
113 |
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
|
114 |
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
|
115 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
116 |
# 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
|
117 |
# 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
|
118 |
class MockSMTP: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
119 |
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
|
120 |
pass |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
121 |
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
|
122 |
pass |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
123 |
def sendmail(self, helo_addr, recipients, msg): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
124 |
MAILBOX.append(Email(recipients, msg)) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
125 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
126 |
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
|
127 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
128 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
129 |
# 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
|
130 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
131 |
class CubicWebTC(TestCase): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
132 |
"""abstract class for test using an apptest environment |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
133 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
134 |
attributes: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
135 |
`vreg`, the vregistry |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
136 |
`schema`, 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
|
137 |
`config`, cubicweb configuration |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
138 |
`cnx`, dbapi connection to the repository using an admin user |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
139 |
`session`, server side session associated to `cnx` |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
140 |
`app`, the cubicweb publisher (for web testing) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
141 |
`repo`, the repository object |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
142 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
143 |
`admlogin`, login of the admin user |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
144 |
`admpassword`, password of the admin user |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
145 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
146 |
""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
147 |
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
|
148 |
configcls = devtools.ApptestConfiguration |
4690
0cfb6b63f90b
[testlib] provide ability to control schema/vreg reset using reset_schema/reset_vreg test class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4689
diff
changeset
|
149 |
reset_schema = reset_vreg = False # reset schema / vreg between tests |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
150 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
151 |
@classproperty |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
152 |
def config(cls): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
153 |
"""return the configuration object. Configuration is cached on the test |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
154 |
class. |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
155 |
""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
156 |
try: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
157 |
return cls.__dict__['_config'] |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
158 |
except KeyError: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
159 |
config = cls._config = cls.configcls(cls.appid) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
160 |
config.mode = 'test' |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
161 |
return config |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
162 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
163 |
@classmethod |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
164 |
def init_config(cls, config): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
165 |
"""configuration initialization hooks. You may want to override this.""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
166 |
source = config.sources()['system'] |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
167 |
cls.admlogin = unicode(source['db-user']) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
168 |
cls.admpassword = source['db-password'] |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
169 |
# uncomment the line below if you want rql queries to be logged |
3163
edfe43ceaa35
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2968
diff
changeset
|
170 |
#config.global_set_option('query-log-file', |
edfe43ceaa35
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2968
diff
changeset
|
171 |
# '/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
|
172 |
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
|
173 |
# 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
|
174 |
# 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
|
175 |
config.global_set_option('default-dest-addrs', ['whatever']) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
176 |
try: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
177 |
send_to = '%s@logilab.fr' % os.getlogin() |
3163
edfe43ceaa35
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2968
diff
changeset
|
178 |
# AttributeError since getlogin not available under all platforms |
edfe43ceaa35
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2968
diff
changeset
|
179 |
except (OSError, AttributeError): |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
180 |
send_to = '%s@logilab.fr' % (os.environ.get('USER') |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
181 |
or os.environ.get('USERNAME') |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
182 |
or os.environ.get('LOGNAME')) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
183 |
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
|
184 |
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
|
185 |
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
|
186 |
config.global_set_option('sender-addr', 'cubicweb-test@logilab.fr') |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
187 |
# 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
|
188 |
config.global_set_option('base-url', devtools.BASE_URL) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
189 |
try: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
190 |
config.global_set_option('embed-allowed', re.compile('.*')) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
191 |
except: # not in server only configuration |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
192 |
pass |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
193 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
194 |
@classmethod |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
195 |
def _init_repo(cls): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
196 |
"""init the repository and connection to it. |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
197 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
198 |
Repository and connection are cached on the test class. Once |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
199 |
initialized, we simply reset connections and repository caches. |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
200 |
""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
201 |
if not 'repo' in cls.__dict__: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
202 |
cls._build_repo() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
203 |
else: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
204 |
cls.cnx.rollback() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
205 |
cls._refresh_repo() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
206 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
207 |
@classmethod |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
208 |
def _build_repo(cls): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
209 |
cls.repo, cls.cnx = devtools.init_test_database(config=cls.config) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
210 |
cls.init_config(cls.config) |
4958
665eacdd8c50
[repo] call server_startup event in start_looping_task the repo is fully started (registered in pyro for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4917
diff
changeset
|
211 |
cls.repo.hm.call_hooks('server_startup', repo=cls.repo) |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
212 |
cls.vreg = cls.repo.vreg |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
213 |
cls._orig_cnx = cls.cnx |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
214 |
cls.config.repository = lambda x=None: cls.repo |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
215 |
# necessary for authentication tests |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
216 |
cls.cnx.login = cls.admlogin |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
217 |
cls.cnx.authinfo = {'password': cls.admpassword} |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
218 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
219 |
@classmethod |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
220 |
def _refresh_repo(cls): |
4690
0cfb6b63f90b
[testlib] provide ability to control schema/vreg reset using reset_schema/reset_vreg test class attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4689
diff
changeset
|
221 |
refresh_repo(cls.repo, cls.reset_schema, cls.reset_vreg) |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
222 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
223 |
# 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
|
224 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
225 |
@property |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
226 |
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
|
227 |
"""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
|
228 |
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
|
229 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
230 |
@property |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
231 |
def session(self): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
232 |
"""return current server side session (using default manager account)""" |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
233 |
session = self.repo._sessions[self.cnx.sessionid] |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
234 |
session.set_pool() |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
235 |
return session |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
236 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
237 |
@property |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
238 |
def adminsession(self): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
239 |
"""return current server side session (using default manager account)""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
240 |
return self.repo._sessions[self._orig_cnx.sessionid] |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
241 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
242 |
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
|
243 |
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
|
244 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
245 |
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
|
246 |
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
|
247 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
248 |
# 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
|
249 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
250 |
def setUp(self): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
251 |
pause_tracing() |
5020
30a539c2074d
When a TC failed to initiliase the database, the following test are SKIPPED
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
4958
diff
changeset
|
252 |
previous_failure = self.__class__.__dict__.get('_repo_init_failed') |
30a539c2074d
When a TC failed to initiliase the database, the following test are SKIPPED
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
4958
diff
changeset
|
253 |
if previous_failure is not None: |
30a539c2074d
When a TC failed to initiliase the database, the following test are SKIPPED
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
4958
diff
changeset
|
254 |
self.skip('repository is not initialised: %r' % previous_failure) |
30a539c2074d
When a TC failed to initiliase the database, the following test are SKIPPED
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
4958
diff
changeset
|
255 |
try: |
30a539c2074d
When a TC failed to initiliase the database, the following test are SKIPPED
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
4958
diff
changeset
|
256 |
self._init_repo() |
30a539c2074d
When a TC failed to initiliase the database, the following test are SKIPPED
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
4958
diff
changeset
|
257 |
except Exception, ex: |
30a539c2074d
When a TC failed to initiliase the database, the following test are SKIPPED
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
4958
diff
changeset
|
258 |
self.__class__._repo_init_failed = ex |
30a539c2074d
When a TC failed to initiliase the database, the following test are SKIPPED
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
4958
diff
changeset
|
259 |
raise |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
260 |
resume_tracing() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
261 |
self.setup_database() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
262 |
self.commit() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
263 |
MAILBOX[:] = [] # reset mailbox |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
264 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
265 |
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
|
266 |
"""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
|
267 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
268 |
# 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
|
269 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
270 |
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
|
271 |
"""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
|
272 |
if req is None: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
273 |
req = self.request() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
274 |
return self.cnx.user(req) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
275 |
else: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
276 |
return req.user |
563
a690996639ca
[testlib] fix pb. related to class scoped variables
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
562
diff
changeset
|
277 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
278 |
def create_user(self, login, groups=('users',), password=None, req=None, |
5055
14906474c55d
[testlib] create_user support extra kwargs to give to create_entity(CWUser)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5036
diff
changeset
|
279 |
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
|
280 |
"""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
|
281 |
if password is None: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
282 |
password = login.encode('utf8') |
5055
14906474c55d
[testlib] create_user support extra kwargs to give to create_entity(CWUser)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5036
diff
changeset
|
283 |
if req is None: |
14906474c55d
[testlib] create_user support extra kwargs to give to create_entity(CWUser)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5036
diff
changeset
|
284 |
req = self._orig_cnx.request() |
14906474c55d
[testlib] create_user support extra kwargs to give to create_entity(CWUser)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5036
diff
changeset
|
285 |
user = req.create_entity('CWUser', login=unicode(login), |
14906474c55d
[testlib] create_user support extra kwargs to give to create_entity(CWUser)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5036
diff
changeset
|
286 |
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
|
287 |
req.execute('SET X in_group G WHERE X eid %%(x)s, G name IN(%s)' |
14906474c55d
[testlib] create_user support extra kwargs to give to create_entity(CWUser)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5036
diff
changeset
|
288 |
% ','.join(repr(g) for g in groups), |
14906474c55d
[testlib] create_user support extra kwargs to give to create_entity(CWUser)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5036
diff
changeset
|
289 |
{'x': user.eid}, 'x') |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
290 |
user.clear_related_cache('in_group', 'subject') |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
291 |
if commit: |
5055
14906474c55d
[testlib] create_user support extra kwargs to give to create_entity(CWUser)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5036
diff
changeset
|
292 |
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
|
293 |
return user |
563
a690996639ca
[testlib] fix pb. related to class scoped variables
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
562
diff
changeset
|
294 |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3589
diff
changeset
|
295 |
def login(self, login, **kwargs): |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
296 |
"""return a connection for the given login/password""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
297 |
if login == self.admlogin: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
298 |
self.restore_connection() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
299 |
else: |
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3589
diff
changeset
|
300 |
if not kwargs: |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3589
diff
changeset
|
301 |
kwargs['password'] = str(login) |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
302 |
self.cnx = repo_connect(self.repo, unicode(login), |
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3589
diff
changeset
|
303 |
cnxprops=ConnectionProperties('inmemory'), |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3589
diff
changeset
|
304 |
**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
|
305 |
if login == self.vreg.config.anonymous_user()[0]: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
306 |
self.cnx.anonymous_connection = True |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
307 |
return self.cnx |
563
a690996639ca
[testlib] fix pb. related to class scoped variables
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
562
diff
changeset
|
308 |
|
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 |
def restore_connection(self): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
310 |
if not self.cnx is self._orig_cnx: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
311 |
try: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
312 |
self.cnx.close() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
313 |
except ProgrammingError: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
314 |
pass # already closed |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
315 |
self.cnx = self._orig_cnx |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
316 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
317 |
# db api ################################################################## |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
318 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
319 |
@nocoverage |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
320 |
def cursor(self, req=None): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
321 |
return self.cnx.cursor(req or self.request()) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
322 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
323 |
@nocoverage |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
324 |
def execute(self, rql, args=None, eidkey=None, req=None): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
325 |
"""executes <rql>, builds a resultset, and returns a couple (rset, req) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
326 |
where req is a FakeRequest |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
327 |
""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
328 |
req = req or self.request(rql=rql) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
329 |
return self.cnx.cursor(req).execute(unicode(rql), args, eidkey) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
330 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
331 |
@nocoverage |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
332 |
def commit(self): |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4835
diff
changeset
|
333 |
try: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4835
diff
changeset
|
334 |
return self.cnx.commit() |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4835
diff
changeset
|
335 |
finally: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4835
diff
changeset
|
336 |
self.session.set_pool() # ensure pool still set after commit |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
337 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
338 |
@nocoverage |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
339 |
def rollback(self): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
340 |
try: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
341 |
self.cnx.rollback() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
342 |
except ProgrammingError: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
343 |
pass |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4835
diff
changeset
|
344 |
finally: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4835
diff
changeset
|
345 |
self.session.set_pool() # ensure pool still set after commit |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
346 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
347 |
# # server side db api ####################################################### |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
348 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
349 |
def sexecute(self, rql, args=None, eid_key=None): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
350 |
self.session.set_pool() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
351 |
return self.session.execute(rql, args, eid_key) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
352 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
353 |
# 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
|
354 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
355 |
def entity(self, rql, args=None, eidkey=None, req=None): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
356 |
return self.execute(rql, args, eidkey, req=req).get_entity(0, 0) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
357 |
|
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
|
358 |
@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
|
359 |
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
|
360 |
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
|
361 |
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
|
362 |
self.vreg.register(obj) |
5128
e5d300d75519
[python] take care to this detail of @contextmanager: if an unhandled exception occurs in the block, it is reraised inside the generator at the point where the yield occurred
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5102
diff
changeset
|
363 |
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
|
364 |
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
|
365 |
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
|
366 |
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
|
367 |
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
|
368 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
369 |
# vregistry inspection utilities ########################################### |
0 | 370 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
371 |
def pviews(self, req, rset): |
3468
b02fa4db2868
[tests] make unittest_viewselectors pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3454
diff
changeset
|
372 |
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
|
373 |
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
|
374 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
375 |
def pactions(self, req, rset, |
3589
a5432f99f2d9
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3468
diff
changeset
|
376 |
skipcategories=('addrelated', 'siteactions', 'useractions', 'footer')): |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3230
diff
changeset
|
377 |
return [(a.__regid__, a.__class__) |
2813
0cf6c8005bf6
R propagate deprecation of CWRegistry.possible_vobjects
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2793
diff
changeset
|
378 |
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
|
379 |
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
|
380 |
|
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 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
|
382 |
return [(a.__regid__, a.__class__) |
2813
0cf6c8005bf6
R propagate deprecation of CWRegistry.possible_vobjects
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2793
diff
changeset
|
383 |
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
|
384 |
if a.category in categories] |
0 | 385 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
386 |
def pactionsdict(self, req, rset, |
3589
a5432f99f2d9
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3468
diff
changeset
|
387 |
skipcategories=('addrelated', 'siteactions', 'useractions', 'footer')): |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
388 |
res = {} |
2813
0cf6c8005bf6
R propagate deprecation of CWRegistry.possible_vobjects
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2793
diff
changeset
|
389 |
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
|
390 |
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
|
391 |
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
|
392 |
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
|
393 |
|
3230 | 394 |
def action_submenu(self, req, rset, id): |
395 |
return self._test_action(self.vreg['actions'].select(id, req, rset=rset)) |
|
396 |
||
397 |
def _test_action(self, action): |
|
398 |
class fake_menu(list): |
|
399 |
@property |
|
400 |
def items(self): |
|
401 |
return self |
|
402 |
class fake_box(object): |
|
403 |
def mk_action(self, label, url, **kwargs): |
|
404 |
return (label, url) |
|
405 |
def box_action(self, action, **kwargs): |
|
406 |
return (action.title, action.url()) |
|
407 |
submenu = fake_menu() |
|
408 |
action.fill_menu(fake_box(), submenu) |
|
409 |
return submenu |
|
0 | 410 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
411 |
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
|
412 |
"""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
|
413 |
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
|
414 |
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
|
415 |
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
|
416 |
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
|
417 |
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
|
418 |
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
|
419 |
continue |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
420 |
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
|
421 |
continue |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
422 |
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
|
423 |
if view.category != 'startupview' |
3454
ad1cddc06997
[testlib] added some new required attributes on FakeRequest, fix dummy NameError
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3418
diff
changeset
|
424 |
and not issubclass(view, notification.NotificationView)] |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
425 |
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
|
426 |
try: |
2774 | 427 |
view = viewsvreg._select_best(views, req, rset=rset) |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
428 |
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
|
429 |
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
|
430 |
else: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
431 |
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
|
432 |
# 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
|
433 |
# 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
|
434 |
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
|
435 |
continue |
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 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
|
438 |
"""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
|
439 |
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
|
440 |
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
|
441 |
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
|
442 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
443 |
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
|
444 |
"""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
|
445 |
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
|
446 |
for box in self.vreg['boxes'].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
|
447 |
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
|
448 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
449 |
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
|
450 |
"""returns the list of startup views""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
451 |
req = self.request() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
452 |
for view in self.vreg['views'].possible_views(req, None): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
453 |
if view.category == 'startupview': |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3230
diff
changeset
|
454 |
yield view.__regid__ |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
455 |
else: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
456 |
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
|
457 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
458 |
# 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
|
459 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
460 |
@property |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
461 |
@cached |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
462 |
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
|
463 |
"""return a cubicweb publisher""" |
4137
3a2d6c38bcd9
override publisher's error handler to ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4083
diff
changeset
|
464 |
publisher = application.CubicWebPublisher(self.config, vreg=self.vreg) |
3a2d6c38bcd9
override publisher's error handler to ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4083
diff
changeset
|
465 |
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
|
466 |
raise |
3a2d6c38bcd9
override publisher's error handler to ease debugging
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4083
diff
changeset
|
467 |
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
|
468 |
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
|
469 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
470 |
requestcls = fake.FakeRequest |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
471 |
def request(self, *args, **kwargs): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
472 |
"""return a web ui request""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
473 |
req = self.requestcls(self.vreg, form=kwargs) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
474 |
req.set_connection(self.cnx) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
475 |
return req |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
476 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
477 |
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
|
478 |
"""remote json call simulation""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
479 |
dump = simplejson.dumps |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
480 |
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
|
481 |
req = self.request(fname=fname, pageid='123', arg=args) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
482 |
ctrl = self.vreg['controllers'].select('json', req) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
483 |
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
|
484 |
|
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
485 |
def app_publish(self, req, path='view'): |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
486 |
return self.app.publish(path, req) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
487 |
|
4138
d087a3a3b16f
clearer api: distinguish app_publish / ctrl_publish
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4137
diff
changeset
|
488 |
def ctrl_publish(self, 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
|
489 |
"""call the publish method of the edit controller""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
490 |
ctrl = self.vreg['controllers'].select('edit', req) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
491 |
try: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
492 |
result = ctrl.publish() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
493 |
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
|
494 |
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
|
495 |
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
|
496 |
raise |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
497 |
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
|
498 |
|
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
499 |
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
|
500 |
"""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
|
501 |
Redirect exception |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
502 |
""" |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
503 |
try: |
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4690
diff
changeset
|
504 |
callback(req) |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
505 |
except Redirect, ex: |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
506 |
try: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
507 |
path, params = ex.location.split('?', 1) |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
508 |
except ValueError: |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
509 |
path = ex.location |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
510 |
params = {} |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
511 |
else: |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
512 |
cleanup = lambda p: (p[0], unquote(p[1])) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
513 |
params = dict(cleanup(p.split('=', 1)) for p in params.split('&') if p) |
5036
3e436a494ce3
[testlib] __errorurl may be a relative path, check we're actually removing base url first
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5020
diff
changeset
|
514 |
if path.startswith(req.base_url()): # may be relative |
3e436a494ce3
[testlib] __errorurl may be a relative path, check we're actually removing base url first
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5020
diff
changeset
|
515 |
path = path[len(req.base_url()):] |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
516 |
return path, params |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
517 |
else: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
518 |
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
|
519 |
|
4523
d8127c2bd6b5
use 'edit' as defaut path for expect_redirect_publish
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4513
diff
changeset
|
520 |
def expect_redirect_publish(self, req, path='edit'): |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
521 |
"""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
|
522 |
get a Redirect exception |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
523 |
""" |
4083
3b285889b8e9
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4053
diff
changeset
|
524 |
return self.expect_redirect(lambda x: self.app_publish(x, path), req) |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
525 |
|
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
526 |
def init_authentication(self, authmode, anonuser=None): |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
527 |
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
|
528 |
self.set_option('anonymous-user', anonuser) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
529 |
req = self.request() |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
530 |
origcnx = req.cnx |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
531 |
req.cnx = None |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
532 |
sh = self.app.session_handler |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
533 |
authm = sh.session_manager.authmanager |
4916 | 534 |
authm.anoninfo = self.vreg.config.anonymous_user() |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
535 |
# not properly cleaned between tests |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
536 |
self.open_sessions = sh.session_manager._sessions = {} |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
537 |
return req, origcnx |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
538 |
|
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
539 |
def assertAuthSuccess(self, req, origcnx, nbsessions=1): |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
540 |
sh = self.app.session_handler |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
541 |
path, params = self.expect_redirect(lambda x: self.app.connect(x), req) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
542 |
cnx = req.cnx |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
543 |
self.assertEquals(len(self.open_sessions), nbsessions, self.open_sessions) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
544 |
self.assertEquals(cnx.login, origcnx.login) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
545 |
self.assertEquals(cnx.anonymous_connection, False) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
546 |
self.assertEquals(path, 'view') |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
547 |
self.assertEquals(params, {'__message': 'welcome %s !' % cnx.user().login}) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
548 |
|
3676
fa170564e1a0
optional nbsessions arg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3657
diff
changeset
|
549 |
def assertAuthFailure(self, req, nbsessions=0): |
3657
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
550 |
self.assertRaises(AuthenticationError, self.app.connect, req) |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
551 |
self.assertEquals(req.cnx, None) |
3676
fa170564e1a0
optional nbsessions arg
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3657
diff
changeset
|
552 |
self.assertEquals(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
|
553 |
clear_cache(req, 'get_authorization') |
706d7bf0ae3d
factor out code reusable for authentication tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3647
diff
changeset
|
554 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
555 |
# content validation ####################################################### |
0 | 556 |
|
557 |
# validators are used to validate (XML, DTD, whatever) view's content |
|
558 |
# validators availables are : |
|
559 |
# DTDValidator : validates XML + declared DTD |
|
560 |
# SaxOnlyValidator : guarantees XML is well formed |
|
561 |
# None : do not try to validate anything |
|
562 |
# 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
|
563 |
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
|
564 |
# 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
|
565 |
# |
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
|
566 |
# 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
|
567 |
# snippets |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
568 |
#'text/html': DTDValidator, |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
569 |
#'application/xhtml+xml': DTDValidator, |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
570 |
'application/xml': htmlparser.SaxOnlyValidator, |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
571 |
'text/xml': htmlparser.SaxOnlyValidator, |
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
572 |
'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
|
573 |
'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
|
574 |
'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
|
575 |
'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
|
576 |
'application/json': None, |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
577 |
'image/png': None, |
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
578 |
} |
562
bdadb26c4a3c
old .validators attribute is now .vid_validators
sylvain.thenault@logilab.fr
parents:
549
diff
changeset
|
579 |
# 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
|
580 |
vid_validators = dict((vid, htmlparser.VALMAP[valkey]) |
562
bdadb26c4a3c
old .validators attribute is now .vid_validators
sylvain.thenault@logilab.fr
parents:
549
diff
changeset
|
581 |
for vid, valkey in VIEW_VALIDATORS.iteritems()) |
1605 | 582 |
|
0 | 583 |
|
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
|
584 |
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
|
585 |
**kwargs): |
0 | 586 |
"""This method tests the view `vid` on `rset` using `template` |
587 |
||
588 |
If no error occured while rendering the view, the HTML is analyzed |
|
589 |
and parsed. |
|
590 |
||
591 |
:returns: an instance of `cubicweb.devtools.htmlparser.PageInfo` |
|
592 |
encapsulation the generated HTML |
|
593 |
""" |
|
1142 | 594 |
req = req or rset and rset.req or self.request() |
0 | 595 |
req.form['vid'] = vid |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
596 |
kwargs['rset'] = rset |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
597 |
viewsreg = self.vreg['views'] |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
598 |
view = viewsreg.select(vid, req, **kwargs) |
0 | 599 |
# set explicit test description |
600 |
if rset is not None: |
|
2070 | 601 |
self.set_description("testing %s, mod=%s (%s)" % ( |
602 |
vid, view.__module__, rset.printable_rql())) |
|
0 | 603 |
else: |
2070 | 604 |
self.set_description("testing %s, mod=%s (no rset)" % ( |
605 |
vid, view.__module__)) |
|
0 | 606 |
if template is None: # raw view testing, no template |
1773 | 607 |
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
|
608 |
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
|
609 |
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
|
610 |
templateview = viewsreg.select(template, req, **kwargs) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
611 |
viewfunc = lambda **k: viewsreg.main_template(req, template, |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
612 |
**kwargs) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
613 |
kwargs.pop('rset') |
829
ea092805d8f8
The main template is now a simple view, there's no need to monkey patch TheMainTemplate._select_view_and_rset
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
823
diff
changeset
|
614 |
return self._test_view(viewfunc, view, template, kwargs) |
0 | 615 |
|
616 |
||
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
|
617 |
def _test_view(self, viewfunc, view, template='main-template', kwargs={}): |
0 | 618 |
"""this method does the actual call to the view |
619 |
||
620 |
If no error occured while rendering the view, the HTML is analyzed |
|
621 |
and parsed. |
|
622 |
||
623 |
:returns: an instance of `cubicweb.devtools.htmlparser.PageInfo` |
|
624 |
encapsulation the generated HTML |
|
625 |
""" |
|
626 |
output = None |
|
627 |
try: |
|
628 |
output = viewfunc(**kwargs) |
|
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
629 |
return self._check_html(output, view, template) |
0 | 630 |
except (SystemExit, KeyboardInterrupt): |
631 |
raise |
|
632 |
except: |
|
633 |
# hijack exception: generative tests stop when the exception |
|
634 |
# is not an AssertionError |
|
635 |
klass, exc, tcbk = sys.exc_info() |
|
636 |
try: |
|
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3230
diff
changeset
|
637 |
msg = '[%s in %s] %s' % (klass, view.__regid__, exc) |
0 | 638 |
except: |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3230
diff
changeset
|
639 |
msg = '[%s in %s] undisplayable exception' % (klass, view.__regid__) |
0 | 640 |
if output is not None: |
641 |
position = getattr(exc, "position", (0,))[0] |
|
642 |
if position: |
|
643 |
# define filter |
|
644 |
output = output.splitlines() |
|
645 |
width = int(log(len(output), 10)) + 1 |
|
646 |
line_template = " %" + ("%i" % width) + "i: %s" |
|
647 |
# XXX no need to iterate the whole file except to get |
|
648 |
# the line number |
|
649 |
output = '\n'.join(line_template % (idx + 1, line) |
|
650 |
for idx, line in enumerate(output) |
|
651 |
if line_context_filter(idx+1, position)) |
|
1133 | 652 |
msg += '\nfor output:\n%s' % output |
0 | 653 |
raise AssertionError, msg, tcbk |
654 |
||
427
e894eec21a1b
move selection of entity types to test in a method to ease overriding
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
655 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
656 |
@nocoverage |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
657 |
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
|
658 |
"""raises an exception if the HTML is invalid""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
659 |
try: |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3230
diff
changeset
|
660 |
validatorclass = self.vid_validators[view.__regid__] |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
661 |
except KeyError: |
3998
94cc7cad3d2d
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
662 |
if view.content_type in ('text/html', 'application/xhtml+xml'): |
94cc7cad3d2d
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
663 |
if template is None: |
94cc7cad3d2d
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
664 |
default_validator = htmlparser.HTMLValidator |
94cc7cad3d2d
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
665 |
else: |
94cc7cad3d2d
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
666 |
default_validator = htmlparser.DTDValidator |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
667 |
else: |
3998
94cc7cad3d2d
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
668 |
default_validator = None |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
669 |
validatorclass = self.content_type_validators.get(view.content_type, |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
670 |
default_validator) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
671 |
if validatorclass is None: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
672 |
return None |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
673 |
validator = validatorclass() |
4346
02f12f39bae5
XXX quick fix html validation by removing <canvas>
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
674 |
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
|
675 |
# 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
|
676 |
output = re.sub('<canvas.*?></canvas>', '', output) |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
677 |
return validator.parse_string(output.strip()) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
678 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
679 |
# deprecated ############################################################### |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
680 |
|
4016
250784a2d105
deprecated one more custom add_entity in favor of req.create_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3998
diff
changeset
|
681 |
@deprecated('[3.6] use self.request().create_entity(...)') |
250784a2d105
deprecated one more custom add_entity in favor of req.create_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3998
diff
changeset
|
682 |
def add_entity(self, etype, req=None, **kwargs): |
250784a2d105
deprecated one more custom add_entity in favor of req.create_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3998
diff
changeset
|
683 |
if req is None: |
250784a2d105
deprecated one more custom add_entity in favor of req.create_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3998
diff
changeset
|
684 |
req = self.request() |
250784a2d105
deprecated one more custom add_entity in favor of req.create_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3998
diff
changeset
|
685 |
return req.create_entity(etype, **kwargs) |
250784a2d105
deprecated one more custom add_entity in favor of req.create_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3998
diff
changeset
|
686 |
|
2793
bfb21f7a0d13
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2774
diff
changeset
|
687 |
@deprecated('[3.4] use self.vreg["etypes"].etype_class(etype)(self.request())') |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
688 |
def etype_instance(self, etype, 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
|
689 |
req = req or self.request() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
690 |
e = self.vreg['etypes'].etype_class(etype)(req) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
691 |
e.eid = None |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
692 |
return e |
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 |
@nocoverage |
4367
fa02f7dccfe4
fix deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4346
diff
changeset
|
695 |
@deprecated('[3.4] use req = self.request(); rset = req.execute()', |
fa02f7dccfe4
fix deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4346
diff
changeset
|
696 |
stacklevel=3) |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
697 |
def rset_and_req(self, rql, optional_args=None, args=None, eidkey=None): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
698 |
"""executes <rql>, builds a resultset, and returns a |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
699 |
couple (rset, req) where req is a FakeRequest |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
700 |
""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
701 |
return (self.execute(rql, args, eidkey), |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
702 |
self.request(rql=rql, **optional_args or {})) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
703 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
704 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
705 |
# 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
|
706 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
707 |
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
|
708 |
|
4513
8abf464d2ffe
fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4367
diff
changeset
|
709 |
# 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
|
710 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
711 |
def how_many_dict(schema, cursor, how_many, skip): |
4513
8abf464d2ffe
fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4367
diff
changeset
|
712 |
"""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
|
713 |
satisfy relations cardinality. |
8abf464d2ffe
fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4367
diff
changeset
|
714 |
|
8abf464d2ffe
fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4367
diff
changeset
|
715 |
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
|
716 |
least. |
8abf464d2ffe
fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4367
diff
changeset
|
717 |
|
8abf464d2ffe
fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4367
diff
changeset
|
718 |
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
|
719 |
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
|
720 |
""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
721 |
relmap = {} |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
722 |
for rschema in schema.relations(): |
3720 | 723 |
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
|
724 |
continue |
4053
7cc66b1d9183
more api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4036
diff
changeset
|
725 |
for subj, obj in rschema.rdefs: |
7cc66b1d9183
more api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
4036
diff
changeset
|
726 |
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
|
727 |
# 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
|
728 |
# obj to satisfy it |
8abf464d2ffe
fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4367
diff
changeset
|
729 |
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
|
730 |
# 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
|
731 |
# 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
|
732 |
# -> 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
|
733 |
# 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
|
734 |
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
|
735 |
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
|
736 |
# 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
|
737 |
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
|
738 |
unprotected = unprotected_entities(schema) |
4513
8abf464d2ffe
fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4367
diff
changeset
|
739 |
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
|
740 |
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
|
741 |
howmanydict = {} |
4513
8abf464d2ffe
fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4367
diff
changeset
|
742 |
# 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
|
743 |
# 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
|
744 |
for etype in unprotected_entities(schema, strict=True): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
745 |
howmanydict[str(etype)] = cursor.execute('Any COUNT(X) WHERE X is %s' % etype)[0][0] |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
746 |
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
|
747 |
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
|
748 |
# 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
|
749 |
# 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
|
750 |
# |
8abf464d2ffe
fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4367
diff
changeset
|
751 |
# 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
|
752 |
# |
8abf464d2ffe
fix and begin to document autofill algorithm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4367
diff
changeset
|
753 |
# XXX we should first check there is no cycle then propagate changes |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
754 |
for (rschema, etype), targets in relmap.iteritems(): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
755 |
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
|
756 |
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
|
757 |
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
|
758 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
759 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
760 |
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
|
761 |
"""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
|
762 |
__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
|
763 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
764 |
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
|
765 |
# 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
|
766 |
# 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
|
767 |
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
|
768 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
769 |
no_auto_populate = () |
3734
b2107f68176c
ignored_relations should be a set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3720
diff
changeset
|
770 |
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
|
771 |
|
427
e894eec21a1b
move selection of entity types to test in a method to ease overriding
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
772 |
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
|
773 |
return unprotected_entities(self.schema, strict=True) |
1605 | 774 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
775 |
def custom_populate(self, how_many, cursor): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
776 |
pass |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
777 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
778 |
def post_populate(self, cursor): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
779 |
pass |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
780 |
|
3720 | 781 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
782 |
@nocoverage |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
783 |
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
|
784 |
"""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
|
785 |
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
|
786 |
""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
787 |
cu = self.cursor() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
788 |
self.custom_populate(how_many, cu) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
789 |
vreg = self.vreg |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
790 |
howmanydict = how_many_dict(self.schema, cu, how_many, self.no_auto_populate) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
791 |
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
|
792 |
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
|
793 |
continue |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
794 |
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
|
795 |
for rql, args in insert_entity_queries(etype, self.schema, vreg, nb): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
796 |
cu.execute(rql, args) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
797 |
edict = {} |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
798 |
for etype in unprotected_entities(self.schema, strict=True): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
799 |
rset = cu.execute('%s X' % etype) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
800 |
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
|
801 |
existingrels = {} |
4036
137be4878127
[mq]: fix_set_addition
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
4016
diff
changeset
|
802 |
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
|
803 |
for rschema in self.schema.relations(): |
3720 | 804 |
if rschema.final or rschema in ignored_relations: |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
805 |
continue |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
806 |
rset = cu.execute('DISTINCT Any X,Y WHERE X %s Y' % rschema) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
807 |
existingrels.setdefault(rschema.type, set()).update((x, y) for x, y in rset) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
808 |
q = make_relations_queries(self.schema, edict, cu, ignored_relations, |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
809 |
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
|
810 |
for rql, args in q: |
3998
94cc7cad3d2d
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
811 |
try: |
94cc7cad3d2d
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
812 |
cu.execute(rql, args) |
94cc7cad3d2d
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
813 |
except ValidationError, ex: |
94cc7cad3d2d
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
814 |
# failed to satisfy some constraint |
94cc7cad3d2d
backport stable into default
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
815 |
print 'error in automatic db population', ex |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
816 |
self.post_populate(cu) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
817 |
self.commit() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
818 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
819 |
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
|
820 |
etypes = etypes or self.to_test_etypes() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
821 |
for etype in etypes: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
822 |
if limit: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
823 |
rql = 'Any X LIMIT %s WHERE X is %s' % (limit, etype) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
824 |
else: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
825 |
rql = 'Any X WHERE X is %s' % etype |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
826 |
rset = self.execute(rql) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
827 |
for row in xrange(len(rset)): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
828 |
if limit and row > limit: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
829 |
break |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
830 |
# XXX iirk |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
831 |
rset2 = rset.limit(limit=1, offset=row) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
832 |
yield rset2 |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
833 |
|
1004
625e59773119
fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
563
diff
changeset
|
834 |
def iter_automatic_rsets(self, limit=10): |
0 | 835 |
"""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
|
836 |
etypes = self.to_test_etypes() |
2219
bb5098e74b82
protect against empty etypes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2070
diff
changeset
|
837 |
if not etypes: |
bb5098e74b82
protect against empty etypes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2070
diff
changeset
|
838 |
return |
0 | 839 |
for etype in etypes: |
1004
625e59773119
fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
563
diff
changeset
|
840 |
yield self.execute('Any X LIMIT %s WHERE X is %s' % (limit, etype)) |
0 | 841 |
etype1 = etypes.pop() |
1775
f450f1594992
fix in case only one type is tested
sylvain.thenault@logilab.fr
parents:
1773
diff
changeset
|
842 |
try: |
f450f1594992
fix in case only one type is tested
sylvain.thenault@logilab.fr
parents:
1773
diff
changeset
|
843 |
etype2 = etypes.pop() |
f450f1594992
fix in case only one type is tested
sylvain.thenault@logilab.fr
parents:
1773
diff
changeset
|
844 |
except KeyError: |
f450f1594992
fix in case only one type is tested
sylvain.thenault@logilab.fr
parents:
1773
diff
changeset
|
845 |
etype2 = etype1 |
0 | 846 |
# test a mixed query (DISTINCT/GROUP to avoid getting duplicate |
847 |
# X which make muledit view failing for instance (html validation fails |
|
848 |
# because of some duplicate "id" attributes) |
|
849 |
yield self.execute('DISTINCT Any X, MAX(Y) GROUPBY X WHERE X is %s, Y is %s' % (etype1, etype2)) |
|
850 |
# test some application-specific queries if defined |
|
851 |
for rql in self.application_rql: |
|
852 |
yield self.execute(rql) |
|
853 |
||
854 |
def _test_everything_for(self, rset): |
|
855 |
"""this method tries to find everything that can be tested |
|
856 |
for `rset` and yields a callable test (as needed in generative tests) |
|
857 |
""" |
|
858 |
propdefs = self.vreg['propertydefs'] |
|
859 |
# make all components visible |
|
860 |
for k, v in propdefs.items(): |
|
861 |
if k.endswith('visible') and not v['default']: |
|
862 |
propdefs[k]['default'] = True |
|
863 |
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
|
864 |
backup_rset = rset.copy(rset.rows, rset.description) |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3230
diff
changeset
|
865 |
yield InnerTest(self._testname(rset, view.__regid__, 'view'), |
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3230
diff
changeset
|
866 |
self.view, view.__regid__, rset, |
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
563
diff
changeset
|
867 |
rset.req.reset_headers(), 'main-template') |
0 | 868 |
# We have to do this because some views modify the |
869 |
# resultset's syntax tree |
|
870 |
rset = backup_rset |
|
871 |
for action in self.list_actions_for(rset): |
|
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3230
diff
changeset
|
872 |
yield InnerTest(self._testname(rset, action.__regid__, 'action'), self._test_action, action) |
0 | 873 |
for box in self.list_boxes_for(rset): |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3230
diff
changeset
|
874 |
yield InnerTest(self._testname(rset, box.__regid__, 'box'), box.render) |
0 | 875 |
|
876 |
@staticmethod |
|
877 |
def _testname(rset, objid, objtype): |
|
878 |
return '%s_%s_%s' % ('_'.join(rset.column_types(0)), objid, objtype) |
|
1605 | 879 |
|
0 | 880 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
881 |
# 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
|
882 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
883 |
class AutomaticWebTest(AutoPopulateTest): |
0 | 884 |
"""import this if you wan automatic tests to be ran""" |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
885 |
def setUp(self): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
886 |
AutoPopulateTest.setUp(self) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
887 |
# 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
|
888 |
# 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
|
889 |
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
|
890 |
|
0 | 891 |
## one each |
892 |
def test_one_each_config(self): |
|
893 |
self.auto_populate(1) |
|
1004
625e59773119
fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
563
diff
changeset
|
894 |
for rset in self.iter_automatic_rsets(limit=1): |
0 | 895 |
for testargs in self._test_everything_for(rset): |
896 |
yield testargs |
|
897 |
||
898 |
## ten each |
|
899 |
def test_ten_each_config(self): |
|
900 |
self.auto_populate(10) |
|
1004
625e59773119
fix automatic test: ensure we actually have
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
563
diff
changeset
|
901 |
for rset in self.iter_automatic_rsets(limit=10): |
0 | 902 |
for testargs in self._test_everything_for(rset): |
903 |
yield testargs |
|
1605 | 904 |
|
0 | 905 |
## startup views |
906 |
def test_startup_views(self): |
|
907 |
for vid in self.list_startup_views(): |
|
908 |
req = self.request() |
|
909 |
yield self.view, vid, None, req |
|
910 |
||
911 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
912 |
# registry instrumentization ################################################### |
0 | 913 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
914 |
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
|
915 |
try: |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
916 |
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
|
917 |
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
|
918 |
pass |
1605 | 919 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
920 |
|
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 |
def vreg_instrumentize(testclass): |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
922 |
# XXX broken |
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
923 |
from cubicweb.devtools.apptest import TestEnvironment |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
924 |
env = testclass._env = TestEnvironment('data', configcls=testclass.configcls) |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
925 |
for reg in env.vreg.values(): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
926 |
reg._selected = {} |
2668
979c7ccb4a86
[testlib] take care of re-monkeypatching which'll cause infinite recursion error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
927 |
try: |
979c7ccb4a86
[testlib] take care of re-monkeypatching which'll cause infinite recursion error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
928 |
orig_select_best = reg.__class__.__orig_select_best |
979c7ccb4a86
[testlib] take care of re-monkeypatching which'll cause infinite recursion error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
929 |
except: |
2770
356e9d7c356d
R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2668
diff
changeset
|
930 |
orig_select_best = reg.__class__._select_best |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
931 |
def instr_select_best(self, *args, **kwargs): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
932 |
selected = orig_select_best(self, *args, **kwargs) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
933 |
try: |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
934 |
self._selected[selected.__class__] += 1 |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
935 |
except KeyError: |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
936 |
self._selected[selected.__class__] = 1 |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
937 |
except AttributeError: |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
938 |
pass # occurs on reg used to restore database |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
939 |
return selected |
2770
356e9d7c356d
R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2668
diff
changeset
|
940 |
reg.__class__._select_best = instr_select_best |
2668
979c7ccb4a86
[testlib] take care of re-monkeypatching which'll cause infinite recursion error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2657
diff
changeset
|
941 |
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
|
942 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2668
diff
changeset
|
943 |
|
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
|
944 |
def print_untested_objects(testclass, skipregs=('hooks', 'etypes')): |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
945 |
for regname, reg in testclass._env.vreg.iteritems(): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2234
diff
changeset
|
946 |
if regname in skipregs: |
534
1368c80276bc
refactor validator selection using a content type based dictionnary (which may be overriden by view id) + functions to instrumentize the registry to check what's tested and what's not
sylvain.thenault@logilab.fr
parents:
514
diff
changeset
|
947 |
continue |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
948 |
for appobjects in reg.itervalues(): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
949 |
for appobject in appobjects: |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
950 |
if not reg._selected.get(appobject): |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
951 |
print 'not tested', regname, appobject |