cubicweb/hooks/test/unittest_integrity.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Sat, 16 Jan 2016 13:48:51 +0100
changeset 11057 0b59724cb3f2
parent 9854 hooks/test/unittest_integrity.py@4071c52b8fca
permissions -rw-r--r--
Reorganize source tree to have a "cubicweb" top-level package Basically: mkdir cubicweb hg mv *.py -X setup.py cubicweb hg mv dataimport devtools entities etwist ext hooks i18n misc schemas server skeleton sobjects test web wsgi cubicweb Other changes: * adjust path to cubicweb-ctl in devtools tests * update setup.py to avoid importing __pkginfo__ (exec it instead), replace os.path.walk by os.walk and prepend `modname` here and there * update tox.ini to account for new test locations * update doc/conf.py so that it still finds __pkginfo__.py and CWDIR in doc/Makefile
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6964
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
9548
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9275
diff changeset
     2
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
6964
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
#
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
# This file is part of CubicWeb.
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
#
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
# CubicWeb is free software: you can redistribute it and/or modify it under the
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
# terms of the GNU Lesser General Public License as published by the Free
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
# Software Foundation, either version 2.1 of the License, or (at your option)
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
# any later version.
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
#
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
# details.
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    16
#
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
# You should have received a copy of the GNU Lesser General Public License along
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
"""functional tests for integrity hooks"""
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    20
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
from cubicweb import ValidationError
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    22
from cubicweb.devtools.testlib import CubicWebTC
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    24
class CoreHooksTC(CubicWebTC):
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    25
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
    def test_delete_internal_entities(self):
