author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Wed, 07 Apr 2010 11:56:30 +0200 | |
branch | stable |
changeset 5171 | 1c2567af37dd |
parent 4252 | 6c4f109c2b03 |
child 5421 | 8167de96c523 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1654
diff
changeset
|
2 |
"""Unit tests for magic_search service |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1654
diff
changeset
|
3 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1654
diff
changeset
|
4 |
:organization: Logilab |
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3362
diff
changeset
|
5 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1654
diff
changeset
|
6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1654
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1654
diff
changeset
|
8 |
""" |
0 | 9 |
|
10 |
import sys |
|
11 |
||
12 |
from logilab.common.testlib import TestCase, unittest_main |
|
13 |
||
14 |
from rql import BadRQLQuery, RQLSyntaxError |
|
15 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2661
diff
changeset
|
16 |
from cubicweb.devtools.testlib import CubicWebTC |
0 | 17 |
|
18 |
||
19 |
translations = { |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
20 |
u'CWUser' : u"Utilisateur", |
0 | 21 |
u'EmailAddress' : u"Adresse", |
22 |
u'name' : u"nom", |
|
23 |
u'alias' : u"nom", |
|
24 |
u'surname' : u"nom", |
|
25 |
u'firstname' : u"prénom", |
|
26 |
u'state' : u"état", |
|
27 |
u'address' : u"adresse", |
|
28 |
u'use_email' : u"adel", |
|
29 |
} |
|
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
|
30 |
|
0 | 31 |
def _translate(msgid): |
32 |
return translations.get(msgid, msgid) |
|
33 |
||
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
|
34 |
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
|
35 |
return _translate(msgid) |
0 | 36 |
|
37 |
from cubicweb.web.views.magicsearch import translate_rql_tree, QSPreProcessor, QueryTranslator |
|
38 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2661
diff
changeset
|
39 |
class QueryTranslatorTC(CubicWebTC): |
0 | 40 |
"""test suite for QueryTranslatorTC""" |
1654 | 41 |
|
0 | 42 |
def setUp(self): |
43 |
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
|
44 |
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
|
45 |
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
|
46 |
proc = self.vreg['components'].select('magicsearch', self.req) |
0 | 47 |
self.proc = [p for p in proc.processors if isinstance(p, QueryTranslator)][0] |
48 |
||
49 |
def test_basic_translations(self): |
|
50 |
"""tests basic translations (no ambiguities)""" |
|
51 |
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
|
52 |
rql, = self.proc.preprocess_query(rql) |
0 | 53 |
self.assertEquals(rql, "Any C WHERE C is EmailAddress, P use_email C, C address 'Logilab'") |
54 |
||
55 |
def test_ambiguous_translations(self): |
|
56 |
"""tests possibly ambiguous translations""" |
|
57 |
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
|
58 |
rql, = self.proc.preprocess_query(rql) |
0 | 59 |
self.assertEquals(rql, "Any P WHERE P use_email C, C is EmailAddress, C alias 'Logilab'") |
60 |
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
|
61 |
rql, = self.proc.preprocess_query(rql) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
62 |
self.assertEquals(rql, "Any P WHERE P is CWUser, P use_email C, P surname 'Smith'") |
0 | 63 |
|
64 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2661
diff
changeset
|
65 |
class QSPreProcessorTC(CubicWebTC): |
0 | 66 |
"""test suite for QSPreProcessor""" |
67 |
def setUp(self): |
|
68 |
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
|
69 |
self.vreg.config.translations = {'en': (_translate, _ctxtranslate)} |
0 | 70 |
self.req = self.request() |
2661
f8df42c9da6b
[vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2643
diff
changeset
|
71 |
proc = self.vreg['components'].select('magicsearch', self.req) |
0 | 72 |
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
|
73 |
self.proc._cw = self.req |
0 | 74 |
|
75 |
def test_entity_translation(self): |
|
76 |
"""tests QSPreProcessor._get_entity_name()""" |
|
77 |
translate = self.proc._get_entity_type |
|
78 |
self.assertEquals(translate(u'EmailAddress'), "EmailAddress") |
|
79 |
self.assertEquals(translate(u'emailaddress'), "EmailAddress") |
|
80 |
self.assertEquals(translate(u'Adresse'), "EmailAddress") |
|
81 |
self.assertEquals(translate(u'adresse'), "EmailAddress") |
|
82 |
self.assertRaises(BadRQLQuery, translate, 'whatever') |
|
83 |
||
84 |
def test_attribute_translation(self): |
|
85 |
"""tests QSPreProcessor._get_attribute_name""" |
|
86 |
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
|
87 |
eschema = self.schema.eschema('CWUser') |
0 | 88 |
self.assertEquals(translate(u'prénom', eschema), "firstname") |
89 |
self.assertEquals(translate(u'nom', eschema), 'surname') |
|
1654 | 90 |
eschema = self.schema.eschema('EmailAddress') |
0 | 91 |
self.assertEquals(translate(u'adresse', eschema), "address") |
92 |
self.assertEquals(translate(u'nom', eschema), 'alias') |
|
93 |
# should fail if the name is not an attribute for the given entity schema |
|
94 |
self.assertRaises(BadRQLQuery, translate, 'whatever', eschema) |
|
95 |
self.assertRaises(BadRQLQuery, translate, 'prénom', eschema) |
|
96 |
||
97 |
def test_one_word_query(self): |
|
98 |
"""tests the 'one word shortcut queries'""" |
|
99 |
transform = self.proc._one_word_query |
|
100 |
self.assertEquals(transform('123'), |
|
101 |
('Any X WHERE X eid %(x)s', {'x': 123}, 'x')) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
102 |
self.assertEquals(transform('CWUser'), |
1654 | 103 |
('CWUser C',)) |
0 | 104 |
self.assertEquals(transform('Utilisateur'), |
1654 | 105 |
('CWUser C',)) |
0 | 106 |
self.assertEquals(transform('Adresse'), |
107 |
('EmailAddress E',)) |
|
108 |
self.assertEquals(transform('adresse'), |
|
109 |
('EmailAddress E',)) |
|
110 |
self.assertRaises(BadRQLQuery, transform, 'Workcases') |
|
111 |
||
112 |
def test_two_words_query(self): |
|
113 |
"""tests the 'two words shortcut queries'""" |
|
114 |
transform = self.proc._two_words_query |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
115 |
self.assertEquals(transform('CWUser', 'E'), |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
116 |
("CWUser E",)) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
117 |
self.assertEquals(transform('CWUser', 'Smith'), |
1654 | 118 |
('CWUser C WHERE C has_text %(text)s', {'text': 'Smith'})) |
0 | 119 |
self.assertEquals(transform('utilisateur', 'Smith'), |
1654 | 120 |
('CWUser C WHERE C has_text %(text)s', {'text': 'Smith'})) |
0 | 121 |
self.assertEquals(transform(u'adresse', 'Logilab'), |
122 |
('EmailAddress E WHERE E has_text %(text)s', {'text': 'Logilab'})) |
|
123 |
self.assertEquals(transform(u'adresse', 'Logi%'), |
|
124 |
('EmailAddress E WHERE E alias LIKE %(text)s', {'text': 'Logi%'})) |
|
125 |
self.assertRaises(BadRQLQuery, transform, "pers", "taratata") |
|
126 |
||
127 |
def test_three_words_query(self): |
|
128 |
"""tests the 'three words shortcut queries'""" |
|
129 |
transform = self.proc._three_words_query |
|
130 |
self.assertEquals(transform('utilisateur', u'prénom', 'cubicweb'), |
|
1654 | 131 |
('CWUser C WHERE C firstname %(text)s', {'text': 'cubicweb'})) |
0 | 132 |
self.assertEquals(transform('utilisateur', 'nom', 'cubicweb'), |
1654 | 133 |
('CWUser C WHERE C surname %(text)s', {'text': 'cubicweb'})) |
0 | 134 |
self.assertEquals(transform(u'adresse', 'nom', 'cubicweb'), |
135 |
('EmailAddress E WHERE E alias %(text)s', {'text': 'cubicweb'})) |
|
136 |
self.assertEquals(transform('EmailAddress', 'nom', 'cubicweb'), |
|
1654 | 137 |
('EmailAddress E WHERE E alias %(text)s', {'text': 'cubicweb'})) |
0 | 138 |
self.assertEquals(transform('utilisateur', u'prénom', 'cubicweb%'), |
1654 | 139 |
('CWUser C WHERE C firstname LIKE %(text)s', {'text': 'cubicweb%'})) |
0 | 140 |
# expanded shortcuts |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
141 |
self.assertEquals(transform('CWUser', 'use_email', 'Logilab'), |
1654 | 142 |
('CWUser C WHERE C use_email C1, C1 has_text %(text)s', {'text': 'Logilab'})) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
143 |
self.assertEquals(transform('CWUser', 'use_email', '%Logilab'), |
1654 | 144 |
('CWUser C WHERE C use_email C1, C1 alias LIKE %(text)s', {'text': '%Logilab'})) |
0 | 145 |
self.assertRaises(BadRQLQuery, transform, 'word1', 'word2', 'word3') |
1654 | 146 |
|
0 | 147 |
def test_quoted_queries(self): |
148 |
"""tests how quoted queries are handled""" |
|
149 |
queries = [ |
|
150 |
(u'Adresse "My own EmailAddress"', ('EmailAddress E WHERE E has_text %(text)s', {'text': u'My own EmailAddress'})), |
|
1654 | 151 |
(u'Utilisateur prénom "Jean Paul"', ('CWUser C WHERE C firstname %(text)s', {'text': 'Jean Paul'})), |
152 |
(u'Utilisateur firstname "Jean Paul"', ('CWUser C WHERE C firstname %(text)s', {'text': 'Jean Paul'})), |
|
153 |
(u'CWUser firstname "Jean Paul"', ('CWUser C WHERE C firstname %(text)s', {'text': 'Jean Paul'})), |
|
0 | 154 |
] |
155 |
transform = self.proc._quoted_words_query |
|
156 |
for query, expected in queries: |
|
157 |
self.assertEquals(transform(query), expected) |
|
158 |
self.assertRaises(BadRQLQuery, transform, "unquoted rql") |
|
159 |
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
|
160 |
self.assertRaises(BadRQLQuery, transform, 'CWUser firstname other "Jean Paul"') |
1654 | 161 |
|
0 | 162 |
def test_process_query(self): |
163 |
"""tests how queries are processed""" |
|
164 |
queries = [ |
|
1654 | 165 |
(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
|
166 |
(u'Utilisateur P', (u"CWUser P",)), |
1654 | 167 |
(u'Utilisateur cubicweb', (u'CWUser C WHERE C has_text %(text)s', {'text': u'cubicweb'})), |
168 |
(u'CWUser prénom cubicweb', (u'CWUser C WHERE C firstname %(text)s', {'text': 'cubicweb'},)), |
|
0 | 169 |
] |
170 |
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
|
171 |
self.assertEquals(self.proc.preprocess_query(query), expected) |
2643
9976f511003b
web test update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2637
diff
changeset
|
172 |
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
|
173 |
self.proc.preprocess_query, 'Any X WHERE X is Something') |
1654 | 174 |
|
0 | 175 |
|
176 |
||
177 |
## Processor Chains tests ############################################ |
|
1654 | 178 |
|
0 | 179 |
|
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2661
diff
changeset
|
180 |
class ProcessorChainTC(CubicWebTC): |
0 | 181 |
"""test suite for magic_search's processor chains""" |
182 |
||
183 |
def setUp(self): |
|
184 |
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
|
185 |
self.vreg.config.translations = {'en': (_translate, _ctxtranslate)} |
0 | 186 |
self.req = self.request() |
2661
f8df42c9da6b
[vreg api update] remove some deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2643
diff
changeset
|
187 |
self.proc = self.vreg['components'].select('magicsearch', self.req) |
0 | 188 |
|
189 |
def test_main_preprocessor_chain(self): |
|
190 |
"""tests QUERY_PROCESSOR""" |
|
191 |
queries = [ |
|
192 |
(u'foo', |
|
193 |
("Any X WHERE X has_text %(text)s", {'text': u'foo'})), |
|
194 |
# 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
|
195 |
# and it fails |
0 | 196 |
(u'Utilisateur Smith', |
1654 | 197 |
('CWUser C WHERE C has_text %(text)s', {'text': u'Smith'})), |
0 | 198 |
(u'utilisateur nom Smith', |
1654 | 199 |
('CWUser C WHERE C surname %(text)s', {'text': u'Smith'})), |
0 | 200 |
(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
|
201 |
('Any P WHERE P is CWUser, P surname "Smith"', None)), |
0 | 202 |
] |
203 |
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
|
204 |
rset = self.proc.process_query(query) |
0 | 205 |
self.assertEquals((rset.rql, rset.args), expected) |
206 |
||
207 |
def test_iso88591_fulltext(self): |
|
208 |
"""we must be able to type accentuated characters in the search field""" |
|
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
|
209 |
rset = self.proc.process_query(u'écrire') |
0 | 210 |
self.assertEquals(rset.rql, "Any X WHERE X has_text %(text)s") |
211 |
self.assertEquals(rset.args, {'text': u'écrire'}) |
|
212 |
||
213 |
def test_explicit_component(self): |
|
214 |
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
|
215 |
self.proc.process_query, u'rql: CWUser E WHERE E noattr "Smith",') |
0 | 216 |
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
|
217 |
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
|
218 |
rset = self.proc.process_query(u'text: utilisateur Smith') |
0 | 219 |
self.assertEquals(rset.rql, 'Any X WHERE X has_text %(text)s') |
220 |
self.assertEquals(rset.args, {'text': u'utilisateur Smith'}) |
|
1654 | 221 |
|
0 | 222 |
if __name__ == '__main__': |
223 |
unittest_main() |