hooks/test/unittest_integrity.py
changeset 11057 0b59724cb3f2
parent 11052 058bb3dc685f
child 11058 23eb30449fe5
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
     1 # -*- coding: utf-8 -*-
       
     2 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     4 #
       
     5 # This file is part of CubicWeb.
       
     6 #
       
     7 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     8 # terms of the GNU Lesser General Public License as published by the Free
       
     9 # Software Foundation, either version 2.1 of the License, or (at your option)
       
    10 # any later version.
       
    11 #
       
    12 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    14 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    15 # details.
       
    16 #
       
    17 # You should have received a copy of the GNU Lesser General Public License along
       
    18 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    19 """functional tests for integrity hooks"""
       
    20 
       
    21 from cubicweb import ValidationError
       
    22 from cubicweb.devtools.testlib import CubicWebTC
       
    23 
       
    24 class CoreHooksTC(CubicWebTC):
       
    25 
       
    26     def test_delete_internal_entities(self):
       
    27         with self.admin_access.repo_cnx() as cnx:
       
    28             self.assertRaises(ValidationError, cnx.execute,
       
    29                               'DELETE CWEType X WHERE X name "CWEType"')
       
    30             cnx.rollback()
       
    31             self.assertRaises(ValidationError, cnx.execute,
       
    32                               'DELETE CWRType X WHERE X name "relation_type"')
       
    33             cnx.rollback()
       
    34             self.assertRaises(ValidationError, cnx.execute,
       
    35                               'DELETE CWGroup X WHERE X name "owners"')
       
    36 
       
    37     def test_delete_required_relations_subject(self):
       
    38         with self.admin_access.repo_cnx() as cnx:
       
    39             cnx.execute('INSERT CWUser X: X login "toto", X upassword "hop", X in_group Y '
       
    40                          'WHERE Y name "users"')
       
    41             cnx.commit()
       
    42             cnx.execute('DELETE X in_group Y WHERE X login "toto", Y name "users"')
       
    43             self.assertRaises(ValidationError, cnx.commit)
       
    44             cnx.rollback()
       
    45             cnx.execute('DELETE X in_group Y WHERE X login "toto"')
       
    46             cnx.execute('SET X in_group Y WHERE X login "toto", Y name "guests"')
       
    47             cnx.commit()
       
    48 
       
    49     def test_static_vocabulary_check(self):
       
    50         with self.admin_access.repo_cnx() as cnx:
       
    51             self.assertRaises(ValidationError,
       
    52                               cnx.execute,
       
    53                               'SET X composite "whatever" WHERE X from_entity FE, FE name "CWUser", '
       
    54                               'X relation_type RT, RT name "in_group"')
       
    55 
       
    56     def test_missing_required_relations_subject_inline(self):
       
    57         with self.admin_access.repo_cnx() as cnx:
       
    58             # missing in_group relation
       
    59             cnx.execute('INSERT CWUser X: X login "toto", X upassword "hop"')
       
    60             self.assertRaises(ValidationError, cnx.commit)
       
    61 
       
    62     def test_composite_1(self):
       
    63         with self.admin_access.repo_cnx() as cnx:
       
    64             cnx.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
       
    65             cnx.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, '
       
    66                         'X content "this is a test"')
       
    67             cnx.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, '
       
    68                         'X recipients Y, X parts P '
       
    69                          'WHERE Y is EmailAddress, P is EmailPart')
       
    70             self.assertTrue(cnx.execute('Email X WHERE X sender Y'))
       
    71             cnx.commit()
       
    72             cnx.execute('DELETE Email X')
       
    73             rset = cnx.execute('Any X WHERE X is EmailPart')
       
    74             self.assertEqual(len(rset), 0)
       
    75             cnx.commit()
       
    76             rset = cnx.execute('Any X WHERE X is EmailPart')
       
    77             self.assertEqual(len(rset), 0)
       
    78 
       
    79     def test_composite_2(self):
       
    80         with self.admin_access.repo_cnx() as cnx:
       
    81             cnx.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
       
    82             cnx.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, '
       
    83                         'X content "this is a test"')
       
    84             cnx.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, '
       
    85                         'X recipients Y, X parts P '
       
    86                          'WHERE Y is EmailAddress, P is EmailPart')
       
    87             cnx.commit()
       
    88             cnx.execute('DELETE Email X')
       
    89             cnx.execute('DELETE EmailPart X')
       
    90             cnx.commit()
       
    91             rset = cnx.execute('Any X WHERE X is EmailPart')
       
    92             self.assertEqual(len(rset), 0)
       
    93 
       
    94     def test_composite_redirection(self):
       
    95         with self.admin_access.repo_cnx() as cnx:
       
    96             cnx.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
       
    97             cnx.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, '
       
    98                         'X content "this is a test"')
       
    99             cnx.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, '
       
   100                         'X recipients Y, X parts P '
       
   101                          'WHERE Y is EmailAddress, P is EmailPart')
       
   102             cnx.execute('INSERT Email X: X messageid "<2345>", X subject "test2", X sender Y, '
       
   103                         'X recipients Y '
       
   104                          'WHERE Y is EmailAddress')
       
   105             cnx.commit()
       
   106             cnx.execute('DELETE X parts Y WHERE X messageid "<1234>"')
       
   107             cnx.execute('SET X parts Y WHERE X messageid "<2345>"')
       
   108             cnx.commit()
       
   109             rset = cnx.execute('Any X WHERE X is EmailPart')
       
   110             self.assertEqual(len(rset), 1)
       
   111             self.assertEqual(rset.get_entity(0, 0).reverse_parts[0].messageid, '<2345>')
       
   112 
       
   113     def test_composite_object_relation_deletion(self):
       
   114         with self.admin_access.repo_cnx() as cnx:
       
   115             root = cnx.create_entity('Folder', name=u'root')
       
   116             a = cnx.create_entity('Folder', name=u'a', parent=root)
       
   117             cnx.create_entity('Folder', name=u'b', parent=a)
       
   118             cnx.create_entity('Folder', name=u'c', parent=root)
       
   119             cnx.commit()
       
   120             cnx.execute('DELETE Folder F WHERE F name "a"')
       
   121             cnx.execute('DELETE F parent R WHERE R name "root"')
       
   122             cnx.commit()
       
   123             self.assertEqual([['root'], ['c']],
       
   124                              cnx.execute('Any NF WHERE F is Folder, F name NF').rows)
       
   125             self.assertEqual([], cnx.execute('Any NF,NP WHERE F parent P, F name NF, P name NP').rows)
       
   126 
       
   127     def test_composite_subject_relation_deletion(self):
       
   128         with self.admin_access.repo_cnx() as cnx:
       
   129             root = cnx.create_entity('Folder', name=u'root')
       
   130             a = cnx.create_entity('Folder', name=u'a')
       
   131             b = cnx.create_entity('Folder', name=u'b')
       
   132             c = cnx.create_entity('Folder', name=u'c')
       
   133             root.cw_set(children=(a, c))
       
   134             a.cw_set(children=b)
       
   135             cnx.commit()
       
   136             cnx.execute('DELETE Folder F WHERE F name "a"')
       
   137             cnx.execute('DELETE R children F WHERE R name "root"')
       
   138             cnx.commit()
       
   139             self.assertEqual([['root'], ['c']],
       
   140                              cnx.execute('Any NF WHERE F is Folder, F name NF').rows)
       
   141             self.assertEqual([], cnx.execute('Any NF,NP WHERE F parent P, F name NF, P name NP').rows)
       
   142 
       
   143     def test_unsatisfied_constraints(self):
       
   144         with self.admin_access.repo_cnx() as cnx:
       
   145             cnx.execute('SET U in_group G WHERE G name "owners", U login "admin"')[0][0]
       
   146             with self.assertRaises(ValidationError) as cm:
       
   147                 cnx.commit()
       
   148         self.assertEqual(cm.exception.errors,
       
   149                          {'in_group-object': u'RQLConstraint NOT O name "owners" failed'})
       
   150 
       
   151     def test_unique_constraint(self):
       
   152         with self.admin_access.repo_cnx() as cnx:
       
   153             entity = cnx.create_entity('CWGroup', name=u'trout')
       
   154             cnx.commit()
       
   155             self.assertRaises(ValidationError, cnx.create_entity, 'CWGroup', name=u'trout')
       
   156             cnx.rollback()
       
   157             cnx.execute('SET X name "trout" WHERE X eid %(x)s', {'x': entity.eid})
       
   158             cnx.commit()
       
   159 
       
   160 if __name__ == '__main__':
       
   161     from logilab.common.testlib import unittest_main
       
   162     unittest_main()