9854
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    27
        with self.admin_access.repo_cnx() as cnx:
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    28
            self.assertRaises(ValidationError, cnx.execute,
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    29
                              'DELETE CWEType X WHERE X name "CWEType"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    30
            cnx.rollback()
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    31
            self.assertRaises(ValidationError, cnx.execute,
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    32
                              'DELETE CWRType X WHERE X name "relation_type"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    33
            cnx.rollback()
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    34
            self.assertRaises(ValidationError, cnx.execute,
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    35
                              'DELETE CWGroup X WHERE X name "owners"')
6964
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
    def test_delete_required_relations_subject(self):
9854
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    38
        with self.admin_access.repo_cnx() as cnx:
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    39
            cnx.execute('INSERT CWUser X: X login "toto", X upassword "hop", X in_group Y '
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    40
                         'WHERE Y name "users"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    41
            cnx.commit()
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    42
            cnx.execute('DELETE X in_group Y WHERE X login "toto", Y name "users"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    43
            self.assertRaises(ValidationError, cnx.commit)
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    44
            cnx.rollback()
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    45
            cnx.execute('DELETE X in_group Y WHERE X login "toto"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    46
            cnx.execute('SET X in_group Y WHERE X login "toto", Y name "guests"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    47
            cnx.commit()
6964
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    48
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    49
    def test_static_vocabulary_check(self):
9854
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    50
        with self.admin_access.repo_cnx() as cnx:
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    51
            self.assertRaises(ValidationError,
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    52
                              cnx.execute,
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    53
                              'SET X composite "whatever" WHERE X from_entity FE, FE name "CWUser", '
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    54
                              'X relation_type RT, RT name "in_group"')
6964
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    55
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    56
    def test_missing_required_relations_subject_inline(self):
9854
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    57
        with self.admin_access.repo_cnx() as cnx:
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    58
            # missing in_group relation
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    59
            cnx.execute('INSERT CWUser X: X login "toto", X upassword "hop"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    60
            self.assertRaises(ValidationError, cnx.commit)
6964
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    61
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    62
    def test_composite_1(self):
9854
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    63
        with self.admin_access.repo_cnx() as cnx:
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    64
            cnx.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    65
            cnx.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, '
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    66
                        'X content "this is a test"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    67
            cnx.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, '
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    68
                        'X recipients Y, X parts P '
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    69
                         'WHERE Y is EmailAddress, P is EmailPart')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    70
            self.assertTrue(cnx.execute('Email X WHERE X sender Y'))
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    71
            cnx.commit()
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    72
            cnx.execute('DELETE Email X')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    73
            rset = cnx.execute('Any X WHERE X is EmailPart')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    74
            self.assertEqual(len(rset), 0)
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    75
            cnx.commit()
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    76
            rset = cnx.execute('Any X WHERE X is EmailPart')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    77
            self.assertEqual(len(rset), 0)
6964
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    78
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    79
    def test_composite_2(self):
9854
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    80
        with self.admin_access.repo_cnx() as cnx:
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    81
            cnx.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    82
            cnx.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, '
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    83
                        'X content "this is a test"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    84
            cnx.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, '
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    85
                        'X recipients Y, X parts P '
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    86
                         'WHERE Y is EmailAddress, P is EmailPart')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    87
            cnx.commit()
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    88
            cnx.execute('DELETE Email X')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    89
            cnx.execute('DELETE EmailPart X')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    90
            cnx.commit()
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    91
            rset = cnx.execute('Any X WHERE X is EmailPart')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    92
            self.assertEqual(len(rset), 0)
6964
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    93
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    94
    def test_composite_redirection(self):
9854
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    95
        with self.admin_access.repo_cnx() as cnx:
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    96
            cnx.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    97
            cnx.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, '
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    98
                        'X content "this is a test"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
    99
            cnx.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, '
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   100
                        'X recipients Y, X parts P '
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   101
                         'WHERE Y is EmailAddress, P is EmailPart')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   102
            cnx.execute('INSERT Email X: X messageid "<2345>", X subject "test2", X sender Y, '
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   103
                        'X recipients Y '
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   104
                         'WHERE Y is EmailAddress')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   105
            cnx.commit()
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   106
            cnx.execute('DELETE X parts Y WHERE X messageid "<1234>"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   107
            cnx.execute('SET X parts Y WHERE X messageid "<2345>"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   108
            cnx.commit()
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   109
            rset = cnx.execute('Any X WHERE X is EmailPart')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   110
            self.assertEqual(len(rset), 1)
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   111
            self.assertEqual(rset.get_entity(0, 0).reverse_parts[0].messageid, '<2345>')
6964
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   112
9548
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9275
diff changeset
   113
    def test_composite_object_relation_deletion(self):
9854
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   114
        with self.admin_access.repo_cnx() as cnx:
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   115
            root = cnx.create_entity('Folder', name=u'root')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   116
            a = cnx.create_entity('Folder', name=u'a', parent=root)
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   117
            cnx.create_entity('Folder', name=u'b', parent=a)
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   118
            cnx.create_entity('Folder', name=u'c', parent=root)
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   119
            cnx.commit()
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   120
            cnx.execute('DELETE Folder F WHERE F name "a"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   121
            cnx.execute('DELETE F parent R WHERE R name "root"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   122
            cnx.commit()
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   123
            self.assertEqual([['root'], ['c']],
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   124
                             cnx.execute('Any NF WHERE F is Folder, F name NF').rows)
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   125
            self.assertEqual([], cnx.execute('Any NF,NP WHERE F parent P, F name NF, P name NP').rows)
9548
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9275
diff changeset
   126
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9275
diff changeset
   127
    def test_composite_subject_relation_deletion(self):
9854
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   128
        with self.admin_access.repo_cnx() as cnx:
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   129
            root = cnx.create_entity('Folder', name=u'root')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   130
            a = cnx.create_entity('Folder', name=u'a')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   131
            b = cnx.create_entity('Folder', name=u'b')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   132
            c = cnx.create_entity('Folder', name=u'c')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   133
            root.cw_set(children=(a, c))
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   134
            a.cw_set(children=b)
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   135
            cnx.commit()
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   136
            cnx.execute('DELETE Folder F WHERE F name "a"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   137
            cnx.execute('DELETE R children F WHERE R name "root"')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   138
            cnx.commit()
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   139
            self.assertEqual([['root'], ['c']],
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   140
                             cnx.execute('Any NF WHERE F is Folder, F name NF').rows)
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   141
            self.assertEqual([], cnx.execute('Any NF,NP WHERE F parent P, F name NF, P name NP').rows)
9548
be001628edad [schema] fix composite deletion handling
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9275
diff changeset
   142
6964
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   143
    def test_unsatisfied_constraints(self):
9854
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   144
        with self.admin_access.repo_cnx() as cnx:
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   145
            cnx.execute('SET U in_group G WHERE G name "owners", U login "admin"')[0][0]
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   146
            with self.assertRaises(ValidationError) as cm:
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   147
                cnx.commit()
6964
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   148
        self.assertEqual(cm.exception.errors,
9854
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   149
                         {'in_group-object': u'RQLConstraint NOT O name "owners" failed'})
6964
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   150
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   151
    def test_unique_constraint(self):
9854
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   152
        with self.admin_access.repo_cnx() as cnx:
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   153
            entity = cnx.create_entity('CWGroup', name=u'trout')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   154
            cnx.commit()
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   155
            self.assertRaises(ValidationError, cnx.create_entity, 'CWGroup', name=u'trout')
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   156
            cnx.rollback()
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   157
            cnx.execute('SET X name "trout" WHERE X eid %(x)s', {'x': entity.eid})
4071c52b8fca [hookstests/integrity] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9548
diff changeset
   158
            cnx.commit()
6964
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   159
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   160
if __name__ == '__main__':
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   161
    from logilab.common.testlib import unittest_main
4813efcee2c6 [test] test extracted from unittest_hooks + new test for unique constraint check
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   162
    unittest_main()