hooks/test/unittest_hooks.py
changeset 6957 ffda12be2e9f
parent 6798 c3d5c0a215b7
child 6969 c527de85ea61
equal deleted inserted replaced
6956:b172c383dbce 6957:ffda12be2e9f
     1 # -*- coding: utf-8 -*-
     1 # -*- coding: utf-8 -*-
     2 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     4 #
     4 #
     5 # This file is part of CubicWeb.
     5 # This file is part of CubicWeb.
     6 #
     6 #
     7 # CubicWeb is free software: you can redistribute it and/or modify it under the
     7 # CubicWeb is free software: you can redistribute it and/or modify it under the
    16 #
    16 #
    17 # You should have received a copy of the GNU Lesser General Public License along
    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/>.
    18 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    19 """functional tests for core hooks
    19 """functional tests for core hooks
    20 
    20 
    21 note: most schemahooks.py hooks are actually tested in unittest_migrations.py
    21 Note:
       
    22   syncschema.py hooks are mostly tested in server/test/unittest_migrations.py
    22 """
    23 """
    23 from __future__ import with_statement
    24 from __future__ import with_statement
    24 
       
    25 from logilab.common.testlib import TestCase, unittest_main
       
    26 
    25 
    27 from datetime import datetime
    26 from datetime import datetime
    28 
    27 
    29 from cubicweb import ValidationError, AuthenticationError, BadConnectionId
    28 from cubicweb import ValidationError, AuthenticationError, BadConnectionId
    30 from cubicweb.devtools.testlib import CubicWebTC
    29 from cubicweb.devtools.testlib import CubicWebTC
    31 
    30 
    32 class CoreHooksTC(CubicWebTC):
    31 class CoreHooksTC(CubicWebTC):
    33 
       
    34     def test_delete_internal_entities(self):
       
    35         self.assertRaises(ValidationError, self.execute,
       
    36                           'DELETE CWEType X WHERE X name "CWEType"')
       
    37         self.assertRaises(ValidationError, self.execute,
       
    38                           'DELETE CWRType X WHERE X name "relation_type"')
       
    39         self.assertRaises(ValidationError, self.execute,
       
    40                           'DELETE CWGroup X WHERE X name "owners"')
       
    41 
       
    42     def test_delete_required_relations_subject(self):
       
    43         self.execute('INSERT CWUser X: X login "toto", X upassword "hop", X in_group Y '
       
    44                      'WHERE Y name "users"')
       
    45         self.commit()
       
    46         self.execute('DELETE X in_group Y WHERE X login "toto", Y name "users"')
       
    47         self.assertRaises(ValidationError, self.commit)
       
    48         self.execute('DELETE X in_group Y WHERE X login "toto"')
       
    49         self.execute('SET X in_group Y WHERE X login "toto", Y name "guests"')
       
    50         self.commit()
       
    51 
       
    52     def test_delete_required_relations_object(self):
       
    53         self.skipTest('no sample in the schema ! YAGNI ? Kermaat ?')
       
    54 
       
    55     def test_static_vocabulary_check(self):
       
    56         self.assertRaises(ValidationError,
       
    57                           self.execute,
       
    58                           'SET X composite "whatever" WHERE X from_entity FE, FE name "CWUser", X relation_type RT, RT name "in_group"')
       
    59 
       
    60     def test_missing_required_relations_subject_inline(self):
       
    61         # missing in_group relation
       
    62         self.execute('INSERT CWUser X: X login "toto", X upassword "hop"')
       
    63         self.assertRaises(ValidationError,
       
    64                           self.commit)
       
    65 
    32 
    66     def test_inlined(self):
    33     def test_inlined(self):
    67         self.assertEqual(self.repo.schema['sender'].inlined, True)
    34         self.assertEqual(self.repo.schema['sender'].inlined, True)
    68         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
    35         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
    69         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
    36         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
    70         eeid = self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P '
    37         eeid = self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P '
    71                             'WHERE Y is EmailAddress, P is EmailPart')[0][0]
    38                             'WHERE Y is EmailAddress, P is EmailPart')[0][0]
    72         self.execute('SET X sender Y WHERE X is Email, Y is EmailAddress')
    39         self.execute('SET X sender Y WHERE X is Email, Y is EmailAddress')
    73         rset = self.execute('Any S WHERE X sender S, X eid %s' % eeid)
    40         rset = self.execute('Any S WHERE X sender S, X eid %s' % eeid)
    74         self.assertEqual(len(rset), 1)
    41         self.assertEqual(len(rset), 1)
    75 
       
    76     def test_composite_1(self):
       
    77         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
       
    78         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
       
    79         self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P '
       
    80                      'WHERE Y is EmailAddress, P is EmailPart')
       
    81         self.failUnless(self.execute('Email X WHERE X sender Y'))
       
    82         self.commit()
       
    83         self.execute('DELETE Email X')
       
    84         rset = self.execute('Any X WHERE X is EmailPart')
       
    85         self.assertEqual(len(rset), 1)
       
    86         self.commit()
       
    87         rset = self.execute('Any X WHERE X is EmailPart')
       
    88         self.assertEqual(len(rset), 0)
       
    89 
       
    90     def test_composite_2(self):
       
    91         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
       
    92         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
       
    93         self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P '
       
    94                      'WHERE Y is EmailAddress, P is EmailPart')
       
    95         self.commit()
       
    96         self.execute('DELETE Email X')
       
    97         self.execute('DELETE EmailPart X')
       
    98         self.commit()
       
    99         rset = self.execute('Any X WHERE X is EmailPart')
       
   100         self.assertEqual(len(rset), 0)
       
   101 
       
   102     def test_composite_redirection(self):
       
   103         self.execute('INSERT EmailAddress X: X address "toto@logilab.fr", X alias "hop"')
       
   104         self.execute('INSERT EmailPart X: X content_format "text/plain", X ordernum 1, X content "this is a test"')
       
   105         self.execute('INSERT Email X: X messageid "<1234>", X subject "test", X sender Y, X recipients Y, X parts P '
       
   106                      'WHERE Y is EmailAddress, P is EmailPart')
       
   107         self.execute('INSERT Email X: X messageid "<2345>", X subject "test2", X sender Y, X recipients Y '
       
   108                      'WHERE Y is EmailAddress')
       
   109         self.commit()
       
   110         self.execute('DELETE X parts Y WHERE X messageid "<1234>"')
       
   111         self.execute('SET X parts Y WHERE X messageid "<2345>"')
       
   112         self.commit()
       
   113         rset = self.execute('Any X WHERE X is EmailPart')
       
   114         self.assertEqual(len(rset), 1)
       
   115         self.assertEqual(rset.get_entity(0, 0).reverse_parts[0].messageid, '<2345>')
       
   116 
       
   117     def test_unsatisfied_constraints(self):
       
   118         releid = self.execute('SET U in_group G WHERE G name "owners", U login "admin"')[0][0]
       
   119         with self.assertRaises(ValidationError) as cm:
       
   120             self.commit()
       
   121         self.assertEqual(cm.exception.errors,
       
   122                           {'in_group-object': u'RQLConstraint NOT O name "owners" failed'})
       
   123 
    42 
   124     def test_html_tidy_hook(self):
    43     def test_html_tidy_hook(self):
   125         req = self.request()
    44         req = self.request()
   126         entity = req.create_entity('Workflow', name=u'wf1', description_format=u'text/html',
    45         entity = req.create_entity('Workflow', name=u'wf1', description_format=u'text/html',
   127                                  description=u'yo')
    46                                  description=u'yo')
   269             self.assertIsInstance(ex.entity, int)
   188             self.assertIsInstance(ex.entity, int)
   270             self.assertEqual(ex.errors, {'login-subject': 'the value "admin" is already used, use another one'})
   189             self.assertEqual(ex.errors, {'login-subject': 'the value "admin" is already used, use another one'})
   271 
   190 
   272 
   191 
   273 if __name__ == '__main__':
   192 if __name__ == '__main__':
       
   193     from logilab.common.testlib import unittest_main
   274     unittest_main()
   194     unittest_main()