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