cubicweb/server/test/unittest_repository.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Sat, 16 Jan 2016 13:48:51 +0100
changeset 11057 0b59724cb3f2
parent 11008 server/test/unittest_repository.py@de86c6592cc7
child 11195 5de859b95988
permissions -rw-r--r--
Reorganize source tree to have a "cubicweb" top-level package Basically: mkdir cubicweb hg mv *.py -X setup.py cubicweb hg mv dataimport devtools entities etwist ext hooks i18n misc schemas server skeleton sobjects test web wsgi cubicweb Other changes: * adjust path to cubicweb-ctl in devtools tests * update setup.py to avoid importing __pkginfo__ (exec it instead), replace os.path.walk by os.walk and prepend `modname` here and there * update tox.ini to account for new test locations * update doc/conf.py so that it still finds __pkginfo__.py and CWDIR in doc/Makefile
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
# -*- coding: iso-8859-1 -*-
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
     2
# copyright 2003-2014 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: 5213
diff changeset
     3
# 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: 5213
diff changeset
     4
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5213
diff changeset
     5
# 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: 5213
diff changeset
     6
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5213
diff changeset
     7
# 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: 5213
diff changeset
     8
# 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: 5213
diff changeset
     9
# 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: 5213
diff changeset
    10
# 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: 5213
diff changeset
    11
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    12
# 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: 5213
diff changeset
    13
# 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: 5213
diff changeset
    14
# 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: 5213
diff changeset
    15
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5213
diff changeset
    16
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5213
diff changeset
    17
# 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: 5213
diff changeset
    18
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5889
014ea69e5200 [test] running transaction while session is closed are now killed, update test accordingly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5582
diff changeset
    19
