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