Skip to content

Commit 18867bf

Browse files
committed
Fix test_logger on Python 3.7
We're using two pytest fixtures (caplog, pytest.raises) which also happen to make the code more robust.
1 parent dca4793 commit 18867bf

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ ignore = E203, E266, E501, W503
33

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

88
[tool:isort]
99
profile=black

test_elasticsearch/test_async/test_server/test_helpers.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717

1818
import asyncio
19+
import logging
1920
from datetime import datetime, timedelta, timezone
2021
from unittest.mock import MagicMock, call, patch
2122

@@ -619,8 +620,10 @@ async def test_no_scroll_id_fast_route(self, async_client, scan_teardown):
619620
scroll_mock.assert_not_called()
620621
clear_mock.assert_not_called()
621622

622-
@patch("elasticsearch._async.helpers.logger")
623-
async def test_logger(self, logger_mock, async_client, scan_teardown):
623+
async def test_logger(
624+
self, caplog: pytest.LogCaptureFixture, async_client, scan_teardown
625+
):
626+
caplog.set_level(logging.WARNING, logger="elasticsearch.helpers")
624627
bulk = []
625628
for x in range(4):
626629
bulk.append({"index": {"_index": "test_index"}})
@@ -640,12 +643,16 @@ async def test_logger(self, logger_mock, async_client, scan_teardown):
640643
clear_scroll=False,
641644
)
642645
]
643-
logger_mock.warning.assert_called()
644646

647+
assert caplog.messages == [
648+
"Scroll request has only succeeded on 4 (+0 skipped) shards out of 5."
649+
]
650+
651+
caplog.clear()
645652
with patch.object(
646653
async_client, "options", return_value=async_client
647654
), patch.object(async_client, "scroll", MockScroll()):
648-
try:
655+
with pytest.raises(ScanError):
649656
_ = [
650657
x
651658
async for x in helpers.async_scan(
@@ -656,14 +663,10 @@ async def test_logger(self, logger_mock, async_client, scan_teardown):
656663
clear_scroll=False,
657664
)
658665
]
659-
except ScanError:
660-
pass
661-
logger_mock.warning.assert_called_with(
662-
"Scroll request has only succeeded on %d (+%d skipped) shards out of %d.",
663-
4,
664-
0,
665-
5,
666-
)
666+
667+
assert caplog.messages == [
668+
"Scroll request has only succeeded on 4 (+0 skipped) shards out of 5."
669+
]
667670

668671
async def test_clear_scroll(self, async_client, scan_teardown):
669672
bulk = []

0 commit comments

Comments
 (0)