cubicweb/hooks/test/unittest_syncschema.py
author Jérémy Bobbio <jeremy.bobbio@irq7.fr>
Wed, 19 Jun 2019 15:44:17 +0200
changeset 12655 5b0ce10a7046
parent 11807 9d478b81f6d7
permissions -rw-r--r--
[crypto] Use Cryptodome namespace instead of Crypto PyCryptodome comes in two flavors: “an almost drop-in replacement for the old PyCrypto library” and “a library independent of the old PyCrypto”. The former uses the Crypto namespace, and is shipped as `pycryptodome` while the latter uses Cryptodome instead and lies in the `pycryptodomex` package. Given the reason to switch to PyCryptodome is that PyCrypto in unmaintained, its probably better to avoid any mistake and mandate the specific usage of the Cryptodome namespace by requiring `pycryptodomex` instead of `pycryptodome`. A more present reason is that Debian buster will only provide a package with the separate namespace flavor. The current Recommends is not working with the current code. Although it's important to note that the package name will probably have to be changed to `python3-pycryptodomex` once https://bugs.debian.org/886291 is solved.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
     1
# copyright 2003-2016 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: 4984
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4984
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4984
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4984
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4984
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4984
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4984
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4984
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4984
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4984
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4984
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4984
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4984
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4984
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4984
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
6412
370357e68837 [test] cleanup / unittest2 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6410
diff changeset
    18
"""cubicweb.server.hooks.syncschema unit and functional tests"""
370357e68837 [test] cleanup / unittest2 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6410
diff changeset
    19
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
    20
from yams.constraints import BoundaryConstraint
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    21
9299
c5eed908117d [schema] store default attribute values in a Bytes field, allowing python objects as default values
Aurélien Campeas <aurelien.campeas@logilab.fr>
parents: 8947
diff changeset
    22