"""unit tests for module cubicweb.server.repository"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
import threading
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
import time
7381
a10b2d90d1a3 [server test] restore logging level after test end
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7343
diff changeset
    23
import logging
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
    25
from six.moves import range
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
    26
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
from yams.constraints import UniqueConstraint
8945
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
    28
from yams import register_base_type, unregister_base_type
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
    29
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
    30
from logilab.database import get_db_helper
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
    32
from cubicweb import (BadConnectionId, ValidationError,
6385
9f91d09ee5fa [repo transaction] fix rollback behaviour as discussed on the mailing-list: instead of rollbacking automatically on Unauthorized/ValidationError, mark the transaction as uncommitable and disallow commiting
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6364
diff changeset
    33
                      UnknownEid, AuthenticationError, Unauthorized, QueryError)
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7795
diff changeset
    34
from cubicweb.predicates import is_instance
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
    35
from cubicweb.schema import RQLConstraint
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
    36
from cubicweb.devtools.testlib import CubicWebTC
1251
af40e615dc89 introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents: 17
diff changeset
    37
from cubicweb.devtools.repotest import tuplify
10236
ef3059a692cb Remove the remote repository-access-through-zmq support
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10235
diff changeset
    38
from cubicweb.server import hook
1251
af40e615dc89 introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents: 17
diff changeset
    39
from cubicweb.server.sqlutils import SQL_PREFIX
5115
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
    40
from cubicweb.server.hook import Hook
4806
4f12f59b1a13 [fti] refactor and fix full text indexation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4680
diff changeset
    41
from cubicweb.server.sources import native
8999
81a5d57d9230 [session] upgrade session closed error from Exception to SessionClosedError
pierre-yves
parents: 8947
diff changeset
    42
from cubicweb.server.session import SessionClosedError
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
    45
class RepositoryTC(CubicWebTC):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
    """ singleton providing access to a persistent storage for entities
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
    and relation
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
    """
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    49
9130
0f1504a9fb51 [constraint] more robust unicity constraint failures reporting for end-users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9128
diff changeset
    50
    def test_unique_together_constraint(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
    51
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
    52
            cnx.execute('INSERT Societe S: S nom "Logilab", S type "SSLL", S cp "75013"')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
    53
            with self.assertRaises(ValidationError) as wraperr:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
    54
                cnx.execute('INSERT Societe S: S nom "Logilab", S type "SSLL", S cp "75013"')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
    55
            self.assertEqual(
10280
2cdab5e33542 [i18n] properly translate error messages related to violated unicity constraint.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9965
diff changeset
    56
                {'cp': u'%(KEY-rtype)s is part of violated unicity constraint',
2cdab5e33542 [i18n] properly translate error messages related to violated unicity constraint.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9965
diff changeset
    57
                 'nom': u'%(KEY-rtype)s is part of violated unicity constraint',
2cdab5e33542 [i18n] properly translate error messages related to violated unicity constraint.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9965
diff changeset
    58
                 'type': u'%(KEY-rtype)s is part of violated unicity constraint',
2cdab5e33542 [i18n] properly translate error messages related to violated unicity constraint.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9965
diff changeset
    59
                 '': u'some relations violate a unicity constraint'},
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
    60
                wraperr.exception.args[1])
8820
dbffb6959564 server/source/native: fix wrong usage of .lstrip that produce garbled error messages (closes #2777641)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8572
diff changeset
    61
9130
0f1504a9fb51 [constraint] more robust unicity constraint failures reporting for end-users
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9128
diff changeset
    62
    def test_unique_together_schema(self):
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6188
diff changeset
    63
        person = self.repo.schema.eschema('Personne')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6236
diff changeset
    64
        self.assertEqual(len(person._unique_together), 1)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6236
diff changeset
    65
        self.assertItemsEqual(person._unique_together[0],
6208
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6188
diff changeset
    66
                                           ('nom', 'prenom', 'inline2'))
07b176640a8c unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6188
diff changeset
    67
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6401
diff changeset
    68
    def test_all_entities_have_owner(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
    69
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
    70
            self.assertFalse(cnx.execute('Any X WHERE NOT X owned_by U'))
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6401
diff changeset
    71
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6401
diff changeset
    72
    def test_all_entities_have_is(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
    73
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
    74
            self.assertFalse(cnx.execute('Any X WHERE NOT X is ET'))
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6401
diff changeset
    75
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6401
diff changeset
    76
    def test_all_entities_have_cw_source(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
    77
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
    78
            self.assertFalse(cnx.execute('Any X WHERE NOT X cw_source S'))
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    79
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
    def test_connect(self):
7186
287f2273917f [test] drop some warnings introduced by new test dabase handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6796
diff changeset
    81
        cnxid = self.repo.connect(self.admlogin, password=self.admpassword)
10600
180aa08cad48 [tests] Replace use of deprecated TestCase.assert_
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10487
diff changeset
    82
        self.assertTrue(cnxid)
7186
287f2273917f [test] drop some warnings introduced by new test dabase handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6796
diff changeset
    83
        self.repo.close(cnxid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
        self.assertRaises(AuthenticationError,
4191
01638461d4b0 test update. All cw tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777
diff changeset
    85
                          self.repo.connect, self.admlogin, password='nimportnawak')
01638461d4b0 test update. All cw tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777
diff changeset
    86
        self.assertRaises(AuthenticationError,
8520
fcd048fa6e6d [source/native] only system source users should be authenticated by the native source (closes #2465883)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8388
diff changeset
    87
                          self.repo.connect, self.admlogin, password='')
fcd048fa6e6d [source/native] only system source users should be authenticated by the native source (closes #2465883)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8388
diff changeset
    88
        self.assertRaises(AuthenticationError,
4191
01638461d4b0 test update. All cw tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777
diff changeset
    89
                          self.repo.connect, self.admlogin, password=None)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
        self.assertRaises(AuthenticationError,
4191
01638461d4b0 test update. All cw tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777
diff changeset
    91
                          self.repo.connect, None, password=None)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
        self.assertRaises(AuthenticationError,
4191
01638461d4b0 test update. All cw tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777
diff changeset
    93
                          self.repo.connect, self.admlogin)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
        self.assertRaises(AuthenticationError,
4191
01638461d4b0 test update. All cw tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777
diff changeset
    95
                          self.repo.connect, None)
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    96
10336
9c38f674e36f [dbapi] retire repo.execute, which was used by the dbapi
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10303
diff changeset
    97
    def test_login_upassword_accent(self):
9c38f674e36f [dbapi] retire repo.execute, which was used by the dbapi
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10303
diff changeset
    98
        with self.admin_access.repo_cnx() as cnx:
9c38f674e36f [dbapi] retire repo.execute, which was used by the dbapi
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10303
diff changeset
    99
            cnx.execute('INSERT CWUser X: X login %(login)s, X upassword %(passwd)s, '
9c38f674e36f [dbapi] retire repo.execute, which was used by the dbapi
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10303
diff changeset
   100
                        'X in_group G WHERE G name "users"',
9c38f674e36f [dbapi] retire repo.execute, which was used by the dbapi
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10303
diff changeset
   101
                        {'login': u"barnab�", 'passwd': u"h�h�h�".encode('UTF8')})
9c38f674e36f [dbapi] retire repo.execute, which was used by the dbapi
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10303
diff changeset
   102
            cnx.commit()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
        repo = self.repo
7186
287f2273917f [test] drop some warnings introduced by new test dabase handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6796
diff changeset
   104
        cnxid = repo.connect(u"barnab�", password=u"h�h�h�".encode('UTF8'))
10600
180aa08cad48 [tests] Replace use of deprecated TestCase.assert_
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10487
diff changeset
   105
        self.assertTrue(cnxid)
7186
287f2273917f [test] drop some warnings introduced by new test dabase handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6796
diff changeset
   106
        repo.close(cnxid)
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   107
6361
843684a50e48 [transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   108
    def test_rollback_on_execute_validation_error(self):
843684a50e48 [transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   109
        class ValidationErrorAfterHook(Hook):
843684a50e48 [transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   110
            __regid__ = 'valerror-after-hook'
843684a50e48 [transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   111
            __select__ = Hook.__select__ & is_instance('CWGroup')
843684a50e48 [transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   112
            events = ('after_update_entity',)
843684a50e48 [transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   113
            def __call__(self):
843684a50e48 [transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   114
                raise ValidationError(self.entity.eid, {})
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   115
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   116
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   117
            with self.temporary_appobjects(ValidationErrorAfterHook):
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   118
                self.assertRaises(ValidationError,
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   119
                                  cnx.execute, 'SET X name "toto" WHERE X is CWGroup, X name "guests"')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   120
                self.assertTrue(cnx.execute('Any X WHERE X is CWGroup, X name "toto"'))
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   121
                with self.assertRaises(QueryError) as cm:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   122
                    cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   123
                self.assertEqual(str(cm.exception), 'transaction must be rolled back')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   124
                cnx.rollback()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   125
                self.assertFalse(cnx.execute('Any X WHERE X is CWGroup, X name "toto"'))
6361
843684a50e48 [transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   126
843684a50e48 [transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   127
    def test_rollback_on_execute_unauthorized(self):
843684a50e48 [transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   128
        class UnauthorizedAfterHook(Hook):
6364
ad9ed9803eb6 cleanup and micro-optimization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6361
diff changeset
   129
            __regid__ = 'unauthorized-after-hook'
6361
843684a50e48 [transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   130
            __select__ = Hook.__select__ & is_instance('CWGroup')
843684a50e48 [transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   131
            events = ('after_update_entity',)
843684a50e48 [transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   132
            def __call__(self):
843684a50e48 [transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   133
                raise Unauthorized()
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   134
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   135
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   136
            with self.temporary_appobjects(UnauthorizedAfterHook):
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   137
                self.assertRaises(Unauthorized,
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   138
                                  cnx.execute, 'SET X name "toto" WHERE X is CWGroup, X name "guests"')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   139
                self.assertTrue(cnx.execute('Any X WHERE X is CWGroup, X name "toto"'))
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   140
                with self.assertRaises(QueryError) as cm:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   141
                    cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   142
                self.assertEqual(str(cm.exception), 'transaction must be rolled back')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   143
                cnx.rollback()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   144
                self.assertFalse(cnx.execute('Any X WHERE X is CWGroup, X name "toto"'))
6361
843684a50e48 [transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   145
843684a50e48 [transaction] to avoid potential db corruption, we should rollback systematically in case of ValidationError
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6340
diff changeset
   146
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
    def test_close(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
        repo = self.repo
4191
01638461d4b0 test update. All cw tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777
diff changeset
   149
        cnxid = repo.connect(self.admlogin, password=self.admpassword)
10600
180aa08cad48 [tests] Replace use of deprecated TestCase.assert_
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10487
diff changeset
   150
        self.assertTrue(cnxid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
        repo.close(cnxid)
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   152
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
    def test_initial_schema(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
        schema = self.repo.schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   156
        # check order of attributes is respected
9128
d988eec2d5d3 [test/schemaserial] swap got/expected to get nicer unittest2 diagnostics
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9008
diff changeset
   157
        notin = set(('eid', 'is', 'is_instance_of', 'identity',
d988eec2d5d3 [test/schemaserial] swap got/expected to get nicer unittest2 diagnostics
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9008
diff changeset
   158
                     'creation_date', 'modification_date', 'cwuri',
d988eec2d5d3 [test/schemaserial] swap got/expected to get nicer unittest2 diagnostics
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9008
diff changeset
   159
                     'owned_by', 'created_by', 'cw_source',
d988eec2d5d3 [test/schemaserial] swap got/expected to get nicer unittest2 diagnostics
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9008
diff changeset
   160
                     'update_permission', 'read_permission',
9395
96dba2efd16d [hooks/security] provide attribute "add" permission
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9267
diff changeset
   161
                     'add_permission', 'in_basket'))
9128
d988eec2d5d3 [test/schemaserial] swap got/expected to get nicer unittest2 diagnostics
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9008
diff changeset
   162
        self.assertListEqual(['relation_type',
d988eec2d5d3 [test/schemaserial] swap got/expected to get nicer unittest2 diagnostics
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9008
diff changeset
   163
                              'from_entity', 'to_entity',
d988eec2d5d3 [test/schemaserial] swap got/expected to get nicer unittest2 diagnostics
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9008
diff changeset
   164
                              'constrained_by',
9965
a8769b752299 [CWEP002] Account for attribute formula in schema bootstrap
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9803
diff changeset
   165
                              'cardinality', 'ordernum', 'formula',
9128
d988eec2d5d3 [test/schemaserial] swap got/expected to get nicer unittest2 diagnostics
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9008
diff changeset
   166
                              'indexed', 'fulltextindexed', 'internationalizable',
d988eec2d5d3 [test/schemaserial] swap got/expected to get nicer unittest2 diagnostics
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9008
diff changeset
   167
                              'defaultval', 'extra_props',
d988eec2d5d3 [test/schemaserial] swap got/expected to get nicer unittest2 diagnostics
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9008
diff changeset
   168
                              'description', 'description_format'],
d988eec2d5d3 [test/schemaserial] swap got/expected to get nicer unittest2 diagnostics
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9008
diff changeset
   169
                             [r.type
d988eec2d5d3 [test/schemaserial] swap got/expected to get nicer unittest2 diagnostics
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9008
diff changeset
   170
                              for r in schema.eschema('CWAttribute').ordered_relations()
d988eec2d5d3 [test/schemaserial] swap got/expected to get nicer unittest2 diagnostics
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9008
diff changeset
   171
                              if r.type not in notin])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6236
diff changeset
   173
        self.assertEqual(schema.eschema('CWEType').main_attribute(), 'name')
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6236
diff changeset
   174
        self.assertEqual(schema.eschema('State').main_attribute(), 'name')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
4191
01638461d4b0 test update. All cw tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777
diff changeset
   176
        constraints = schema.rschema('name').rdef('CWEType', 'String').constraints
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6236
diff changeset
   177
        self.assertEqual(len(constraints), 2)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
        for cstr in constraints[:]:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
            if isinstance(cstr, UniqueConstraint):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
                constraints.remove(cstr)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
                break
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
            self.fail('unique constraint not found')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   184
        sizeconstraint = constraints[0]
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6236
diff changeset
   185
        self.assertEqual(sizeconstraint.min, None)
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6236
diff changeset
   186
        self.assertEqual(sizeconstraint.max, 64)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   187
4191
01638461d4b0 test update. All cw tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777
diff changeset
   188
        constraints = schema.rschema('relation_type').rdef('CWAttribute', 'CWRType').constraints
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6236
diff changeset
   189
        self.assertEqual(len(constraints), 1)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   190
        cstr = constraints[0]
10600
180aa08cad48 [tests] Replace use of deprecated TestCase.assert_
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10487
diff changeset
   191
        self.assertIsInstance(cstr, RQLConstraint)
7161
e3f69df8dac7 fix failing tests before 3.12 release
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7148
diff changeset
   192
        self.assertEqual(cstr.expression, 'O final TRUE')
0
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
        ownedby = schema.rschema('owned_by')
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6236
diff changeset
   195
        self.assertEqual(ownedby.objects('CWEType'), ('CWUser',))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
    def test_internal_api(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
        repo = self.repo
4191
01638461d4b0 test update. All cw tests OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3777
diff changeset
   199
        cnxid = repo.connect(self.admlogin, password=self.admpassword)
10346
b926ff4ef4a8 [repoapi,session] remove all session-as-cnx backward compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10342
diff changeset
   200
        session = repo._get_session(cnxid)
b926ff4ef4a8 [repoapi,session] remove all session-as-cnx backward compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10342
diff changeset
   201
        with session.new_cnx() as cnx:
10365
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   202
            self.assertEqual(repo.type_and_source_from_eid(2, cnx),
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   203
                             ('CWGroup', None, 'system'))
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   204
            self.assertEqual(repo.type_from_eid(2, cnx), 'CWGroup')
7186
287f2273917f [test] drop some warnings introduced by new test dabase handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6796
diff changeset
   205
        repo.close(cnxid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
    def test_public_api(self):
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6236
diff changeset
   208
        self.assertEqual(self.repo.get_schema(), self.repo.schema)
7563
8e129bb6b571 [tests] update repository tests with new `use_cwuri_as_url` attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7543
diff changeset
   209
        self.assertEqual(self.repo.source_defs(), {'system': {'type': 'native',
8e129bb6b571 [tests] update repository tests with new `use_cwuri_as_url` attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7543
diff changeset
   210
                                                              'uri': 'system',
8e129bb6b571 [tests] update repository tests with new `use_cwuri_as_url` attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7543
diff changeset
   211
                                                              'use-cwuri-as-url': False}
8e129bb6b571 [tests] update repository tests with new `use_cwuri_as_url` attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7543
diff changeset
   212
                                                  })
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
        # .properties() return a result set
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6236
diff changeset
   214
        self.assertEqual(self.repo.properties().rql, 'Any K,V WHERE P is CWProperty,P pkey K, P value V, NOT P for_user U')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
2588
3a590ff82e99 [F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2200
diff changeset
   216
    def test_schema_is_relation(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   217
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   218
            no_is_rset = cnx.execute('Any X WHERE NOT X is ET')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   219
            self.assertFalse(no_is_rset, no_is_rset.description)
2588
3a590ff82e99 [F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2200
diff changeset
   220
2923
b97a0f8dd4dc fix test schema and update some tests to work again with wf changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   221
    def test_delete_if_singlecard1(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   222
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   223
            note = cnx.create_entity('Affaire')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   224
            p1 = cnx.create_entity('Personne', nom=u'toto')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   225
            cnx.execute('SET A todo_by P WHERE A eid %(x)s, P eid %(p)s',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   226
                        {'x': note.eid, 'p': p1.eid})
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   227
            rset = cnx.execute('Any P WHERE A todo_by P, A eid %(x)s',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   228
                               {'x': note.eid})
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   229
            self.assertEqual(len(rset), 1)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   230
            p2 = cnx.create_entity('Personne', nom=u'tutu')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   231
            cnx.execute('SET A todo_by P WHERE A eid %(x)s, P eid %(p)s',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   232
                        {'x': note.eid, 'p': p2.eid})
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   233
            rset = cnx.execute('Any P WHERE A todo_by P, A eid %(x)s',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   234
                                {'x': note.eid})
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   235
            self.assertEqual(len(rset), 1)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   236
            self.assertEqual(rset.rows[0][0], p2.eid)
2923
b97a0f8dd4dc fix test schema and update some tests to work again with wf changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2920
diff changeset
   237
6188
e1c9610b2360 [integrity] fix case where we can get two entities related while the cardinality is single on the object of an inlined relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5890
diff changeset
   238
    def test_delete_if_object_inlined_singlecard(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   239
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   240
            c = cnx.create_entity('Card', title=u'Carte')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   241
            cnx.create_entity('Personne', nom=u'Vincent', fiche=c)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   242
            cnx.create_entity('Personne', nom=u'Florent', fiche=c)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   243
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   244
            self.assertEqual(len(c.reverse_fiche), 1)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   245
10285
d14db30b90d6 [repository] don't attempt to delete computed relation, they have no table in the database. Closes #5162935
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10280
diff changeset
   246
    def test_delete_computed_relation_nonregr(self):
d14db30b90d6 [repository] don't attempt to delete computed relation, they have no table in the database. Closes #5162935
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10280
diff changeset
   247
        with self.admin_access.repo_cnx() as cnx:
d14db30b90d6 [repository] don't attempt to delete computed relation, they have no table in the database. Closes #5162935
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10280
diff changeset
   248
            c = cnx.create_entity('Personne', nom=u'Adam', login_user=cnx.user.eid)
d14db30b90d6 [repository] don't attempt to delete computed relation, they have no table in the database. Closes #5162935
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10280
diff changeset
   249
            cnx.commit()
d14db30b90d6 [repository] don't attempt to delete computed relation, they have no table in the database. Closes #5162935
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10280
diff changeset
   250
            c.cw_delete()
d14db30b90d6 [repository] don't attempt to delete computed relation, they have no table in the database. Closes #5162935
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10280
diff changeset
   251
            cnx.commit()
d14db30b90d6 [repository] don't attempt to delete computed relation, they have no table in the database. Closes #5162935
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10280
diff changeset
   252
8483
4ba11607d84a [entity api] unify set_attributes / set_relations into a cw_set method. Closes #2423719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8388
diff changeset
   253
    def test_cw_set_in_before_update(self):
5115
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   254
        # local hook
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   255
        class DummyBeforeHook(Hook):
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   256
            __regid__ = 'dummy-before-hook'
5877
0c7b7b76a84f [selectors] provide a new, optimized, is_instance selector that should at some point replace implements (along with the adaptable selector)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5717
diff changeset
   257
            __select__ = Hook.__select__ & is_instance('EmailAddress')
5115
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   258
            events = ('before_update_entity',)
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   259
            def __call__(self):
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   260
                # safety belt: avoid potential infinite recursion if the test
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   261
                #              fails (i.e. RuntimeError not raised)
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   262
                pendings = self._cw.transaction_data.setdefault('pending', set())
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   263
                if self.entity.eid not in pendings:
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   264
                    pendings.add(self.entity.eid)
8483
4ba11607d84a [entity api] unify set_attributes / set_relations into a cw_set method. Closes #2423719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8388
diff changeset
   265
                    self.entity.cw_set(alias=u'foo')
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   266
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   267
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   268
            with self.temporary_appobjects(DummyBeforeHook):
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   269
                addr = cnx.create_entity('EmailAddress', address=u'a@b.fr')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   270
                addr.cw_set(address=u'a@b.com')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   271
                rset = cnx.execute('Any A,AA WHERE X eid %(x)s, X address A, X alias AA',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   272
                                   {'x': addr.eid})
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   273
                self.assertEqual(rset.rows, [[u'a@b.com', u'foo']])
5115
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   274
8483
4ba11607d84a [entity api] unify set_attributes / set_relations into a cw_set method. Closes #2423719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8388
diff changeset
   275
    def test_cw_set_in_before_add(self):
5115
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   276
        # local hook
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   277
        class DummyBeforeHook(Hook):
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   278
            __regid__ = 'dummy-before-hook'
5877
0c7b7b76a84f [selectors] provide a new, optimized, is_instance selector that should at some point replace implements (along with the adaptable selector)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5717
diff changeset
   279
            __select__ = Hook.__select__ & is_instance('EmailAddress')
5115
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   280
            events = ('before_add_entity',)
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   281
            def __call__(self):
8483
4ba11607d84a [entity api] unify set_attributes / set_relations into a cw_set method. Closes #2423719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8388
diff changeset
   282
                # cw_set is forbidden within before_add_entity()
4ba11607d84a [entity api] unify set_attributes / set_relations into a cw_set method. Closes #2423719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8388
diff changeset
   283
                self.entity.cw_set(alias=u'foo')
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   284
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   285
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   286
            with self.temporary_appobjects(DummyBeforeHook):
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   287
                # XXX will fail with python -O
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   288
                self.assertRaises(AssertionError, cnx.create_entity,
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   289
                                  'EmailAddress', address=u'a@b.fr')
5115
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   290
8483
4ba11607d84a [entity api] unify set_attributes / set_relations into a cw_set method. Closes #2423719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8388
diff changeset
   291
    def test_multiple_edit_cw_set(self):
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6030
diff changeset
   292
        """make sure cw_edited doesn't get cluttered
