web/test/unittest_magicsearch.py
changeset 5768 1e73a466aa69
parent 5424 8ecbcbff9777
child 6340 470d8e828fda
equal deleted inserted replaced
5766:c397819f2482 5768:1e73a466aa69
    14 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    14 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
    15 # details.
    15 # details.
    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 """Unit tests for magic_search service
    19 """Unit tests for cw.web.views.magicsearch"""
    20 
       
    21 """
       
    22 
    20 
    23 import sys
    21 import sys
    24 
    22 
    25 from logilab.common.testlib import TestCase, unittest_main
    23 from logilab.common.testlib import TestCase, unittest_main
    26 
    24 
   126         """tests the 'two words shortcut queries'"""
   124         """tests the 'two words shortcut queries'"""
   127         transform = self.proc._two_words_query
   125         transform = self.proc._two_words_query
   128         self.assertEquals(transform('CWUser', 'E'),
   126         self.assertEquals(transform('CWUser', 'E'),
   129                           ("CWUser E",))
   127                           ("CWUser E",))
   130         self.assertEquals(transform('CWUser', 'Smith'),
   128         self.assertEquals(transform('CWUser', 'Smith'),
   131                           ('CWUser C WHERE C has_text %(text)s', {'text': 'Smith'}))
   129                           ('CWUser C ORDERBY FTIRANK(C) DESC WHERE C has_text %(text)s', {'text': 'Smith'}))
   132         self.assertEquals(transform('utilisateur', 'Smith'),
   130         self.assertEquals(transform('utilisateur', 'Smith'),
   133                           ('CWUser C WHERE C has_text %(text)s', {'text': 'Smith'}))
   131                           ('CWUser C ORDERBY FTIRANK(C) DESC WHERE C has_text %(text)s', {'text': 'Smith'}))
   134         self.assertEquals(transform(u'adresse', 'Logilab'),
   132         self.assertEquals(transform(u'adresse', 'Logilab'),
   135                           ('EmailAddress E WHERE E has_text %(text)s', {'text': 'Logilab'}))
   133                           ('EmailAddress E ORDERBY FTIRANK(E) DESC WHERE E has_text %(text)s', {'text': 'Logilab'}))
   136         self.assertEquals(transform(u'adresse', 'Logi%'),
   134         self.assertEquals(transform(u'adresse', 'Logi%'),
   137                           ('EmailAddress E WHERE E alias LIKE %(text)s', {'text': 'Logi%'}))
   135                           ('EmailAddress E WHERE E alias LIKE %(text)s', {'text': 'Logi%'}))
   138         self.assertRaises(BadRQLQuery, transform, "pers", "taratata")
   136         self.assertRaises(BadRQLQuery, transform, "pers", "taratata")
   139 
   137 
   140     def test_three_words_query(self):
   138     def test_three_words_query(self):
   150                           ('EmailAddress E WHERE E alias %(text)s', {'text': 'cubicweb'}))
   148                           ('EmailAddress E WHERE E alias %(text)s', {'text': 'cubicweb'}))
   151         self.assertEquals(transform('utilisateur', u'prénom', 'cubicweb%'),
   149         self.assertEquals(transform('utilisateur', u'prénom', 'cubicweb%'),
   152                           ('CWUser C WHERE C firstname LIKE %(text)s', {'text': 'cubicweb%'}))
   150                           ('CWUser C WHERE C firstname LIKE %(text)s', {'text': 'cubicweb%'}))
   153         # expanded shortcuts
   151         # expanded shortcuts
   154         self.assertEquals(transform('CWUser', 'use_email', 'Logilab'),
   152         self.assertEquals(transform('CWUser', 'use_email', 'Logilab'),
   155                           ('CWUser C WHERE C use_email C1, C1 has_text %(text)s', {'text': 'Logilab'}))
   153                           ('CWUser C ORDERBY FTIRANK(C1) DESC WHERE C use_email C1, C1 has_text %(text)s', {'text': 'Logilab'}))
   156         self.assertEquals(transform('CWUser', 'use_email', '%Logilab'),
   154         self.assertEquals(transform('CWUser', 'use_email', '%Logilab'),
   157                           ('CWUser C WHERE C use_email C1, C1 alias LIKE %(text)s', {'text': '%Logilab'}))
   155                           ('CWUser C WHERE C use_email C1, C1 alias LIKE %(text)s', {'text': '%Logilab'}))
   158         self.assertRaises(BadRQLQuery, transform, 'word1', 'word2', 'word3')
   156         self.assertRaises(BadRQLQuery, transform, 'word1', 'word2', 'word3')
   159 
   157 
   160     def test_quoted_queries(self):
   158     def test_quoted_queries(self):
   161         """tests how quoted queries are handled"""
   159         """tests how quoted queries are handled"""
   162         queries = [
   160         queries = [
   163             (u'Adresse "My own EmailAddress"', ('EmailAddress E WHERE E has_text %(text)s', {'text': u'My own EmailAddress'})),
   161             (u'Adresse "My own EmailAddress"', ('EmailAddress E ORDERBY FTIRANK(E) DESC WHERE E has_text %(text)s', {'text': u'My own EmailAddress'})),
   164             (u'Utilisateur prénom "Jean Paul"', ('CWUser C WHERE C firstname %(text)s', {'text': 'Jean Paul'})),
   162             (u'Utilisateur prénom "Jean Paul"', ('CWUser C WHERE C firstname %(text)s', {'text': 'Jean Paul'})),
   165             (u'Utilisateur firstname "Jean Paul"', ('CWUser C WHERE C firstname %(text)s', {'text': 'Jean Paul'})),
   163             (u'Utilisateur firstname "Jean Paul"', ('CWUser C WHERE C firstname %(text)s', {'text': 'Jean Paul'})),
   166             (u'CWUser firstname "Jean Paul"', ('CWUser C WHERE C firstname %(text)s', {'text': 'Jean Paul'})),
   164             (u'CWUser firstname "Jean Paul"', ('CWUser C WHERE C firstname %(text)s', {'text': 'Jean Paul'})),
   167             ]
   165             ]
   168         transform = self.proc._quoted_words_query
   166         transform = self.proc._quoted_words_query
   175     def test_process_query(self):
   173     def test_process_query(self):
   176         """tests how queries are processed"""
   174         """tests how queries are processed"""
   177         queries = [
   175         queries = [
   178             (u'Utilisateur', (u"CWUser C",)),
   176             (u'Utilisateur', (u"CWUser C",)),
   179             (u'Utilisateur P', (u"CWUser P",)),
   177             (u'Utilisateur P', (u"CWUser P",)),
   180             (u'Utilisateur cubicweb', (u'CWUser C WHERE C has_text %(text)s', {'text': u'cubicweb'})),
   178             (u'Utilisateur cubicweb', (u'CWUser C ORDERBY FTIRANK(C) DESC WHERE C has_text %(text)s', {'text': u'cubicweb'})),
   181             (u'CWUser prénom cubicweb', (u'CWUser C WHERE C firstname %(text)s', {'text': 'cubicweb'},)),
   179             (u'CWUser prénom cubicweb', (u'CWUser C WHERE C firstname %(text)s', {'text': 'cubicweb'},)),
   182             ]
   180             ]
   183         for query, expected in queries:
   181         for query, expected in queries:
   184             self.assertEquals(self.proc.preprocess_query(query), expected)
   182             self.assertEquals(self.proc.preprocess_query(query), expected)
   185         self.assertRaises(BadRQLQuery,
   183         self.assertRaises(BadRQLQuery,
   201 
   199 
   202     def test_main_preprocessor_chain(self):
   200     def test_main_preprocessor_chain(self):
   203         """tests QUERY_PROCESSOR"""
   201         """tests QUERY_PROCESSOR"""
   204         queries = [
   202         queries = [
   205             (u'foo',
   203             (u'foo',
   206              ("Any X WHERE X has_text %(text)s", {'text': u'foo'})),
   204              ("Any X ORDERBY FTIRANK(X) DESC WHERE X has_text %(text)s", {'text': u'foo'})),
   207             # XXX this sounds like a language translator test...
   205             # XXX this sounds like a language translator test...
   208             # and it fails
   206             # and it fails
   209             (u'Utilisateur Smith',
   207             (u'Utilisateur Smith',
   210              ('CWUser C WHERE C has_text %(text)s', {'text': u'Smith'})),
   208              ('CWUser C ORDERBY FTIRANK(C) DESC WHERE C has_text %(text)s', {'text': u'Smith'})),
   211             (u'utilisateur nom Smith',
   209             (u'utilisateur nom Smith',
   212              ('CWUser C WHERE C surname %(text)s', {'text': u'Smith'})),
   210              ('CWUser C WHERE C surname %(text)s', {'text': u'Smith'})),
   213             (u'Any P WHERE P is Utilisateur, P nom "Smith"',
   211             (u'Any P WHERE P is Utilisateur, P nom "Smith"',
   214              ('Any P WHERE P is CWUser, P surname "Smith"', None)),
   212              ('Any P WHERE P is CWUser, P surname "Smith"', None)),
   215             ]
   213             ]
   216         for query, expected in queries:
   214         for query, expected in queries:
   217             rset = self.proc.process_query(query)
   215             rset = self.proc.process_query(query)
   218             self.assertEquals((rset.rql, rset.args), expected)
   216             self.assertEquals((rset.rql, rset.args), expected)
   219 
   217 
   220     def test_iso88591_fulltext(self):
   218     def test_accentuated_fulltext(self):
   221         """we must be able to type accentuated characters in the search field"""
   219         """we must be able to type accentuated characters in the search field"""
   222         rset = self.proc.process_query(u'écrire')
   220         rset = self.proc.process_query(u'écrire')
   223         self.assertEquals(rset.rql, "Any X WHERE X has_text %(text)s")
   221         self.assertEquals(rset.rql, "Any X ORDERBY FTIRANK(X) DESC WHERE X has_text %(text)s")
   224         self.assertEquals(rset.args, {'text': u'écrire'})
   222         self.assertEquals(rset.args, {'text': u'écrire'})
   225 
   223 
   226     def test_explicit_component(self):
   224     def test_explicit_component(self):
   227         self.assertRaises(RQLSyntaxError,
   225         self.assertRaises(RQLSyntaxError,
   228                           self.proc.process_query, u'rql: CWUser E WHERE E noattr "Smith",')
   226                           self.proc.process_query, u'rql: CWUser E WHERE E noattr "Smith",')
   229         self.assertRaises(BadRQLQuery,
   227         self.assertRaises(BadRQLQuery,
   230                           self.proc.process_query, u'rql: CWUser E WHERE E noattr "Smith"')
   228                           self.proc.process_query, u'rql: CWUser E WHERE E noattr "Smith"')
   231         rset = self.proc.process_query(u'text: utilisateur Smith')
   229         rset = self.proc.process_query(u'text: utilisateur Smith')
   232         self.assertEquals(rset.rql, 'Any X WHERE X has_text %(text)s')
   230         self.assertEquals(rset.rql, 'Any X ORDERBY FTIRANK(X) DESC WHERE X has_text %(text)s')
   233         self.assertEquals(rset.args, {'text': u'utilisateur Smith'})
   231         self.assertEquals(rset.args, {'text': u'utilisateur Smith'})
   234 
   232 
   235 if __name__ == '__main__':
   233 if __name__ == '__main__':
   236     unittest_main()
   234     unittest_main()