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