author | Florent Cayré <florent.cayre@gmail.com> |
Thu, 15 Sep 2011 16:40:52 +0200 | |
changeset 7794 | aed065b97f12 |
parent 6340 | 470d8e828fda |
child 8505 | dcd9bc1d1bca |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
2 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
3 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
4 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
5 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
6 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
7 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
8 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
9 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
10 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
11 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
12 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
13 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
14 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
15 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
16 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
17 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
18 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
19 |
"""Unit tests for cw.web.views.magicsearch""" |
0 | 20 |
|
21 |
import sys |
|
22 |
||
23 |
from logilab.common.testlib import TestCase, unittest_main |
|
24 |
||
25 |
from rql import BadRQLQuery, RQLSyntaxError |
|
26 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2661
diff
changeset
|
27 |
from cubicweb.devtools.testlib import CubicWebTC |
0 | 28 |
|
29 |
||
30 |
translations = { |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
31 |
u'CWUser' : u"Utilisateur", |
0 | 32 |
u'EmailAddress' : u"Adresse", |
33 |
u'name' : u"nom", |
|
34 |
u'alias' : u"nom", |
|
35 |
u'surname' : u"nom", |
|
36 |
u'firstname' : u"prénom", |
|
37 |
u'state' : u"état", |
|
38 |
u'address' : u"adresse", |
|
39 |
u'use_email' : u"adel", |
|
40 |
} |
|
3362
2a2dcfb379a0
[magicsearch] update to match new i18n API: config.translations[lang] now returns a couple of function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2661
diff
changeset
|
41 |
|
0 | 42 |
def _translate(msgid): |
43 |
return translations.get(msgid, msgid) |
|
44 |
||
3362
2a2dcfb379a0
[magicsearch] update to match new i18n API: config.translations[lang] now returns a couple of function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2661
diff
changeset
|
45 |
def _ctxtranslate(ctx, msgid): |
2a2dcfb379a0
[magicsearch] update to match new i18n API: config.translations[lang] now returns a couple of function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2661
diff
changeset
|
46 |
return _translate(msgid) |
0 | 47 |
|
48 |
from cubicweb.web.views.magicsearch import translate_rql_tree, QSPreProcessor, QueryTranslator |
|
49 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2661
diff
changeset
|
50 |
class QueryTranslatorTC(CubicWebTC): |
0 | 51 |
"""test suite for QueryTranslatorTC""" |
1654 | 52 |
|
0 | 53 |
def setUp(self): |
54 |
super(QueryTranslatorTC, self).setUp() |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2661
diff
changeset
|
55 |
self.req = self.request() |
3362
2a2dcfb379a0
[magicsearch] update to match new i18n API: config.translations[lang] now returns a couple of function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2661
diff
changeset
|
56 |
self.vreg.config.translations = {'en': (_translate, _ctxtranslate)} |
2661
f8df42c9da6b
[vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2643
diff
changeset
|
57 |
proc = self.vreg['components'].select('magicsearch', self.req) |
0 | 58 |
self.proc = [p for p in proc.processors if isinstance(p, QueryTranslator)][0] |
59 |
||
60 |
def test_basic_translations(self): |
|
61 |
"""tests basic translations (no ambiguities)""" |
|
62 |
rql = "Any C WHERE C is Adresse, P adel C, C adresse 'Logilab'" |
|
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
63 |
rql, = self.proc.preprocess_query(rql) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
64 |
self.assertEqual(rql, "Any C WHERE C is EmailAddress, P use_email C, C address 'Logilab'") |
0 | 65 |
|
66 |
def test_ambiguous_translations(self): |
|
67 |
"""tests possibly ambiguous translations""" |
|
68 |
rql = "Any P WHERE P adel C, C is EmailAddress, C nom 'Logilab'" |
|
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
69 |
rql, = self.proc.preprocess_query(rql) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
70 |
self.assertEqual(rql, "Any P WHERE P use_email C, C is EmailAddress, C alias 'Logilab'") |
0 | 71 |
rql = "Any P WHERE P is Utilisateur, P adel C, P nom 'Smith'" |
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
72 |
rql, = self.proc.preprocess_query(rql) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
73 |
self.assertEqual(rql, "Any P WHERE P is CWUser, P use_email C, P surname 'Smith'") |
0 | 74 |
|
75 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2661
diff
changeset
|
76 |
class QSPreProcessorTC(CubicWebTC): |
0 | 77 |
"""test suite for QSPreProcessor""" |
78 |
def setUp(self): |
|
79 |
super(QSPreProcessorTC, self).setUp() |
|
3362
2a2dcfb379a0
[magicsearch] update to match new i18n API: config.translations[lang] now returns a couple of function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2661
diff
changeset
|
80 |
self.vreg.config.translations = {'en': (_translate, _ctxtranslate)} |
0 | 81 |
self.req = self.request() |
2661
f8df42c9da6b
[vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2643
diff
changeset
|
82 |
proc = self.vreg['components'].select('magicsearch', self.req) |
0 | 83 |
self.proc = [p for p in proc.processors if isinstance(p, QSPreProcessor)][0] |
3462
3a79fecdd2b4
[magicsearch] make tests pass again: base preprocessor must have access to vreg
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
84 |
self.proc._cw = self.req |
0 | 85 |
|
86 |
def test_entity_translation(self): |
|
87 |
"""tests QSPreProcessor._get_entity_name()""" |
|
88 |
translate = self.proc._get_entity_type |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
89 |
self.assertEqual(translate(u'EmailAddress'), "EmailAddress") |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
90 |
self.assertEqual(translate(u'emailaddress'), "EmailAddress") |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
91 |
self.assertEqual(translate(u'Adresse'), "EmailAddress") |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
92 |
self.assertEqual(translate(u'adresse'), "EmailAddress") |
0 | 93 |
self.assertRaises(BadRQLQuery, translate, 'whatever') |
94 |
||
95 |
def test_attribute_translation(self): |
|
96 |
"""tests QSPreProcessor._get_attribute_name""" |
|
97 |
translate = self.proc._get_attribute_name |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
98 |
eschema = self.schema.eschema('CWUser') |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
99 |
self.assertEqual(translate(u'prénom', eschema), "firstname") |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
100 |
self.assertEqual(translate(u'nom', eschema), 'surname') |
1654 | 101 |
eschema = self.schema.eschema('EmailAddress') |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
102 |
self.assertEqual(translate(u'adresse', eschema), "address") |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
103 |
self.assertEqual(translate(u'nom', eschema), 'alias') |
0 | 104 |
# should fail if the name is not an attribute for the given entity schema |
105 |
self.assertRaises(BadRQLQuery, translate, 'whatever', eschema) |
|
106 |
self.assertRaises(BadRQLQuery, translate, 'prénom', eschema) |
|
107 |
||
108 |
def test_one_word_query(self): |
|
109 |
"""tests the 'one word shortcut queries'""" |
|
110 |
transform = self.proc._one_word_query |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
111 |
self.assertEqual(transform('123'), |
0 | 112 |
('Any X WHERE X eid %(x)s', {'x': 123}, 'x')) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
113 |
self.assertEqual(transform('CWUser'), |
1654 | 114 |
('CWUser C',)) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
115 |
self.assertEqual(transform('Utilisateur'), |
1654 | 116 |
('CWUser C',)) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
117 |
self.assertEqual(transform('Adresse'), |
0 | 118 |
('EmailAddress E',)) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
119 |
self.assertEqual(transform('adresse'), |
0 | 120 |
('EmailAddress E',)) |
121 |
self.assertRaises(BadRQLQuery, transform, 'Workcases') |
|
122 |
||
123 |
def test_two_words_query(self): |
|
124 |
"""tests the 'two words shortcut queries'""" |
|
125 |
transform = self.proc._two_words_query |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
126 |
self.assertEqual(transform('CWUser', 'E'), |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
127 |
("CWUser E",)) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
128 |
self.assertEqual(transform('CWUser', 'Smith'), |
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
129 |
('CWUser C ORDERBY FTIRANK(C) DESC WHERE C has_text %(text)s', {'text': 'Smith'})) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
130 |
self.assertEqual(transform('utilisateur', 'Smith'), |
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
131 |
('CWUser C ORDERBY FTIRANK(C) DESC WHERE C has_text %(text)s', {'text': 'Smith'})) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
132 |
self.assertEqual(transform(u'adresse', 'Logilab'), |
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
133 |
('EmailAddress E ORDERBY FTIRANK(E) DESC WHERE E has_text %(text)s', {'text': 'Logilab'})) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
134 |
self.assertEqual(transform(u'adresse', 'Logi%'), |
0 | 135 |
('EmailAddress E WHERE E alias LIKE %(text)s', {'text': 'Logi%'})) |
136 |
self.assertRaises(BadRQLQuery, transform, "pers", "taratata") |
|
137 |
||
138 |
def test_three_words_query(self): |
|
139 |
"""tests the 'three words shortcut queries'""" |
|
140 |
transform = self.proc._three_words_query |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
141 |
self.assertEqual(transform('utilisateur', u'prénom', 'cubicweb'), |
1654 | 142 |
('CWUser C WHERE C firstname %(text)s', {'text': 'cubicweb'})) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
143 |
self.assertEqual(transform('utilisateur', 'nom', 'cubicweb'), |
1654 | 144 |
('CWUser C WHERE C surname %(text)s', {'text': 'cubicweb'})) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
145 |
self.assertEqual(transform(u'adresse', 'nom', 'cubicweb'), |
0 | 146 |
('EmailAddress E WHERE E alias %(text)s', {'text': 'cubicweb'})) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
147 |
self.assertEqual(transform('EmailAddress', 'nom', 'cubicweb'), |
1654 | 148 |
('EmailAddress E WHERE E alias %(text)s', {'text': 'cubicweb'})) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
149 |
self.assertEqual(transform('utilisateur', u'prénom', 'cubicweb%'), |
1654 | 150 |
('CWUser C WHERE C firstname LIKE %(text)s', {'text': 'cubicweb%'})) |
0 | 151 |
# expanded shortcuts |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
152 |
self.assertEqual(transform('CWUser', 'use_email', 'Logilab'), |
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
153 |
('CWUser C ORDERBY FTIRANK(C1) DESC WHERE C use_email C1, C1 has_text %(text)s', {'text': 'Logilab'})) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
154 |
self.assertEqual(transform('CWUser', 'use_email', '%Logilab'), |
1654 | 155 |
('CWUser C WHERE C use_email C1, C1 alias LIKE %(text)s', {'text': '%Logilab'})) |
0 | 156 |
self.assertRaises(BadRQLQuery, transform, 'word1', 'word2', 'word3') |
1654 | 157 |
|
0 | 158 |
def test_quoted_queries(self): |
159 |
"""tests how quoted queries are handled""" |
|
160 |
queries = [ |
|
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
161 |
(u'Adresse "My own EmailAddress"', ('EmailAddress E ORDERBY FTIRANK(E) DESC WHERE E has_text %(text)s', {'text': u'My own EmailAddress'})), |
1654 | 162 |
(u'Utilisateur prénom "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'})), |
|
164 |
(u'CWUser firstname "Jean Paul"', ('CWUser C WHERE C firstname %(text)s', {'text': 'Jean Paul'})), |
|
0 | 165 |
] |
166 |
transform = self.proc._quoted_words_query |
|
167 |
for query, expected in queries: |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
168 |
self.assertEqual(transform(query), expected) |
0 | 169 |
self.assertRaises(BadRQLQuery, transform, "unquoted rql") |
170 |
self.assertRaises(BadRQLQuery, transform, 'pers "Jean Paul"') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
171 |
self.assertRaises(BadRQLQuery, transform, 'CWUser firstname other "Jean Paul"') |
1654 | 172 |
|
0 | 173 |
def test_process_query(self): |
174 |
"""tests how queries are processed""" |
|
175 |
queries = [ |
|
1654 | 176 |
(u'Utilisateur', (u"CWUser C",)), |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
177 |
(u'Utilisateur P', (u"CWUser P",)), |
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
178 |
(u'Utilisateur cubicweb', (u'CWUser C ORDERBY FTIRANK(C) DESC WHERE C has_text %(text)s', {'text': u'cubicweb'})), |
1654 | 179 |
(u'CWUser prénom cubicweb', (u'CWUser C WHERE C firstname %(text)s', {'text': 'cubicweb'},)), |
0 | 180 |
] |
181 |
for query, expected in queries: |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
182 |
self.assertEqual(self.proc.preprocess_query(query), expected) |
2643
9976f511003b
web test update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2637
diff
changeset
|
183 |
self.assertRaises(BadRQLQuery, |
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
184 |
self.proc.preprocess_query, 'Any X WHERE X is Something') |
1654 | 185 |
|
0 | 186 |
|
187 |
||
188 |
## Processor Chains tests ############################################ |
|
1654 | 189 |
|
0 | 190 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2661
diff
changeset
|
191 |
class ProcessorChainTC(CubicWebTC): |
0 | 192 |
"""test suite for magic_search's processor chains""" |
193 |
||
194 |
def setUp(self): |
|
195 |
super(ProcessorChainTC, self).setUp() |
|
3362
2a2dcfb379a0
[magicsearch] update to match new i18n API: config.translations[lang] now returns a couple of function
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2661
diff
changeset
|
196 |
self.vreg.config.translations = {'en': (_translate, _ctxtranslate)} |
0 | 197 |
self.req = self.request() |
2661
f8df42c9da6b
[vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2643
diff
changeset
|
198 |
self.proc = self.vreg['components'].select('magicsearch', self.req) |
0 | 199 |
|
200 |
def test_main_preprocessor_chain(self): |
|
201 |
"""tests QUERY_PROCESSOR""" |
|
202 |
queries = [ |
|
203 |
(u'foo', |
|
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
204 |
("Any X ORDERBY FTIRANK(X) DESC WHERE X has_text %(text)s", {'text': u'foo'})), |
0 | 205 |
# XXX this sounds like a language translator test... |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2661
diff
changeset
|
206 |
# and it fails |
0 | 207 |
(u'Utilisateur Smith', |
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
208 |
('CWUser C ORDERBY FTIRANK(C) DESC WHERE C has_text %(text)s', {'text': u'Smith'})), |
0 | 209 |
(u'utilisateur nom Smith', |
1654 | 210 |
('CWUser C WHERE C surname %(text)s', {'text': u'Smith'})), |
0 | 211 |
(u'Any P WHERE P is Utilisateur, P nom "Smith"', |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
212 |
('Any P WHERE P is CWUser, P surname "Smith"', None)), |
0 | 213 |
] |
214 |
for query, expected in queries: |
|
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
215 |
rset = self.proc.process_query(query) |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
216 |
self.assertEqual((rset.rql, rset.args), expected) |
0 | 217 |
|
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
218 |
def test_accentuated_fulltext(self): |
0 | 219 |
"""we must be able to type accentuated characters in the search field""" |
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
220 |
rset = self.proc.process_query(u'écrire') |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
221 |
self.assertEqual(rset.rql, "Any X ORDERBY FTIRANK(X) DESC WHERE X has_text %(text)s") |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
222 |
self.assertEqual(rset.args, {'text': u'écrire'}) |
0 | 223 |
|
224 |
def test_explicit_component(self): |
|
225 |
self.assertRaises(RQLSyntaxError, |
|
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
226 |
self.proc.process_query, u'rql: CWUser E WHERE E noattr "Smith",') |
0 | 227 |
self.assertRaises(BadRQLQuery, |
3469
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
228 |
self.proc.process_query, u'rql: CWUser E WHERE E noattr "Smith"') |
1e28876c4b55
[magicsearch] (pre_)process_query doesn't need the req argument, instances already have access to self._cw
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3462
diff
changeset
|
229 |
rset = self.proc.process_query(u'text: utilisateur Smith') |
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
230 |
self.assertEqual(rset.rql, 'Any X ORDERBY FTIRANK(X) DESC WHERE X has_text %(text)s') |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5768
diff
changeset
|
231 |
self.assertEqual(rset.args, {'text': u'utilisateur Smith'}) |
1654 | 232 |
|
0 | 233 |
if __name__ == '__main__': |
234 |
unittest_main() |