|
16 | 16 | SearchDocumentManagerMixin,
|
17 | 17 | SearchDocumentMixin,
|
18 | 18 | SearchQuery,
|
| 19 | + SearchResponseParser, |
19 | 20 | )
|
20 | 21 |
|
21 | 22 | from .models import (
|
@@ -412,6 +413,57 @@ def test_from_search_results(self) -> None:
|
412 | 413 | assert obj.search_score == 3.0
|
413 | 414 |
|
414 | 415 |
|
| 416 | +@pytest.mark.django_db |
| 417 | +class SearchResponseParserTests: |
| 418 | + def test_parse(self) -> None: |
| 419 | + response = mock.MagicMock(spec=ObjectApiResponse) |
| 420 | + response.body = { |
| 421 | + "took": 31, |
| 422 | + "timed_out": False, |
| 423 | + "_shards": {"total": 1, "successful": 1, "skipped": 0, "failed": 0}, |
| 424 | + "hits": { |
| 425 | + "total": {"value": 190, "relation": "eq"}, |
| 426 | + "max_score": 7.563781, |
| 427 | + "hits": [ |
| 428 | + { |
| 429 | + "_index": "foo", |
| 430 | + "_id": "123", |
| 431 | + "_score": 7.563781, |
| 432 | + "_source": {"country": "GB", "city": "London", "name": "Fred"}, |
| 433 | + "fields": {"country": ["gb"], "city": ["london"]}, |
| 434 | + "highlight": {"country": ["<em>gb</em>"]}, |
| 435 | + } |
| 436 | + ], |
| 437 | + }, |
| 438 | + "aggregations": { |
| 439 | + "countries": { |
| 440 | + "doc_count_error_upper_bound": 0, |
| 441 | + "sum_other_doc_count": 0, |
| 442 | + "buckets": [{"key": "gb", "doc_count": 100}], |
| 443 | + } |
| 444 | + }, |
| 445 | + } |
| 446 | + parser = SearchResponseParser(response) |
| 447 | + assert parser.total_hits == 190 |
| 448 | + assert parser.total_hits_relation == "eq" |
| 449 | + assert parser.hits == [ |
| 450 | + { |
| 451 | + "id": "123", |
| 452 | + "index": "foo", |
| 453 | + "score": 7.563781, |
| 454 | + "fields": {"city": ["london"], "country": ["gb"]}, |
| 455 | + "highlight": {"country": ["<em>gb</em>"]}, |
| 456 | + } |
| 457 | + ] |
| 458 | + assert parser.aggregations == { |
| 459 | + "countries": { |
| 460 | + "doc_count_error_upper_bound": 0, |
| 461 | + "sum_other_doc_count": 0, |
| 462 | + "buckets": [{"key": "gb", "doc_count": 100}], |
| 463 | + } |
| 464 | + } |
| 465 | + |
| 466 | + |
415 | 467 | @pytest.mark.django_db
|
416 | 468 | class ExecuteFunctionTests:
|
417 | 469 | raw_hits = [
|
|
0 commit comments