Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: third_party/boto/tests/unit/cloudsearch2/test_search.py

Issue 698893003: Update checked in version of gsutil to version 4.6 (Closed) Base URL: http://dart.googlecode.com/svn/third_party/gsutil/
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 #!/usr/bin env python
2
3 from tests.unit import unittest
4 from httpretty import HTTPretty
5
6 import urlparse
7 import json
8 import mock
9 import requests
10
11 from boto.cloudsearch2.search import SearchConnection, SearchServiceException
12
13 HOSTNAME = "search-demo-userdomain.us-east-1.cloudsearch.amazonaws.com"
14 FULL_URL = 'http://%s/2013-01-01/search' % HOSTNAME
15
16
17 class CloudSearchSearchBaseTest(unittest.TestCase):
18
19 hits = [
20 {
21 'id': '12341',
22 'fields': {
23 'title': 'Document 1',
24 'rank': 1
25 }
26 },
27 {
28 'id': '12342',
29 'fields': {
30 'title': 'Document 2',
31 'rank': 2
32 }
33 },
34 {
35 'id': '12343',
36 'fields': {
37 'title': 'Document 3',
38 'rank': 3
39 }
40 },
41 {
42 'id': '12344',
43 'fields': {
44 'title': 'Document 4',
45 'rank': 4
46 }
47 },
48 {
49 'id': '12345',
50 'fields': {
51 'title': 'Document 5',
52 'rank': 5
53 }
54 },
55 {
56 'id': '12346',
57 'fields': {
58 'title': 'Document 6',
59 'rank': 6
60 }
61 },
62 {
63 'id': '12347',
64 'fields': {
65 'title': 'Document 7',
66 'rank': 7
67 }
68 },
69 ]
70
71 content_type = "text/xml"
72 response_status = 200
73
74 def get_args(self, requestline):
75 (_, request, _) = requestline.split(" ")
76 (_, request) = request.split("?", 1)
77 args = urlparse.parse_qs(request)
78 return args
79
80 def setUp(self):
81 HTTPretty.enable()
82 body = self.response
83
84 if not isinstance(body, basestring):
85 body = json.dumps(body)
86
87 HTTPretty.register_uri(HTTPretty.GET, FULL_URL,
88 body=body,
89 content_type=self.content_type,
90 status=self.response_status)
91
92 def tearDown(self):
93 HTTPretty.disable()
94
95 class CloudSearchSearchTest(CloudSearchSearchBaseTest):
96 response = {
97 'rank': '-text_relevance',
98 'match-expr':"Test",
99 'hits': {
100 'found': 30,
101 'start': 0,
102 'hit':CloudSearchSearchBaseTest.hits
103 },
104 'status': {
105 'rid':'b7c167f6c2da6d93531b9a7b314ad030b3a74803b4b7797edb905ba5a6a08 ',
106 'time-ms': 2,
107 'cpu-time-ms': 0
108 }
109
110 }
111
112 def test_cloudsearch_qsearch(self):
113 search = SearchConnection(endpoint=HOSTNAME)
114
115 search.search(q='Test')
116
117 args = self.get_args(HTTPretty.last_request.raw_requestline)
118
119 self.assertEqual(args['q'], ["Test"])
120 self.assertEqual(args['start'], ["0"])
121 self.assertEqual(args['size'], ["10"])
122
123
124 def test_cloudsearch_search_details(self):
125 search = SearchConnection(endpoint=HOSTNAME)
126
127 search.search(q='Test', size=50, start=20)
128
129 args = self.get_args(HTTPretty.last_request.raw_requestline)
130
131 self.assertEqual(args['q'], ["Test"])
132 self.assertEqual(args['size'], ["50"])
133 self.assertEqual(args['start'], ["20"])
134
135 def test_cloudsearch_facet_constraint_single(self):
136 search = SearchConnection(endpoint=HOSTNAME)
137
138 search.search(
139 q='Test',
140 facet={'author': "'John Smith','Mark Smith'"})
141
142 args = self.get_args(HTTPretty.last_request.raw_requestline)
143
144 self.assertEqual(args['facet.author'],
145 ["'John Smith','Mark Smith'"])
146
147 def test_cloudsearch_facet_constraint_multiple(self):
148 search = SearchConnection(endpoint=HOSTNAME)
149
150 search.search(
151 q='Test',
152 facet={'author': "'John Smith','Mark Smith'",
153 'category': "'News','Reviews'"})
154
155 args = self.get_args(HTTPretty.last_request.raw_requestline)
156
157 self.assertEqual(args['facet.author'],
158 ["'John Smith','Mark Smith'"])
159 self.assertEqual(args['facet.category'],
160 ["'News','Reviews'"])
161
162 def test_cloudsearch_facet_sort_single(self):
163 search = SearchConnection(endpoint=HOSTNAME)
164
165 search.search(q='Test', facet={'author': {'sort':'alpha'}})
166
167 args = self.get_args(HTTPretty.last_request.raw_requestline)
168
169 print args
170
171 self.assertEqual(args['facet.author'], ['{"sort": "alpha"}'])
172
173 def test_cloudsearch_facet_sort_multiple(self):
174 search = SearchConnection(endpoint=HOSTNAME)
175
176 search.search(q='Test', facet={'author': {'sort': 'alpha'},
177 'cat': {'sort': 'count'}})
178
179 args = self.get_args(HTTPretty.last_request.raw_requestline)
180
181 self.assertEqual(args['facet.author'], ['{"sort": "alpha"}'])
182 self.assertEqual(args['facet.cat'], ['{"sort": "count"}'])
183
184 def test_cloudsearch_result_fields_single(self):
185 search = SearchConnection(endpoint=HOSTNAME)
186
187 search.search(q='Test', return_fields=['author'])
188
189 args = self.get_args(HTTPretty.last_request.raw_requestline)
190
191 self.assertEqual(args['return'], ['author'])
192
193 def test_cloudsearch_result_fields_multiple(self):
194 search = SearchConnection(endpoint=HOSTNAME)
195
196 search.search(q='Test', return_fields=['author', 'title'])
197
198 args = self.get_args(HTTPretty.last_request.raw_requestline)
199
200 self.assertEqual(args['return'], ['author,title'])
201
202 def test_cloudsearch_results_meta(self):
203 """Check returned metadata is parsed correctly"""
204 search = SearchConnection(endpoint=HOSTNAME)
205
206 results = search.search(q='Test')
207
208 # These rely on the default response which is fed into HTTPretty
209 self.assertEqual(results.hits, 30)
210 self.assertEqual(results.docs[0]['fields']['rank'], 1)
211
212 def test_cloudsearch_results_info(self):
213 """Check num_pages_needed is calculated correctly"""
214 search = SearchConnection(endpoint=HOSTNAME)
215
216 results = search.search(q='Test')
217
218 # This relies on the default response which is fed into HTTPretty
219 self.assertEqual(results.num_pages_needed, 3.0)
220
221 def test_cloudsearch_results_matched(self):
222 """
223 Check that information objects are passed back through the API
224 correctly.
225 """
226 search = SearchConnection(endpoint=HOSTNAME)
227 query = search.build_query(q='Test')
228
229 results = search(query)
230
231 self.assertEqual(results.search_service, search)
232 self.assertEqual(results.query, query)
233
234 def test_cloudsearch_results_hits(self):
235 """Check that documents are parsed properly from AWS"""
236 search = SearchConnection(endpoint=HOSTNAME)
237
238 results = search.search(q='Test')
239
240 hits = map(lambda x: x['id'], results.docs)
241
242 # This relies on the default response which is fed into HTTPretty
243 self.assertEqual(
244 hits, ["12341", "12342", "12343", "12344",
245 "12345", "12346", "12347"])
246
247 def test_cloudsearch_results_iterator(self):
248 """Check the results iterator"""
249 search = SearchConnection(endpoint=HOSTNAME)
250
251 results = search.search(q='Test')
252 results_correct = iter(["12341", "12342", "12343", "12344",
253 "12345", "12346", "12347"])
254 for x in results:
255 self.assertEqual(x['id'], results_correct.next())
256
257
258 def test_cloudsearch_results_internal_consistancy(self):
259 """Check the documents length matches the iterator details"""
260 search = SearchConnection(endpoint=HOSTNAME)
261
262 results = search.search(q='Test')
263
264 self.assertEqual(len(results), len(results.docs))
265
266 def test_cloudsearch_search_nextpage(self):
267 """Check next page query is correct"""
268 search = SearchConnection(endpoint=HOSTNAME)
269 query1 = search.build_query(q='Test')
270 query2 = search.build_query(q='Test')
271
272 results = search(query2)
273
274 self.assertEqual(results.next_page().query.start,
275 query1.start + query1.size)
276 self.assertEqual(query1.q, query2.q)
277
278 class CloudSearchSearchFacetTest(CloudSearchSearchBaseTest):
279 response = {
280 'rank': '-text_relevance',
281 'match-expr':"Test",
282 'hits': {
283 'found': 30,
284 'start': 0,
285 'hit':CloudSearchSearchBaseTest.hits
286 },
287 'status': {
288 'rid':'b7c167f6c2da6d93531b9a7b314ad030b3a74803b4b7797edb905ba5a6a08 ',
289 'time-ms': 2,
290 'cpu-time-ms': 0
291 },
292 'facets': {
293 'tags': {},
294 'animals': {'buckets': [{'count': '2', 'value': 'fish'}, {'count': ' 1', 'value':'lions'}]},
295 }
296 }
297
298 def test_cloudsearch_search_facets(self):
299 #self.response['facets'] = {'tags': {}}
300
301 search = SearchConnection(endpoint=HOSTNAME)
302
303 results = search.search(q='Test', facet={'tags': {}})
304
305 self.assertTrue('tags' not in results.facets)
306 self.assertEqual(results.facets['animals'], {u'lions': u'1', u'fish': u' 2'})
307
308
309 class CloudSearchNonJsonTest(CloudSearchSearchBaseTest):
310 response = '<html><body><h1>500 Internal Server Error</h1></body></html>'
311 response_status = 500
312 content_type = 'text/xml'
313
314 def test_response(self):
315 search = SearchConnection(endpoint=HOSTNAME)
316
317 with self.assertRaises(SearchServiceException):
318 search.search(q='Test')
319
320
321 class CloudSearchUnauthorizedTest(CloudSearchSearchBaseTest):
322 response = '<html><body><h1>403 Forbidden</h1>foo bar baz</body></html>'
323 response_status = 403
324 content_type = 'text/html'
325
326 def test_response(self):
327 search = SearchConnection(endpoint=HOSTNAME)
328
329 with self.assertRaisesRegexp(SearchServiceException, 'foo bar baz'):
330 search.search(q='Test')
331
332
333 class FakeResponse(object):
334 status_code = 405
335 content = ''
336
337
338 class CloudSearchConnectionTest(unittest.TestCase):
339 cloudsearch = True
340
341 def setUp(self):
342 super(CloudSearchConnectionTest, self).setUp()
343 self.conn = SearchConnection(
344 endpoint='test-domain.cloudsearch.amazonaws.com'
345 )
346
347 def test_expose_additional_error_info(self):
348 mpo = mock.patch.object
349 fake = FakeResponse()
350 fake.content = 'Nopenopenope'
351
352 # First, in the case of a non-JSON, non-403 error.
353 with mpo(self.conn.session, 'get', return_value=fake) as mock_request:
354 with self.assertRaises(SearchServiceException) as cm:
355 self.conn.search(q='not_gonna_happen')
356
357 self.assertTrue('non-json response' in str(cm.exception))
358 self.assertTrue('Nopenopenope' in str(cm.exception))
359
360 # Then with JSON & an 'error' key within.
361 fake.content = json.dumps({
362 'error': "Something went wrong. Oops."
363 })
364
365 with mpo(self.conn.session, 'get', return_value=fake) as mock_request:
366 with self.assertRaises(SearchServiceException) as cm:
367 self.conn.search(q='no_luck_here')
368
369 self.assertTrue('Unknown error' in str(cm.exception))
370 self.assertTrue('went wrong. Oops' in str(cm.exception))
OLDNEW
« no previous file with comments | « third_party/boto/tests/unit/cloudsearch2/test_exceptions.py ('k') | third_party/boto/tests/unit/dynamodb2/test_table.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698