From 842f8e0eb2855671f7021b4e359b169f6918b6af Mon Sep 17 00:00:00 2001 From: Fenimore Love Date: Thu, 4 Feb 2021 14:56:42 -0500 Subject: [PATCH] Update Search API return result --- elasticmock/fake_elasticsearch.py | 2 +- tests/fake_elasticsearch/test_delete.py | 4 +-- tests/fake_elasticsearch/test_index.py | 4 +-- tests/fake_elasticsearch/test_scroll.py | 2 +- tests/fake_elasticsearch/test_search.py | 40 ++++++++++++------------- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/elasticmock/fake_elasticsearch.py b/elasticmock/fake_elasticsearch.py index 63cc9c0..c0bf33c 100644 --- a/elasticmock/fake_elasticsearch.py +++ b/elasticmock/fake_elasticsearch.py @@ -510,7 +510,7 @@ def search(self, index=None, doc_type=None, body=None, params=None, headers=None result = { 'hits': { - 'total': len(matches), + 'total': {'value': len(matches), 'relation': 'eq'}, 'max_score': 1.0 }, '_shards': { diff --git a/tests/fake_elasticsearch/test_delete.py b/tests/fake_elasticsearch/test_delete.py index 6cfc7d9..14ec435 100644 --- a/tests/fake_elasticsearch/test_delete.py +++ b/tests/fake_elasticsearch/test_delete.py @@ -22,12 +22,12 @@ def test_should_not_raise_exception_when_delete_nonindexed_document_if_ignored_l def test_should_delete_indexed_document(self): doc_indexed = self.es.index(index=INDEX_NAME, doc_type=DOC_TYPE, body=BODY) search = self.es.search(index=INDEX_NAME) - self.assertEqual(1, search.get('hits').get('total')) + self.assertEqual(1, search.get('hits').get('total').get('value')) doc_id = doc_indexed.get('_id') doc_deleted = self.es.delete(index=INDEX_NAME, doc_type=DOC_TYPE, id=doc_id) search = self.es.search(index=INDEX_NAME) - self.assertEqual(0, search.get('hits').get('total')) + self.assertEqual(0, search.get('hits').get('total').get('value')) expected_doc_deleted = { 'found': True, diff --git a/tests/fake_elasticsearch/test_index.py b/tests/fake_elasticsearch/test_index.py index 5c8b88c..911559c 100644 --- a/tests/fake_elasticsearch/test_index.py +++ b/tests/fake_elasticsearch/test_index.py @@ -35,10 +35,10 @@ def test_doc_type_can_be_list(self): self.es.index(index=INDEX_NAME, doc_type=doc_type, body={}) result = self.es.search(doc_type=[doc_types[0]]) - self.assertEqual(count_per_doc_type, result.get('hits').get('total')) + self.assertEqual(count_per_doc_type, result.get('hits').get('total').get('value')) result = self.es.search(doc_type=doc_types[:2]) - self.assertEqual(count_per_doc_type * 2, result.get('hits').get('total')) + self.assertEqual(count_per_doc_type * 2, result.get('hits').get('total').get('value')) def test_update_existing_doc(self): data = self.es.index(index=INDEX_NAME, doc_type=DOC_TYPE, body=BODY) diff --git a/tests/fake_elasticsearch/test_scroll.py b/tests/fake_elasticsearch/test_scroll.py index 22d400b..1b90191 100644 --- a/tests/fake_elasticsearch/test_scroll.py +++ b/tests/fake_elasticsearch/test_scroll.py @@ -24,4 +24,4 @@ def __assert_scroll(self, result, expected_scroll_hits): self.assertNotEqual(None, result.get('_scroll_id', None)) self.assertEqual(expected_scroll_hits, len(hits.get('hits'))) - self.assertEqual(100, hits.get('total')) + self.assertEqual(100, hits.get('total').get('value')) diff --git a/tests/fake_elasticsearch/test_search.py b/tests/fake_elasticsearch/test_search.py index 348f82e..227c7e8 100644 --- a/tests/fake_elasticsearch/test_search.py +++ b/tests/fake_elasticsearch/test_search.py @@ -15,7 +15,7 @@ def test_should_raise_notfounderror_when_search_for_unexistent_index(self): def test_should_return_hits_hits_even_when_no_result(self): search = self.es.search() - self.assertEqual(0, search.get('hits').get('total')) + self.assertEqual(0, search.get('hits').get('total').get('value')) self.assertListEqual([], search.get('hits').get('hits')) def test_should_return_skipped_shards(self): @@ -28,7 +28,7 @@ def test_should_return_all_documents(self): self.es.index(index='index_{0}'.format(i), doc_type=DOC_TYPE, body={'data': 'test_{0}'.format(i)}) search = self.es.search() - self.assertEqual(index_quantity, search.get('hits').get('total')) + self.assertEqual(index_quantity, search.get('hits').get('total').get('value')) def test_should_return_all_documents_match_all(self): index_quantity = 10 @@ -36,7 +36,7 @@ def test_should_return_all_documents_match_all(self): self.es.index(index='index_{0}'.format(i), doc_type=DOC_TYPE, body={'data': 'test_{0}'.format(i)}) search = self.es.search(body={'query': {'match_all': {}}}) - self.assertEqual(index_quantity, search.get('hits').get('total')) + self.assertEqual(index_quantity, search.get('hits').get('total').get('value')) def test_should_return_only_indexed_documents_on_index(self): index_quantity = 2 @@ -44,7 +44,7 @@ def test_should_return_only_indexed_documents_on_index(self): self.es.index(index=INDEX_NAME, doc_type=DOC_TYPE, body={'data': 'test_{0}'.format(i)}) search = self.es.search(index=INDEX_NAME) - self.assertEqual(index_quantity, search.get('hits').get('total')) + self.assertEqual(index_quantity, search.get('hits').get('total').get('value')) def test_should_return_only_indexed_documents_on_index_with_doc_type(self): index_quantity = 2 @@ -53,7 +53,7 @@ def test_should_return_only_indexed_documents_on_index_with_doc_type(self): self.es.index(index=INDEX_NAME, doc_type='another-Doctype', body={'data': 'test'}) search = self.es.search(index=INDEX_NAME, doc_type=DOC_TYPE) - self.assertEqual(index_quantity, search.get('hits').get('total')) + self.assertEqual(index_quantity, search.get('hits').get('total').get('value')) def test_should_search_in_multiple_indexes(self): self.es.index(index='groups', doc_type='groups', body={'budget': 1000}) @@ -61,7 +61,7 @@ def test_should_search_in_multiple_indexes(self): self.es.index(index='pcs', doc_type='pcs', body={'model': 'macbook'}) result = self.es.search(index=['users', 'pcs']) - self.assertEqual(2, result.get('hits').get('total')) + self.assertEqual(2, result.get('hits').get('total').get('value')) def test_usage_of_aggregations(self): self.es.index(index='index', doc_type='document', body={'genre': 'rock'}) @@ -78,7 +78,7 @@ def test_search_with_scroll_param(self): result = self.es.search(index='groups', params={'scroll': '1m', 'size': 30}) self.assertNotEqual(None, result.get('_scroll_id', None)) self.assertEqual(30, len(result.get('hits').get('hits'))) - self.assertEqual(100, result.get('hits').get('total')) + self.assertEqual(100, result.get('hits').get('total').get('value')) def test_search_with_match_query(self): for i in range(0, 10): @@ -86,12 +86,12 @@ def test_search_with_match_query(self): response = self.es.search(index='index_for_search', doc_type=DOC_TYPE, body={'query': {'match': {'data': 'TEST'}}}) - self.assertEqual(response['hits']['total'], 10) + self.assertEqual(response['hits']['total']['value'], 10) hits = response['hits']['hits'] self.assertEqual(len(hits), 10) response = self.es.search(index='index_for_search', doc_type=DOC_TYPE, body={'query': {'match': {'data': '3'}}}) - self.assertEqual(response['hits']['total'], 1) + self.assertEqual(response['hits']['total']['value'], 1) hits = response['hits']['hits'] self.assertEqual(len(hits), 1) self.assertEqual(hits[0]['_source'], {'data': 'test_3'}) @@ -100,7 +100,7 @@ def test_search_with_match_query_in_int_list(self): for i in range(0, 10): self.es.index(index='index_for_search', doc_type=DOC_TYPE, body={'data': [i, 11, 13]}) response = self.es.search(index='index_for_search', doc_type=DOC_TYPE, body={'query': {'match': {'data': 1}}}) - self.assertEqual(response['hits']['total'], 1) + self.assertEqual(response['hits']['total']['value'], 1) hits = response['hits']['hits'] self.assertEqual(len(hits), 1) self.assertEqual(hits[0]['_source'], {'data': [1, 11, 13]}) @@ -110,7 +110,7 @@ def test_search_with_match_query_in_string_list(self): self.es.index(index='index_for_search', doc_type=DOC_TYPE, body={'data': [str(i), 'two', 'three']}) response = self.es.search(index='index_for_search', doc_type=DOC_TYPE, body={'query': {'match': {'data': '1'}}}) - self.assertEqual(response['hits']['total'], 1) + self.assertEqual(response['hits']['total']['value'], 1) hits = response['hits']['hits'] self.assertEqual(len(hits), 1) self.assertEqual(hits[0]['_source'], {'data': ['1', 'two', 'three']}) @@ -121,12 +121,12 @@ def test_search_with_term_query(self): response = self.es.search(index='index_for_search', doc_type=DOC_TYPE, body={'query': {'term': {'data': 'TEST'}}}) - self.assertEqual(response['hits']['total'], 0) + self.assertEqual(response['hits']['total']['value'], 0) hits = response['hits']['hits'] self.assertEqual(len(hits), 0) response = self.es.search(index='index_for_search', doc_type=DOC_TYPE, body={'query': {'term': {'data': '3'}}}) - self.assertEqual(response['hits']['total'], 1) + self.assertEqual(response['hits']['total']['value'], 1) hits = response['hits']['hits'] self.assertEqual(len(hits), 1) self.assertEqual(hits[0]['_source'], {'data': 'test_3'}) @@ -137,7 +137,7 @@ def test_search_with_bool_query(self): response = self.es.search(index='index_for_search', doc_type=DOC_TYPE, body={'query': {'bool': {'filter': [{'term': {'id': 1}}]}}}) - self.assertEqual(response['hits']['total'], 1) + self.assertEqual(response['hits']['total']['value'], 1) hits = response['hits']['hits'] self.assertEqual(len(hits), 1) @@ -147,7 +147,7 @@ def test_search_with_terms_query(self): response = self.es.search(index='index_for_search', doc_type=DOC_TYPE, body={'query': {'terms': {'id': [1, 2, 3]}}}) - self.assertEqual(response['hits']['total'], 3) + self.assertEqual(response['hits']['total']['value'], 3) hits = response['hits']['hits'] self.assertEqual(len(hits), 3) @@ -159,7 +159,7 @@ def test_query_on_nested_data(self): for term, value, i in [('data.x', 1, 1), ('data.y', 'yes', 0)]: response = self.es.search(index='index_for_search', doc_type=DOC_TYPE, body={'query': {'term': {term: value}}}) - self.assertEqual(1, response['hits']['total']) + self.assertEqual(1, response['hits']['total']['value']) doc = response['hits']['hits'][0]['_source'] self.assertEqual(i, doc['id']) @@ -185,7 +185,7 @@ def test_search_with_bool_query_and_multi_match(self): } response = self.es.search(index='index_for_search', doc_type=DOC_TYPE, body=search_body) - self.assertEqual(response['hits']['total'], 10) + self.assertEqual(response['hits']['total']['value'], 10) hits = response['hits']['hits'] self.assertEqual(len(hits), 10) @@ -221,10 +221,10 @@ def test_msearch(self): result = self.es.msearch(index='index_for_search', body=body) response1, response2 = result['responses'] - self.assertEqual(response1['hits']['total'], 10) + self.assertEqual(response1['hits']['total']['value'], 10) hits1 = response1['hits']['hits'] self.assertEqual(len(hits1), 10) - self.assertEqual(response2['hits']['total'], 10) + self.assertEqual(response2['hits']['total']['value'], 10) hits2 = response2['hits']['hits'] self.assertEqual(len(hits2), 10) @@ -302,7 +302,7 @@ def test_search_with_range_query(self, _, query_range, expected_ids): body={'query': {'range': query_range}}, ) - self.assertEqual(len(expected_ids), response['hits']['total']) + self.assertEqual(len(expected_ids), response['hits']['total']['value']) hits = response['hits']['hits'] self.assertEqual(set(expected_ids), set(hit['_source']['id'] for hit in hits))