hooks/test/unittest_syncschema.py
author Julien Cristau <julien.cristau@logilab.fr>
Tue, 22 Dec 2015 17:35:39 +0100
changeset 11040 2c40ecf2a4fc
parent 10813 ab626726a70a
permissions -rw-r--r--
[pkg] remove no-longer-used addresses from debian control file
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
     1
# 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: 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
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    20
from logilab.common.testlib import unittest_main
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    21
10813
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
    22
from yams.constraints import BoundaryConstraint
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
    23
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
    24
from cubicweb.schema import META_RTYPES
10442
b2d2099dfae8 [test] run unittest_syncschema with postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10436
diff changeset
    25
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
    26
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
    27
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
    28
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
    29
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
    30
10442
b2d2099dfae8 [test] run unittest_syncschema with postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10436
diff changeset
    31
def setUpModule():
b2d2099dfae8 [test] run unittest_syncschema with postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10436
diff changeset
    32
    startpgcluster(__file__)
b2d2099dfae8 [test] run unittest_syncschema with postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10436
diff changeset
    33
b2d2099dfae8 [test] run unittest_syncschema with postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10436
diff changeset
    34
6781
5062d86d6ffe [unittest2] use unittest2 module fixture api
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 6412
diff changeset
    35
def tearDownModule(*args):
10442
b2d2099dfae8 [test] run unittest_syncschema with postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10436
diff changeset
    36
    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
    37
    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
    38
10442
b2d2099dfae8 [test] run unittest_syncschema with postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10436
diff changeset
    39
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    40
class SchemaModificationHooksTC(CubicWebTC):
10442
b2d2099dfae8 [test] run unittest_syncschema with postgresql
Julien Cristau <julien.cristau@logilab.fr>
parents: 10436
diff changeset
    41
    configcls = PostgresApptestConfiguration
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    42
7078
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6781
diff changeset
    43
    def setUp(self):
bad26a22fe29 [test] New Handling of database for test.
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6781
diff changeset
    44
        super(SchemaModificationHooksTC, self).setUp()
