Skip to content

Commit

Permalink
Merge pull request #32 from adityaghosh/fixing-existing-doc-update
Browse files Browse the repository at this point in the history
Adding fix for updating existing doc using index.
  • Loading branch information
vrcmarcos authored Feb 4, 2020
2 parents 4df0f86 + fa98320 commit 30d61d1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions elasticmock/fake_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ def info(self, 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()

version = 1

if id is None:
id = get_random_id()

version = 1
else:
doc = self.get(index, id, doc_type)
version = doc['_version'] + 1
delete_response = self.delete(index, doc_type, id)

self.__documents_dict[index].append({
'_type': doc_type,
Expand Down
18 changes: 18 additions & 0 deletions tests/test_elasticmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def setUp(self):
self.index_name = 'test_index'
self.doc_type = 'doc-Type'
self.body = {'string': 'content', 'id': 1}
self.updated_body = {'string': 'content-updated', 'id': 1}

def test_should_create_fake_elasticsearch_instance(self):
self.assertIsInstance(self.es, FakeElasticsearch)
Expand Down Expand Up @@ -286,5 +287,22 @@ def test_scrolling(self):
self.assertEqual(10, len(result.get('hits').get('hits')))
self.assertEqual(100, result.get('hits').get('total'))

def test_update_existing_doc(self):
data = self.es.index(index=self.index_name, doc_type=self.doc_type, body=self.body)
document_id = data.get('_id')
data = self.es.index(index=self.index_name, id=document_id, doc_type=self.doc_type, body=self.updated_body)
target_doc = self.es.get(index=self.index_name, id=document_id)

expected = {
'_type': self.doc_type,
'_source': self.updated_body,
'_index': self.index_name,
'_version': 2,
'found': True,
'_id': document_id
}

self.assertDictEqual(expected, target_doc)

if __name__ == '__main__':
unittest.main()

0 comments on commit 30d61d1

Please sign in to comment.