From 203658174b7b89ae2762f6a1555fa0cb3903157f Mon Sep 17 00:00:00 2001 From: Marcos Cardoso Date: Sun, 16 Feb 2020 22:45:39 -0300 Subject: [PATCH] Fix https://github.com/vrcmarcos/elasticmock/issues/34 --- elasticmock/fake_elasticsearch.py | 8 ++++---- tests/test_elasticmock.py | 10 ++++++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/elasticmock/fake_elasticsearch.py b/elasticmock/fake_elasticsearch.py index 4d52338..fa0f32b 100644 --- a/elasticmock/fake_elasticsearch.py +++ b/elasticmock/fake_elasticsearch.py @@ -52,10 +52,10 @@ def index(self, index, body, doc_type='_doc', id=None, params=None): if id is None: id = get_random_id() - else: - doc = self.get(index, id, doc_type) + elif self.exists(index, doc_type, id, params=params): + doc = self.get(index, id, doc_type, params=params) version = doc['_version'] + 1 - delete_response = self.delete(index, doc_type, id) + self.delete(index, doc_type, id) self.__documents_dict[index].append({ '_type': doc_type, @@ -75,7 +75,7 @@ def index(self, index, body, doc_type='_doc', id=None, params=None): @query_params('consistency', 'op_type', 'parent', 'refresh', 'replication', 'routing', 'timeout', 'timestamp', 'ttl', 'version', 'version_type') - def bulk(self, body, params=None): + def bulk(self, body, index=None, doc_type=None, params=None): version = 1 items = [] diff --git a/tests/test_elasticmock.py b/tests/test_elasticmock.py index cc9d0c4..96b07f2 100644 --- a/tests/test_elasticmock.py +++ b/tests/test_elasticmock.py @@ -2,6 +2,8 @@ import json import unittest +from datetime import datetime + import elasticsearch from elasticsearch.exceptions import NotFoundError @@ -15,7 +17,11 @@ def setUp(self): self.es = elasticsearch.Elasticsearch(hosts=[{'host': 'localhost', 'port': 9200}]) self.index_name = 'test_index' self.doc_type = 'doc-Type' - self.body = {'string': 'content', 'id': 1} + self.body = { + 'author': 'kimchy', + 'text': 'Elasticsearch: cool. bonsai cool.', + 'timestamp': datetime.now(), + } self.updated_body = {'string': 'content-updated', 'id': 1} def test_should_create_fake_elasticsearch_instance(self): @@ -32,7 +38,7 @@ def test_should_index_document(self): def test_should_bulk_index_documents(self): action = {'index': {'_index': self.index_name, '_type': self.doc_type}} action_json = json.dumps(action) - body_json = json.dumps(self.body) + body_json = json.dumps(self.body, default=str) num_of_documents = 10 lines = []