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