5213
8604000bf3b2 [repository] fix edited_attributes management in multiple SET queries
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5126
diff changeset
   293
        by previous entities on multiple set
8604000bf3b2 [repository] fix edited_attributes management in multiple SET queries
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5126
diff changeset
   294
        """
8604000bf3b2 [repository] fix edited_attributes management in multiple SET queries
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5126
diff changeset
   295
        # local hook
8604000bf3b2 [repository] fix edited_attributes management in multiple SET queries
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5126
diff changeset
   296
        class DummyBeforeHook(Hook):
8604000bf3b2 [repository] fix edited_attributes management in multiple SET queries
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5126
diff changeset
   297
            _test = self # keep reference to test instance
8604000bf3b2 [repository] fix edited_attributes management in multiple SET queries
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5126
diff changeset
   298
            __regid__ = 'dummy-before-hook'
5877
0c7b7b76a84f [selectors] provide a new, optimized, is_instance selector that should at some point replace implements (along with the adaptable selector)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5717
diff changeset
   299
            __select__ = Hook.__select__ & is_instance('Affaire')
5213
8604000bf3b2 [repository] fix edited_attributes management in multiple SET queries
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5126
diff changeset
   300
            events = ('before_update_entity',)
8604000bf3b2 [repository] fix edited_attributes management in multiple SET queries
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5126
diff changeset
   301
            def __call__(self):
8604000bf3b2 [repository] fix edited_attributes management in multiple SET queries
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5126
diff changeset
   302
                # invoiced attribute shouldn't be considered "edited" before the hook
7791
31bb51ea5485 [deprecation] fix unittest pending deprecation warnings on failIf/failUnless methods family
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7723
diff changeset
   303
                self._test.assertFalse('invoiced' in self.entity.cw_edited,
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6030
diff changeset
   304
                                  'cw_edited cluttered by previous update')
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6030
diff changeset
   305
                self.entity.cw_edited['invoiced'] = 10
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   306
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   307
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   308
            with self.temporary_appobjects(DummyBeforeHook):
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   309
                cnx.create_entity('Affaire', ref=u'AFF01')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   310
                cnx.create_entity('Affaire', ref=u'AFF02')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   311
                cnx.execute('SET A duration 10 WHERE A is Affaire')
5213
8604000bf3b2 [repository] fix edited_attributes management in multiple SET queries
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 5126
diff changeset
   312
9226
653f1d4a1101 [repository] properly use IUserFriendlyError when UniqueTogetherError is raised during entity update. Closes #3096638
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9130
diff changeset
   313
653f1d4a1101 [repository] properly use IUserFriendlyError when UniqueTogetherError is raised during entity update. Closes #3096638
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9130
diff changeset
   314
    def test_user_friendly_error(self):
653f1d4a1101 [repository] properly use IUserFriendlyError when UniqueTogetherError is raised during entity update. Closes #3096638
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9130
diff changeset
   315
        from cubicweb.entities.adapters import IUserFriendlyUniqueTogether
653f1d4a1101 [repository] properly use IUserFriendlyError when UniqueTogetherError is raised during entity update. Closes #3096638
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9130
diff changeset
   316
        class MyIUserFriendlyUniqueTogether(IUserFriendlyUniqueTogether):
653f1d4a1101 [repository] properly use IUserFriendlyError when UniqueTogetherError is raised during entity update. Closes #3096638
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9130
diff changeset
   317
            __select__ = IUserFriendlyUniqueTogether.__select__ & is_instance('Societe')
653f1d4a1101 [repository] properly use IUserFriendlyError when UniqueTogetherError is raised during entity update. Closes #3096638
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9130
diff changeset
   318
            def raise_user_exception(self):
653f1d4a1101 [repository] properly use IUserFriendlyError when UniqueTogetherError is raised during entity update. Closes #3096638
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9130
diff changeset
   319
                raise ValidationError(self.entity.eid, {'hip': 'hop'})
653f1d4a1101 [repository] properly use IUserFriendlyError when UniqueTogetherError is raised during entity update. Closes #3096638
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9130
diff changeset
   320
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   321
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   322
            with self.temporary_appobjects(MyIUserFriendlyUniqueTogether):
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   323
                s = cnx.create_entity('Societe', nom=u'Logilab', type=u'ssll', cp=u'75013')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   324
                cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   325
                with self.assertRaises(ValidationError) as cm:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   326
                    cnx.create_entity('Societe', nom=u'Logilab', type=u'ssll', cp=u'75013')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   327
                self.assertEqual(cm.exception.errors, {'hip': 'hop'})
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   328
                cnx.rollback()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   329
                cnx.create_entity('Societe', nom=u'Logilab', type=u'ssll', cp=u'31400')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   330
                with self.assertRaises(ValidationError) as cm:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   331
                    s.cw_set(cp=u'31400')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   332
                self.assertEqual(cm.exception.entity, s.eid)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   333
                self.assertEqual(cm.exception.errors, {'hip': 'hop'})
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   334
                cnx.rollback()
9226
653f1d4a1101 [repository] properly use IUserFriendlyError when UniqueTogetherError is raised during entity update. Closes #3096638
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9130
diff changeset
   335
10998
5b646ab6821b [server] add test demonstrating cache update pb since cw > 3.21
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10609
diff changeset
   336
    def test_attribute_cache(self):
5b646ab6821b [server] add test demonstrating cache update pb since cw > 3.21
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10609
diff changeset
   337
        with self.admin_access.repo_cnx() as cnx:
5b646ab6821b [server] add test demonstrating cache update pb since cw > 3.21
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10609
diff changeset
   338
            bk = cnx.create_entity('Bookmark', title=u'index', path=u'/')
5b646ab6821b [server] add test demonstrating cache update pb since cw > 3.21
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10609
diff changeset
   339
            cnx.commit()
5b646ab6821b [server] add test demonstrating cache update pb since cw > 3.21
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10609
diff changeset
   340
            self.assertEqual(bk.title, 'index')
5b646ab6821b [server] add test demonstrating cache update pb since cw > 3.21
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10609
diff changeset
   341
            bk.cw_set(title=u'root')
5b646ab6821b [server] add test demonstrating cache update pb since cw > 3.21
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10609
diff changeset
   342
            self.assertEqual(bk.title, 'root')
5b646ab6821b [server] add test demonstrating cache update pb since cw > 3.21
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10609
diff changeset
   343
            cnx.commit()
5b646ab6821b [server] add test demonstrating cache update pb since cw > 3.21
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 10609
diff changeset
   344
            self.assertEqual(bk.title, 'root')
9226
653f1d4a1101 [repository] properly use IUserFriendlyError when UniqueTogetherError is raised during entity update. Closes #3096638
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9130
diff changeset
   345
8945
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   346
class SchemaDeserialTC(CubicWebTC):
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   347
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   348
    appid = 'data-schemaserial'
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   349
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   350
    @classmethod
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   351
    def setUpClass(cls):
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   352
        register_base_type('BabarTestType', ('jungle_speed',))
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   353
        helper = get_db_helper('sqlite')
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   354
        helper.TYPE_MAPPING['BabarTestType'] = 'TEXT'
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   355
        helper.TYPE_CONVERTERS['BabarTestType'] = lambda x: '"%s"' % x
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   356
        super(SchemaDeserialTC, cls).setUpClass()
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   357
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   358
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   359
    @classmethod
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   360
    def tearDownClass(cls):
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   361
        unregister_base_type('BabarTestType')
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   362
        helper = get_db_helper('sqlite')
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   363
        helper.TYPE_MAPPING.pop('BabarTestType', None)
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   364
        helper.TYPE_CONVERTERS.pop('BabarTestType', None)
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   365
        super(SchemaDeserialTC, cls).tearDownClass()
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   366
8947
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8945
diff changeset
   367
    def test_deserialization_base(self):
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8945
diff changeset
   368
        """Check the following deserialization
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8945
diff changeset
   369
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8945
diff changeset
   370
        * all CWEtype has name
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8945
diff changeset
   371
        * Final type
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8945
diff changeset
   372
        * CWUniqueTogetherConstraint
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8945
diff changeset
   373
        * _unique_together__ content"""
8945
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   374
        origshema = self.repo.schema
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   375
        try:
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   376
            self.repo.config.repairing = True # avoid versions checking
8947
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8945
diff changeset
   377
            self.repo.set_schema(self.repo.deserialize_schema())
8945
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   378
            table = SQL_PREFIX + 'CWEType'
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   379
            namecol = SQL_PREFIX + 'name'
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   380
            finalcol = SQL_PREFIX + 'final'
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   381
            with self.admin_access.repo_cnx() as cnx:
10365
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   382
                cu = cnx.system_sql('SELECT %s FROM %s WHERE %s is NULL'
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   383
                                    % (namecol, table, finalcol))
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   384
                self.assertEqual(cu.fetchall(), [])
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   385
                cu = cnx.system_sql('SELECT %s FROM %s '
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   386
                                    'WHERE %s=%%(final)s ORDER BY %s'
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   387
                                    % (namecol, table, finalcol, namecol),
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   388
                                    {'final': True})
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   389
                self.assertEqual(cu.fetchall(),
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   390
                                 [(u'BabarTestType',),
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   391
                                  (u'BigInt',), (u'Boolean',), (u'Bytes',),
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   392
                                  (u'Date',), (u'Datetime',),
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   393
                                  (u'Decimal',),(u'Float',),
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   394
                                  (u'Int',),
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   395
                                  (u'Interval',), (u'Password',),
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   396
                                  (u'String',),
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   397
                                  (u'TZDatetime',), (u'TZTime',), (u'Time',)])
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   398
                sql = ("SELECT etype.cw_eid, etype.cw_name, cstr.cw_eid, rel.eid_to "
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   399
                       "FROM cw_CWUniqueTogetherConstraint as cstr, "
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   400
                       "     relations_relation as rel, "
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   401
                       "     cw_CWEType as etype "
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   402
                       "WHERE cstr.cw_eid = rel.eid_from "
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   403
                       "  AND cstr.cw_constraint_of = etype.cw_eid "
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   404
                       "  AND etype.cw_name = 'Personne' "
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   405
                       ";")
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   406
                cu = cnx.system_sql(sql)
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   407
                rows = cu.fetchall()
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   408
                self.assertEqual(len(rows), 3)
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   409
                person = self.repo.schema.eschema('Personne')
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   410
                self.assertEqual(len(person._unique_together), 1)
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   411
                self.assertItemsEqual(person._unique_together[0],
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   412
                                      ('nom', 'prenom', 'inline2'))
8945
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   413
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   414
        finally:
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   415
            self.repo.set_schema(origshema)
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   416
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   417
    def test_custom_attribute_param(self):
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   418
        origshema = self.repo.schema
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   419
        try:
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   420
            self.repo.config.repairing = True # avoid versions checking
8947
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8945
diff changeset
   421
            self.repo.set_schema(self.repo.deserialize_schema())
8945
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   422
            pes = self.repo.schema['Personne']
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   423
            attr = pes.rdef('custom_field_of_jungle')
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   424
            self.assertIn('jungle_speed', vars(attr))
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   425
            self.assertEqual(42, attr.jungle_speed)
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   426
        finally:
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   427
            self.repo.set_schema(origshema)
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   428
ba9e3fbfa5a5 [schemaserial] serialize additional yams parameter for customs type
Vincent Michel <vincent.michel@logilab.fr>
parents: 8874
diff changeset
   429
5115
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5106
diff changeset
   430
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   431
class DataHelpersTC(CubicWebTC):
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   432
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   433
    def test_type_from_eid(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   434
        with self.admin_access.repo_cnx() as cnx:
10365
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   435
            self.assertEqual(self.repo.type_from_eid(2, cnx), 'CWGroup')
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   436
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   437
    def test_type_from_eid_raise(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   438
        with self.admin_access.repo_cnx() as cnx:
10365
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   439
            self.assertRaises(UnknownEid, self.repo.type_from_eid, -2, cnx)
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   440
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   441
    def test_add_delete_info(self):
9802
9f815b1f2516 [repository,tests] use the new connection api (part 1/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9469
diff changeset
   442
        with self.admin_access.repo_cnx() as cnx:
10365
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   443
            entity = self.repo.vreg['etypes'].etype_class('Personne')(cnx)
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   444
            entity.eid = -1
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   445
            entity.complete = lambda x: None
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   446
            self.repo.add_info(cnx, entity, self.repo.system_source)
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   447
            cu = cnx.system_sql('SELECT * FROM entities WHERE eid = -1')
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   448
            data = cu.fetchall()
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   449
            self.assertEqual(tuplify(data), [(-1, 'Personne', 'system', None)])
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   450
            self.repo._delete_cascade_multi(cnx, [entity])
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   451
            self.repo.system_source.delete_info_multi(cnx, [entity])
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   452
            cu = cnx.system_sql('SELECT * FROM entities WHERE eid = -1')
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   453
            data = cu.fetchall()
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   454
            self.assertEqual(data, [])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   455
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   456
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   457
class FTITC(CubicWebTC):
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   458
4806
4f12f59b1a13 [fti] refactor and fix full text indexation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4680
diff changeset
   459
    def test_fulltext_container_entity(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   460
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   461
            assert self.schema.rschema('use_email').fulltext_container == 'subject'
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   462
            toto = cnx.create_entity('EmailAddress', address=u'toto@logilab.fr')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   463
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   464
            rset = cnx.execute('Any X WHERE X has_text %(t)s', {'t': 'toto'})
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   465
            self.assertEqual(rset.rows, [])
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   466
            cnx.user.cw_set(use_email=toto)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   467
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   468
            rset = cnx.execute('Any X WHERE X has_text %(t)s', {'t': 'toto'})
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   469
            self.assertEqual(rset.rows, [[cnx.user.eid]])
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   470
            cnx.execute('DELETE X use_email Y WHERE X login "admin", Y eid %(y)s',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   471
                        {'y': toto.eid})
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   472
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   473
            rset = cnx.execute('Any X WHERE X has_text %(t)s', {'t': 'toto'})
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   474
            self.assertEqual(rset.rows, [])
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   475
            tutu = cnx.create_entity('EmailAddress', address=u'tutu@logilab.fr')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   476
            cnx.user.cw_set(use_email=tutu)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   477
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   478
            rset = cnx.execute('Any X WHERE X has_text %(t)s', {'t': 'tutu'})
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   479
            self.assertEqual(rset.rows, [[cnx.user.eid]])
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   480
            tutu.cw_set(address=u'hip@logilab.fr')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   481
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   482
            rset = cnx.execute('Any X WHERE X has_text %(t)s', {'t': 'tutu'})
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   483
            self.assertEqual(rset.rows, [])
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   484
            rset = cnx.execute('Any X WHERE X has_text %(t)s', {'t': 'hip'})
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   485
            self.assertEqual(rset.rows, [[cnx.user.eid]])
4806
4f12f59b1a13 [fti] refactor and fix full text indexation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4680
diff changeset
   486
4f12f59b1a13 [fti] refactor and fix full text indexation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4680
diff changeset
   487
    def test_no_uncessary_ftiindex_op(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   488
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   489
            cnx.create_entity('Workflow',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   490
                              name=u'dummy workflow',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   491
                              description=u'huuuuu')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   492
            self.assertFalse(any(x for x in cnx.pending_operations
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   493
                                 if isinstance(x, native.FTIndexEntityOp)))
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   494
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   495
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   496
class DBInitTC(CubicWebTC):
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   497
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   498
    def test_versions_inserted(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   499
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   500
            inserted = [r[0]
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   501
                        for r in cnx.execute('Any K ORDERBY K '
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   502
                                             'WHERE P pkey K, P pkey ~= "system.version.%"')]
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   503
            self.assertEqual(inserted,
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   504
                             [u'system.version.basket',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   505
                              u'system.version.card',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   506
                              u'system.version.comment',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   507
                              u'system.version.cubicweb',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   508
                              u'system.version.file',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   509
                              u'system.version.localperms',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   510
                              u'system.version.tag'])
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   511
3445
32492e9e734b update and simplify test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
   512
CALLED = []
32492e9e734b update and simplify test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
   513
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   514
class InlineRelHooksTC(CubicWebTC):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   515
    """test relation hooks are called for inlined relations
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   516
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   517
    def setUp(self):
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   518
        CubicWebTC.setUp(self)
