web/test/unittest_magicsearch.py
changeset 2773 b2530e3e0afb
parent 2661 f8df42c9da6b
child 3369 7b88d12b4ee2
equal deleted inserted replaced
2767:58c519e5a31f 2773:b2530e3e0afb
    11 
    11 
    12 from logilab.common.testlib import TestCase, unittest_main
    12 from logilab.common.testlib import TestCase, unittest_main
    13 
    13 
    14 from rql import BadRQLQuery, RQLSyntaxError
    14 from rql import BadRQLQuery, RQLSyntaxError
    15 
    15 
    16 from cubicweb.devtools.apptest import EnvBasedTC, TestEnvironment
    16 from cubicweb.devtools.testlib import CubicWebTC
    17 
    17 
    18 
    18 
    19 translations = {
    19 translations = {
    20     u'CWUser' : u"Utilisateur",
    20     u'CWUser' : u"Utilisateur",
    21 #    u'Workcase' : u"Affaire",
       
    22     u'EmailAddress' : u"Adresse",
    21     u'EmailAddress' : u"Adresse",
    23 #    u'Division' : u"Division",
       
    24 #    u'Comment' : u"Commentaire",
       
    25     u'name' : u"nom",
    22     u'name' : u"nom",
    26     u'alias' : u"nom",
    23     u'alias' : u"nom",
    27     u'surname' : u"nom",
    24     u'surname' : u"nom",
    28     u'firstname' : u"prénom",
    25     u'firstname' : u"prénom",
    29     u'state' : u"état",
    26     u'state' : u"état",
    30 #    u'subject' : u"sujet",
       
    31     u'address' : u"adresse",
    27     u'address' : u"adresse",
    32     u'use_email' : u"adel",
    28     u'use_email' : u"adel",
    33     }
    29     }
    34 def _translate(msgid):
    30 def _translate(msgid):
    35     return translations.get(msgid, msgid)
    31     return translations.get(msgid, msgid)
    36 
    32 
    37 
    33 
    38 from cubicweb.web.views.magicsearch import translate_rql_tree, QSPreProcessor, QueryTranslator
    34 from cubicweb.web.views.magicsearch import translate_rql_tree, QSPreProcessor, QueryTranslator
    39 
    35 
    40 class QueryTranslatorTC(EnvBasedTC):
    36 class QueryTranslatorTC(CubicWebTC):
    41     """test suite for QueryTranslatorTC"""
    37     """test suite for QueryTranslatorTC"""
    42 
    38 
    43     def setUp(self):
    39     def setUp(self):
    44         super(QueryTranslatorTC, self).setUp()
    40         super(QueryTranslatorTC, self).setUp()
    45         self.req = self.env.create_request()
    41         self.req = self.request()
    46         self.vreg.config.translations = {'en': _translate}
    42         self.vreg.config.translations = {'en': _translate}
    47         proc = self.vreg['components'].select('magicsearch', self.req)
    43         proc = self.vreg['components'].select('magicsearch', self.req)
    48         self.proc = [p for p in proc.processors if isinstance(p, QueryTranslator)][0]
    44         self.proc = [p for p in proc.processors if isinstance(p, QueryTranslator)][0]
    49 
    45 
    50     def test_basic_translations(self):
    46     def test_basic_translations(self):
    61         rql = "Any P WHERE P is Utilisateur, P adel C, P nom 'Smith'"
    57         rql = "Any P WHERE P is Utilisateur, P adel C, P nom 'Smith'"
    62         rql, = self.proc.preprocess_query(rql, self.req)
    58         rql, = self.proc.preprocess_query(rql, self.req)
    63         self.assertEquals(rql, "Any P WHERE P is CWUser, P use_email C, P surname 'Smith'")
    59         self.assertEquals(rql, "Any P WHERE P is CWUser, P use_email C, P surname 'Smith'")
    64 
    60 
    65 
    61 
    66 class QSPreProcessorTC(EnvBasedTC):
    62 class QSPreProcessorTC(CubicWebTC):
    67     """test suite for QSPreProcessor"""
    63     """test suite for QSPreProcessor"""
    68     def setUp(self):
    64     def setUp(self):
    69         super(QSPreProcessorTC, self).setUp()
    65         super(QSPreProcessorTC, self).setUp()
    70         self.vreg.config.translations = {'en': _translate}
    66         self.vreg.config.translations = {'en': _translate}
    71         self.req = self.request()
    67         self.req = self.request()
    86         """tests QSPreProcessor._get_attribute_name"""
    82         """tests QSPreProcessor._get_attribute_name"""
    87         translate = self.proc._get_attribute_name
    83         translate = self.proc._get_attribute_name
    88         eschema = self.schema.eschema('CWUser')
    84         eschema = self.schema.eschema('CWUser')
    89         self.assertEquals(translate(u'prénom', eschema), "firstname")
    85         self.assertEquals(translate(u'prénom', eschema), "firstname")
    90         self.assertEquals(translate(u'nom', eschema), 'surname')
    86         self.assertEquals(translate(u'nom', eschema), 'surname')
    91         #self.assert_(translate(u'nom') in ('name', 'surname'))
       
    92         eschema = self.schema.eschema('EmailAddress')
    87         eschema = self.schema.eschema('EmailAddress')
    93         self.assertEquals(translate(u'adresse', eschema), "address")
    88         self.assertEquals(translate(u'adresse', eschema), "address")
    94         self.assertEquals(translate(u'nom', eschema), 'alias')
    89         self.assertEquals(translate(u'nom', eschema), 'alias')
    95         # should fail if the name is not an attribute for the given entity schema
    90         # should fail if the name is not an attribute for the given entity schema
    96         self.assertRaises(BadRQLQuery, translate, 'whatever', eschema)
    91         self.assertRaises(BadRQLQuery, translate, 'whatever', eschema)
   123         self.assertEquals(transform(u'adresse', 'Logilab'),
   118         self.assertEquals(transform(u'adresse', 'Logilab'),
   124                           ('EmailAddress E WHERE E has_text %(text)s', {'text': 'Logilab'}))
   119                           ('EmailAddress E WHERE E has_text %(text)s', {'text': 'Logilab'}))
   125         self.assertEquals(transform(u'adresse', 'Logi%'),
   120         self.assertEquals(transform(u'adresse', 'Logi%'),
   126                           ('EmailAddress E WHERE E alias LIKE %(text)s', {'text': 'Logi%'}))
   121                           ('EmailAddress E WHERE E alias LIKE %(text)s', {'text': 'Logi%'}))
   127         self.assertRaises(BadRQLQuery, transform, "pers", "taratata")
   122         self.assertRaises(BadRQLQuery, transform, "pers", "taratata")
   128         #self.assertEquals(transform('CWUser', '%mi'), 'CWUser E WHERE P surname LIKE "%mi"')
       
   129 
   123 
   130     def test_three_words_query(self):
   124     def test_three_words_query(self):
   131         """tests the 'three words shortcut queries'"""
   125         """tests the 'three words shortcut queries'"""
   132         transform = self.proc._three_words_query
   126         transform = self.proc._three_words_query
   133         self.assertEquals(transform('utilisateur', u'prénom', 'cubicweb'),
   127         self.assertEquals(transform('utilisateur', u'prénom', 'cubicweb'),
   178 
   172 
   179 
   173 
   180 ## Processor Chains tests ############################################
   174 ## Processor Chains tests ############################################
   181 
   175 
   182 
   176 
   183 class ProcessorChainTC(EnvBasedTC):
   177 class ProcessorChainTC(CubicWebTC):
   184     """test suite for magic_search's processor chains"""
   178     """test suite for magic_search's processor chains"""
   185 
   179 
   186     def setUp(self):
   180     def setUp(self):
   187         super(ProcessorChainTC, self).setUp()
   181         super(ProcessorChainTC, self).setUp()
   188         self.vreg.config.translations = {'en': _translate}
   182         self.vreg.config.translations = {'en': _translate}
   193         """tests QUERY_PROCESSOR"""
   187         """tests QUERY_PROCESSOR"""
   194         queries = [
   188         queries = [
   195             (u'foo',
   189             (u'foo',
   196              ("Any X WHERE X has_text %(text)s", {'text': u'foo'})),
   190              ("Any X WHERE X has_text %(text)s", {'text': u'foo'})),
   197             # XXX this sounds like a language translator test...
   191             # XXX this sounds like a language translator test...
   198             # and it fail
   192             # and it fails
   199             (u'Utilisateur Smith',
   193             (u'Utilisateur Smith',
   200              ('CWUser C WHERE C has_text %(text)s', {'text': u'Smith'})),
   194              ('CWUser C WHERE C has_text %(text)s', {'text': u'Smith'})),
   201             (u'utilisateur nom Smith',
   195             (u'utilisateur nom Smith',
   202              ('CWUser C WHERE C surname %(text)s', {'text': u'Smith'})),
   196              ('CWUser C WHERE C surname %(text)s', {'text': u'Smith'})),
   203             (u'Any P WHERE P is Utilisateur, P nom "Smith"',
   197             (u'Any P WHERE P is Utilisateur, P nom "Smith"',