Skip to content

Commit

Permalink
Fix test_logger on Python 3.7
Browse files Browse the repository at this point in the history
We're using two pytest fixtures (caplog, pytest.raises) which also
happen to make the code more robust.
  • Loading branch information
pquentin committed Nov 7, 2023
1 parent dca4793 commit 18867bf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ignore = E203, E266, E501, W503

[tool:pytest]
junit_family=legacy
addopts = -vvv -p no:logging --cov-report=term-missing --cov=elasticsearch --cov-config=.coveragerc
addopts = -vvv --cov-report=term-missing --cov=elasticsearch --cov-config=.coveragerc

[tool:isort]
profile=black
Expand Down
27 changes: 15 additions & 12 deletions test_elasticsearch/test_async/test_server/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# under the License.

import asyncio
import logging
from datetime import datetime, timedelta, timezone
from unittest.mock import MagicMock, call, patch

Expand Down Expand Up @@ -619,8 +620,10 @@ async def test_no_scroll_id_fast_route(self, async_client, scan_teardown):
scroll_mock.assert_not_called()
clear_mock.assert_not_called()

@patch("elasticsearch._async.helpers.logger")
async def test_logger(self, logger_mock, async_client, scan_teardown):
async def test_logger(
self, caplog: pytest.LogCaptureFixture, async_client, scan_teardown
):
caplog.set_level(logging.WARNING, logger="elasticsearch.helpers")
bulk = []
for x in range(4):
bulk.append({"index": {"_index": "test_index"}})
Expand All @@ -640,12 +643,16 @@ async def test_logger(self, logger_mock, async_client, scan_teardown):
clear_scroll=False,
)
]
logger_mock.warning.assert_called()

assert caplog.messages == [
"Scroll request has only succeeded on 4 (+0 skipped) shards out of 5."
]

caplog.clear()
with patch.object(
async_client, "options", return_value=async_client
), patch.object(async_client, "scroll", MockScroll()):
try:
with pytest.raises(ScanError):
_ = [
x
async for x in helpers.async_scan(
Expand All @@ -656,14 +663,10 @@ async def test_logger(self, logger_mock, async_client, scan_teardown):
clear_scroll=False,
)
]
except ScanError:
pass
logger_mock.warning.assert_called_with(
"Scroll request has only succeeded on %d (+%d skipped) shards out of %d.",
4,
0,
5,
)

assert caplog.messages == [
"Scroll request has only succeeded on 4 (+0 skipped) shards out of 5."
]

async def test_clear_scroll(self, async_client, scan_teardown):
bulk = []
Expand Down

0 comments on commit 18867bf

Please sign in to comment.