author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 15 Sep 2010 11:13:17 +0200 | |
branch | stable |
changeset 6245 | e7e9d73d0c07 |
parent 6164 | ceb6951f9d2f |
child 6281 | a013a68fe8ec |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5267
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""Test tools for cubicweb |
19 |
||
20 |
""" |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
23 |
import os |
|
5754
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
24 |
import sys |
0 | 25 |
import logging |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
937
diff
changeset
|
26 |
from datetime import timedelta |
0 | 27 |
from os.path import (abspath, join, exists, basename, dirname, normpath, split, |
28 |
isfile, isabs) |
|
29 |
||
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4436
diff
changeset
|
30 |
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
|
31 |
from cubicweb import CW_SOFTWARE_ROOT, ConfigurationError, schema, cwconfig |
0 | 32 |
from cubicweb.server.serverconfig import ServerConfiguration |
33 |
from cubicweb.etwist.twconfig import TwistedConfiguration |
|
34 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
35 |
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
|
36 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
37 |
# 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
|
38 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
39 |
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
|
40 |
'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
|
41 |
'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
|
42 |
'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
|
43 |
)) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
44 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
45 |
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
|
46 |
# 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
|
47 |
'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
|
48 |
'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
|
49 |
'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
|
50 |
'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
|
51 |
# cwproperty |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
52 |
'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
|
53 |
# 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
|
54 |
'specializes', |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
55 |
'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
|
56 |
'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
|
57 |
'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
|
58 |
# permission |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
59 |
'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
|
60 |
# 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
|
61 |
'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
|
62 |
)) |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
63 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
64 |
# 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
|
65 |
|
0 | 66 |
# validators are used to validate (XML, DTD, whatever) view's content |
67 |
# validators availables are : |
|
68 |
# 'dtd' : validates XML + declared DTD |
|
69 |
# 'xml' : guarantees XML is well formed |
|
70 |
# 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
|
71 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
72 |
# {'vid': validator} |
0 | 73 |
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
|
74 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
75 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
76 |
# 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
|
77 |
|
0 | 78 |
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
|
79 |
|
0 | 80 |
DEFAULT_SOURCES = {'system': {'adapter' : 'native', |
81 |
'db-encoding' : 'UTF-8', #'ISO-8859-1', |
|
82 |
'db-user' : u'admin', |
|
83 |
'db-password' : 'gingkow', |
|
84 |
'db-name' : 'tmpdb', |
|
85 |
'db-driver' : 'sqlite', |
|
86 |
'db-host' : None, |
|
87 |
}, |
|
88 |
'admin' : {'login': u'admin', |
|
89 |
'password': u'gingkow', |
|
90 |
}, |
|
91 |
} |
|
92 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
93 |
|
0 | 94 |
class TestServerConfiguration(ServerConfiguration): |
95 |
mode = 'test' |
|
96 |
set_language = False |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1978
diff
changeset
|
97 |
read_instance_schema = False |
0 | 98 |
init_repository = True |
6164
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
99 |
db_require_setup = 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
|
100 |
options = cwconfig.merge_options(ServerConfiguration.options + ( |
0 | 101 |
('anonymous-user', |
102 |
{'type' : 'string', |
|
103 |
'default': None, |
|
104 |
'help': 'login of the CubicWeb user account to use for anonymous user (if you want to allow anonymous)', |
|
5323
329b4f6d18b4
[config] with lgc >= 0.50, option's dict inputlevel becomes level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5274
diff
changeset
|
105 |
'group': 'main', 'level': 1, |
0 | 106 |
}), |
107 |
('anonymous-password', |
|
108 |
{'type' : 'string', |
|
109 |
'default': None, |
|
110 |
'help': 'password of the CubicWeb user account matching login', |
|
5323
329b4f6d18b4
[config] with lgc >= 0.50, option's dict inputlevel becomes level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5274
diff
changeset
|
111 |
'group': 'main', 'level': 1, |
0 | 112 |
}), |
113 |
)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
114 |
|
0 | 115 |
def __init__(self, appid, log_threshold=logging.CRITICAL+10): |
116 |
ServerConfiguration.__init__(self, appid) |
|
117 |
self.init_log(log_threshold, force=True) |
|
118 |
# need this, usually triggered by cubicweb-ctl |
|
119 |
self.load_cwctl_plugins() |
|
120 |
||
121 |
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
|
122 |
|
0 | 123 |
@property |
124 |
def apphome(self): |
|
125 |
if exists(self.appid): |
|
126 |
return abspath(self.appid) |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1978
diff
changeset
|
127 |
# cube test |
0 | 128 |
return abspath('..') |
129 |
appdatahome = apphome |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
130 |
|
3435
84036ad2c82d
add anon user for TestServerConfiguration, no only AppTestConfiguration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
131 |
def load_configuration(self): |
84036ad2c82d
add anon user for TestServerConfiguration, no only AppTestConfiguration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
132 |
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
|
133 |
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
|
134 |
self.global_set_option('anonymous-password', 'anon') |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4761
diff
changeset
|
135 |
# no undo support in tests |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4761
diff
changeset
|
136 |
self.global_set_option('undo-support', '') |
3435
84036ad2c82d
add anon user for TestServerConfiguration, no only AppTestConfiguration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
137 |
|
0 | 138 |
def main_config_file(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1978
diff
changeset
|
139 |
"""return instance's control configuration file""" |
0 | 140 |
return join(self.apphome, '%s.conf' % self.name) |
141 |
||
142 |
def instance_md5_version(self): |
|
143 |
return '' |
|
144 |
||
145 |
def bootstrap_cubes(self): |
|
146 |
try: |
|
147 |
super(TestServerConfiguration, self).bootstrap_cubes() |
|
148 |
except IOError: |
|
149 |
# no cubes |
|
150 |
self.init_cubes( () ) |
|
151 |
||
152 |
sourcefile = None |
|
153 |
def sources_file(self): |
|
154 |
"""define in subclasses self.sourcefile if necessary""" |
|
155 |
if self.sourcefile: |
|
156 |
print 'Reading sources from', self.sourcefile |
|
157 |
sourcefile = self.sourcefile |
|
158 |
if not isabs(sourcefile): |
|
159 |
sourcefile = join(self.apphome, sourcefile) |
|
160 |
else: |
|
161 |
sourcefile = super(TestServerConfiguration, self).sources_file() |
|
162 |
return sourcefile |
|
163 |
||
164 |
def sources(self): |
|
165 |
"""By default, we run tests with the sqlite DB backend. One may use its |
|
166 |
own configuration by just creating a 'sources' file in the test |
|
167 |
directory from wich tests are launched or by specifying an alternative |
|
168 |
sources file using self.sourcefile. |
|
169 |
""" |
|
170 |
sources = super(TestServerConfiguration, self).sources() |
|
171 |
if not sources: |
|
172 |
sources = DEFAULT_SOURCES |
|
173 |
return sources |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
174 |
|
0 | 175 |
|
176 |
class BaseApptestConfiguration(TestServerConfiguration, TwistedConfiguration): |
|
177 |
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
|
178 |
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
|
179 |
+ 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
|
180 |
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
|
181 |
cube_appobject_path = TestServerConfiguration.cube_appobject_path | TwistedConfiguration.cube_appobject_path |
0 | 182 |
|
183 |
def available_languages(self, *args): |
|
184 |
return ('en', 'fr', 'de') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
185 |
|
0 | 186 |
def pyro_enabled(self): |
5754
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
187 |
# but export PYRO_MULTITHREAD=0 or you get problems with sqlite and |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
188 |
# threads |
0 | 189 |
return True |
190 |
||
191 |
||
192 |
class ApptestConfiguration(BaseApptestConfiguration): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
193 |
|
0 | 194 |
def __init__(self, appid, log_threshold=logging.CRITICAL, sourcefile=None): |
195 |
BaseApptestConfiguration.__init__(self, appid, log_threshold=log_threshold) |
|
196 |
self.init_repository = sourcefile is None |
|
197 |
self.sourcefile = sourcefile |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
198 |
|
6164
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
199 |
class RealDatabaseConfiguration(ApptestConfiguration): |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
200 |
"""configuration class for tests to run on a real database. |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
201 |
|
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
202 |
The intialization is done by specifying a source file path. |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
203 |
|
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
204 |
Important note: init_test_database / reset_test_database steps are |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
205 |
skipped. It's thus up to the test developer to implement setUp/tearDown |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
206 |
accordingly. |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
207 |
|
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
208 |
Example usage:: |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
209 |
|
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
210 |
class MyTests(CubicWebTC): |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
211 |
_config = RealDatabseConfiguration('myapp', |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
212 |
sourcefile='/path/to/sources') |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
213 |
def test_something(self): |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
214 |
rset = self.execute('Any X WHERE X is CWUser') |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
215 |
self.view('foaf', rset) |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
216 |
|
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
217 |
""" |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
218 |
db_require_setup = False # skip init_db / reset_db steps |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
219 |
read_instance_schema = True # read schema from database |
0 | 220 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
221 |
# test database handling ####################################################### |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
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 |
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
|
224 |
"""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
|
225 |
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
|
226 |
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
|
227 |
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
|
228 |
driver = sources['system']['db-driver'] |
6164
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
229 |
if config.db_require_setup: |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
230 |
if driver == 'sqlite': |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
231 |
init_test_database_sqlite(config) |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
232 |
elif driver == 'postgres': |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
233 |
init_test_database_postgres(config) |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
234 |
else: |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
235 |
raise ValueError('no initialization function for driver %r' % driver) |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
236 |
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
|
237 |
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
|
238 |
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
|
239 |
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
|
240 |
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
|
241 |
return repo, cnx |
0 | 242 |
|
243 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
244 |
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
|
245 |
"""init a test database for a specific driver""" |
6164
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
246 |
if not config.db_require_setup: |
ceb6951f9d2f
[devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5754
diff
changeset
|
247 |
return |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
248 |
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
|
249 |
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
|
250 |
reset_test_database_sqlite(config) |
5754
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
251 |
elif driver == 'postgres': |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
252 |
init_test_database_postgres(config) |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
253 |
else: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
254 |
raise ValueError('no reset function for driver %r' % driver) |
0 | 255 |
|
256 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
257 |
### postgres test database handling ############################################ |
0 | 258 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
259 |
def init_test_database_postgres(config): |
5166
1e96d01728c4
enable live test against sqlserver 2005 database
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5043
diff
changeset
|
260 |
"""initialize a fresh postgresql databse used for testing purpose""" |
5754
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
261 |
from logilab.database import get_db_helper |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
262 |
from cubicweb.server import init_repository |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
263 |
from cubicweb.server.serverctl import (createdb, system_source_cnx, |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
264 |
_db_sys_cnx) |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
265 |
source = config.sources()['system'] |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
266 |
dbname = source['db-name'] |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
267 |
templdbname = dbname + '_template' |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
268 |
helper = get_db_helper('postgres') |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
269 |
# connect on the dbms system base to create our base |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
270 |
dbcnx = _db_sys_cnx(source, 'CREATE DATABASE and / or USER', verbose=0) |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
271 |
cursor = dbcnx.cursor() |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
272 |
try: |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
273 |
if dbname in helper.list_databases(cursor): |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
274 |
cursor.execute('DROP DATABASE %s' % dbname) |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
275 |
if not templdbname in helper.list_databases(cursor): |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
276 |
source['db-name'] = templdbname |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
277 |
createdb(helper, source, dbcnx, cursor) |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
278 |
dbcnx.commit() |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
279 |
cnx = system_source_cnx(source, special_privs='LANGUAGE C', verbose=0) |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
280 |
templcursor = cnx.cursor() |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
281 |
# XXX factorize with db-create code |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
282 |
helper.init_fti_extensions(templcursor) |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
283 |
# install plpythonu/plpgsql language if not installed by the cube |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
284 |
langs = sys.platform == 'win32' and ('plpgsql',) or ('plpythonu', 'plpgsql') |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
285 |
for extlang in langs: |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
286 |
helper.create_language(templcursor, extlang) |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
287 |
cnx.commit() |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
288 |
templcursor.close() |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
289 |
cnx.close() |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
290 |
init_repository(config, interactive=False) |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
291 |
source['db-name'] = dbname |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
292 |
except: |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
293 |
dbcnx.rollback() |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
294 |
# XXX drop template |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
295 |
raise |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
296 |
createdb(helper, source, dbcnx, cursor, template=templdbname) |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
297 |
dbcnx.commit() |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
298 |
dbcnx.close() |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
299 |
|
5754
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
300 |
### sqlserver2005 test database handling ####################################### |
5166
1e96d01728c4
enable live test against sqlserver 2005 database
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5043
diff
changeset
|
301 |
|
1e96d01728c4
enable live test against sqlserver 2005 database
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5043
diff
changeset
|
302 |
def init_test_database_sqlserver2005(config): |
1e96d01728c4
enable live test against sqlserver 2005 database
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5043
diff
changeset
|
303 |
"""initialize a fresh sqlserver databse used for testing purpose""" |
5169
240e21b94a28
fixed indentation error in devtools/__init__.py
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5166
diff
changeset
|
304 |
if config.init_repository: |
240e21b94a28
fixed indentation error in devtools/__init__.py
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5166
diff
changeset
|
305 |
from cubicweb.server import init_repository |
5693
8af6623f3d4e
[pylint] fix detected name errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5541
diff
changeset
|
306 |
init_repository(config, interactive=False, drop=True) |
5212
a545eb9add6f
[testlib] fix dummy bug in reset_test_database(): s/if/elif
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5210
diff
changeset
|
307 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
308 |
### 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
|
309 |
|
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
310 |
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
|
311 |
try: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
312 |
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
|
313 |
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
|
314 |
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
|
315 |
pass |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
316 |
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
|
317 |
try: |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
318 |
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
|
319 |
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
|
320 |
pass |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
321 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
322 |
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
|
323 |
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
|
324 |
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
|
325 |
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
|
326 |
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
|
327 |
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
|
328 |
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
|
329 |
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
|
330 |
return False |
0 | 331 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2732
diff
changeset
|
332 |
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
|
333 |
"""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
|
334 |
# 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
|
335 |
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
|
336 |
# 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
|
337 |
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
|
338 |
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
|
339 |
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
|
340 |
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
|
341 |
shutil.copy(dbfile, '%s-template' % dbfile) |
0 | 342 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1016
diff
changeset
|
343 |
|
2732 | 344 |
def install_sqlite_patch(querier): |
0 | 345 |
"""This patch hotfixes the following sqlite bug : |
298
3e6d32667140
[doc] Fix docstring indentation.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
0
diff
changeset
|
346 |
- http://www.sqlite.org/cvstrac/tktview?tn=1327,33 |
3e6d32667140
[doc] Fix docstring indentation.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
0
diff
changeset
|
347 |
(some dates are returned as strings rather thant date objects) |
0 | 348 |
""" |
937 | 349 |
if hasattr(querier.__class__, '_devtools_sqlite_patched'): |
350 |
return # already monkey patched |
|
0 | 351 |
def wrap_execute(base_execute): |
352 |
def new_execute(*args, **kwargs): |
|
353 |
rset = base_execute(*args, **kwargs) |
|
354 |
if rset.description: |
|
355 |
found_date = False |
|
356 |
for row, rowdesc in zip(rset, rset.description): |
|
357 |
for cellindex, (value, vtype) in enumerate(zip(row, rowdesc)): |
|
358 |
if vtype in ('Date', 'Datetime') and type(value) is unicode: |
|
359 |
found_date = True |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
937
diff
changeset
|
360 |
value = value.rsplit('.', 1)[0] |
0 | 361 |
try: |
362 |
row[cellindex] = strptime(value, '%Y-%m-%d %H:%M:%S') |
|
363 |
except: |
|
364 |
row[cellindex] = strptime(value, '%Y-%m-%d') |
|
365 |
if vtype == 'Time' and type(value) is unicode: |
|
366 |
found_date = True |
|
367 |
try: |
|
368 |
row[cellindex] = strptime(value, '%H:%M:%S') |
|
369 |
except: |
|
370 |
# DateTime used as Time? |
|
371 |
row[cellindex] = strptime(value, '%Y-%m-%d %H:%M:%S') |
|
372 |
if vtype == 'Interval' and type(value) is int: |
|
373 |
found_date = True |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
937
diff
changeset
|
374 |
row[cellindex] = timedelta(0, value, 0) # XXX value is in number of seconds? |
0 | 375 |
if not found_date: |
376 |
break |
|
377 |
return rset |
|
378 |
return new_execute |
|
379 |
querier.__class__.execute = wrap_execute(querier.__class__.execute) |
|
937 | 380 |
querier.__class__._devtools_sqlite_patched = True |