From f56ec6afe98e5a6528b4a511fbe9307ba51d8d62 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Mohanty Date: Mon, 30 Sep 2019 23:48:09 +0530 Subject: [PATCH 1/2] Add default value to doc_type in index method as it is by default set to '_doc', as while mocking with es 7.x it fails if we do not add the doc type specifically when calling the index method with the index mock to accept doc_type by default to _doc as with elastic search 7.x the value is set to _doc by default, and when mocking the index method we do have to pass the doc_type as mandatory --- elasticmock/fake_elasticsearch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elasticmock/fake_elasticsearch.py b/elasticmock/fake_elasticsearch.py index 5a556d5..de1e0ee 100644 --- a/elasticmock/fake_elasticsearch.py +++ b/elasticmock/fake_elasticsearch.py @@ -44,7 +44,7 @@ def info(self, params=None): @query_params('consistency', 'op_type', 'parent', 'refresh', 'replication', 'routing', 'timeout', 'timestamp', 'ttl', 'version', 'version_type') - def index(self, index, doc_type, body, id=None, params=None): + def index(self, index, body, doc_type='_doc', id=None, params=None): if index not in self.__documents_dict: self.__documents_dict[index] = list() From e576f2c97b665bb24f7bf901540f98d4f68c68c9 Mon Sep 17 00:00:00 2001 From: Ashish Kumar Mohanty Date: Tue, 1 Oct 2019 00:04:07 +0530 Subject: [PATCH 2/2] Add unit test --- tests/test_elasticmock.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_elasticmock.py b/tests/test_elasticmock.py index 27deaac..d1b7e3a 100644 --- a/tests/test_elasticmock.py +++ b/tests/test_elasticmock.py @@ -27,6 +27,14 @@ def test_should_index_document(self): self.assertEqual(1, data.get('_version')) self.assertEqual(self.index_name, data.get('_index')) + def test_should_index_document_without_doc_type(self): + data = self.es.index(index=self.index_name, body=self.body) + + self.assertEqual('_doc', data.get('_type')) + self.assertTrue(data.get('created')) + self.assertEqual(1, data.get('_version')) + self.assertEqual(self.index_name, data.get('_index')) + def test_should_raise_notfounderror_when_nonindexed_id_is_used(self): with self.assertRaises(NotFoundError): self.es.get(index=self.index_name, id='1')