from cubicweb import ValidationError, Binary
8715
ab0cd0765076 [hooks/syncschema] do not crash when adding a new entity type (closes #2741643)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7845
diff changeset
    23
from cubicweb.schema import META_RTYPES
10442
b2d2099dfae8 [test] run unittest_syncschema with postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10436
diff changeset
    24
from cubicweb.devtools import startpgcluster, stoppgcluster, PostgresApptestConfiguration
4689
4eb1f4490538 [test] skipping versions checking during test is enough, no need for monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4681
diff changeset
    25
from cubicweb.devtools.testlib import CubicWebTC
4eb1f4490538 [test] skipping versions checking during test is enough, no need for monkey patch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4681
diff changeset
    26
from cubicweb.server.sqlutils import SQL_PREFIX
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    27
from cubicweb.devtools.repotest import schema_eids_idx
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    28
8715
ab0cd0765076 [hooks/syncschema] do not crash when adding a new entity type (closes #2741643)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7845
diff changeset
    29
10442
b2d2099dfae8 [test] run unittest_syncschema with postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10436
diff changeset
    30
def setUpModule():
b2d2099dfae8 [test] run unittest_syncschema with postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10436
diff changeset
    31
    startpgcluster(__file__)
b2d2099dfae8 [test] run unittest_syncschema with postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10436
diff changeset
    32
b2d2099dfae8 [test] run unittest_syncschema with postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10436
diff changeset
    33
6781
5062d86d6ffe [unittest2] use unittest2 module fixture api
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6412
diff changeset
    34
def tearDownModule(*args):
10442
b2d2099dfae8 [test] run unittest_syncschema with postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10436
diff changeset
    35
    stoppgcluster(__file__)
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: 4691
diff changeset
    36
    del SchemaModificationHooksTC.schema_eids
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4691
diff changeset
    37
10442
b2d2099dfae8 [test] run unittest_syncschema with postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10436
diff changeset
    38
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    39
class SchemaModificationHooksTC(CubicWebTC):
10442
b2d2099dfae8 [test] run unittest_syncschema with postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10436
diff changeset
    40
    configcls = PostgresApptestConfiguration
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    41
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6781
diff changeset
    42
    def setUp(self):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6781
diff changeset
    43
        super(SchemaModificationHooksTC, self).setUp()
8947
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8718
diff changeset
    44
        self.repo.set_schema(self.repo.deserialize_schema(), resetvreg=False)
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6781
diff changeset
    45
        self.__class__.schema_eids = schema_eids_idx(self.repo.schema)
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    46
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    47
    def index_exists(self, cnx, etype, attr, unique=False):
9463
d62e13eba033 [multi-sources-removal] Simplify ConnectionsSet internal structures and public methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9402
diff changeset
    48
        dbhelper = self.repo.system_source.dbhelper
10365
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9851
diff changeset
    49
        sqlcursor = cnx.cnxset.cu
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9851
diff changeset
    50
        return dbhelper.index_exists(sqlcursor,
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9851
diff changeset
    51
                                     SQL_PREFIX + etype,
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9851
diff changeset
    52
                                     SQL_PREFIX + attr,
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9851
diff changeset
    53
                                     unique=unique)
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    54
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    55
    def _set_perms(self, cnx, eid):
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    56
        cnx.execute('SET X read_permission G WHERE X eid %(x)s, G is CWGroup',
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    57
                    {'x': eid})
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    58
        cnx.execute('SET X add_permission G WHERE X eid %(x)s, G is CWGroup, '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    59
                    'G name "managers"', {'x': eid})
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    60
        cnx.execute('SET X delete_permission G WHERE X eid %(x)s, G is CWGroup, '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    61
                    'G name "owners"', {'x': eid})
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    62
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    63
    def _set_attr_perms(self, cnx, eid):
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    64
        cnx.execute('SET X read_permission G WHERE X eid %(x)s, G is CWGroup',
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    65
                    {'x': eid})
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    66
        cnx.execute('SET X update_permission G WHERE X eid %(x)s, G is CWGroup, G name "managers"',
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    67
                    {'x': eid})
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    68
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    69
    def test_base(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    70
        with self.admin_access.repo_cnx() as cnx:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    71
            schema = self.repo.schema
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    72
            self.assertFalse(schema.has_entity('Societe2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    73
            self.assertFalse(schema.has_entity('concerne2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    74
            # schema should be update on insertion (after commit)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    75
            eeid = cnx.execute('INSERT CWEType X: X name "Societe2", '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    76
                               'X description "", X final FALSE')[0][0]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    77
            self._set_perms(cnx, eeid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    78
            cnx.execute('INSERT CWRType X: X name "concerne2", X description "", '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    79
                        'X final FALSE, X symmetric FALSE')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    80
            self.assertFalse(schema.has_entity('Societe2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    81
            self.assertFalse(schema.has_entity('concerne2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    82
            # have to commit before adding definition relations
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    83
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    84
            self.assertTrue(schema.has_entity('Societe2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    85
            self.assertTrue(schema.has_relation('concerne2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    86
            attreid = cnx.execute('INSERT CWAttribute X: X cardinality "11", '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    87
                                  'X defaultval %(default)s, X indexed TRUE, '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    88
                                  'X relation_type RT, X from_entity E, X to_entity F '
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
    89
                                  'WHERE RT name "name", E name "Societe2", '
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    90
                                  'F name "String"',
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    91
                                   {'default': Binary.zpickle('noname')})[0][0]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    92
            self._set_attr_perms(cnx, attreid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    93
            concerne2_rdef_eid = cnx.execute(
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    94
                'INSERT CWRelation X: X cardinality "**", X relation_type RT, '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    95
                'X from_entity E, X to_entity E '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    96
                'WHERE RT name "concerne2", E name "Societe2"')[0][0]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    97
            self._set_perms(cnx, concerne2_rdef_eid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    98
            self.assertNotIn('name', schema['Societe2'].subject_relations())
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    99
            self.assertNotIn('concerne2', schema['Societe2'].subject_relations())
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   100
            self.assertFalse(self.index_exists(cnx, 'Societe2', 'name'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   101
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   102
            self.assertIn('name', schema['Societe2'].subject_relations())
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   103
            self.assertIn('concerne2', schema['Societe2'].subject_relations())
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   104
            self.assertTrue(self.index_exists(cnx, 'Societe2', 'name'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   105
            # now we should be able to insert and query Societe2
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   106
            s2eid = cnx.execute('INSERT Societe2 X: X name "logilab"')[0][0]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   107
            cnx.execute('Societe2 X WHERE X name "logilab"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   108
            cnx.execute('SET X concerne2 X WHERE X name "logilab"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   109
            rset = cnx.execute('Any X WHERE X concerne2 Y')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   110
            self.assertEqual(rset.rows, [[s2eid]])
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   111
            # check that when a relation definition is deleted, existing relations are deleted
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   112
            rdefeid = cnx.execute('INSERT CWRelation X: X cardinality "**", X relation_type RT, '
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   113
                                  '   X from_entity E, X to_entity E '
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   114
                                  'WHERE RT name "concerne2", E name "CWUser"')[0][0]
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   115
            self._set_perms(cnx, rdefeid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   116
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   117
            cnx.execute('DELETE CWRelation X WHERE X eid %(x)s', {'x': concerne2_rdef_eid})
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   118
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   119
            self.assertIn('concerne2', schema['CWUser'].subject_relations())
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   120
            self.assertNotIn('concerne2', schema['Societe2'].subject_relations())
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   121
            self.assertFalse(cnx.execute('Any X WHERE X concerne2 Y'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   122
            # schema should be cleaned on delete (after commit)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   123
            cnx.execute('DELETE CWEType X WHERE X name "Societe2"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   124
            cnx.execute('DELETE CWRType X WHERE X name "concerne2"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   125
            self.assertTrue(self.index_exists(cnx, 'Societe2', 'name'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   126
            self.assertTrue(schema.has_entity('Societe2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   127
            self.assertTrue(schema.has_relation('concerne2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   128
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   129
            self.assertFalse(self.index_exists(cnx, 'Societe2', 'name'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   130
            self.assertFalse(schema.has_entity('Societe2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   131
            self.assertFalse(schema.has_entity('concerne2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   132
            self.assertNotIn('concerne2', schema['CWUser'].subject_relations())
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   133
8715
ab0cd0765076 [hooks/syncschema] do not crash when adding a new entity type (closes #2741643)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7845
diff changeset
   134
    def test_metartype_with_nordefs(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   135
        with self.admin_access.repo_cnx() as cnx:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   136
            META_RTYPES.add('custom_meta')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   137
            cnx.execute('INSERT CWRType X: X name "custom_meta", X description "", '
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   138
                        'X final FALSE, X symmetric FALSE')
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   139
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   140
            eeid = cnx.execute('INSERT CWEType X: X name "NEWEtype", '
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   141
                               'X description "", X final FALSE')[0][0]
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   142
            self._set_perms(cnx, eeid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   143
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   144
            META_RTYPES.remove('custom_meta')
8715
ab0cd0765076 [hooks/syncschema] do not crash when adding a new entity type (closes #2741643)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7845
diff changeset
   145
ab0cd0765076 [hooks/syncschema] do not crash when adding a new entity type (closes #2741643)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7845
diff changeset
   146
    def test_metartype_with_somerdefs(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   147
        with self.admin_access.repo_cnx() as cnx:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   148
            META_RTYPES.add('custom_meta')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   149
            cnx.execute('INSERT CWRType X: X name "custom_meta", X description "", '
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   150
                        'X final FALSE, X symmetric FALSE')
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   151
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   152
            rdefeid = cnx.execute('INSERT CWRelation X: X cardinality "**", X relation_type RT, '
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   153
                                  '   X from_entity E, X to_entity E '
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   154
                                  'WHERE RT name "custom_meta", E name "CWUser"')[0][0]
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   155
            self._set_perms(cnx, rdefeid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   156
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   157
            eeid = cnx.execute('INSERT CWEType X: X name "NEWEtype", '
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   158
                               'X description "", X final FALSE')[0][0]
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   159
            self._set_perms(cnx, eeid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   160
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   161
            META_RTYPES.remove('custom_meta')
8715
ab0cd0765076 [hooks/syncschema] do not crash when adding a new entity type (closes #2741643)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7845
diff changeset
   162
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   163
    def test_is_instance_of_insertions(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   164
        with self.admin_access.repo_cnx() as cnx:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   165
            seid = cnx.execute('INSERT Transition T: T name "subdiv"')[0][0]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   166
            is_etypes = [etype for etype, in cnx.execute('Any ETN WHERE X eid %s, '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   167
                                                         'X is ET, ET name ETN' % seid)]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   168
            self.assertEqual(is_etypes, ['Transition'])
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   169
            instanceof_etypes = [etype
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   170
                                 for etype, in cnx.execute('Any ETN WHERE X eid %s, '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   171
                                                           'X is_instance_of ET, ET name ETN'
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   172
                                                           % seid)]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   173
            self.assertEqual(sorted(instanceof_etypes), ['BaseTransition', 'Transition'])
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   174
            snames = [name for name, in cnx.execute('Any N WHERE S is BaseTransition, S name N')]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   175
            self.assertNotIn('subdiv', snames)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   176
            snames = [name for name, in cnx.execute('Any N WHERE S is_instance_of BaseTransition, '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   177
                                                    'S name N')]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   178
            self.assertIn('subdiv', snames)
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   179
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   180
    def test_perms_synchronization_1(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   181
        with self.admin_access.repo_cnx() as cnx:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   182
            schema = self.repo.schema
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   183
            self.assertEqual(schema['CWUser'].get_groups('read'), set(('managers', 'users')))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   184
            self.assertTrue(cnx.execute('Any X, Y WHERE X is CWEType, X name "CWUser", '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   185
                                        'Y is CWGroup, Y name "users"')[0])
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   186
            cnx.execute('DELETE X read_permission Y '
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   187
                        'WHERE X is CWEType, X name "CWUser", Y name "users"')
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   188
            self.assertEqual(schema['CWUser'].get_groups('read'), set(('managers', 'users', )))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   189
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   190
            self.assertEqual(schema['CWUser'].get_groups('read'), set(('managers',)))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   191
            cnx.execute('SET X read_permission Y WHERE X is CWEType, '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   192
                        'X name "CWUser", Y name "users"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   193
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   194
            self.assertEqual(schema['CWUser'].get_groups('read'),
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   195
                             set(('managers', 'users',)))
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   196
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   197
    def test_perms_synchronization_2(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   198
        with self.admin_access.repo_cnx() as cnx:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   199
            schema = self.repo.schema['in_group'].rdefs[('CWUser', 'CWGroup')]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   200
            self.assertEqual(schema.get_groups('read'),
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   201
                             set(('managers', 'users', 'guests')))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   202
            cnx.execute('DELETE X read_permission Y WHERE X relation_type RT, '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   203
                        'RT name "in_group", Y name "guests"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   204
            self.assertEqual(schema.get_groups('read'),
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   205
                             set(('managers', 'users', 'guests')))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   206
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   207
            self.assertEqual(schema.get_groups('read'),
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   208
                             set(('managers', 'users')))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   209
            cnx.execute('SET X read_permission Y WHERE X relation_type RT, '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   210
                        'RT name "in_group", Y name "guests"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   211
            self.assertEqual(schema.get_groups('read'),
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   212
                             set(('managers', 'users')))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   213
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   214
            self.assertEqual(schema.get_groups('read'),
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   215
                             set(('managers', 'users', 'guests')))
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   216
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   217
    def test_nonregr_user_edit_itself(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   218
        with self.admin_access.repo_cnx() as cnx:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   219
            ueid = cnx.user.eid
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   220
            groupeids = [eid for eid, in cnx.execute('CWGroup G WHERE G name '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   221
                                                     'in ("managers", "users")')]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   222
            cnx.execute('DELETE X in_group Y WHERE X eid %s' % ueid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   223
            cnx.execute('SET X surname "toto" WHERE X eid %s' % ueid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   224
            cnx.execute('SET X in_group Y WHERE X eid %s, Y name "managers"' % ueid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   225
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   226
            eeid = cnx.execute('Any X WHERE X is CWEType, X name "CWEType"')[0][0]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   227
            cnx.execute('DELETE X read_permission Y WHERE X eid %s' % eeid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   228
            cnx.execute('SET X final FALSE WHERE X eid %s' % eeid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   229
            cnx.execute('SET X read_permission Y WHERE X eid %s, Y eid in (%s, %s)'
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   230
                        % (eeid, groupeids[0], groupeids[1]))
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   231
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   232
            cnx.execute('Any X WHERE X is CWEType, X name "CWEType"')
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   233
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   234
    # schema modification hooks tests #########################################
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   235
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   236
    def test_uninline_relation(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   237
        with self.admin_access.repo_cnx() as cnx:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   238
            try:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   239
                self.assertTrue(self.schema['state_of'].inlined)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   240
                cnx.execute('SET X inlined FALSE WHERE X name "state_of"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   241
                self.assertTrue(self.schema['state_of'].inlined)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   242
                cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   243
                self.assertFalse(self.schema['state_of'].inlined)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   244
                self.assertFalse(self.index_exists(cnx, 'State', 'state_of'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   245
                rset = cnx.execute('Any X, Y WHERE X state_of Y')
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   246
                self.assertEqual(len(rset), 2)  # user states
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   247
            finally:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   248
                cnx.execute('SET X inlined TRUE WHERE X name "state_of"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   249
                self.assertFalse(self.schema['state_of'].inlined)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   250
                cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   251
                self.assertTrue(self.schema['state_of'].inlined)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   252
                self.assertTrue(self.index_exists(cnx, 'State', 'state_of'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   253
                rset = cnx.execute('Any X, Y WHERE X state_of Y')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   254
                self.assertEqual(len(rset), 2)
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   255
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   256
    def test_indexed_change(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   257
        with self.admin_access.repo_cnx() as cnx:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   258
            try:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   259
                cnx.execute('SET X indexed FALSE WHERE X relation_type R, R name "name"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   260
                self.assertTrue(self.schema['name'].rdef('Workflow', 'String').indexed)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   261
                self.assertTrue(self.index_exists(cnx, 'Workflow', 'name'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   262
                cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   263
                self.assertFalse(self.schema['name'].rdef('Workflow', 'String').indexed)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   264
                self.assertFalse(self.index_exists(cnx, 'Workflow', 'name'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   265
            finally:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   266
                cnx.execute('SET X indexed TRUE WHERE X relation_type R, R name "name"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   267
                self.assertFalse(self.schema['name'].rdef('Workflow', 'String').indexed)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   268
                self.assertFalse(self.index_exists(cnx, 'Workflow', 'name'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   269
                cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   270
                self.assertTrue(self.schema['name'].rdef('Workflow', 'String').indexed)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   271
                self.assertTrue(self.index_exists(cnx, 'Workflow', 'name'))
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   272
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   273
    def test_unique_change(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   274
        with self.admin_access.repo_cnx() as cnx:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   275
            try:
11358
179b5ff3f428 Update to yams 0.44 API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11341
diff changeset
   276
                eid = cnx.execute('INSERT CWConstraint X: X cstrtype CT, X value "{}", '
179b5ff3f428 Update to yams 0.44 API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11341
diff changeset
   277
                                  '                       DEF constrained_by X '
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   278
                                  'WHERE CT name "UniqueConstraint", DEF relation_type RT, '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   279
                                  'DEF from_entity E, RT name "name", '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   280
                                  'E name "Workflow"').rows[0][0]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   281
                self.assertFalse(self.schema['Workflow'].has_unique_values('name'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   282
                self.assertFalse(self.index_exists(cnx, 'Workflow', 'name', unique=True))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   283
                cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   284
                self.assertTrue(self.schema['Workflow'].has_unique_values('name'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   285
                self.assertTrue(self.index_exists(cnx, 'Workflow', 'name', unique=True))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   286
            finally:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   287
                cnx.execute('DELETE CWConstraint C WHERE C eid %(eid)s', {'eid': eid})
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   288
                cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   289
                self.assertFalse(self.schema['Workflow'].has_unique_values('name'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   290
                self.assertFalse(self.index_exists(cnx, 'Workflow', 'name', unique=True))
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   291
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   292
    def test_required_change_1(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   293
        with self.admin_access.repo_cnx() as cnx:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   294
            cnx.execute('SET DEF cardinality "?1" '
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   295
                        'WHERE DEF relation_type RT, DEF from_entity E,'
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   296
                        'RT name "title", E name "Bookmark"')
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   297
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   298
            # should now be able to add bookmark without title
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   299
            cnx.execute('INSERT Bookmark X: X path "/view"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   300
            cnx.commit()
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   301
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   302
    def test_required_change_2(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   303
        with self.admin_access.repo_cnx() as cnx:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   304
            cnx.execute('SET DEF cardinality "11" '
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   305
                        'WHERE DEF relation_type RT, DEF from_entity E,'
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   306
                        'RT name "surname", E name "CWUser"')
10436
6493c8bf521d [test] don't leave NULL columns around when making an attribute required
Julien Cristau <julien.cristau@logilab.fr>
parents: 10365
diff changeset
   307
            cnx.execute('SET U surname "Doe" WHERE U surname NULL')
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   308
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   309
            # should not be able anymore to add cwuser without surname
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   310
            self.assertRaises(ValidationError, self.create_user, cnx, "toto")
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   311
            cnx.rollback()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   312
            cnx.execute('SET DEF cardinality "?1" '
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   313
                        'WHERE DEF relation_type RT, DEF from_entity E,'
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   314
                        'RT name "surname", E name "CWUser"')
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   315
            cnx.commit()
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   316
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   317
    def test_add_attribute_to_base_class(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   318
        with self.admin_access.repo_cnx() as cnx:
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   319
            attreid = cnx.execute(
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   320
                'INSERT CWAttribute X: X cardinality "11", X defaultval %(default)s, '
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   321
                'X indexed TRUE, X relation_type RT, X from_entity E, X to_entity F '
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   322
                'WHERE RT name "messageid", E name "BaseTransition", F name "String"',
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   323
                {'default': Binary.zpickle('noname')})[0][0]
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   324
            assert cnx.execute('SET X read_permission Y WHERE X eid %(x)s, Y name "managers"',
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   325
                               {'x': attreid})
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   326
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   327
            self.schema.rebuild_infered_relations()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   328
            self.assertIn('Transition', self.schema['messageid'].subjects())
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   329
            self.assertIn('WorkflowTransition', self.schema['messageid'].subjects())
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   330
            cnx.execute('Any X WHERE X is_instance_of BaseTransition, X messageid "hop"')
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   331
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   332
    def test_change_fulltextindexed(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   333
        with self.admin_access.repo_cnx() as cnx:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   334
            target = cnx.create_entity(u'Email', messageid=u'1234',
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   335
                                       subject=u'rick.roll@dance.com')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   336
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   337
            rset = cnx.execute('Any X WHERE X has_text "rick.roll"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   338
            self.assertIn(target.eid, [item[0] for item in rset])
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   339
            assert cnx.execute('SET A fulltextindexed FALSE '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   340
                               'WHERE E is CWEType, E name "Email", A is CWAttribute,'
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   341
                               'A from_entity E, A relation_type R, R name "subject"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   342
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   343
            rset = cnx.execute('Any X WHERE X has_text "rick.roll"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   344
            self.assertFalse(rset)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   345
            assert cnx.execute('SET A fulltextindexed TRUE '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   346
                               'WHERE A from_entity E, A relation_type R, '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   347
                               'E name "Email", R name "subject"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   348
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   349
            rset = cnx.execute('Any X WHERE X has_text "rick.roll"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   350
            self.assertIn(target.eid, [item[0] for item in rset])
4826
7eba168407c3 [schema hook] fulltext_container is a relation *type* property. Test and fix (+ more fti properties change testing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4691
diff changeset
   351
7eba168407c3 [schema hook] fulltext_container is a relation *type* property. Test and fix (+ more fti properties change testing)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4691
diff changeset
   352
    def test_change_fulltext_container(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   353
        with self.admin_access.repo_cnx() as cnx:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   354
            target = cnx.create_entity(u'EmailAddress', address=u'rick.roll@dance.com')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   355
            target.cw_set(reverse_use_email=cnx.user)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   356
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   357
            rset = cnx.execute('Any X WHERE X has_text "rick.roll"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   358
            self.assertIn(cnx.user.eid, [item[0] for item in rset])
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   359
            assert cnx.execute('SET R fulltext_container NULL '
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   360
                               'WHERE R name "use_email"')
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   361
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   362
            rset = cnx.execute('Any X WHERE X has_text "rick.roll"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   363
            self.assertIn(target.eid, [item[0] for item in rset])
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   364
            assert cnx.execute('SET R fulltext_container "subject" '
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   365
                               'WHERE R name "use_email"')
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   366
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   367
            rset = cnx.execute('Any X WHERE X has_text "rick.roll"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   368
            self.assertIn(cnx.user.eid, [item[0] for item in rset])
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   369
4927
19fd1952ad63 [schema sync] test and fix bug when updating multiple constraint for the same rdef in the same transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4840
diff changeset
   370
    def test_update_constraint(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   371
        with self.admin_access.repo_cnx() as cnx:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   372
            rdef = self.schema['Transition'].rdef('type')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   373
            cstr = rdef.constraint_by_type('StaticVocabularyConstraint')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   374
            cnx.execute('SET X value %(v)s WHERE X eid %(x)s',
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   375
                        {'x': cstr.eid, 'v': u"u'normal', u'auto', u'new'"})
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   376
            cnx.execute('INSERT CWConstraint X: X value %(value)s, X cstrtype CT, '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   377
                        'EDEF constrained_by X WHERE CT name %(ct)s, EDEF eid %(x)s',
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   378
                        {'ct': 'SizeConstraint', 'value': u'max=10', 'x': rdef.eid})
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   379
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   380
            cstr = rdef.constraint_by_type('StaticVocabularyConstraint')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   381
            self.assertEqual(cstr.values, (u'normal', u'auto', u'new'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   382
            cnx.execute('INSERT Transition T: T name "hop", T type "new"')
4968
79d80cfaab6f missing unittest_main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4927
diff changeset
   383
10813
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   384
    def test_add_constraint(self):
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   385
        with self.admin_access.repo_cnx() as cnx:
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   386
            rdef = self.schema['EmailPart'].rdef('ordernum')
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   387
            cstr = BoundaryConstraint('>=', 0)
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   388
            cnx.execute('INSERT CWConstraint X: X value %(v)s, X cstrtype CT, '
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   389
                        'EDEF constrained_by X WHERE CT name %(ct)s, EDEF eid %(x)s',
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   390
                        {'ct': cstr.__class__.__name__, 'v': cstr.serialize(), 'x': rdef.eid})
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   391
            cnx.commit()
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   392
            cstr2 = rdef.constraint_by_type('BoundaryConstraint')
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   393
            self.assertEqual(cstr, cstr2)
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   394
            cstr3 = BoundaryConstraint('<=', 1000)
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   395
            cnx.execute('INSERT CWConstraint X: X value %(v)s, X cstrtype CT, '
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   396
                        'EDEF constrained_by X WHERE CT name %(ct)s, EDEF eid %(x)s',
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   397
                        {'ct': cstr3.__class__.__name__, 'v': cstr3.serialize(), 'x': rdef.eid})
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   398
            cnx.commit()
11341
bd3cd3691ade [test] Avoid assertCountEqual in unittest_syncschema.py
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   399
            # Do not use assertCountEqual as it does "strange" equality
bd3cd3691ade [test] Avoid assertCountEqual in unittest_syncschema.py
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   400
            # comparison on Python 2.
bd3cd3691ade [test] Avoid assertCountEqual in unittest_syncschema.py
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11057
diff changeset
   401
            self.assertEqual(set(rdef.constraints), set([cstr, cstr3]))
10813
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   402
11807
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11428
diff changeset
   403
    def test_eschema_composite_properties(self):
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11428
diff changeset
   404
        with self.admin_access.repo_cnx() as cnx:
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11428
diff changeset
   405
            part_eschema = self.schema['EmailPart']
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11428
diff changeset
   406
            email_eschema = self.schema['Email']
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11428
diff changeset
   407
            parts_rdef = email_eschema.rdef('parts')
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11428
diff changeset
   408
            self.assertEqual(part_eschema.composite_rdef_roles, [])
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11428
diff changeset
   409
            self.assertEqual(part_eschema.is_composite, False)
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11428
diff changeset
   410
            self.assertEqual(email_eschema.composite_rdef_roles,
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11428
diff changeset
   411
                             [(parts_rdef, 'subject')])
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11428
diff changeset
   412
            self.assertEqual(email_eschema.is_composite, True)
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11428
diff changeset
   413
            cnx.execute('DELETE CWRType X WHERE X name "parts"')
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11428
diff changeset
   414
            cnx.commit()
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11428
diff changeset
   415
            self.assertEqual(part_eschema.composite_rdef_roles, [])
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11428
diff changeset
   416
            self.assertEqual(part_eschema.is_composite, False)
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11428
diff changeset
   417
            self.assertEqual(email_eschema.composite_rdef_roles, [])
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11428
diff changeset
   418
            self.assertEqual(email_eschema.is_composite, False)
9d478b81f6d7 [hooks] Delete some properties cached on entities schema on schema updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11428
diff changeset
   419
10813
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   420
4968
79d80cfaab6f missing unittest_main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4927
diff changeset
   421
if __name__ == '__main__':
11417
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   422
    import unittest
5e5e224239c3 pep8 bits
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11358
diff changeset
   423
    unittest.main()