devtools/__init__.py
author Julien Cristau <julien.cristau@logilab.fr>
Mon, 13 Jan 2014 13:47:47 +0100
changeset 9402 2c48c091b6a2
parent 9117 e25c5abc667c
parent 9340 b1e933b0e850
child 9456 a79e88aad555
permissions -rw-r--r--
merge 3.18.0 in 3.19 branch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8544
3d049071957e massive copyright update to avoid clutering later patches
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8543
diff changeset
     1
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 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/>.
6315
8ca3ee849bee [test] fix broken tests and minor cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6281
diff changeset
    18
"""Test tools for cubicweb"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
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
    23
import sys
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
import logging
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
    25
import shutil
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
    26
import pickle
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
    27
import glob
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
    28
import warnings
9337
70f6b183085a [devtools] write db config through a temporary file
Julien Cristau <julien.cristau@logilab.fr>
parents: 9235
diff changeset
    29
import tempfile
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
    30
from hashlib import sha1 # pylint: disable=E0611
1016
26387b836099 use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents: 937
diff changeset
    31
from datetime import timedelta
8930
6a02be304486 remove unused import
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    32
from os.path import (abspath, join, exists, split, isabs, isdir)
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
    33
from functools import partial
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
4466
8b0ca7904820 moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
    35
from logilab.common.date import strptime
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
    36
from logilab.common.decorators import cached, clear_cache
7301
93e96700e0c0 [configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7240
diff changeset
    37
8930
6a02be304486 remove unused import
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    38
from cubicweb import ExecutionError, BadConnectionId
6a02be304486 remove unused import
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8694
diff changeset
    39
from cubicweb import schema, cwconfig
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
from cubicweb.server.serverconfig import ServerConfiguration
9252
01bca75ee8bd [devtools,etwist] rename TwistedConfiguration to WebConfigurationBase (follows #2919310)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9235
diff changeset
    41
from cubicweb.etwist.twconfig import WebConfigurationBase
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
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
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
    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
# 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
    46
6377
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6315
diff changeset
    47
SYSTEM_ENTITIES = (schema.SCHEMA_TYPES
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6315
diff changeset
    48
                   | schema.INTERNAL_TYPES
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6315
diff changeset
    49
                   | schema.WORKFLOW_TYPES
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6315
diff changeset
    50
                   | set(('CWGroup', 'CWUser',))
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6315
diff changeset
    51
                   )
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6315
diff changeset
    52
SYSTEM_RELATIONS = (schema.META_RTYPES
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6315
diff changeset
    53
                    | schema.WORKFLOW_RTYPES
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6315
diff changeset
    54
                    | schema.WORKFLOW_DEF_RTYPES
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6315
diff changeset
    55
                    | schema.SYSTEM_RTYPES
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6315
diff changeset
    56
                    | schema.SCHEMA_TYPES
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6315
diff changeset
    57
                    | set(('primary_email', # deducted from other relations
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6315
diff changeset
    58
                           ))
3bb415310d4f [schema] introduce some new sets categorizing entity/relation types and benefits from them where possible
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6315
diff changeset
    59
                    )
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
# 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
    62
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
# validators are used to validate (XML, DTD, whatever) view's content
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
# validators availables are :
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
#  'dtd' : validates XML + declared DTD
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
#  'xml' : guarantees XML is well formed
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
#  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
    68
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2732
diff changeset
    69
# {'vid': validator}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
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
    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
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2732
diff changeset
    73
# 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
    74
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
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
    76
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
DEFAULT_SOURCES = {'system': {'adapter' : 'native',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
                              'db-encoding' : 'UTF-8', #'ISO-8859-1',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
                              'db-user' : u'admin',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
                              'db-password' : 'gingkow',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
                              'db-name' : 'tmpdb',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
                              'db-driver' : 'sqlite',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
                              'db-host' : None,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
                              },
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
                   'admin' : {'login': u'admin',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
                              'password': u'gingkow',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
                              },
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
                   }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
    90
def turn_repo_off(repo):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
    91
    """ Idea: this is less costly than a full re-creation of the repo object.
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
    92
    off:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
    93
    * session are closed,
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7380
diff changeset
    94
    * cnxsets are closed
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
    95
    * system source is shutdown
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
    96
    """
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
    97
    if not repo._needs_refresh:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
    98
        for sessionid in list(repo._sessions):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
    99
            warnings.warn('%s Open session found while turning repository off'
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   100
                          %sessionid, RuntimeWarning)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   101
            try:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   102
                repo.close(sessionid)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   103
            except BadConnectionId: #this is strange ? thread issue ?
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   104
                print 'XXX unknown session', sessionid
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7380
diff changeset
   105
        for cnxset in repo.cnxsets:
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7380
diff changeset
   106
            cnxset.close(True)
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   107
        repo.system_source.shutdown()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   108
        repo._needs_refresh = True
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   109
        repo._has_started = False
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   110
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   111
def turn_repo_on(repo):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   112
    """Idea: this is less costly than a full re-creation of the repo object.
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   113
    on:
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7380
diff changeset
   114
    * cnxsets are connected
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   115
    * cache are cleared
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   116
    """
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   117
    if repo._needs_refresh:
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7380
diff changeset
   118
        for cnxset in repo.cnxsets:
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7380
diff changeset
   119
            cnxset.reconnect()
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   120
        repo._type_source_cache = {}
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   121
        repo._extid_cache = {}
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   122
        repo.querier._rql_cache = {}
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   123
        for source in repo.sources:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   124
            source.reset_caches()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   125
        repo._needs_refresh = False
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   126
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2732
diff changeset
   127
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
class TestServerConfiguration(ServerConfiguration):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
    mode = 'test'
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1978
diff changeset
   130
    read_instance_schema = False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
    init_repository = True
7239
078cfa8a5187 fix server/test/unittest_checkintegrity (failing since 70538ea2532d)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7220
diff changeset
   132
    skip_db_create_and_restore = False
7065
6a6ea9966931 [testlib] simplify code by using a class attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7056
diff changeset
   133
    def __init__(self, appid='data', apphome=None, log_threshold=logging.CRITICAL+10):
6585
9af22f2c0c4c [test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6441
diff changeset
   134
        # must be set before calling parent __init__
9af22f2c0c4c [test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6441
diff changeset
   135
        if apphome is None:
6593
0fd8792c9c8a [testlib] self.appid not yet defined
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6585
diff changeset
   136
            if exists(appid):
0fd8792c9c8a [testlib] self.appid not yet defined
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6585
diff changeset
   137
                apphome = abspath(appid)
6585
9af22f2c0c4c [test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6441
diff changeset
   138
            else: # cube test
9af22f2c0c4c [test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6441
diff changeset
   139
                apphome = abspath('..')
9af22f2c0c4c [test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6441
diff changeset
   140
        self._apphome = apphome
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
        ServerConfiguration.__init__(self, appid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
        self.init_log(log_threshold, force=True)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
        # need this, usually triggered by cubicweb-ctl
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
        self.load_cwctl_plugins()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
7071
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7065
diff changeset
   146
    # By default anonymous login are allow but some test need to deny of to
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7065
diff changeset
   147
    # change the default user. Set it to None to prevent anonymous login.
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7065
diff changeset
   148
    anonymous_credential = ('anon', 'anon')
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7065
diff changeset
   149
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7065
diff changeset
   150
    def anonymous_user(self):
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7065
diff changeset
   151
        if not self.anonymous_credential:
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7065
diff changeset
   152
            return None, None
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7065
diff changeset
   153
        return self.anonymous_credential
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7065
diff changeset
   154
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7065
diff changeset
   155
    def set_anonymous_allowed(self, allowed, anonuser='anon'):
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7065
diff changeset
   156
        if allowed:
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7065
diff changeset
   157
            self.anonymous_credential = (anonuser, anonuser)
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7065
diff changeset
   158
        else:
db7608cb32bc [devtools] fix anonymous user handling in test classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7065
diff changeset
   159
            self.anonymous_credential = None
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1016
diff changeset
   160
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   161
    @property
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   162
    def apphome(self):
6585
9af22f2c0c4c [test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6441
diff changeset
   163
        return self._apphome
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
    appdatahome = apphome
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1016
diff changeset
   165
3435
84036ad2c82d add anon user for TestServerConfiguration, no only AppTestConfiguration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   166
    def load_configuration(self):
84036ad2c82d add anon user for TestServerConfiguration, no only AppTestConfiguration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   167
        super(TestServerConfiguration, self).load_configuration()
4913
083b4d454192 server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4761
diff changeset
   168
        # no undo support in tests
8306
4da49700b06a [config, undo] Fix undo-support option migration
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 8265
diff changeset
   169
        self.global_set_option('undo-enabled', 'n')
3435
84036ad2c82d add anon user for TestServerConfiguration, no only AppTestConfiguration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   170
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
    def main_config_file(self):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1978
diff changeset
   172
        """return instance's control configuration file"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
        return join(self.apphome, '%s.conf' % self.name)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
    def bootstrap_cubes(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   177
            super(TestServerConfiguration, self).bootstrap_cubes()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
        except IOError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
            # no cubes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
            self.init_cubes( () )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
    sourcefile = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
    def sources_file(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   184
        """define in subclasses self.sourcefile if necessary"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
        if self.sourcefile:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
            print 'Reading sources from', self.sourcefile
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   187
            sourcefile = self.sourcefile
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   188
            if not isabs(sourcefile):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   189
                sourcefile = join(self.apphome, sourcefile)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   190
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   191
            sourcefile = super(TestServerConfiguration, self).sources_file()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
        return sourcefile
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
    def sources(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
        """By default, we run tests with the sqlite DB backend.  One may use its
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
        own configuration by just creating a 'sources' file in the test
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
        directory from wich tests are launched or by specifying an alternative
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
        sources file using self.sourcefile.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
        """
7301
93e96700e0c0 [configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7240
diff changeset
   200
        try:
93e96700e0c0 [configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7240
diff changeset
   201
            sources = super(TestServerConfiguration, self).sources()
93e96700e0c0 [configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7240
diff changeset
   202
        except ExecutionError:
93e96700e0c0 [configuration] exit with proper message when sources file is unreadable (you usually started cw while logged with a wrong user). Closes #1631238
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7240
diff changeset
   203
            sources = {}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
        if not sources:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
            sources = DEFAULT_SOURCES
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6425
diff changeset
   206
        if 'admin' not in sources:
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6425
diff changeset
   207
            sources['admin'] = DEFAULT_SOURCES['admin']
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
        return sources
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1016
diff changeset
   209
6441
adbce42ff1b5 TestServerConfig needs default_base_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6424
diff changeset
   210
    # web config methods needed here for cases when we use this config as a web
adbce42ff1b5 TestServerConfig needs default_base_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6424
diff changeset
   211
    # config
adbce42ff1b5 TestServerConfig needs default_base_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6424
diff changeset
   212
adbce42ff1b5 TestServerConfig needs default_base_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6424
diff changeset
   213
    def default_base_url(self):
adbce42ff1b5 TestServerConfig needs default_base_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6424
diff changeset
   214
        return BASE_URL
adbce42ff1b5 TestServerConfig needs default_base_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6424
diff changeset
   215
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
9252
01bca75ee8bd [devtools,etwist] rename TwistedConfiguration to WebConfigurationBase (follows #2919310)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9235
diff changeset
   217
class BaseApptestConfiguration(TestServerConfiguration, WebConfigurationBase):
6843
b70a26ca271c [devtools] set configuration name to all-in-one so test/data/all-in-one.conf file will be loaded as one may expected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6750
diff changeset
   218
    name = 'all-in-one' # so it search for all-in-one.conf, not repository.conf
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2732
diff changeset
   219
    options = cwconfig.merge_options(TestServerConfiguration.options
9252
01bca75ee8bd [devtools,etwist] rename TwistedConfiguration to WebConfigurationBase (follows #2919310)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9235
diff changeset
   220
                                     + WebConfigurationBase.options)
01bca75ee8bd [devtools,etwist] rename TwistedConfiguration to WebConfigurationBase (follows #2919310)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9235
diff changeset
   221
    cubicweb_appobject_path = TestServerConfiguration.cubicweb_appobject_path | WebConfigurationBase.cubicweb_appobject_path
01bca75ee8bd [devtools,etwist] rename TwistedConfiguration to WebConfigurationBase (follows #2919310)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9235
diff changeset
   222
    cube_appobject_path = TestServerConfiguration.cube_appobject_path | WebConfigurationBase.cube_appobject_path
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
    def available_languages(self, *args):
6749
48f468f33704 [config, i18n] Create default translation mechanism for all supported languages.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 6729
diff changeset
   225
        return self.cw_languages()
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1016
diff changeset
   226
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
    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
   228
        # 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
   229
        # threads
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   230
        return True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   231
6424
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6315
diff changeset
   232
# XXX merge with BaseApptestConfiguration ?
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   233
class ApptestConfiguration(BaseApptestConfiguration):
7214
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   234
    # `skip_db_create_and_restore` controls wether or not the test database
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   235
    # should be created / backuped / restored. If set to True, those
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   236
    # steps are completely skipped, the database is used as is and is
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   237
    # considered initialized
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   238
    skip_db_create_and_restore = False
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1016
diff changeset
   239
6585
9af22f2c0c4c [test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6441
diff changeset
   240
    def __init__(self, appid, apphome=None,
9af22f2c0c4c [test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6441
diff changeset
   241
                 log_threshold=logging.CRITICAL, sourcefile=None):
9af22f2c0c4c [test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6441
diff changeset
   242
        BaseApptestConfiguration.__init__(self, appid, apphome,
9af22f2c0c4c [test] start to make tests independant from cwd (more to do for tests which don't inherit from CubicWebTC
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6441
diff changeset
   243
                                          log_threshold=log_threshold)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   244
        self.init_repository = sourcefile is None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   245
        self.sourcefile = sourcefile
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1016
diff changeset
   246
6424
f443a2b8a5c7 [devtools] refactor http server initialization in a much saner way
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6315
diff changeset
   247
6164
ceb6951f9d2f [devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5754
diff changeset
   248
class RealDatabaseConfiguration(ApptestConfiguration):
ceb6951f9d2f [devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5754
diff changeset
   249
    """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
   250
ceb6951f9d2f [devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5754
diff changeset
   251
    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
   252
ceb6951f9d2f [devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5754
diff changeset
   253
    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
   254
    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
   255
    accordingly.
ceb6951f9d2f [devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5754
diff changeset
   256
ceb6951f9d2f [devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5754
diff changeset
   257
    Example usage::
ceb6951f9d2f [devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5754
diff changeset
   258
ceb6951f9d2f [devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5754
diff changeset
   259
      class MyTests(CubicWebTC):
7856
51a3fb272bf3 [skeleton] fix new class configuration in realdbtest
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7815
diff changeset
   260
          _config = RealDatabaseConfiguration('myapp',
51a3fb272bf3 [skeleton] fix new class configuration in realdbtest
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7815
diff changeset
   261
                                              sourcefile='/path/to/sources')
51a3fb272bf3 [skeleton] fix new class configuration in realdbtest
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 7815
diff changeset
   262
6164
ceb6951f9d2f [devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5754
diff changeset
   263
          def test_something(self):
ceb6951f9d2f [devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5754
diff changeset
   264
              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
   265
              self.view('foaf', rset)
ceb6951f9d2f [devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5754
diff changeset
   266
ceb6951f9d2f [devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5754
diff changeset
   267
    """
7214
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   268
    skip_db_create_and_restore = True
6164
ceb6951f9d2f [devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5754
diff changeset
   269
    read_instance_schema = True # read schema from database
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   270
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2732
diff changeset
   271
# test database handling #######################################################
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1016
diff changeset
   272
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   273
DEFAULT_EMPTY_DB_ID = '__default_empty_db__'
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   274
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   275
class TestDataBaseHandler(object):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   276
    DRIVER = None
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   277
    db_cache = {}
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   278
    explored_glob = set()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   279
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   280
    def __init__(self, config):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   281
        self.config = config
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   282
        self._repo = None
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   283
        # pure consistency check
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   284
        assert self.system_source['db-driver'] == self.DRIVER
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   285
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   286
    def _ensure_test_backup_db_dir(self):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   287
        """Return path of directory for database backup.
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   288
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   289
        The function create it if necessary"""
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   290
        backupdir = join(self.config.apphome, 'database')
9235
3eb8d65824f5 [devtools] fix race when creating backupdir
Julien Cristau <julien.cristau@logilab.fr>
parents: 8930
diff changeset
   291
        try:
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   292
            os.makedirs(backupdir)
9235
3eb8d65824f5 [devtools] fix race when creating backupdir
Julien Cristau <julien.cristau@logilab.fr>
parents: 8930
diff changeset
   293
        except:
3eb8d65824f5 [devtools] fix race when creating backupdir
Julien Cristau <julien.cristau@logilab.fr>
parents: 8930
diff changeset
   294
            if not isdir(backupdir):
3eb8d65824f5 [devtools] fix race when creating backupdir
Julien Cristau <julien.cristau@logilab.fr>
parents: 8930
diff changeset
   295
                raise
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   296
        return backupdir
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   297
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   298
    def config_path(self, db_id):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   299
        """Path for config backup of a given database id"""
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   300
        return self.absolute_backup_file(db_id, 'config')
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   301
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   302
    def absolute_backup_file(self, db_id, suffix):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   303
        """Path for config backup of a given database id"""
7217
4b860903d8c7 [testlib] fix bug in backup file name generation: it may erroneously replace '-' by '_' in database **parent directories**. This should fix random error when test are run under apycot.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7214
diff changeset
   304
        # in case db name is an absolute path, we don't want to replace anything
4b860903d8c7 [testlib] fix bug in backup file name generation: it may erroneously replace '-' by '_' in database **parent directories**. This should fix random error when test are run under apycot.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7214
diff changeset
   305
        # in parent directories
4b860903d8c7 [testlib] fix bug in backup file name generation: it may erroneously replace '-' by '_' in database **parent directories**. This should fix random error when test are run under apycot.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7214
diff changeset
   306
        directory, basename = split(self.dbname)
4b860903d8c7 [testlib] fix bug in backup file name generation: it may erroneously replace '-' by '_' in database **parent directories**. This should fix random error when test are run under apycot.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7214
diff changeset
   307
        dbname = basename.replace('-', '_')
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   308
        assert '.' not in db_id
7217
4b860903d8c7 [testlib] fix bug in backup file name generation: it may erroneously replace '-' by '_' in database **parent directories**. This should fix random error when test are run under apycot.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7214
diff changeset
   309
        filename = join(directory, '%s-%s.%s' % (dbname, db_id, suffix))
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   310
        return join(self._ensure_test_backup_db_dir(), filename)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   311
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   312
    def db_cache_key(self, db_id, dbname=None):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   313
        """Build a database cache key for a db_id with the current config
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   314
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   315
        This key is meant to be used in the cls.db_cache mapping"""
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   316
        if dbname is None:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   317
            dbname = self.dbname
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   318
        dbname = os.path.basename(dbname)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   319
        dbname = dbname.replace('-', '_')
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   320
        return (self.config.apphome, dbname, db_id)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   321
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   322
    def backup_database(self, db_id):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   323
        """Store the content of the current database as <db_id>
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   324
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   325
        The config used are also stored."""
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   326
        backup_data = self._backup_database(db_id)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   327
        config_path = self.config_path(db_id)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   328
        # XXX we dump a dict of the config
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   329
        # This is an experimental to help config dependant setup (like BFSS) to
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   330
        # be propertly restored
9337
70f6b183085a [devtools] write db config through a temporary file
Julien Cristau <julien.cristau@logilab.fr>
parents: 9235
diff changeset
   331
        with tempfile.NamedTemporaryFile(dir=os.path.dirname(config_path), delete=False) as conf_file:
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   332
            conf_file.write(pickle.dumps(dict(self.config)))
9337
70f6b183085a [devtools] write db config through a temporary file
Julien Cristau <julien.cristau@logilab.fr>
parents: 9235
diff changeset
   333
        os.rename(conf_file.name, config_path)
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   334
        self.db_cache[self.db_cache_key(db_id)] = (backup_data, config_path)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   335
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   336
    def _backup_database(self, db_id):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   337
        """Actual backup the current database.
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   338
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   339
        return a value to be stored in db_cache to allow restoration"""
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   340
        raise NotImplementedError()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   341
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   342
    def restore_database(self, db_id):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   343
        """Restore a database.
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   344
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   345
        takes as argument value stored in db_cache by self._backup_database"""
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   346
        # XXX set a clearer error message ???
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   347
        backup_coordinates, config_path = self.db_cache[self.db_cache_key(db_id)]
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   348
        # reload the config used to create the database.
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   349
        config = pickle.loads(open(config_path, 'rb').read())
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   350
        # shutdown repo before changing database content
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   351
        if self._repo is not None:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   352
            self._repo.turn_repo_off()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   353
        self._restore_database(backup_coordinates, config)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   354
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   355
    def _restore_database(self, backup_coordinates, config):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   356
        """Actual restore of the current database.
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   357
8543
b7c9443d8625 minor cleanups/typos
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8306
diff changeset
   358
        Use the value stored in db_cache as input """
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   359
        raise NotImplementedError()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   360
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   361
    def get_repo(self, startup=False):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   362
        """ return Repository object on the current database.
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   363
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   364
        (turn the current repo object "on" if there is one or recreate one)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   365
        if startup is True, server startup server hooks will be called if needed
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   366
        """
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   367
        if self._repo is None:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   368
            self._repo = self._new_repo(self.config)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   369
        repo = self._repo
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   370
        repo.turn_repo_on()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   371
        if startup and not repo._has_started:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   372
            repo.hm.call_hooks('server_startup', repo=repo)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   373
            repo._has_started = True
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   374
        return repo
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   375
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   376
    def _new_repo(self, config):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   377
        """Factory method to create a new Repository Instance"""
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   378
        from cubicweb.dbapi import in_memory_repo
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   379
        config._cubes = None
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   380
        repo = in_memory_repo(config)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   381
        # extending Repository class
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   382
        repo._has_started = False
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   383
        repo._needs_refresh = False
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   384
        repo.turn_repo_on = partial(turn_repo_on, repo)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   385
        repo.turn_repo_off = partial(turn_repo_off, repo)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   386
        return repo
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   387
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   388
    def get_cnx(self):
7222
fcb8932082a5 [testlib] refactor create_user and grant_permission to make them usable from pre_setup_database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7217
diff changeset
   389
        """return Connection object on the current repository"""
8673
8ea63a2cc2cc [db-api] rename repo_connect into _repo_connect to mark it private. Closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8669
diff changeset
   390
        from cubicweb.dbapi import _repo_connect
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   391
        repo = self.get_repo()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   392
        sources = self.config.sources()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   393
        login  = unicode(sources['admin']['login'])
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   394
        password = sources['admin']['password'] or 'xxx'
8673
8ea63a2cc2cc [db-api] rename repo_connect into _repo_connect to mark it private. Closes #2521848
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8669
diff changeset
   395
        cnx = _repo_connect(repo, login, password=password)
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   396
        return cnx
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   397
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   398
    def get_repo_and_cnx(self, db_id=DEFAULT_EMPTY_DB_ID):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   399
        """Reset database with the current db_id and return (repo, cnx)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   400
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   401
        A database *MUST* have been build with the current <db_id> prior to
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   402
        call this method. See the ``build_db_cache`` method. The returned
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   403
        repository have it's startup hooks called and the connection is
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   404
        establised as admin."""
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   405
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   406
        self.restore_database(db_id)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   407
        repo = self.get_repo(startup=True)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   408
        cnx  = self.get_cnx()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   409
        return repo, cnx
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   410
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   411
    @property
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   412
    def system_source(self):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   413
        sources = self.config.sources()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   414
        return sources['system']
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   415
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   416
    @property
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   417
    def dbname(self):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   418
        return self.system_source['db-name']
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   419
7220
eb0f5f46138f [testlib] fix typos / NameErrors in devtools/__init__.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7217
diff changeset
   420
    def init_test_database(self):
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   421
        """actual initialisation of the database"""
7220
eb0f5f46138f [testlib] fix typos / NameErrors in devtools/__init__.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7217
diff changeset
   422
        raise ValueError('no initialization function for driver %r' % self.DRIVER)
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   423
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   424
    def has_cache(self, db_id):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   425
        """Check if a given database id exist in cb cache for the current config"""
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   426
        cache_glob = self.absolute_backup_file('*', '*')
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   427
        if cache_glob not in self.explored_glob:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   428
            self.discover_cached_db()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   429
        return self.db_cache_key(db_id) in self.db_cache
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   430
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   431
    def discover_cached_db(self):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   432
        """Search available db_if for the current config"""
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   433
        cache_glob = self.absolute_backup_file('*', '*')
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   434
        directory = os.path.dirname(cache_glob)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   435
        entries={}
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   436
        candidates = glob.glob(cache_glob)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   437
        for filepath in candidates:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   438
            data = os.path.basename(filepath)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   439
            # database backup are in the forms are <dbname>-<db_id>.<backtype>
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   440
            dbname, data = data.split('-', 1)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   441
            db_id, filetype = data.split('.', 1)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   442
            entries.setdefault((dbname, db_id), {})[filetype] = filepath
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   443
        for (dbname, db_id), entry in entries.iteritems():
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   444
            # apply necessary transformation from the driver
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   445
            value = self.process_cache_entry(directory, dbname, db_id, entry)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   446
            assert 'config' in entry
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   447
            if value is not None: # None value means "not handled by this driver
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   448
                                  # XXX Ignored value are shadowed to other Handler if cache are common.
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   449
                key = self.db_cache_key(db_id, dbname=dbname)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   450
                self.db_cache[key] = value, entry['config']
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   451
        self.explored_glob.add(cache_glob)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   452
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   453
    def process_cache_entry(self, directory, dbname, db_id, entry):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   454
        """Transforms potential cache entry to proper backup coordinate
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   455
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   456
        entry argument is a "filetype" -> "filepath" mapping
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   457
        Return None if an entry should be ignored."""
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   458
        return None
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   459
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   460
    def build_db_cache(self, test_db_id=DEFAULT_EMPTY_DB_ID, pre_setup_func=None):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   461
        """Build Database cache for ``test_db_id`` if a cache doesn't exist
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   462
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   463
        if ``test_db_id is DEFAULT_EMPTY_DB_ID`` self.init_test_database is
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   464
        called. otherwise, DEFAULT_EMPTY_DB_ID is build/restored and
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   465
        ``pre_setup_func`` to setup the database.
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   466
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   467
        This function backup any database it build"""
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   468
        if self.has_cache(test_db_id):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   469
            return #test_db_id, 'already in cache'
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   470
        if test_db_id is DEFAULT_EMPTY_DB_ID:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   471
            self.init_test_database()
6164
ceb6951f9d2f [devtools] restore ability to test on real databases
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5754
diff changeset
   472
        else:
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   473
            print 'Building %s for database %s' % (test_db_id, self.dbname)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   474
            self.build_db_cache(DEFAULT_EMPTY_DB_ID)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   475
            self.restore_database(DEFAULT_EMPTY_DB_ID)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   476
            repo = self.get_repo(startup=True)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   477
            cnx = self.get_cnx()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   478
            session = repo._sessions[cnx.sessionid]
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7380
diff changeset
   479
            session.set_cnxset()
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   480
            _commit = session.commit
8188
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8175
diff changeset
   481
            def keep_cnxset_commit(free_cnxset=False):
1867e252e487 [repository] ldap-feed source. Closes #2086984
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8175
diff changeset
   482
                _commit(free_cnxset=free_cnxset)
7398
26695dd703d8 [repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7380
diff changeset
   483
            session.commit = keep_cnxset_commit
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   484
            pre_setup_func(session, self.config)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   485
            session.commit()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   486
            cnx.close()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   487
        self.backup_database(test_db_id)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   488
7214
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   489
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   490
class NoCreateDropDatabaseHandler(TestDataBaseHandler):
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   491
    """This handler is used if config.skip_db_create_and_restore is True
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   492
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   493
    This is typically the case with RealDBConfig. In that case,
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   494
    we explicitely want to skip init / backup / restore phases.
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   495
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   496
    This handler redefines the three corresponding methods and delegates
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   497
    to original handler for any other method / attribute
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   498
    """
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   499
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   500
    def __init__(self, base_handler):
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   501
        self.base_handler = base_handler
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   502
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   503
    # override init / backup / restore methods
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   504
    def init_test_database(self):
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   505
        pass
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   506
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   507
    def backup_database(self, db_id):
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   508
        pass
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   509
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   510
    def restore_database(self, db_id):
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   511
        pass
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   512
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   513
    # delegate to original handler in all other cases
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   514
    def __getattr__(self, attrname):
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   515
        return getattr(self.base_handler, attrname)
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   516
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   517
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2732
diff changeset
   518
### postgres test database handling ############################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   519
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   520
class PostgresTestDataBaseHandler(TestDataBaseHandler):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   521
    DRIVER = 'postgres'
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   522
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   523
    @property
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   524
    @cached
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   525
    def helper(self):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   526
        from logilab.database import get_db_helper
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   527
        return get_db_helper('postgres')
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   528
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   529
    @property
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   530
    def dbcnx(self):
7520
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   531
        try:
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   532
            return self._cnx
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   533
        except AttributeError:
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   534
            from cubicweb.server.serverctl import _db_sys_cnx
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   535
            try:
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   536
                self._cnx = _db_sys_cnx(
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   537
                    self.system_source, 'CREATE DATABASE and / or USER',
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   538
                    interactive=False)
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   539
                return self._cnx
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   540
            except Exception:
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   541
                self._cnx = None
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   542
                raise
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   543
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   544
    @property
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   545
    @cached
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   546
    def cursor(self):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   547
        return self.dbcnx.cursor()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   548
7214
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   549
    def process_cache_entry(self, directory, dbname, db_id, entry):
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   550
        backup_name = self._backup_name(db_id)
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   551
        if backup_name in self.helper.list_databases(self.cursor):
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   552
            return backup_name
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   553
        return None
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   554
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   555
    def init_test_database(self):
7214
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   556
        """initialize a fresh postgresql database used for testing purpose"""
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   557
        from cubicweb.server import init_repository
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   558
        from cubicweb.server.serverctl import system_source_cnx, createdb
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   559
        # connect on the dbms system base to create our base
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   560
        try:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   561
            self._drop(self.dbname)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   562
            createdb(self.helper, self.system_source, self.dbcnx, self.cursor)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   563
            self.dbcnx.commit()
7112
bb27cc300040 [db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7095
diff changeset
   564
            cnx = system_source_cnx(self.system_source, special_privs='LANGUAGE C',
bb27cc300040 [db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7095
diff changeset
   565
                                    interactive=False)
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
   566
            templcursor = cnx.cursor()
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   567
            try:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   568
                # XXX factorize with db-create code
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   569
                self.helper.init_fti_extensions(templcursor)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   570
                # install plpythonu/plpgsql language if not installed by the cube
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   571
                langs = sys.platform == 'win32' and ('plpgsql',) or ('plpythonu', 'plpgsql')
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   572
                for extlang in langs:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   573
                    self.helper.create_language(templcursor, extlang)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   574
                cnx.commit()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   575
            finally:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   576
                templcursor.close()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   577
                cnx.close()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   578
            init_repository(self.config, interactive=False)
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7765
diff changeset
   579
        except BaseException:
7520
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   580
            if self.dbcnx is not None:
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   581
                self.dbcnx.rollback()
7896
4c954e1e73ef [lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 7879
diff changeset
   582
            sys.stderr.write('building %s failed\n' % self.dbname)
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   583
            #self._drop(self.dbname)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   584
            raise
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   585
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   586
    def helper_clear_cache(self):
7520
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   587
        if self.dbcnx is not None:
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   588
            self.dbcnx.commit()
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   589
            self.dbcnx.close()
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   590
            del self._cnx
b7456b454334 [devtools pg handler] some fixes so we get proper error when pg host is not reachable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7506
diff changeset
   591
            clear_cache(self, 'cursor')
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   592
        clear_cache(self, 'helper')
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   593
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   594
    def __del__(self):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   595
        self.helper_clear_cache()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   596
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   597
    @property
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   598
    def _config_id(self):
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7856
diff changeset
   599
        return sha1(self.config.apphome).hexdigest()[:10]
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   600
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   601
    def _backup_name(self, db_id): # merge me with parent
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   602
        backup_name = '_'.join(('cache', self._config_id, self.dbname, db_id))
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   603
        return backup_name.lower()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   604
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   605
    def _drop(self, db_name):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   606
        if db_name in self.helper.list_databases(self.cursor):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   607
            self.cursor.execute('DROP DATABASE %s' % db_name)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   608
            self.dbcnx.commit()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   609
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   610
    def _backup_database(self, db_id):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   611
        """Actual backup the current database.
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   612
7506
c8cda2aca92e [testlib, realdb] we should close connections prior to create a new db using current db as template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7240
diff changeset
   613
        return a value to be stored in db_cache to allow restoration
c8cda2aca92e [testlib, realdb] we should close connections prior to create a new db using current db as template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7240
diff changeset
   614
        """
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   615
        from cubicweb.server.serverctl import createdb
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   616
        orig_name = self.system_source['db-name']
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   617
        try:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   618
            backup_name = self._backup_name(db_id)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   619
            self._drop(backup_name)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   620
            self.system_source['db-name'] = backup_name
8175
89fcaca0bca0 [testlib, postgres] take care, _repo is None when backuping postgres test db, crashing database generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7896
diff changeset
   621
            # during postgres database initialization, there is no repo set here.
89fcaca0bca0 [testlib, postgres] take care, _repo is None when backuping postgres test db, crashing database generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7896
diff changeset
   622
            assert self._repo is None
89fcaca0bca0 [testlib, postgres] take care, _repo is None when backuping postgres test db, crashing database generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7896
diff changeset
   623
            #self._repo.turn_repo_off()
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   624
            createdb(self.helper, self.system_source, self.dbcnx, self.cursor, template=orig_name)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   625
            self.dbcnx.commit()
8175
89fcaca0bca0 [testlib, postgres] take care, _repo is None when backuping postgres test db, crashing database generation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7896
diff changeset
   626
            #self._repo.turn_repo_on()
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   627
            return backup_name
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   628
        finally:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   629
            self.system_source['db-name'] = orig_name
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   630
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   631
    def _restore_database(self, backup_coordinates, config):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   632
        from cubicweb.server.serverctl import createdb
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   633
        """Actual restore of the current database.
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   634
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   635
        Use the value tostored in db_cache as input """
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   636
        self._drop(self.dbname)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   637
        createdb(self.helper, self.system_source, self.dbcnx, self.cursor,
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   638
                 template=backup_coordinates)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   639
        self.dbcnx.commit()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   640
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   641
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1016
diff changeset
   642
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
   643
### sqlserver2005 test database handling #######################################
5166
1e96d01728c4 enable live test against sqlserver 2005 database
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5043
diff changeset
   644
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   645
class SQLServerTestDataBaseHandler(TestDataBaseHandler):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   646
    DRIVER = 'sqlserver'
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   647
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   648
    # XXX complete me
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   649
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   650
    def init_test_database(self):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   651
        """initialize a fresh sqlserver databse used for testing purpose"""
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   652
        if self.config.init_repository:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   653
            from cubicweb.server import init_repository
7220
eb0f5f46138f [testlib] fix typos / NameErrors in devtools/__init__.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7217
diff changeset
   654
            init_repository(self.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
   655
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2732
diff changeset
   656
### 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
   657
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   658
class SQLiteTestDataBaseHandler(TestDataBaseHandler):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   659
    DRIVER = 'sqlite'
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   660
7722
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   661
    __TMPDB = set()
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   662
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   663
    @classmethod
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   664
    def _cleanup_all_tmpdb(cls):
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   665
        for dbpath in cls.__TMPDB:
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   666
            cls._cleanup_database(dbpath)
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   667
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   668
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   669
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   670
    def __init__(self, *args, **kwargs):
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   671
        super(SQLiteTestDataBaseHandler, self).__init__(*args, **kwargs)
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   672
        # use a dedicated base for each process.
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   673
        if 'global-db-name' not in self.system_source:
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   674
            self.system_source['global-db-name'] = self.system_source['db-name']
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   675
            process_db = self.system_source['db-name'] + str(os.getpid())
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   676
            self.system_source['db-name'] = process_db
7765
6f6253e44bdd devtools: Fix cleanup of temporary database (closes #1908656)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7722
diff changeset
   677
        process_db = self.absolute_dbfile() # update db-name to absolute path
6f6253e44bdd devtools: Fix cleanup of temporary database (closes #1908656)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7722
diff changeset
   678
        self.__TMPDB.add(process_db)
7722
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   679
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   680
    @staticmethod
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   681
    def _cleanup_database(dbfile):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2732
diff changeset
   682
        try:
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   683
            os.remove(dbfile)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   684
            os.remove('%s-journal' % dbfile)
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2732
diff changeset
   685
        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
   686
            pass
1802
d628defebc17 delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1016
diff changeset
   687
7722
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   688
    @property
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   689
    def dbname(self):
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   690
        return self.system_source['global-db-name']
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   691
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   692
    def absolute_dbfile(self):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   693
        """absolute path of current database file"""
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   694
        dbfile = join(self._ensure_test_backup_db_dir(),
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   695
                      self.config.sources()['system']['db-name'])
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   696
        self.config.sources()['system']['db-name'] = dbfile
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   697
        return dbfile
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   698
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   699
    def process_cache_entry(self, directory, dbname, db_id, entry):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   700
        return entry.get('sqlite')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   701
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   702
    def _backup_database(self, db_id=DEFAULT_EMPTY_DB_ID):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   703
        # XXX remove database file if it exists ???
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   704
        dbfile = self.absolute_dbfile()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   705
        backup_file = self.absolute_backup_file(db_id, 'sqlite')
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   706
        shutil.copy(dbfile, backup_file)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   707
        # Usefull to debug WHO write a database
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   708
        # backup_stack = self.absolute_backup_file(db_id, '.stack')
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   709
        #with open(backup_stack, 'w') as backup_stack_file:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   710
        #    import traceback
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   711
        #    traceback.print_stack(file=backup_stack_file)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   712
        return backup_file
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   713
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   714
    def _new_repo(self, config):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   715
        repo = super(SQLiteTestDataBaseHandler, self)._new_repo(config)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   716
        install_sqlite_patch(repo.querier)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   717
        return repo
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   718
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   719
    def _restore_database(self, backup_coordinates, _config):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   720
        # remove database file if it exists ?
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   721
        dbfile = self.absolute_dbfile()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   722
        self._cleanup_database(dbfile)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   723
        shutil.copy(backup_coordinates, dbfile)
8543
b7c9443d8625 minor cleanups/typos
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8306
diff changeset
   724
        self.get_repo()
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   725
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   726
    def init_test_database(self):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   727
        """initialize a fresh sqlite databse used for testing purpose"""
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2732
diff changeset
   728
        # 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
   729
        from cubicweb.server import init_repository
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   730
        self._cleanup_database(self.absolute_dbfile())
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   731
        init_repository(self.config, interactive=False)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   732
7722
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   733
import atexit
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   734
atexit.register(SQLiteTestDataBaseHandler._cleanup_all_tmpdb)
fb231d62adda [devtool] Make the sqlite database used for test execution process specific (closes #1230401)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   735
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   736
2732
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2657
diff changeset
   737
def install_sqlite_patch(querier):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   738
    """This patch hotfixes the following sqlite bug :
298
3e6d32667140 [doc] Fix docstring indentation.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 0
diff changeset
   739
       - http://www.sqlite.org/cvstrac/tktview?tn=1327,33
3e6d32667140 [doc] Fix docstring indentation.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 0
diff changeset
   740
       (some dates are returned as strings rather thant date objects)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   741
    """
937
491dbd818f9b avoid patching if already done
sylvain.thenault@logilab.fr
parents: 298
diff changeset
   742
    if hasattr(querier.__class__, '_devtools_sqlite_patched'):
491dbd818f9b avoid patching if already done
sylvain.thenault@logilab.fr
parents: 298
diff changeset
   743
        return # already monkey patched
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   744
    def wrap_execute(base_execute):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   745
        def new_execute(*args, **kwargs):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   746
            rset = base_execute(*args, **kwargs)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   747
            if rset.description:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   748
                found_date = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   749
                for row, rowdesc in zip(rset, rset.description):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   750
                    for cellindex, (value, vtype) in enumerate(zip(row, rowdesc)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   751
                        if vtype in ('Date', 'Datetime') and type(value) is unicode:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   752
                            found_date = True
1016
26387b836099 use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents: 937
diff changeset
   753
                            value = value.rsplit('.', 1)[0]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   754
                            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   755
                                row[cellindex] = strptime(value, '%Y-%m-%d %H:%M:%S')
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7765
diff changeset
   756
                            except Exception:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   757
                                row[cellindex] = strptime(value, '%Y-%m-%d')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   758
                        if vtype == 'Time' and type(value) is unicode:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   759
                            found_date = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   760
                            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   761
                                row[cellindex] = strptime(value, '%H:%M:%S')
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7765
diff changeset
   762
                            except Exception:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   763
                                # DateTime used as Time?
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   764
                                row[cellindex] = strptime(value, '%Y-%m-%d %H:%M:%S')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   765
                        if vtype == 'Interval' and type(value) is int:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   766
                            found_date = True
1016
26387b836099 use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents: 937
diff changeset
   767
                            row[cellindex] = timedelta(0, value, 0) # XXX value is in number of seconds?
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   768
                    if not found_date:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   769
                        break
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   770
            return rset
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   771
        return new_execute
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   772
    querier.__class__.execute = wrap_execute(querier.__class__.execute)
937
491dbd818f9b avoid patching if already done
sylvain.thenault@logilab.fr
parents: 298
diff changeset
   773
    querier.__class__._devtools_sqlite_patched = True
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   774
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   775
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   776
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   777
HANDLERS = {}
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   778
7090
d9e6e79e023a [testlib] require explicite overwriting of TestDataBaseHandler.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7078
diff changeset
   779
def register_handler(handlerkls, overwrite=False):
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   780
    assert handlerkls is not None
7090
d9e6e79e023a [testlib] require explicite overwriting of TestDataBaseHandler.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7078
diff changeset
   781
    if overwrite or handlerkls.DRIVER not in HANDLERS:
d9e6e79e023a [testlib] require explicite overwriting of TestDataBaseHandler.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7078
diff changeset
   782
        HANDLERS[handlerkls.DRIVER] = handlerkls
d9e6e79e023a [testlib] require explicite overwriting of TestDataBaseHandler.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7078
diff changeset
   783
    else:
7093
dccc22c60473 fix stupid typo
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7090
diff changeset
   784
        msg = "%s: Handler already exists use overwrite if it's intended\n"\
7090
d9e6e79e023a [testlib] require explicite overwriting of TestDataBaseHandler.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7078
diff changeset
   785
              "(existing handler class is %r)"
d9e6e79e023a [testlib] require explicite overwriting of TestDataBaseHandler.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7078
diff changeset
   786
        raise ValueError(msg % (handlerkls.DRIVER, HANDLERS[handlerkls.DRIVER]))
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   787
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   788
register_handler(PostgresTestDataBaseHandler)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   789
register_handler(SQLiteTestDataBaseHandler)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   790
register_handler(SQLServerTestDataBaseHandler)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   791
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   792
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   793
class HCache(object):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   794
    """Handler cache object: store database handler for a given configuration.
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   795
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   796
    We only keep one repo in cache to prevent too much objects to stay alive
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   797
    (database handler holds a reference to a repository). As at the moment a new
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   798
    handler is created for each TestCase class and all test methods are executed
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   799
    sequentialy whithin this class, there should not have more cache miss that
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   800
    if we had a wider cache as once a Handler stop being used it won't be used
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   801
    again.
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   802
    """
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   803
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   804
    def __init__(self):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   805
        self.config = None
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   806
        self.handler = None
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   807
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   808
    def get(self, config):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   809
        if config is self.config:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   810
            return self.handler
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   811
        else:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   812
            return None
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   813
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   814
    def set(self, config, handler):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   815
        self.config = config
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   816
        self.handler = handler
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   817
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   818
HCACHE = HCache()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   819
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   820
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   821
# XXX a class method on Test ?
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   822
def get_test_db_handler(config):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   823
    handler = HCACHE.get(config)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   824
    if handler is not None:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   825
        return handler
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   826
    sources = config.sources()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   827
    driver = sources['system']['db-driver']
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   828
    key = (driver, config)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   829
    handlerkls = HANDLERS.get(driver, None)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   830
    if handlerkls is not None:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   831
        handler = handlerkls(config)
7214
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   832
        if config.skip_db_create_and_restore:
70538ea2532d [testlib] fix readlb config handling, avoid systematic recreation of db with postgres (closes #1618089)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7129
diff changeset
   833
            handler = NoCreateDropDatabaseHandler(handler)
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   834
        HCACHE.set(config, handler)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   835
        return handler
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   836
    else:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   837
        raise ValueError('no initialization function for driver %r' % driver)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   838
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   839
### compatibility layer ##############################################
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   840
from logilab.common.deprecation import deprecated
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   841
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   842
@deprecated("please use the new DatabaseHandler mecanism")
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   843
def init_test_database(config=None, configdir='data', apphome=None):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   844
    """init a test database for a specific driver"""
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   845
    if config is None:
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   846
        config = TestServerConfiguration(apphome=apphome)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   847
    handler = get_test_db_handler(config)
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   848
    handler.build_db_cache()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   849
    return handler.get_repo_and_cnx()
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   850
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7071
diff changeset
   851