Coverage for test/esxport/retry_test.py: 84%
19 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-22 17:59 +0530
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-22 17:59 +0530
1"""Retry requests."""
2from __future__ import annotations
4from typing import TYPE_CHECKING, Any
5from unittest import mock
7import pytest
8from elasticsearch.exceptions import ConnectionError
10from src.constant import TIMES_TO_TRY
12if TYPE_CHECKING:
13 from unittest.mock import Mock
15 from typing_extensions import Self
17 from src.esxport import EsXport
20class TestRetry:
21 """Test that retry happens on connection errors."""
23 def test_retry_happens_on_connection_error(self: Self, mocker: Mock, esxport_obj: EsXport) -> None:
24 """Test that retry happens on connect errors."""
25 esxport_obj._check_indexes.retry.sleep = mock.Mock() # type: ignore[attr-defined]
26 mocker.patch.object(
27 esxport_obj.es_client,
28 "indices_exists",
29 side_effect=ConnectionError("mocked error"),
30 )
31 with pytest.raises(ConnectionError):
32 esxport_obj._check_indexes()
34 stats: dict[str, Any] = esxport_obj._check_indexes.retry.statistics # type: ignore[attr-defined]
35 assert "attempt_number" in stats
36 assert stats["attempt_number"] == TIMES_TO_TRY