hooks/test/unittest_integrity.py
changeset 6964 4813efcee2c6
child 7791 31bb51ea5485
equal deleted inserted replaced
6963:5774d4ba4306 6964:4813efcee2c6
       
     1 # -*- coding: utf-8 -*-
       
     2 # copyright 2003-2011 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 __future__ import with_statement
       
    22 
       
    23 from cubicweb import ValidationError
       
    24 from cubicweb.devtools.testlib import CubicWebTC
       
    25 
       
    26 class CoreHooksTC(CubicWebTC):
       
    27 
       
    28     def test_delete_internal_entities(self):
       
    29         self.assertRaises(ValidationError, self.execute,
       
    30                           'DELETE CWEType X WHERE X name "CWEType"')
       
    31         self.assertRaises(ValidationError, self.execute,
       
    32                           'DELETE CWRType X WHERE X name "relation_type"')
       
    33         self.assertRaises(ValidationError, self.execute,
       
    34                           'DELETE CWGroup X WHERE X name "owners"')
       
    35 
       
    36     def test_delete_required_relations_subject(self):
       
    37         self.execute('INSERT CWUser X: X login "toto", X upassword "hop", X in_group Y '
       
    38                      'WHERE Y name "users"')
       
    39         self.commit()
       
    40         self.execute('DELETE X in_group Y WHERE X login "toto", Y name "users"')
       
    41         self.assertRaises(ValidationError, self.commit)
       
    42         self.execute('DELETE X in_group Y WHERE X login "toto"')
       
    43         self.execute('SET X in_group Y WHERE X login "toto", Y name "guests"')
       
    44         self.commit()
       
    45 
       
    46     def test_delete_required_relations_object(self):
       
    47         self.skipTest('no sample in the schema ! YAGNI ? Kermaat ?')
       
    48 
       
    49     def test_static_vocabulary_check(self):
       
    50         self.assertRaises(ValidationError,
       
    51                           self.execute,
       
    52                           'SET X composite "whatever" WHERE X from_entity FE, FE name "CWUser", X relation_type RT, RT name "in_group"')
       
    53 
       
    54     def test_missing_required_relations_subject_inline(self):
       
    55         # missing in_group relation
       
    56         self.execute('INSERT CWUser X: X login "toto", X upassword "hop"')
       
    57         self.assertRaises(ValidationError,
       
    58                           self.commit)
       
    59 
       
    60     def test_composite_1(self):
       
    61         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
       
    62         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
       
    63         self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P '
       
    64                      'WHERE Y is EmailAddress, P is EmailPart')
       
    65         self.failUnless(self.execute('Email X WHERE X sender Y'))
       
    66         self.commit()
       
    67         self.execute('DELETE Email X')
       
    68         rset = self.execute('Any X WHERE X is EmailPart')
       
    69         self.assertEqual(len(rset), 1)
       
    70         self.commit()
       
    71         rset = self.execute('Any X WHERE X is EmailPart')
       
    72         self.assertEqual(len(rset), 0)
       
    73 
       
    74     def test_composite_2(self):
       
    75         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
       
    76         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
       
    77         self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P '
       
    78                      'WHERE Y is EmailAddress, P is EmailPart')
       
    79         self.commit()
       
    80         self.execute('DELETE Email X')
       
    81         self.execute('DELETE EmailPart X')
       
    82         self.commit()
       
    83         rset = self.execute('Any X WHERE X is EmailPart')
       
    84         self.assertEqual(len(rset), 0)
       
    85 
       
    86     def test_composite_redirection(self):
       
    87         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
       
    88         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
       
    89         self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P '
       
    90                      'WHERE Y is EmailAddress, P is EmailPart')
       
    91         self.execute('INSERT Email X: X messageid "<2345>", X subject "test2", X sender Y, X recipients Y '
       
    92                      'WHERE Y is EmailAddress')
       
    93         self.commit()
       
    94         self.execute('DELETE X parts Y WHERE X messageid "<1234>"')
       
    95         self.execute('SET X parts Y WHERE X messageid "<2345>"')
       
    96         self.commit()
       
    97         rset = self.execute('Any X WHERE X is EmailPart')
       
    98         self.assertEqual(len(rset), 1)
       
    99         self.assertEqual(rset.get_entity(0, 0).reverse_parts[0].messageid, '<2345>')
       
   100 
       
   101     def test_unsatisfied_constraints(self):
       
   102         releid = self.execute('SET U in_group G WHERE G name "owners", U login "admin"')[0][0]
       
   103         with self.assertRaises(ValidationError) as cm:
       
   104             self.commit()
       
   105         self.assertEqual(cm.exception.errors,
       
   106                           {'in_group-object': u'RQLConstraint NOT O name "owners" failed'})
       
   107 
       
   108     def test_unique_constraint(self):
       
   109         req = self.request()
       
   110         entity = req.create_entity('CWGroup', name=u'trout')
       
   111         self.commit()
       
   112         self.assertRaises(ValidationError, req.create_entity, 'CWGroup', name=u'trout')
       
   113         self.rollback()
       
   114         req.execute('SET X name "trout" WHERE X eid %(x)s', {'x': entity.eid})
       
   115         self.commit()
       
   116 
       
   117 if __name__ == '__main__':
       
   118     from logilab.common.testlib import unittest_main
       
   119     unittest_main()