author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Thu, 23 Jul 2009 15:28:41 +0200 | |
changeset 2456 | aa25d6b244c8 |
parent 2058 | 7ef12c03447c |
child 2637 | 07103211e511 |
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} |
|
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
47 |
proc = self.vreg.select('components', '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() |
|
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
72 |
proc = self.vreg.select('components', '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_multiple_words_query(self): |
151 |
"""tests multiple_words_query()""" |
|
152 |
self.assertEquals(self.proc._multiple_words_query(['a', 'b', 'c', 'd', 'e']), |
|
153 |
('a b c d e',)) |
|
154 |
||
155 |
def test_quoted_queries(self): |
|
156 |
"""tests how quoted queries are handled""" |
|
157 |
queries = [ |
|
158 |
(u'Adresse "My own EmailAddress"', ('EmailAddress E WHERE E has_text %(text)s', {'text': u'My own EmailAddress'})), |
|
1654 | 159 |
(u'Utilisateur prénom "Jean Paul"', ('CWUser C WHERE C firstname %(text)s', {'text': 'Jean Paul'})), |
160 |
(u'Utilisateur firstname "Jean Paul"', ('CWUser C WHERE C firstname %(text)s', {'text': 'Jean Paul'})), |
|
161 |
(u'CWUser firstname "Jean Paul"', ('CWUser C WHERE C firstname %(text)s', {'text': 'Jean Paul'})), |
|
0 | 162 |
] |
163 |
transform = self.proc._quoted_words_query |
|
164 |
for query, expected in queries: |
|
165 |
self.assertEquals(transform(query), expected) |
|
166 |
self.assertRaises(BadRQLQuery, transform, "unquoted rql") |
|
167 |
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
|
168 |
self.assertRaises(BadRQLQuery, transform, 'CWUser firstname other "Jean Paul"') |
1654 | 169 |
|
0 | 170 |
def test_process_query(self): |
171 |
"""tests how queries are processed""" |
|
172 |
queries = [ |
|
1654 | 173 |
(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
|
174 |
(u'Utilisateur P', (u"CWUser P",)), |
1654 | 175 |
(u'Utilisateur cubicweb', (u'CWUser C WHERE C has_text %(text)s', {'text': u'cubicweb'})), |
176 |
(u'CWUser prénom cubicweb', (u'CWUser C WHERE C firstname %(text)s', {'text': 'cubicweb'},)), |
|
0 | 177 |
(u'Any X WHERE X is Something', (u"Any X WHERE X is Something",)), |
178 |
] |
|
179 |
for query, expected in queries: |
|
180 |
self.assertEquals(self.proc.preprocess_query(query, self.req), expected) |
|
1654 | 181 |
|
0 | 182 |
|
183 |
||
184 |
## Processor Chains tests ############################################ |
|
1654 | 185 |
|
0 | 186 |
|
187 |
class ProcessorChainTC(EnvBasedTC): |
|
188 |
"""test suite for magic_search's processor chains""" |
|
189 |
||
190 |
def setUp(self): |
|
191 |
super(ProcessorChainTC, self).setUp() |
|
192 |
self.vreg.config.translations = {'en': _translate} |
|
193 |
self.req = self.request() |
|
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
194 |
self.proc = self.vreg.select('components', 'magicsearch', self.req) |
0 | 195 |
|
196 |
def test_main_preprocessor_chain(self): |
|
197 |
"""tests QUERY_PROCESSOR""" |
|
198 |
queries = [ |
|
199 |
(u'foo', |
|
200 |
("Any X WHERE X has_text %(text)s", {'text': u'foo'})), |
|
201 |
# XXX this sounds like a language translator test... |
|
202 |
# and it fail |
|
203 |
(u'Utilisateur Smith', |
|
1654 | 204 |
('CWUser C WHERE C has_text %(text)s', {'text': u'Smith'})), |
0 | 205 |
(u'utilisateur nom Smith', |
1654 | 206 |
('CWUser C WHERE C surname %(text)s', {'text': u'Smith'})), |
0 | 207 |
(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
|
208 |
('Any P WHERE P is CWUser, P surname "Smith"', None)), |
0 | 209 |
] |
210 |
for query, expected in queries: |
|
211 |
rset = self.proc.process_query(query, self.req) |
|
212 |
self.assertEquals((rset.rql, rset.args), expected) |
|
213 |
||
214 |
def test_iso88591_fulltext(self): |
|
215 |
"""we must be able to type accentuated characters in the search field""" |
|
216 |
rset = self.proc.process_query(u'écrire', self.req) |
|
217 |
self.assertEquals(rset.rql, "Any X WHERE X has_text %(text)s") |
|
218 |
self.assertEquals(rset.args, {'text': u'écrire'}) |
|
219 |
||
220 |
def test_explicit_component(self): |
|
221 |
self.assertRaises(RQLSyntaxError, |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
222 |
self.proc.process_query, u'rql: CWUser E WHERE E noattr "Smith",', self.req) |
0 | 223 |
self.assertRaises(BadRQLQuery, |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
224 |
self.proc.process_query, u'rql: CWUser E WHERE E noattr "Smith"', self.req) |
0 | 225 |
rset = self.proc.process_query(u'text: utilisateur Smith', self.req) |
226 |
self.assertEquals(rset.rql, 'Any X WHERE X has_text %(text)s') |
|
227 |
self.assertEquals(rset.args, {'text': u'utilisateur Smith'}) |
|
1654 | 228 |
|
0 | 229 |
if __name__ == '__main__': |
230 |
unittest_main() |