Added fix for key error because of missing 'hits' key. (#616)

Updated CHANGELOG.md.



nox formatting applied.



Added new unit test for actions scan function.



Added type hints & nox formatting.



Added fix to async scan function & added matching unit tests for async.

Signed-off-by: Djcarrillo6 <[email protected]>
This commit is contained in:
DJ Carrillo
2023-12-04 09:26:25 -05:00
committed by GitHub
parent 44f916ca52
commit db61b5943b
5 changed files with 70 additions and 12 deletions
@@ -776,6 +776,34 @@ class TestScan(object):
}
assert async_client.scroll.call_args[1]["sort"] == "asc"
async def test_async_scan_with_missing_hits_key(
self, async_client: Any, scan_teardown: Any
) -> None:
with patch.object(
async_client,
"search",
return_value=MockResponse({"_scroll_id": "dummy_scroll_id", "_shards": {}}),
):
with patch.object(
async_client,
"scroll",
return_value=MockResponse(
{"_scroll_id": "dummy_scroll_id", "_shards": {}}
),
):
with patch.object(
async_client, "clear_scroll", return_value=MockResponse({})
):
async_scan_result = [
hit
async for hit in actions.async_scan(
async_client, query={"query": {"match_all": {}}}
)
]
assert (
async_scan_result == []
), "Expected empty results when 'hits' key is missing"
@pytest.fixture(scope="function") # type: ignore
async def reindex_setup(async_client: Any) -> Any:
@@ -28,6 +28,7 @@
import threading
import time
from typing import Any
from unittest.mock import Mock
import mock
import pytest
@@ -270,3 +271,24 @@ class TestExpandActions(TestCase):
self.assertEqual(
('{"index":{}}', "whatever"), helpers.expand_action("whatever")
)
class TestScanFunction(TestCase):
@mock.patch("opensearchpy.OpenSearch.clear_scroll")
@mock.patch("opensearchpy.OpenSearch.scroll")
@mock.patch("opensearchpy.OpenSearch.search")
def test_scan_with_missing_hits_key(
self, mock_search: Mock, mock_scroll: Mock, mock_clear_scroll: Mock
) -> None:
# Simulate a response where the 'hits' key is missing
mock_search.return_value = {"_scroll_id": "dummy_scroll_id", "_shards": {}}
mock_scroll.side_effect = [{"_scroll_id": "dummy_scroll_id", "_shards": {}}]
mock_clear_scroll.return_value = None
client = OpenSearch()
# The test should pass without raising a KeyError
scan_result = list(helpers.scan(client, query={"query": {"match_all": {}}}))
assert scan_result == [], "Expected empty results when 'hits' key is missing"