3445
32492e9e734b update and simplify test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
   519
        CALLED[:] = ()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   520
3445
32492e9e734b update and simplify test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
   521
    def test_inline_relation(self):
32492e9e734b update and simplify test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
   522
        """make sure <event>_relation hooks are called for inlined relation"""
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   523
4766
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4680
diff changeset
   524
        class EcritParHook(hook.Hook):
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4680
diff changeset
   525
            __regid__ = 'inlinedrelhook'
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4680
diff changeset
   526
            __select__ = hook.Hook.__select__ & hook.match_rtype('ecrit_par')
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4680
diff changeset
   527
            events = ('before_add_relation', 'after_add_relation',
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4680
diff changeset
   528
                      'before_delete_relation', 'after_delete_relation')
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4680
diff changeset
   529
            def __call__(self):
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4680
diff changeset
   530
                CALLED.append((self.event, self.eidfrom, self.rtype, self.eidto))
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4680
diff changeset
   531
5105
57e0fc953676 [test] use new temporary_appobjects context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5103
diff changeset
   532
        with self.temporary_appobjects(EcritParHook):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   533
            with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   534
                eidp = cnx.execute('INSERT Personne X: X nom "toto"')[0][0]
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   535
                eidn = cnx.execute('INSERT Note X: X type "T"')[0][0]
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   536
                cnx.execute('SET N ecrit_par Y WHERE N type "T", Y nom "toto"')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   537
                self.assertEqual(CALLED, [('before_add_relation', eidn, 'ecrit_par', eidp),
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   538
                                          ('after_add_relation', eidn, 'ecrit_par', eidp)])
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   539
                CALLED[:] = ()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   540
                cnx.execute('DELETE N ecrit_par Y WHERE N type "T", Y nom "toto"')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   541
                self.assertEqual(CALLED, [('before_delete_relation', eidn, 'ecrit_par', eidp),
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   542
                                          ('after_delete_relation', eidn, 'ecrit_par', eidp)])
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   543
                CALLED[:] = ()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   544
                eidn = cnx.execute('INSERT Note N: N ecrit_par P WHERE P nom "toto"')[0][0]
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   545
                self.assertEqual(CALLED, [('before_add_relation', eidn, 'ecrit_par', eidp),
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   546
                                          ('after_add_relation', eidn, 'ecrit_par', eidp)])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   547
