From 42ee1eaa03656bb77a24abf31aa5dffdc0b49640 Mon Sep 17 00:00:00 2001 From: Seth Michael Larson Date: Thu, 3 Dec 2020 09:40:19 -0600 Subject: [PATCH] Add UpdateByQueryResponse.success() --- elasticsearch_dsl/response/__init__.py | 3 +++ tests/test_integration/test_update_by_query.py | 2 ++ tests/test_update_by_query.py | 12 ++++++++++++ 3 files changed, 17 insertions(+) diff --git a/elasticsearch_dsl/response/__init__.py b/elasticsearch_dsl/response/__init__.py index d35decb4b..022ab158b 100644 --- a/elasticsearch_dsl/response/__init__.py +++ b/elasticsearch_dsl/response/__init__.py @@ -113,3 +113,6 @@ def __init__(self, search, response, doc_class=None): super(AttrDict, self).__setattr__("_search", search) super(AttrDict, self).__setattr__("_doc_class", doc_class) super(UpdateByQueryResponse, self).__init__(response) + + def success(self): + return not self.timed_out and not self.failures diff --git a/tests/test_integration/test_update_by_query.py b/tests/test_integration/test_update_by_query.py index 1acf153b6..64485391a 100644 --- a/tests/test_integration/test_update_by_query.py +++ b/tests/test_integration/test_update_by_query.py @@ -35,6 +35,7 @@ def test_update_by_query_no_script(write_client, setup_ubq_tests): assert response.updated == 52 assert response.deleted == 0 assert response.took > 0 + assert response.success() def test_update_by_query_with_script(write_client, setup_ubq_tests): @@ -69,3 +70,4 @@ def test_delete_by_query_with_script(write_client, setup_ubq_tests): assert response.total == 1 assert response.deleted == 1 + assert response.success() diff --git a/tests/test_update_by_query.py b/tests/test_update_by_query.py index 4f2393e1c..ab1d54866 100644 --- a/tests/test_update_by_query.py +++ b/tests/test_update_by_query.py @@ -18,6 +18,7 @@ from copy import deepcopy from elasticsearch_dsl import Q, UpdateByQuery +from elasticsearch_dsl.response import UpdateByQueryResponse def test_ubq_starts_with_no_query(): @@ -156,3 +157,14 @@ def test_overwrite_script(): } == ubq.to_dict() ubq = ubq.script(source="ctx._source.likes++") assert {"script": {"source": "ctx._source.likes++"}} == ubq.to_dict() + + +def test_update_by_query_response_success(): + ubqr = UpdateByQueryResponse({}, {"timed_out": False, "failures": []}) + assert ubqr.success() + + ubqr = UpdateByQueryResponse({}, {"timed_out": True, "failures": []}) + assert not ubqr.success() + + ubqr = UpdateByQueryResponse({}, {"timed_out": False, "failures": [{}]}) + assert not ubqr.success()