|
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 = { |
|
14 u'EUser' : u"Utilisateur", |
|
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) |
|
57 self.assertEquals(rql, "Any P WHERE P is EUser, P use_email C, P surname 'Smith'") |
|
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 |
|
82 eschema = self.schema.eschema('EUser') |
|
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')) |
|
98 self.assertEquals(transform('EUser'), |
|
99 ('EUser E',)) |
|
100 self.assertEquals(transform('Utilisateur'), |
|
101 ('EUser E',)) |
|
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 |
|
111 self.assertEquals(transform('EUser', 'E'), |
|
112 ("EUser E",)) |
|
113 self.assertEquals(transform('EUser', 'Smith'), |
|
114 ('EUser E WHERE E has_text %(text)s', {'text': 'Smith'})) |
|
115 self.assertEquals(transform('utilisateur', 'Smith'), |
|
116 ('EUser E WHERE E has_text %(text)s', {'text': 'Smith'})) |
|
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") |
|
122 #self.assertEquals(transform('EUser', '%mi'), 'EUser E WHERE P surname LIKE "%mi"') |
|
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'), |
|
128 ('EUser E WHERE E firstname %(text)s', {'text': 'cubicweb'})) |
|
129 self.assertEquals(transform('utilisateur', 'nom', 'cubicweb'), |
|
130 ('EUser E WHERE E surname %(text)s', {'text': 'cubicweb'})) |
|
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%'), |
|
136 ('EUser E WHERE E firstname LIKE %(text)s', {'text': 'cubicweb%'})) |
|
137 # expanded shortcuts |
|
138 self.assertEquals(transform('EUser', 'use_email', 'Logilab'), |
|
139 ('EUser E WHERE E use_email E1, E1 has_text %(text)s', {'text': 'Logilab'})) |
|
140 self.assertEquals(transform('EUser', 'use_email', '%Logilab'), |
|
141 ('EUser E WHERE E use_email E1, E1 alias LIKE %(text)s', {'text': '%Logilab'})) |
|
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'})), |
|
153 (u'Utilisateur prénom "Jean Paul"', ('EUser E WHERE E firstname %(text)s', {'text': 'Jean Paul'})), |
|
154 (u'Utilisateur firstname "Jean Paul"', ('EUser E WHERE E firstname %(text)s', {'text': 'Jean Paul'})), |
|
155 (u'EUser firstname "Jean Paul"', ('EUser E WHERE E firstname %(text)s', {'text': 'Jean Paul'})), |
|
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"') |
|
162 self.assertRaises(BadRQLQuery, transform, 'EUser firstname other "Jean Paul"') |
|
163 |
|
164 def test_process_query(self): |
|
165 """tests how queries are processed""" |
|
166 queries = [ |
|
167 (u'Utilisateur', (u"EUser E",)), |
|
168 (u'Utilisateur P', (u"EUser P",)), |
|
169 (u'Utilisateur cubicweb', (u'EUser E WHERE E has_text %(text)s', {'text': u'cubicweb'})), |
|
170 (u'EUser prénom cubicweb', (u'EUser E WHERE E firstname %(text)s', {'text': 'cubicweb'},)), |
|
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', |
|
198 ('EUser E WHERE E has_text %(text)s', {'text': u'Smith'})), |
|
199 (u'utilisateur nom Smith', |
|
200 ('EUser E WHERE E surname %(text)s', {'text': u'Smith'})), |
|
201 (u'Any P WHERE P is Utilisateur, P nom "Smith"', |
|
202 ('Any P WHERE P is EUser, P surname "Smith"', None)), |
|
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, |
|
216 self.proc.process_query, u'rql: EUser E WHERE E noattr "Smith",', self.req) |
|
217 self.assertRaises(BadRQLQuery, |
|
218 self.proc.process_query, u'rql: EUser E WHERE E noattr "Smith"', self.req) |
|
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() |