OLD | NEW |
(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 |
| 9 from boto.cloudsearch.search import SearchConnection |
| 10 |
| 11 HOSTNAME = "search-demo-userdomain.us-east-1.cloudsearch.amazonaws.com" |
| 12 FULL_URL = 'http://%s/2011-02-01/search' % HOSTNAME |
| 13 |
| 14 |
| 15 class CloudSearchSearchTest(unittest.TestCase): |
| 16 |
| 17 hits = [ |
| 18 { |
| 19 'id': '12341', |
| 20 'title': 'Document 1', |
| 21 }, |
| 22 { |
| 23 'id': '12342', |
| 24 'title': 'Document 2', |
| 25 }, |
| 26 { |
| 27 'id': '12343', |
| 28 'title': 'Document 3', |
| 29 }, |
| 30 { |
| 31 'id': '12344', |
| 32 'title': 'Document 4', |
| 33 }, |
| 34 { |
| 35 'id': '12345', |
| 36 'title': 'Document 5', |
| 37 }, |
| 38 { |
| 39 'id': '12346', |
| 40 'title': 'Document 6', |
| 41 }, |
| 42 { |
| 43 'id': '12347', |
| 44 'title': 'Document 7', |
| 45 }, |
| 46 ] |
| 47 |
| 48 response = { |
| 49 'rank': '-text_relevance', |
| 50 'match-expr':"Test", |
| 51 'hits': { |
| 52 'found': 30, |
| 53 'start': 0, |
| 54 'hit':hits |
| 55 }, |
| 56 'info': { |
| 57 'rid':'b7c167f6c2da6d93531b9a7b314ad030b3a74803b4b7797edb905ba5a6a08
', |
| 58 'time-ms': 2, |
| 59 'cpu-time-ms': 0 |
| 60 } |
| 61 |
| 62 } |
| 63 |
| 64 def get_args(self, requestline): |
| 65 (_, request, _) = requestline.split(" ") |
| 66 (_, request) = request.split("?", 1) |
| 67 args = urlparse.parse_qs(request) |
| 68 return args |
| 69 |
| 70 def setUp(self): |
| 71 HTTPretty.enable() |
| 72 HTTPretty.register_uri(HTTPretty.GET, FULL_URL, |
| 73 body=json.dumps(self.response), |
| 74 content_type="text/xml") |
| 75 |
| 76 def tearDown(self): |
| 77 HTTPretty.disable() |
| 78 |
| 79 def test_cloudsearch_qsearch(self): |
| 80 search = SearchConnection(endpoint=HOSTNAME) |
| 81 |
| 82 search.search(q='Test') |
| 83 |
| 84 args = self.get_args(HTTPretty.last_request.raw_requestline) |
| 85 |
| 86 self.assertEqual(args['q'], ["Test"]) |
| 87 self.assertEqual(args['start'], ["0"]) |
| 88 self.assertEqual(args['size'], ["10"]) |
| 89 |
| 90 def test_cloudsearch_bqsearch(self): |
| 91 search = SearchConnection(endpoint=HOSTNAME) |
| 92 |
| 93 search.search(bq="'Test'") |
| 94 |
| 95 args = self.get_args(HTTPretty.last_request.raw_requestline) |
| 96 |
| 97 self.assertEqual(args['bq'], ["'Test'"]) |
| 98 |
| 99 def test_cloudsearch_search_details(self): |
| 100 search = SearchConnection(endpoint=HOSTNAME) |
| 101 |
| 102 search.search(q='Test', size=50, start=20) |
| 103 |
| 104 args = self.get_args(HTTPretty.last_request.raw_requestline) |
| 105 |
| 106 self.assertEqual(args['q'], ["Test"]) |
| 107 self.assertEqual(args['size'], ["50"]) |
| 108 self.assertEqual(args['start'], ["20"]) |
| 109 |
| 110 def test_cloudsearch_facet_single(self): |
| 111 search = SearchConnection(endpoint=HOSTNAME) |
| 112 |
| 113 search.search(q='Test', facet=["Author"]) |
| 114 |
| 115 args = self.get_args(HTTPretty.last_request.raw_requestline) |
| 116 |
| 117 self.assertEqual(args['facet'], ["Author"]) |
| 118 |
| 119 def test_cloudsearch_facet_multiple(self): |
| 120 search = SearchConnection(endpoint=HOSTNAME) |
| 121 |
| 122 search.search(q='Test', facet=["author", "cat"]) |
| 123 |
| 124 args = self.get_args(HTTPretty.last_request.raw_requestline) |
| 125 |
| 126 self.assertEqual(args['facet'], ["author,cat"]) |
| 127 |
| 128 def test_cloudsearch_facet_constraint_single(self): |
| 129 search = SearchConnection(endpoint=HOSTNAME) |
| 130 |
| 131 search.search( |
| 132 q='Test', |
| 133 facet_constraints={'author': "'John Smith','Mark Smith'"}) |
| 134 |
| 135 args = self.get_args(HTTPretty.last_request.raw_requestline) |
| 136 |
| 137 self.assertEqual(args['facet-author-constraints'], |
| 138 ["'John Smith','Mark Smith'"]) |
| 139 |
| 140 def test_cloudsearch_facet_constraint_multiple(self): |
| 141 search = SearchConnection(endpoint=HOSTNAME) |
| 142 |
| 143 search.search( |
| 144 q='Test', |
| 145 facet_constraints={'author': "'John Smith','Mark Smith'", |
| 146 'category': "'News','Reviews'"}) |
| 147 |
| 148 args = self.get_args(HTTPretty.last_request.raw_requestline) |
| 149 |
| 150 self.assertEqual(args['facet-author-constraints'], |
| 151 ["'John Smith','Mark Smith'"]) |
| 152 self.assertEqual(args['facet-category-constraints'], |
| 153 ["'News','Reviews'"]) |
| 154 |
| 155 def test_cloudsearch_facet_sort_single(self): |
| 156 search = SearchConnection(endpoint=HOSTNAME) |
| 157 |
| 158 search.search(q='Test', facet_sort={'author': 'alpha'}) |
| 159 |
| 160 args = self.get_args(HTTPretty.last_request.raw_requestline) |
| 161 |
| 162 self.assertEqual(args['facet-author-sort'], ['alpha']) |
| 163 |
| 164 def test_cloudsearch_facet_sort_multiple(self): |
| 165 search = SearchConnection(endpoint=HOSTNAME) |
| 166 |
| 167 search.search(q='Test', facet_sort={'author': 'alpha', |
| 168 'cat': 'count'}) |
| 169 |
| 170 args = self.get_args(HTTPretty.last_request.raw_requestline) |
| 171 |
| 172 self.assertEqual(args['facet-author-sort'], ['alpha']) |
| 173 self.assertEqual(args['facet-cat-sort'], ['count']) |
| 174 |
| 175 def test_cloudsearch_top_n_single(self): |
| 176 search = SearchConnection(endpoint=HOSTNAME) |
| 177 |
| 178 search.search(q='Test', facet_top_n={'author': 5}) |
| 179 |
| 180 args = self.get_args(HTTPretty.last_request.raw_requestline) |
| 181 |
| 182 self.assertEqual(args['facet-author-top-n'], ['5']) |
| 183 |
| 184 def test_cloudsearch_top_n_multiple(self): |
| 185 search = SearchConnection(endpoint=HOSTNAME) |
| 186 |
| 187 search.search(q='Test', facet_top_n={'author': 5, 'cat': 10}) |
| 188 |
| 189 args = self.get_args(HTTPretty.last_request.raw_requestline) |
| 190 |
| 191 self.assertEqual(args['facet-author-top-n'], ['5']) |
| 192 self.assertEqual(args['facet-cat-top-n'], ['10']) |
| 193 |
| 194 def test_cloudsearch_rank_single(self): |
| 195 search = SearchConnection(endpoint=HOSTNAME) |
| 196 |
| 197 search.search(q='Test', rank=["date"]) |
| 198 |
| 199 args = self.get_args(HTTPretty.last_request.raw_requestline) |
| 200 |
| 201 self.assertEqual(args['rank'], ['date']) |
| 202 |
| 203 def test_cloudsearch_rank_multiple(self): |
| 204 search = SearchConnection(endpoint=HOSTNAME) |
| 205 |
| 206 search.search(q='Test', rank=["date", "score"]) |
| 207 |
| 208 args = self.get_args(HTTPretty.last_request.raw_requestline) |
| 209 |
| 210 self.assertEqual(args['rank'], ['date,score']) |
| 211 |
| 212 def test_cloudsearch_result_fields_single(self): |
| 213 search = SearchConnection(endpoint=HOSTNAME) |
| 214 |
| 215 search.search(q='Test', return_fields=['author']) |
| 216 |
| 217 args = self.get_args(HTTPretty.last_request.raw_requestline) |
| 218 |
| 219 self.assertEqual(args['return-fields'], ['author']) |
| 220 |
| 221 def test_cloudsearch_result_fields_multiple(self): |
| 222 search = SearchConnection(endpoint=HOSTNAME) |
| 223 |
| 224 search.search(q='Test', return_fields=['author', 'title']) |
| 225 |
| 226 args = self.get_args(HTTPretty.last_request.raw_requestline) |
| 227 |
| 228 self.assertEqual(args['return-fields'], ['author,title']) |
| 229 |
| 230 |
| 231 def test_cloudsearch_t_field_single(self): |
| 232 search = SearchConnection(endpoint=HOSTNAME) |
| 233 |
| 234 search.search(q='Test', t={'year':'2001..2007'}) |
| 235 |
| 236 args = self.get_args(HTTPretty.last_request.raw_requestline) |
| 237 |
| 238 self.assertEqual(args['t-year'], ['2001..2007']) |
| 239 |
| 240 def test_cloudsearch_t_field_multiple(self): |
| 241 search = SearchConnection(endpoint=HOSTNAME) |
| 242 |
| 243 search.search(q='Test', t={'year':'2001..2007', 'score':'10..50'}) |
| 244 |
| 245 args = self.get_args(HTTPretty.last_request.raw_requestline) |
| 246 |
| 247 self.assertEqual(args['t-year'], ['2001..2007']) |
| 248 self.assertEqual(args['t-score'], ['10..50']) |
| 249 |
| 250 |
| 251 def test_cloudsearch_results_meta(self): |
| 252 """Check returned metadata is parsed correctly""" |
| 253 search = SearchConnection(endpoint=HOSTNAME) |
| 254 |
| 255 results = search.search(q='Test') |
| 256 |
| 257 # These rely on the default response which is fed into HTTPretty |
| 258 self.assertEqual(results.rank, "-text_relevance") |
| 259 self.assertEqual(results.match_expression, "Test") |
| 260 |
| 261 def test_cloudsearch_results_info(self): |
| 262 """Check num_pages_needed is calculated correctly""" |
| 263 search = SearchConnection(endpoint=HOSTNAME) |
| 264 |
| 265 results = search.search(q='Test') |
| 266 |
| 267 # This relies on the default response which is fed into HTTPretty |
| 268 self.assertEqual(results.num_pages_needed, 3.0) |
| 269 |
| 270 def test_cloudsearch_results_matched(self): |
| 271 """ |
| 272 Check that information objects are passed back through the API |
| 273 correctly. |
| 274 """ |
| 275 search = SearchConnection(endpoint=HOSTNAME) |
| 276 query = search.build_query(q='Test') |
| 277 |
| 278 results = search(query) |
| 279 |
| 280 self.assertEqual(results.search_service, search) |
| 281 self.assertEqual(results.query, query) |
| 282 |
| 283 def test_cloudsearch_results_hits(self): |
| 284 """Check that documents are parsed properly from AWS""" |
| 285 search = SearchConnection(endpoint=HOSTNAME) |
| 286 |
| 287 results = search.search(q='Test') |
| 288 |
| 289 hits = map(lambda x: x['id'], results.docs) |
| 290 |
| 291 # This relies on the default response which is fed into HTTPretty |
| 292 self.assertEqual( |
| 293 hits, ["12341", "12342", "12343", "12344", |
| 294 "12345", "12346", "12347"]) |
| 295 |
| 296 def test_cloudsearch_results_iterator(self): |
| 297 """Check the results iterator""" |
| 298 search = SearchConnection(endpoint=HOSTNAME) |
| 299 |
| 300 results = search.search(q='Test') |
| 301 results_correct = iter(["12341", "12342", "12343", "12344", |
| 302 "12345", "12346", "12347"]) |
| 303 for x in results: |
| 304 self.assertEqual(x['id'], results_correct.next()) |
| 305 |
| 306 |
| 307 def test_cloudsearch_results_internal_consistancy(self): |
| 308 """Check the documents length matches the iterator details""" |
| 309 search = SearchConnection(endpoint=HOSTNAME) |
| 310 |
| 311 results = search.search(q='Test') |
| 312 |
| 313 self.assertEqual(len(results), len(results.docs)) |
| 314 |
| 315 def test_cloudsearch_search_nextpage(self): |
| 316 """Check next page query is correct""" |
| 317 search = SearchConnection(endpoint=HOSTNAME) |
| 318 query1 = search.build_query(q='Test') |
| 319 query2 = search.build_query(q='Test') |
| 320 |
| 321 results = search(query2) |
| 322 |
| 323 self.assertEqual(results.next_page().query.start, |
| 324 query1.start + query1.size) |
| 325 self.assertEqual(query1.q, query2.q) |
OLD | NEW |