test/unittest_toolsutils.py
changeset 9740 c0239d8ae742
equal deleted inserted replaced
9737:c6f47e635845 9740:c0239d8ae742
       
     1 # copyright 2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 
       
    19 
       
    20 from logilab.common.testlib import TestCase, unittest_main
       
    21 
       
    22 from cubicweb.toolsutils import RQLExecuteMatcher
       
    23 
       
    24 
       
    25 class RQLExecuteMatcherTests(TestCase):
       
    26     def matched_query(self, text):
       
    27         match = RQLExecuteMatcher.match(text)
       
    28         if match is None:
       
    29             return None
       
    30         return match['rql_query']
       
    31 
       
    32     def test_unknown_function_dont_match(self):
       
    33         self.assertIsNone(self.matched_query('foo'))
       
    34         self.assertIsNone(self.matched_query('rql('))
       
    35         self.assertIsNone(self.matched_query('hell("")'))
       
    36         self.assertIsNone(self.matched_query('eval("rql(\'bla\''))
       
    37 
       
    38     def test_rql_other_parameters_dont_match(self):
       
    39         self.assertIsNone(self.matched_query('rql("Any X WHERE X eid %(x)s")'))
       
    40         self.assertIsNone(self.matched_query('rql("Any X WHERE X eid %(x)s", {'))
       
    41         self.assertIsNone(self.matched_query('session.execute("Any X WHERE X eid %(x)s")'))
       
    42         self.assertIsNone(self.matched_query('session.execute("Any X WHERE X eid %(x)s", {'))
       
    43 
       
    44     def test_rql_function_match(self):
       
    45         for func_expr in ('rql', 'session.execute'):
       
    46             query = self.matched_query('%s("Any X WHERE X is ' % func_expr)
       
    47             self.assertEqual(query, 'Any X WHERE X is ')
       
    48 
       
    49     def test_offseted_rql_function_match(self):
       
    50         """check indentation is allowed"""
       
    51         for func_expr in ('  rql', '  session.execute'):
       
    52             query = self.matched_query('%s("Any X WHERE X is ' % func_expr)
       
    53             self.assertEqual(query, 'Any X WHERE X is ')
       
    54 
       
    55 
       
    56 if __name__ == '__main__':
       
    57     unittest_main()