author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 23 Mar 2010 18:35:04 +0100 | |
branch | stable |
changeset 4976 | 34fd21f65702 |
parent 4761 | e37932b89a6a |
child 4913 | 083b4d454192 |
permissions | -rw-r--r-- |
0 | 1 |
"""Test tools for cubicweb |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3901
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:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
import os |
|
11 |
import logging |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
937
diff
changeset
|
12 |
from datetime import timedelta |
0 | 13 |
from os.path import (abspath, join, exists, basename, dirname, normpath, split, |
14 |
isfile, isabs) |
|
15 |
||
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4436
diff
changeset
|
16 |
from logilab.common.date import strptime |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
17 |
from cubicweb import CW_SOFTWARE_ROOT, ConfigurationError, schema, cwconfig |
0 | 18 |
from cubicweb.server.serverconfig import ServerConfiguration |
19 |
from cubicweb.etwist.twconfig import TwistedConfiguration |
|
20 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
21 |
cwconfig.CubicWebConfiguration.cls_adjust_sys_path() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
22 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
23 |
# db auto-population configuration ############################################# |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
24 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
25 |
SYSTEM_ENTITIES = schema.SCHEMA_TYPES | set(( |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
26 |
'CWGroup', 'CWUser', 'CWProperty', |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2773
diff
changeset
|
27 |
'Workflow', 'State', 'BaseTransition', 'Transition', 'WorkflowTransition', |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2773
diff
changeset
|
28 |
'TrInfo', 'SubWorkflowExitPoint', |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
29 |
)) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
30 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
31 |
SYSTEM_RELATIONS = schema.META_RTYPES | set(( |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
32 |
# workflow related |
4761
e37932b89a6a
[testlib] automatic test shouldn't try to populate default_workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4520
diff
changeset
|
33 |
'workflow_of', 'state_of', 'transition_of', 'initial_state', 'default_workflow', |
e37932b89a6a
[testlib] automatic test shouldn't try to populate default_workflow
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4520
diff
changeset
|
34 |
'allowed_transition', 'destination_state', 'from_state', 'to_state', |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2773
diff
changeset
|
35 |
'condition', 'subworkflow', 'subworkflow_state', 'subworkflow_exit', |
4520
1d2c7b3adc39
don't play with the custom_workflow relation in autofill
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
36 |
'custom_workflow', 'in_state', 'wf_info_for', |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
37 |
# cwproperty |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
38 |
'for_user', |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
39 |
# schema definition |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
40 |
'specializes', |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
41 |
'relation_type', 'from_entity', 'to_entity', |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
42 |
'constrained_by', 'cstrtype', 'widget', |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
43 |
'read_permission', 'update_permission', 'delete_permission', 'add_permission', |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
44 |
# permission |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
45 |
'in_group', 'require_group', 'require_permission', |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
46 |
# deducted from other relations |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
47 |
'primary_email', |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
48 |
)) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
49 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
50 |
# content validation configuration ############################################# |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
51 |
|
0 | 52 |
# validators are used to validate (XML, DTD, whatever) view's content |
53 |
# validators availables are : |
|
54 |
# 'dtd' : validates XML + declared DTD |
|
55 |
# 'xml' : guarantees XML is well formed |
|
56 |
# None : do not try to validate anything |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
57 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
58 |
# {'vid': validator} |
0 | 59 |
VIEW_VALIDATORS = {} |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
60 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
61 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
62 |
# cubicweb test configuration ################################################## |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
63 |
|
0 | 64 |
BASE_URL = 'http://testing.fr/cubicweb/' |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
65 |
|
0 | 66 |
DEFAULT_SOURCES = {'system': {'adapter' : 'native', |
67 |
'db-encoding' : 'UTF-8', #'ISO-8859-1', |
|
68 |
'db-user' : u'admin', |
|
69 |
'db-password' : 'gingkow', |
|
70 |
'db-name' : 'tmpdb', |
|
71 |
'db-driver' : 'sqlite', |
|
72 |
'db-host' : None, |
|
73 |
}, |
|
74 |
'admin' : {'login': u'admin', |
|
75 |
'password': u'gingkow', |
|
76 |
}, |
|
77 |
} |
|
78 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
79 |
|
0 | 80 |
class TestServerConfiguration(ServerConfiguration): |
81 |
mode = 'test' |
|
82 |
set_language = False |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1978
diff
changeset
|
83 |
read_instance_schema = False |
0 | 84 |
bootstrap_schema = False |
85 |
init_repository = True |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
86 |
options = cwconfig.merge_options(ServerConfiguration.options + ( |
0 | 87 |
('anonymous-user', |
88 |
{'type' : 'string', |
|
89 |
'default': None, |
|
90 |
'help': 'login of the CubicWeb user account to use for anonymous user (if you want to allow anonymous)', |
|
91 |
'group': 'main', 'inputlevel': 1, |
|
92 |
}), |
|
93 |
('anonymous-password', |
|
94 |
{'type' : 'string', |
|
95 |
'default': None, |
|
96 |
'help': 'password of the CubicWeb user account matching login', |
|
97 |
'group': 'main', 'inputlevel': 1, |
|
98 |
}), |
|
99 |
)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
100 |
|
0 | 101 |
if not os.environ.get('APYCOT_ROOT'): |
102 |
REGISTRY_DIR = normpath(join(CW_SOFTWARE_ROOT, '../cubes')) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
103 |
|
0 | 104 |
def __init__(self, appid, log_threshold=logging.CRITICAL+10): |
105 |
ServerConfiguration.__init__(self, appid) |
|
106 |
self.init_log(log_threshold, force=True) |
|
107 |
# need this, usually triggered by cubicweb-ctl |
|
108 |
self.load_cwctl_plugins() |
|
3435
84036ad2c82d
add anon user for TestServerConfiguration, no only AppTestConfiguration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
109 |
self.global_set_option('anonymous-user', 'anon') |
84036ad2c82d
add anon user for TestServerConfiguration, no only AppTestConfiguration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
110 |
self.global_set_option('anonymous-password', 'anon') |
0 | 111 |
|
112 |
anonymous_user = TwistedConfiguration.anonymous_user.im_func |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
113 |
|
0 | 114 |
@property |
115 |
def apphome(self): |
|
116 |
if exists(self.appid): |
|
117 |
return abspath(self.appid) |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1978
diff
changeset
|
118 |
# cube test |
0 | 119 |
return abspath('..') |
120 |
appdatahome = apphome |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
121 |
|
3435
84036ad2c82d
add anon user for TestServerConfiguration, no only AppTestConfiguration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
122 |
def load_configuration(self): |
84036ad2c82d
add anon user for TestServerConfiguration, no only AppTestConfiguration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
123 |
super(TestServerConfiguration, self).load_configuration() |
84036ad2c82d
add anon user for TestServerConfiguration, no only AppTestConfiguration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
124 |
self.global_set_option('anonymous-user', 'anon') |
84036ad2c82d
add anon user for TestServerConfiguration, no only AppTestConfiguration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
125 |
self.global_set_option('anonymous-password', 'anon') |
84036ad2c82d
add anon user for TestServerConfiguration, no only AppTestConfiguration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
126 |
|
0 | 127 |
def main_config_file(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1978
diff
changeset
|
128 |
"""return instance's control configuration file""" |
0 | 129 |
return join(self.apphome, '%s.conf' % self.name) |
130 |
||
131 |
def instance_md5_version(self): |
|
132 |
return '' |
|
133 |
||
134 |
def bootstrap_cubes(self): |
|
135 |
try: |
|
136 |
super(TestServerConfiguration, self).bootstrap_cubes() |
|
137 |
except IOError: |
|
138 |
# no cubes |
|
139 |
self.init_cubes( () ) |
|
140 |
||
141 |
sourcefile = None |
|
142 |
def sources_file(self): |
|
143 |
"""define in subclasses self.sourcefile if necessary""" |
|
144 |
if self.sourcefile: |
|
145 |
print 'Reading sources from', self.sourcefile |
|
146 |
sourcefile = self.sourcefile |
|
147 |
if not isabs(sourcefile): |
|
148 |
sourcefile = join(self.apphome, sourcefile) |
|
149 |
else: |
|
150 |
sourcefile = super(TestServerConfiguration, self).sources_file() |
|
151 |
return sourcefile |
|
152 |
||
153 |
def sources(self): |
|
154 |
"""By default, we run tests with the sqlite DB backend. One may use its |
|
155 |
own configuration by just creating a 'sources' file in the test |
|
156 |
directory from wich tests are launched or by specifying an alternative |
|
157 |
sources file using self.sourcefile. |
|
158 |
""" |
|
159 |
sources = super(TestServerConfiguration, self).sources() |
|
160 |
if not sources: |
|
161 |
sources = DEFAULT_SOURCES |
|
162 |
return sources |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
163 |
|
0 | 164 |
|
165 |
class BaseApptestConfiguration(TestServerConfiguration, TwistedConfiguration): |
|
166 |
repo_method = 'inmemory' |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
167 |
options = cwconfig.merge_options(TestServerConfiguration.options |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
168 |
+ TwistedConfiguration.options) |
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
169 |
cubicweb_appobject_path = TestServerConfiguration.cubicweb_appobject_path | TwistedConfiguration.cubicweb_appobject_path |
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
170 |
cube_appobject_path = TestServerConfiguration.cube_appobject_path | TwistedConfiguration.cube_appobject_path |
0 | 171 |
|
172 |
def available_languages(self, *args): |
|
173 |
return ('en', 'fr', 'de') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
174 |
|
0 | 175 |
def ext_resources_file(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1978
diff
changeset
|
176 |
"""return instance's external resources file""" |
0 | 177 |
return join(self.apphome, 'data', 'external_resources') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
178 |
|
0 | 179 |
def pyro_enabled(self): |
180 |
# but export PYRO_MULTITHREAD=0 or you get problems with sqlite and threads |
|
181 |
return True |
|
182 |
||
183 |
||
184 |
class ApptestConfiguration(BaseApptestConfiguration): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
185 |
|
0 | 186 |
def __init__(self, appid, log_threshold=logging.CRITICAL, sourcefile=None): |
187 |
BaseApptestConfiguration.__init__(self, appid, log_threshold=log_threshold) |
|
188 |
self.init_repository = sourcefile is None |
|
189 |
self.sourcefile = sourcefile |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
190 |
|
0 | 191 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
192 |
# test database handling ####################################################### |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
193 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
194 |
def init_test_database(config=None, configdir='data'): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
195 |
"""init a test database for a specific driver""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
196 |
from cubicweb.dbapi import in_memory_cnx |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
197 |
config = config or TestServerConfiguration(configdir) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
198 |
sources = config.sources() |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
199 |
driver = sources['system']['db-driver'] |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
200 |
if driver == 'sqlite': |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
201 |
init_test_database_sqlite(config) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
202 |
elif driver == 'postgres': |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
203 |
init_test_database_postgres(config) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
204 |
else: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
205 |
raise ValueError('no initialization function for driver %r' % driver) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
206 |
config._cubes = None # avoid assertion error |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
207 |
repo, cnx = in_memory_cnx(config, unicode(sources['admin']['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:
3435
diff
changeset
|
208 |
password=sources['admin']['password'] or 'xxx') |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
209 |
if driver == 'sqlite': |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
210 |
install_sqlite_patch(repo.querier) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
211 |
return repo, cnx |
0 | 212 |
|
213 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
214 |
def reset_test_database(config): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
215 |
"""init a test database for a specific driver""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
216 |
driver = config.sources()['system']['db-driver'] |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
217 |
if driver == 'sqlite': |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
218 |
reset_test_database_sqlite(config) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
219 |
else: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
220 |
raise ValueError('no reset function for driver %r' % driver) |
0 | 221 |
|
222 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
223 |
### postgres test database handling ############################################ |
0 | 224 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
225 |
def init_test_database_postgres(config): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
226 |
"""initialize a fresh sqlite databse used for testing purpose""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
227 |
if config.init_repository: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
228 |
from cubicweb.server import init_repository |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
229 |
init_repository(config, interactive=False, drop=True) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
230 |
|
0 | 231 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
232 |
### sqlite test database handling ############################################## |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
233 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
234 |
def cleanup_sqlite(dbfile, removetemplate=False): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
235 |
try: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
236 |
os.remove(dbfile) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
237 |
os.remove('%s-journal' % dbfile) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
238 |
except OSError: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
239 |
pass |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
240 |
if removetemplate: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
241 |
try: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
242 |
os.remove('%s-template' % dbfile) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
243 |
except OSError: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
244 |
pass |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
245 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
246 |
def reset_test_database_sqlite(config): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
247 |
import shutil |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
248 |
dbfile = config.sources()['system']['db-name'] |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
249 |
cleanup_sqlite(dbfile) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
250 |
template = '%s-template' % dbfile |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
251 |
if exists(template): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
252 |
shutil.copy(template, dbfile) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
253 |
return True |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
254 |
return False |
0 | 255 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
256 |
def init_test_database_sqlite(config): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
257 |
"""initialize a fresh sqlite databse used for testing purpose""" |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
258 |
# remove database file if it exists |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
259 |
dbfile = config.sources()['system']['db-name'] |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
260 |
if not reset_test_database_sqlite(config): |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
261 |
# initialize the database |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
262 |
import shutil |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
263 |
from cubicweb.server import init_repository |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
264 |
init_repository(config, interactive=False) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
265 |
dbfile = config.sources()['system']['db-name'] |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
266 |
shutil.copy(dbfile, '%s-template' % dbfile) |
0 | 267 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
268 |
|
2732 | 269 |
def install_sqlite_patch(querier): |
0 | 270 |
"""This patch hotfixes the following sqlite bug : |
298
3e6d32667140
[doc] Fix docstring indentation.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
0
diff
changeset
|
271 |
- http://www.sqlite.org/cvstrac/tktview?tn=1327,33 |
3e6d32667140
[doc] Fix docstring indentation.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
0
diff
changeset
|
272 |
(some dates are returned as strings rather thant date objects) |
0 | 273 |
""" |
937 | 274 |
if hasattr(querier.__class__, '_devtools_sqlite_patched'): |
275 |
return # already monkey patched |
|
0 | 276 |
def wrap_execute(base_execute): |
277 |
def new_execute(*args, **kwargs): |
|
278 |
rset = base_execute(*args, **kwargs) |
|
279 |
if rset.description: |
|
280 |
found_date = False |
|
281 |
for row, rowdesc in zip(rset, rset.description): |
|
282 |
for cellindex, (value, vtype) in enumerate(zip(row, rowdesc)): |
|
283 |
if vtype in ('Date', 'Datetime') and type(value) is unicode: |
|
284 |
found_date = True |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
937
diff
changeset
|
285 |
value = value.rsplit('.', 1)[0] |
0 | 286 |
try: |
287 |
row[cellindex] = strptime(value, '%Y-%m-%d %H:%M:%S') |
|
288 |
except: |
|
289 |
row[cellindex] = strptime(value, '%Y-%m-%d') |
|
290 |
if vtype == 'Time' and type(value) is unicode: |
|
291 |
found_date = True |
|
292 |
try: |
|
293 |
row[cellindex] = strptime(value, '%H:%M:%S') |
|
294 |
except: |
|
295 |
# DateTime used as Time? |
|
296 |
row[cellindex] = strptime(value, '%Y-%m-%d %H:%M:%S') |
|
297 |
if vtype == 'Interval' and type(value) is int: |
|
298 |
found_date = True |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
937
diff
changeset
|
299 |
row[cellindex] = timedelta(0, value, 0) # XXX value is in number of seconds? |
0 | 300 |
if not found_date: |
301 |
break |
|
302 |
return rset |
|
303 |
return new_execute |
|
304 |
querier.__class__.execute = wrap_execute(querier.__class__.execute) |
|
937 | 305 |
querier.__class__._devtools_sqlite_patched = True |