1
+ # import pytest
2
+ # import time
3
+ # import asyncio
4
+ # from unittest.mock import Mock, AsyncMock, patch
5
+ # from datetime import datetime
6
+ # import logging
7
+ # from apps.data_handler.handlers.loan_states.vesu.events import VesuLoanEntity
8
+ # from apps.shared.background_tasks.data_handler.event_tasks import process_vesu_events
9
+
10
+
11
+ # logging.basicConfig(level=logging.INFO)
12
+ # logger = logging.getLogger(__name__)
13
+
14
+ # @pytest.fixture
15
+ # def mock_vesu_entity():
16
+ # """Fixture to mock VesuLoanEntity."""
17
+ # mock = Mock(spec=VesuLoanEntity)
18
+ # mock.last_processed_block = 654244 # Initial block
19
+ # return mock
20
+
21
+ # @pytest.fixture
22
+ # def caplog(caplog):
23
+ # """Fixture to capture log output."""
24
+ # caplog.set_level(logging.INFO, logger="apps.shared.background_tasks.data_handler.event_tasks")
25
+ # return caplog
26
+
27
+ # def test_process_vesu_events_success(mock_vesu_entity, caplog, monkeypatch):
28
+ # """Test successful execution of process_vesu_events."""
29
+ # # Mock VesuLoanEntity Class
30
+ # monkeypatch.setattr(
31
+ # "apps.shared.background_tasks.data_handler.event_tasks.VesuLoanEntity",
32
+ # Mock(return_value=mock_vesu_entity)
33
+ # )
34
+ # # Mock update_positions_data
35
+ # async def mock_update():
36
+ # mock_vesu_entity.last_processed_block = 654249
37
+ # mock_vesu_entity.update_positions_data = AsyncMock(side_effect=mock_update)
38
+
39
+ # process_vesu_events()
40
+
41
+ # assert "Starting Vesu event processing" in caplog.text
42
+ # assert "Successfully processed Vesu events" in caplog.text
43
+ # assert "Blocks: 654244 to 654249" in caplog.text
44
+ # assert "(UTC)" in caplog.text
45
+ # assert mock_vesu_entity.last_processed_block == 654249
46
+
47
+ # def test_process_vesu_events_value_error(mock_vesu_entity, caplog, monkeypatch):
48
+ # """Test handling of ValueError."""
49
+ # monkeypatch.setattr(
50
+ # "apps.shared.background_tasks.data_handler.event_tasks.VesuLoanEntity",
51
+ # Mock(return_value=mock_vesu_entity)
52
+ # )
53
+ # mock_vesu_entity.update_positions_data = AsyncMock(side_effect=ValueError("Failed to fetch events from Starknet"))
54
+ # # Mock retry to prevent Retry exception
55
+ # with patch.object(process_vesu_events, "retry", side_effect=Exception("Retry mocked")):
56
+ # with pytest.raises(Exception, match="Retry mocked"):
57
+ # process_vesu_events()
58
+
59
+ # assert "Starting Vesu event processing" in caplog.text
60
+ # assert "Error processing Vesu events" in caplog.text
61
+ # assert "Failed to fetch events from Starknet" in caplog.text
62
+
63
+ # def test_process_vesu_events_unexpected_error(mock_vesu_entity, caplog, monkeypatch):
64
+ # """Test handling of unexpected errors."""
65
+ # monkeypatch.setattr(
66
+ # "apps.shared.background_tasks.data_handler.event_tasks.VesuLoanEntity",
67
+ # Mock(return_value=mock_vesu_entity)
68
+ # )
69
+ # mock_vesu_entity.update_positions_data = AsyncMock(side_effect=Exception("Unexpected Starknet error"))
70
+ # # Mock retry to prevent Retry exception
71
+ # with patch.object(process_vesu_events, "retry", side_effect=Exception("Retry mocked")):
72
+ # with pytest.raises(Exception, match="Retry mocked"):
73
+ # process_vesu_events()
74
+
75
+ # assert "Starting Vesu event processing" in caplog.text
76
+ # assert "Unexpected error processing Vesu events" in caplog.text
77
+ # assert "Unexpected Starknet error" in caplog.text
78
+
79
+ # def test_process_vesu_events_scheduled(mock_vesu_entity, caplog, monkeypatch):
80
+ # """Test periodic execution of process_vesu_events with a 3-second interval."""
81
+ # chunk_size = 5
82
+ # # Mock VesuLoanEntity
83
+ # monkeypatch.setattr(
84
+ # "apps.shared.background_tasks.data_handler.event_tasks.VesuLoanEntity",
85
+ # Mock(return_value=mock_vesu_entity)
86
+ # )
87
+
88
+ # async def mock_update():
89
+ # mock_vesu_entity.last_processed_block += chunk_size
90
+ # mock_vesu_entity.update_positions_data = AsyncMock(side_effect=mock_update)
91
+
92
+ # # Simulate 3 runs with 10-second intervals
93
+ # initial_block = mock_vesu_entity.last_processed_block
94
+ # num_runs = 3
95
+ # for i in range(num_runs):
96
+ # process_vesu_events()
97
+ # if i < num_runs - 1:
98
+ # time.sleep(10)
99
+
100
+
101
+ # expected_blocks = [
102
+ # (initial_block, initial_block + chunk_size),
103
+ # (initial_block + chunk_size, initial_block + 2 * chunk_size),
104
+ # (initial_block + 2 * chunk_size, initial_block + 3 * chunk_size)
105
+ # ]
106
+ # for start_block, end_block in expected_blocks:
107
+ # assert f"Blocks: {start_block} to {end_block}" in caplog.text
108
+
109
+
110
+ # assert mock_vesu_entity.last_processed_block == initial_block + num_runs * chunk_size
111
+ # assert "Starting Vesu event processing" in caplog.text
112
+ # assert "Successfully processed Vesu events" in caplog.text
113
+ # assert "(UTC)" in caplog.text
0 commit comments