/
test_sunburnt.py
376 lines (321 loc) · 13.5 KB
/
test_sunburnt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
from __future__ import absolute_import
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
import cgi, datetime, urlparse
from lxml.builder import E
from lxml.etree import tostring
from .sunburnt import SolrInterface
from nose.tools import assert_equal, assert_in
debug = False
schema_string = \
"""<schema name="timetric" version="1.1">
<types>
<fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="text" class="solr.TextField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="boolean" class="solr.BoolField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="int" class="solr.IntField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="sint" class="solr.SortableIntField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="long" class="solr.LongField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="slong" class="solr.SortableLongField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="float" class="solr.FloatField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="sfloat" class="solr.SortableFloatField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="double" class="solr.DoubleField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="sdouble" class="solr.SortableDoubleField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="date" class="solr.DateField" sortMissingLast="true" omitNorms="true"/>
</types>
<fields>
<field name="string_field" required="true" type="string" multiValued="true"/>
<field name="text_field" required="true" type="text"/>
<field name="boolean_field" required="false" type="boolean"/>
<field name="int_field" required="true" type="int"/>
<field name="sint_field" type="sint"/>
<field name="long_field" type="long"/>
<field name="slong_field" type="slong"/>
<field name="long_field" type="long"/>
<field name="slong_field" type="slong"/>
<field name="float_field" type="float"/>
<field name="sfloat_field" type="sfloat"/>
<field name="double_field" type="double"/>
<field name="sdouble_field" type="sdouble"/>
<field name="date_field" type="date"/>
</fields>
<defaultSearchField>text_field</defaultSearchField>
<uniqueKey>int_field</uniqueKey>
</schema>"""
class MockResponse(object):
mock_doc_seeds = [
(0, 'zero'),
(1, 'one'),
(2, 'two'),
(3, 'three'),
(4, 'four'),
(5, 'five'),
(6, 'six'),
(7, 'seven'),
(8, 'eight'),
(9, 'nine'),
]
mock_docs = [
dict(zip(("int_field", "string_field"), m)) for m in mock_doc_seeds
]
def __init__(self, start, rows):
self.start = start
self.rows = rows
@staticmethod
def xmlify_doc(d):
return E.doc(
E.int({'name':'int_field'}, str(d['int_field'])),
E.str({'name':'string_field'}, d['string_field'])
)
def extra_response_parts(self):
return []
def xml_response(self):
response_portions = [
E.lst({'name':'responseHeader'},
E.int({'name':'status'}, '0'), E.int({'name':'QTime'}, '0')
),
E.result({'name':'response', 'numFound':str(len(self.mock_docs)), 'start':str(self.start)},
*[self.xmlify_doc(doc) for doc in self.mock_docs[self.start:self.start+self.rows]]
)
] + self.extra_response_parts()
return tostring(E.response(*response_portions))
class MockConnection(object):
file_dict = {'schema.xml': schema_string}
class MockStatus(object):
def __init__(self, status):
self.status = status
def __init__(self, tracking_dict=None):
if tracking_dict is None:
tracking_dict = {}
self.tracking_dict = tracking_dict
def request(self, uri, method='GET', body=None, headers=None):
u = urlparse.urlparse(uri)
params = cgi.parse_qs(u.query)
self.tracking_dict.update(url=uri,
params=params,
method=method,
body=body or '',
headers=headers or {})
if method == 'GET' and u.path.endswith('/admin/file/'):
filename = params.get("file")[0]
if filename in self.file_dict:
return self.MockStatus(200), self.file_dict[filename]
else:
return self.MockStatus(404), None
rc = self._handle_request(u, params, method, body, headers)
if rc is not None:
return rc
raise ValueError("Can't handle this URI")
class PaginationMockConnection(MockConnection):
def _handle_request(self, uri_obj, params, method, body, headers):
if method == 'GET' and uri_obj.path.endswith('/select/'):
start = int(params.get("start", [0])[0])
rows = int(params.get("rows", [10])[0])
return self.MockStatus(200), MockResponse(start, rows).xml_response()
conn = SolrInterface("http://test.example.com/", http_connection=PaginationMockConnection())
pagination_slice_tests = (
((None, None), range(0, 10),
(slice(None, None, None),
slice(0, 10, None),
slice(0, 10, 1),
slice(0, 5, None),
slice(5, 10, None),
slice(0, 5, 2),
slice(5, 10, 2),
slice(9, None, -1),
slice(None, 0, -1),
slice(7, 3, -2),
# out of range but ok
slice(0, 12, None),
slice(-100, 12, None),
# out of range but empty
slice(12, 20, None),
slice(-100, -90),
# negative offsets
slice(0, -1, None),
slice(-5, -1, None),
slice(-1, -5, -1),
# zero-range produced
slice(10, 0, None),
slice(0, 10, -1),
slice(0, -3, -1),
slice(-5, -9, None),
slice(-9, -5, -1))),
### and now with pre-paginated queries:
((2, 6), range(2, 8),
(slice(None, None, None),
slice(0, 6, None),
slice(0, 6, 1),
slice(0, 5, None),
slice(5, 6, None),
slice(0, 5, 2),
slice(3, 6, 2),
slice(5, None, -1),
slice(None, 0, -1),
# out of range but ok
slice(0, 12, None),
slice(-100, 12, None),
# negative offsets
slice(0, -1, None),
slice(-3, -1, None),
slice(-1, -3, -1),
# zero-range produced
slice(6, 0, None),
slice(0, 6, -1),
slice(0, -3, -1),
slice(-2, -5, None),
slice(-5, -2, -1))),
)
def check_slice_pagination(p_args, a, s):
assert [d['int_field'] for d in conn.query("*").paginate(*p_args)[s]] == a[s]
def test_slice_pagination():
for p_args, a, slices in pagination_slice_tests:
for s in slices:
yield check_slice_pagination, p_args, a, s
# indexing to cells
# IndexErrors as appropriate
pagination_index_tests = (
((None, None), range(0, 10),
((0, None),
(5, None),
(9, None),
(-1, None),
(-5, None),
(-9, None),
(10, IndexError),
(20, IndexError),
(-10, IndexError),
(-20, IndexError))),
((2, 6), range(2, 8),
((0, None),
(3, None),
(5, None),
(-1, None),
(-3, None),
(-6, None),
(6, IndexError),
(20, IndexError),
(-7, IndexError),
(-20, IndexError))),
)
def check_index_pagination(p_args, a, s, e):
if e is None:
assert conn.query("*").paginate(*p_args)[s]['int_field'] == a[s]
else:
q = conn.query("*").paginate(*p_args)
try:
q[s]
except IndexError:
pass
def test_index_pagination():
for p_args, a, slices in pagination_index_tests:
for s, e in slices:
yield check_index_pagination, p_args, a, s, e
class MLTMockConnection(MockConnection):
def _handle_request(self, u, params, method, body, headers):
return self.MockStatus(200), MockResponse(1, 2).xml_response()
mlt_query_tests = (
# basic query
(("Content", None, None), ({'stream.body': ['Content'], 'mlt.fl': ['text_field']}, 'GET', ''), None),
(("Content with space", None, None), ({'stream.body': ['Content with space'], 'mlt.fl': ['text_field']}, 'GET', ''), None),
((None, None, "http://source.example.com"), ({'stream.url': ['http://source.example.com'], 'mlt.fl': ['text_field']}, 'GET', ''), None),
(("long "*1024+"content", None, None), ({'mlt.fl': ['text_field']}, 'POST', 'long '*1024+"content"), None),
(("Content", None, "http://source.example.com"), (), ValueError),
((None, None, None), ({'mlt.fl': ['text_field']}, 'GET', ''), None),
(('Content', 'not-an-encoding', None), (), LookupError),
((u'Content', None, None), ({'stream.body': ['Content'], 'mlt.fl': ['text_field']}, 'GET', ''), None),
(('Cont\xe9nt', 'iso-8859-1', None), ({'stream.body': ['Cont\xc3\xa9nt'], 'mlt.fl': ['text_field']}, 'GET', ''), None),
)
def check_mlt_query(i, o, E):
if E is None:
query_params, method, body = o
content, content_charset, url = i
d = {}
conn = SolrInterface("http://test.example.com/", http_connection=MLTMockConnection(d))
if E is None:
conn.mlt_query(content=content, content_charset=content_charset, url=url).execute()
assert_equal(d['params'], query_params)
assert_equal(d['method'], method)
assert_equal(d['body'], body)
else:
try:
conn.mlt_query(content=content, content_charset=content_charset, url=url).execute()
except E:
pass
else:
assert False
def test_mlt_queries():
for i, o, E in mlt_query_tests:
yield check_mlt_query, i, o, E
schema_string_with_xinclude = \
"""<schema name="timetric" version="1.1">
<xi:include href="schema_extra_types.xml" xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:fallback/>
</xi:include>
<!-- Following is a dynamic way to include other fields, added by other contrib modules -->
<xi:include href="schema_extra_fields.xml" xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:fallback/>
</xi:include>
<xi:include href="schema_not_available.xml" xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:fallback/>
</xi:include>
<defaultSearchField>text_field</defaultSearchField>
<uniqueKey>int_field</uniqueKey>
</schema>"""
schema_fieldtypes_to_be_included = \
"""<types>
<fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="text" class="solr.TextField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="boolean" class="solr.BoolField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="int" class="solr.IntField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="sint" class="solr.SortableIntField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="long" class="solr.LongField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="slong" class="solr.SortableLongField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="float" class="solr.FloatField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="sfloat" class="solr.SortableFloatField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="double" class="solr.DoubleField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="sdouble" class="solr.SortableDoubleField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="date" class="solr.DateField" sortMissingLast="true" omitNorms="true"/>
</types>"""
schema_fields_to_be_included = \
"""<fields>
<field name="string_field" required="true" type="string" multiValued="true"/>
<field name="text_field" required="true" type="text"/>
<field name="boolean_field" required="false" type="boolean"/>
<field name="int_field" required="true" type="int"/>
<field name="sint_field" type="sint"/>
<field name="long_field" type="long"/>
<field name="slong_field" type="slong"/>
<field name="float_field" type="float"/>
<field name="sfloat_field" type="sfloat"/>
<field name="double_field" type="double"/>
<field name="sdouble_field" type="sdouble"/>
<field name="date_field" type="date"/>
</fields>"""
class XincludeMockConnection(MockConnection):
file_dict = {
'schema.xml': schema_string_with_xinclude,
'schema_extra_types.xml': schema_fieldtypes_to_be_included,
'schema_extra_fields.xml': schema_fields_to_be_included,
}
def test_schema_with_xinclude_gets_assembled():
si = SolrInterface("http://test.example.com/", http_connection=XincludeMockConnection())
assert_equal(12, len(si.schema.fields))
def test_schema_file_cache_gets_filled():
si = SolrInterface("http://test.example.com/", http_connection=XincludeMockConnection())
assert_equal(schema_string_with_xinclude, si.file_cache['schema.xml'])
assert_equal(schema_fieldtypes_to_be_included, si.file_cache['schema_extra_types.xml'])
assert_equal(schema_fields_to_be_included, si.file_cache['schema_extra_fields.xml'])
def test_all_xincludes_found():
si = SolrInterface("http://test.example.com/", http_connection=XincludeMockConnection())
assert_equal(3, len(si.get_xinclude_list_for_file('schema.xml')))
assert_equal(0, len(si.get_xinclude_list_for_file('schema_extra_fields.xml')))
def test_get_file_and_included_files_list_includes_all_required_files():
si = SolrInterface("http://test.example.com/", http_connection=XincludeMockConnection())
file_list = si.get_file_and_included_files('schema.xml')
assert_equal(3, len(file_list))
assert_in('schema.xml', file_list)
assert_in('schema_extra_fields.xml', file_list)
assert_in('schema_extra_types.xml', file_list)