6236
9138f23ee263 [repo tests] add test case for RQLUniqueConstraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6208
diff changeset
   548
    def test_unique_contraint(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   549
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   550
            toto = cnx.create_entity('Personne', nom=u'toto')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   551
            a01 = cnx.create_entity('Affaire', ref=u'A01', todo_by=toto)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   552
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   553
            cnx.create_entity('Note', type=u'todo', inline1=a01)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   554
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   555
            cnx.create_entity('Note', type=u'todo', inline1=a01)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   556
            with self.assertRaises(ValidationError) as cm:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   557
                cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   558
            self.assertEqual(cm.exception.errors,
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   559
                             {'inline1-subject': u'RQLUniqueConstraint S type T, S inline1 A1, '
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   560
                              'A1 todo_by C, Y type T, Y inline1 A2, A2 todo_by C failed'})
1787
71c143c0ada3 fix test
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   561
7238
576abb8c4626 fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7237
diff changeset
   562
    def test_add_relations_at_creation_with_del_existing_rel(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   563
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   564
            person = cnx.create_entity('Personne',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   565
                                       nom=u'Toto',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   566
                                       prenom=u'Lanturlu',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   567
                                       sexe=u'M')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   568
            users_rql = 'Any U WHERE U is CWGroup, U name "users"'
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   569
            users = cnx.execute(users_rql).get_entity(0, 0)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   570
            cnx.create_entity('CWUser',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   571
                              login=u'Toto',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   572
                              upassword=u'firstname',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   573
                              firstname=u'firstname',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   574
                              surname=u'surname',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   575
                              reverse_login_user=person,
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   576
                              in_group=users)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   577
            cnx.commit()
7236
b91205ada414 added test case to monitor speed improvements in the repository code.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7194
diff changeset
   578
b91205ada414 added test case to monitor speed improvements in the repository code.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7194
diff changeset
   579
b91205ada414 added test case to monitor speed improvements in the repository code.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7194
diff changeset
   580
class PerformanceTest(CubicWebTC):
7381
a10b2d90d1a3 [server test] restore logging level after test end
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7343
diff changeset
   581
    def setUp(self):
a10b2d90d1a3 [server test] restore logging level after test end
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7343
diff changeset
   582
        super(PerformanceTest, self).setUp()
7236
b91205ada414 added test case to monitor speed improvements in the repository code.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7194
diff changeset
   583
        logger = logging.getLogger('cubicweb.session')
b91205ada414 added test case to monitor speed improvements in the repository code.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7194
diff changeset
   584
        #logger.handlers = [logging.StreamHandler(sys.stdout)]
b91205ada414 added test case to monitor speed improvements in the repository code.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7194
diff changeset
   585
        logger.setLevel(logging.INFO)
b91205ada414 added test case to monitor speed improvements in the repository code.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7194
diff changeset
   586
        self.info = logger.info
b91205ada414 added test case to monitor speed improvements in the repository code.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7194
diff changeset
   587
7381
a10b2d90d1a3 [server test] restore logging level after test end
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7343
diff changeset
   588
    def tearDown(self):
a10b2d90d1a3 [server test] restore logging level after test end
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7343
diff changeset
   589
        super(PerformanceTest, self).tearDown()
7382
91061a22755d fix name error introduced in previous changeset
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7381
diff changeset
   590
        logger = logging.getLogger('cubicweb.session')
7381
a10b2d90d1a3 [server test] restore logging level after test end
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7343
diff changeset
   591
        logger.setLevel(logging.CRITICAL)
a10b2d90d1a3 [server test] restore logging level after test end
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7343
diff changeset
   592
7236
b91205ada414 added test case to monitor speed improvements in the repository code.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7194
diff changeset
   593
    def test_composite_deletion(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   594
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   595
            personnes = []
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   596
            t0 = time.time()
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
   597
            for i in range(2000):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   598
                p = cnx.create_entity('Personne', nom=u'Doe%03d'%i, prenom=u'John', sexe=u'M')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   599
                personnes.append(p)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   600
            abraham = cnx.create_entity('Personne', nom=u'Abraham', prenom=u'John', sexe=u'M')
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
   601
            for j in range(0, 2000, 100):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   602
                abraham.cw_set(personne_composite=personnes[j:j+100])
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   603
            t1 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   604
            self.info('creation: %.2gs', (t1 - t0))
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   605
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   606
            t2 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   607
            self.info('commit creation: %.2gs', (t2 - t1))
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   608
            cnx.execute('DELETE Personne P WHERE P eid %(eid)s', {'eid': abraham.eid})
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   609
            t3 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   610
            self.info('deletion: %.2gs', (t3 - t2))
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   611
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   612
            t4 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   613
            self.info("commit deletion: %2gs", (t4 - t3))
7236
b91205ada414 added test case to monitor speed improvements in the repository code.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7194
diff changeset
   614
b91205ada414 added test case to monitor speed improvements in the repository code.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7194
diff changeset
   615
    def test_add_relation_non_inlined(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   616
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   617
            personnes = []
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
   618
            for i in range(2000):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   619
                p = cnx.create_entity('Personne', nom=u'Doe%03d'%i, prenom=u'John', sexe=u'M')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   620
                personnes.append(p)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   621
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   622
            t0 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   623
            abraham = cnx.create_entity('Personne', nom=u'Abraham', prenom=u'John', sexe=u'M',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   624
                                        personne_composite=personnes[:100])
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   625
            t1 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   626
            self.info('creation: %.2gs', (t1 - t0))
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
   627
            for j in range(100, 2000, 100):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   628
                abraham.cw_set(personne_composite=personnes[j:j+100])
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   629
            t2 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   630
            self.info('more relations: %.2gs', (t2-t1))
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   631
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   632
            t3 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   633
            self.info('commit creation: %.2gs', (t3 - t2))
7236
b91205ada414 added test case to monitor speed improvements in the repository code.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7194
diff changeset
   634
b91205ada414 added test case to monitor speed improvements in the repository code.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7194
diff changeset
   635
    def test_add_relation_inlined(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   636
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   637
            personnes = []
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
   638
            for i in range(2000):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   639
                p = cnx.create_entity('Personne', nom=u'Doe%03d'%i, prenom=u'John', sexe=u'M')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   640
                personnes.append(p)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   641
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   642
            t0 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   643
            abraham = cnx.create_entity('Personne', nom=u'Abraham', prenom=u'John', sexe=u'M',
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   644
                                        personne_inlined=personnes[:100])
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   645
            t1 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   646
            self.info('creation: %.2gs', (t1 - t0))
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
   647
            for j in range(100, 2000, 100):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   648
                abraham.cw_set(personne_inlined=personnes[j:j+100])
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   649
            t2 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   650
            self.info('more relations: %.2gs', (t2-t1))
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   651
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   652
            t3 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   653
            self.info('commit creation: %.2gs', (t3 - t2))
7236
b91205ada414 added test case to monitor speed improvements in the repository code.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7194
diff changeset
   654
7237
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7236
diff changeset
   655
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7236
diff changeset
   656
    def test_session_add_relation(self):
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7236
diff changeset
   657
        """ to be compared with test_session_add_relations"""
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   658
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   659
            personnes = []
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
   660
            for i in range(2000):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   661
                p = cnx.create_entity('Personne', nom=u'Doe%03d'%i, prenom=u'John', sexe=u'M')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   662
                personnes.append(p)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   663
            abraham = cnx.create_entity('Personne', nom=u'Abraham', prenom=u'John', sexe=u'M')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   664
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   665
            t0 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   666
            add_relation = cnx.add_relation
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   667
            for p in personnes:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   668
                add_relation(abraham.eid, 'personne_composite', p.eid)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   669
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   670
            t1 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   671
            self.info('add relation: %.2gs', t1-t0)
7237
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7236
diff changeset
   672
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7236
diff changeset
   673
    def test_session_add_relations (self):
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7236
diff changeset
   674
        """ to be compared with test_session_add_relation"""
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   675
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   676
            personnes = []
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
   677
            for i in range(2000):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   678
                p = cnx.create_entity('Personne', nom=u'Doe%03d'%i, prenom=u'John', sexe=u'M')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   679
                personnes.append(p)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   680
            abraham = cnx.create_entity('Personne', nom=u'Abraham', prenom=u'John', sexe=u'M')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   681
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   682
            t0 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   683
            add_relations = cnx.add_relations
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   684
            relations = [('personne_composite', [(abraham.eid, p.eid) for p in personnes])]
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   685
            add_relations(relations)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   686
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   687
            t1 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   688
            self.info('add relations: %.2gs', t1-t0)
7543
570522300e22 [ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7539
diff changeset
   689
7237
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7236
diff changeset
   690
    def test_session_add_relation_inlined(self):
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7236
diff changeset
   691
        """ to be compared with test_session_add_relations"""
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   692
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   693
            personnes = []
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
   694
            for i in range(2000):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   695
                p = cnx.create_entity('Personne', nom=u'Doe%03d'%i, prenom=u'John', sexe=u'M')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   696
                personnes.append(p)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   697
            abraham = cnx.create_entity('Personne', nom=u'Abraham', prenom=u'John', sexe=u'M')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   698
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   699
            t0 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   700
            add_relation = cnx.add_relation
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   701
            for p in personnes:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   702
                add_relation(abraham.eid, 'personne_inlined', p.eid)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   703
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   704
            t1 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   705
            self.info('add relation (inlined): %.2gs', t1-t0)
7237
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7236
diff changeset
   706
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7236
diff changeset
   707
    def test_session_add_relations_inlined (self):
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7236
diff changeset
   708
        """ to be compared with test_session_add_relation"""
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   709
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   710
            personnes = []
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10600
diff changeset
   711
            for i in range(2000):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   712
                p = cnx.create_entity('Personne', nom=u'Doe%03d'%i, prenom=u'John', sexe=u'M')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   713
                personnes.append(p)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   714
            abraham = cnx.create_entity('Personne', nom=u'Abraham', prenom=u'John', sexe=u'M')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   715
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   716
            t0 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   717
            add_relations = cnx.add_relations
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   718
            relations = [('personne_inlined', [(abraham.eid, p.eid) for p in personnes])]
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   719
            add_relations(relations)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   720
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   721
            t1 = time.time()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   722
            self.info('add relations (inlined): %.2gs', t1-t0)
7237
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7236
diff changeset
   723
7513
8f4422391e5a [repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7343
diff changeset
   724
    def test_optional_relation_reset_1(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   725
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   726
            p1 = cnx.create_entity('Personne', nom=u'Vincent')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   727
            p2 = cnx.create_entity('Personne', nom=u'Florent')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   728
            w = cnx.create_entity('Affaire', ref=u'wc')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   729
            w.cw_set(todo_by=[p1,p2])
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   730
            w.cw_clear_all_caches()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   731
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   732
            self.assertEqual(len(w.todo_by), 1)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   733
            self.assertEqual(w.todo_by[0].eid, p2.eid)
7513
8f4422391e5a [repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7343
diff changeset
   734
8f4422391e5a [repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7343
diff changeset
   735
    def test_optional_relation_reset_2(self):
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   736
        with self.admin_access.repo_cnx() as cnx:
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   737
            p1 = cnx.create_entity('Personne', nom=u'Vincent')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   738
            p2 = cnx.create_entity('Personne', nom=u'Florent')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   739
            w = cnx.create_entity('Affaire', ref=u'wc')
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   740
            w.cw_set(todo_by=p1)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   741
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   742
            w.cw_set(todo_by=p2)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   743
            w.cw_clear_all_caches()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   744
            cnx.commit()
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   745
            self.assertEqual(len(w.todo_by), 1)
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   746
            self.assertEqual(w.todo_by[0].eid, p2.eid)
7238
576abb8c4626 fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7237
diff changeset
   747
576abb8c4626 fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7237
diff changeset
   748
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   749
if __name__ == '__main__':
9803
5bd395e9649a [tests/repository] use the new connection api (part 2/2)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9802
diff changeset
   750
    from logilab.common.testlib import unittest_main
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   751
    unittest_main()