8947
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8718
diff changeset
    45
        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
    46
        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
    47
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    48
    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
    49
        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
    50
        sqlcursor = cnx.cnxset.cu
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9851
diff changeset
    51
        return dbhelper.index_exists(sqlcursor,
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9851
diff changeset
    52
                                     SQL_PREFIX + etype,
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9851
diff changeset
    53
                                     SQL_PREFIX + attr,
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9851
diff changeset
    54
                                     unique=unique)
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    55
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    56
    def _set_perms(self, cnx, eid):
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    57
        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
    58
                    {'x': eid})
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    59
        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
    60
                    'G name "managers"', {'x': eid})
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    61
        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
    62
                    '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
    63
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    64
    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
    65
        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
    66
                    {'x': eid})
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    67
        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
    68
                    {'x': eid})
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    69
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
    70
    def test_base(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    71
        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
    72
            schema = self.repo.schema
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    73
            self.assertFalse(schema.has_entity('Societe2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    74
            self.assertFalse(schema.has_entity('concerne2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    75
            # 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
    76
            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
    77
                               '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
    78
            self._set_perms(cnx, eeid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    79
            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
    80
                        'X final FALSE, X symmetric FALSE')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    81
            self.assertFalse(schema.has_entity('Societe2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    82
            self.assertFalse(schema.has_entity('concerne2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    83
            # 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
    84
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    85
            self.assertTrue(schema.has_entity('Societe2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    86
            self.assertTrue(schema.has_relation('concerne2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    87
            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
    88
                                  '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
    89
                                  'X relation_type RT, X from_entity E, X to_entity F '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    90
                                   'WHERE RT name "name", E name "Societe2", '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    91
                                  'F name "String"',
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    92
                                   {'default': Binary.zpickle('noname')})[0][0]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    93
            self._set_attr_perms(cnx, attreid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    94
            concerne2_rdef_eid = cnx.execute(
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
    95
                '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
    96
                '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
    97
                '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
    98
            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
    99
            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
   100
            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
   101
            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
   102
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   103
            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
   104
            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
   105
            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
   106
            # 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
   107
            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
   108
            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
   109
            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
   110
            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
   111
            self.assertEqual(rset.rows, [[s2eid]])
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   112
            # 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
   113
            rdefeid = cnx.execute('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
   114
                                   '   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
   115
                                   'WHERE RT name "concerne2", E name "CWUser"')[0][0]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   116
            self._set_perms(cnx, rdefeid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   117
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   118
            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
   119
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   120
            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
   121
            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
   122
            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
   123
            # 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
   124
            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
   125
            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
   126
            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
   127
            self.assertTrue(schema.has_entity('Societe2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   128
            self.assertTrue(schema.has_relation('concerne2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   129
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   130
            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
   131
            self.assertFalse(schema.has_entity('Societe2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   132
            self.assertFalse(schema.has_entity('concerne2'))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   133
            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
   134
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
   135
    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
   136
        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
   137
            META_RTYPES.add('custom_meta')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   138
            cnx.execute('INSERT CWRType X: X name "custom_meta", X description "", '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   139
                         'X final FALSE, X symmetric FALSE')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   140
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   141
            eeid = cnx.execute('INSERT CWEType X: X name "NEWEtype", '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   142
                                '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
   143
            self._set_perms(cnx, eeid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   144
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   145
            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
   146
ab0cd0765076 [hooks/syncschema] do not crash when adding a new entity type (closes #2741643)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7845
diff changeset
   147
    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
   148
        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
   149
            META_RTYPES.add('custom_meta')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   150
            cnx.execute('INSERT CWRType X: X name "custom_meta", X description "", '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   151
                         'X final FALSE, X symmetric FALSE')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   152
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   153
            rdefeid = cnx.execute('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
   154
                                   '   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
   155
                                   'WHERE RT name "custom_meta", E name "CWUser"')[0][0]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   156
            self._set_perms(cnx, rdefeid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   157
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   158
            eeid = cnx.execute('INSERT CWEType X: X name "NEWEtype", '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   159
                                '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
   160
            self._set_perms(cnx, eeid)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   161
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   162
            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
   163
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   164
    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
   165
        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
   166
            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
   167
            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
   168
                                                         '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
   169
            self.assertEqual(is_etypes, ['Transition'])
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   170
            instanceof_etypes = [etype
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   171
                                 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
   172
                                                           '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
   173
                                                           % seid)]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   174
            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
   175
            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
   176
            self.assertNotIn('subdiv', snames)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   177
            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
   178
                                                    'S name N')]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   179
            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
   180
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   181
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   182
    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
   183
        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
   184
            schema = self.repo.schema
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   185
            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
   186
            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
   187
                                        'Y is CWGroup, Y name "users"')[0])
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   188
            cnx.execute('DELETE X read_permission Y WHERE X is CWEType, X name "CWUser", Y name "users"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   189
            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
   190
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   191
            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
   192
            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
   193
                        'X name "CWUser", Y name "users"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   194
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   195
            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
   196
                             set(('managers', 'users',)))
4667
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   197
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   198
    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
   199
        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
   200
            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
   201
            self.assertEqual(schema.get_groups('read'),
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   202
                             set(('managers', 'users', 'guests')))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   203
            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
   204
                        '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
   205
            self.assertEqual(schema.get_groups('read'),
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   206
                             set(('managers', 'users', 'guests')))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   207
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   208
            self.assertEqual(schema.get_groups('read'),
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   209
                             set(('managers', 'users')))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   210
            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
   211
                        '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
   212
            self.assertEqual(schema.get_groups('read'),
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   213
                             set(('managers', 'users')))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   214
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   215
            self.assertEqual(schema.get_groups('read'),
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   216
                             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
   217
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   218
    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
   219
        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
   220
            ueid = cnx.user.eid
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   221
            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
   222
                                                     'in ("managers", "users")')]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   223
            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
   224
            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
   225
            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
   226
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   227
            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
   228
            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
   229
            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
   230
            cnx.execute('SET X read_permission Y WHERE X eid %s, Y eid in (%s, %s)'
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   231
                         % (eeid, groupeids[0], groupeids[1]))
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   232
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   233
            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
   234
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   235
    # schema modification hooks tests #########################################
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   236
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   237
    def test_uninline_relation(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   238
        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
   239
            try:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   240
                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
   241
                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
   242
                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
   243
                cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   244
                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
   245
                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
   246
                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
   247
                self.assertEqual(len(rset), 2) # user states
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   248
            finally:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   249
                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
   250
                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
   251
                cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   252
                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
   253
                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
   254
                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
   255
                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
   256
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   257
    def test_indexed_change(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   258
        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
   259
            try:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   260
                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
   261
                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
   262
                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
   263
                cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   264
                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
   265
                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
   266
            finally:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   267
                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
   268
                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
   269
                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
   270
                cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   271
                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
   272
                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
   273
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   274
    def test_unique_change(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   275
        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
   276
            try:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   277
                eid = cnx.execute('INSERT CWConstraint X: X cstrtype CT, DEF constrained_by X '
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" '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   295
                         'WHERE DEF relation_type RT, DEF from_entity E,'
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   296
                         'RT name "title", E name "Bookmark"')
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" '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   305
                         'WHERE DEF relation_type RT, DEF from_entity E,'
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
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" '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   313
                         'WHERE DEF relation_type RT, DEF from_entity E,'
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   314
                         'RT name "surname", E name "CWUser"')
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:
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   319
            attreid = cnx.execute('INSERT CWAttribute X: X cardinality "11", X defaultval %(default)s, '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   320
                                   'X indexed TRUE, X relation_type RT, X from_entity E, X to_entity F '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   321
                                   'WHERE RT name "messageid", E name "BaseTransition", F name "String"',
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   322
                                   {'default': Binary.zpickle('noname')})[0][0]
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   323
            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
   324
                               {'x': attreid})
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   325
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   326
            self.schema.rebuild_infered_relations()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   327
            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
   328
            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
   329
            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
   330
6c8eccb1b695 [fix] Apply fulltextindexed change in the actual index
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
diff changeset
   331
    def test_change_fulltextindexed(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   332
        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
   333
            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
   334
                                       subject=u'rick.roll@dance.com')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   335
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   336
            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
   337
            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
   338
            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
   339
                               '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
   340
                               '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
   341
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   342
            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
   343
            self.assertFalse(rset)
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   344
            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
   345
                               '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
   346
                               'E name "Email", R name "subject"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   347
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   348
            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
   349
            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
   350
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
    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
   352
        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
   353
            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
   354
            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
   355
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   356
            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
   357
            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
   358
            assert cnx.execute('SET R fulltext_container NULL '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   359
                                'WHERE R name "use_email"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   360
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   361
            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
   362
            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
   363
            assert cnx.execute('SET R fulltext_container "subject" '
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   364
                                'WHERE R name "use_email"')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   365
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   366
            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
   367
            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
   368
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
   369
    def test_update_constraint(self):
9851
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   370
        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
   371
            rdef = self.schema['Transition'].rdef('type')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   372
            cstr = rdef.constraint_by_type('StaticVocabularyConstraint')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   373
            if not getattr(cstr, 'eid', None):
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   374
                # bug in schema reloading, constraint's eid not restored
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   375
                self.skipTest('start me alone')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   376
            cnx.execute('SET X value %(v)s WHERE X eid %(x)s',
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   377
                         {'x': cstr.eid, 'v': u"u'normal', u'auto', u'new'"})
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   378
            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
   379
                        'EDEF constrained_by X WHERE CT name %(ct)s, EDEF eid %(x)s',
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   380
                         {'ct': 'SizeConstraint', 'value': u'max=10', 'x': rdef.eid})
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   381
            cnx.commit()
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   382
            cstr = rdef.constraint_by_type('StaticVocabularyConstraint')
42a874c666b1 [hookstests/syncschema] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9675
diff changeset
   383
            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
   384
            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
   385
10813
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   386
    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
   387
        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
   388
            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
   389
            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
   390
            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
   391
                        '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
   392
                        {'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
   393
            cnx.commit()
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   394
            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
   395
            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
   396
            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
   397
            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
   398
                        '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
   399
                        {'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
   400
            cnx.commit()
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   401
            self.assertCountEqual(rdef.constraints, [cstr, cstr3])
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   402
ab626726a70a [syncschema] don't arbitrarily remove constraints from the in-memory schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 10442
diff changeset
   403
4968
79d80cfaab6f missing unittest_main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4927
diff changeset
   404
if __name__ == '__main__':
79d80cfaab6f missing unittest_main
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4927
diff changeset
   405
    unittest_main()