8
8
TEST_ERROR = "Test error"
9
9
TEST_QUERY = "SELECT 1 + 2 AS sum"
10
10
11
+
11
12
@pytest .fixture
12
13
def mock_connection ():
13
14
"""Mock a YDB connection to avoid actual connections."""
14
- with unittest .mock .patch (' ydb.connection.Connection.ready_factory' ) as mock_factory :
15
+ with unittest .mock .patch (" ydb.connection.Connection.ready_factory" ) as mock_factory :
15
16
# Setup the mock to return a connection-like object
16
17
mock_connection = unittest .mock .MagicMock ()
17
18
# Use the endpoint fixture value via the function parameter
@@ -24,12 +25,12 @@ def mock_connection():
24
25
@pytest .fixture
25
26
def mock_aio_connection ():
26
27
"""Mock a YDB async connection to avoid actual connections."""
27
- with unittest .mock .patch (' ydb.aio.connection.Connection.__init__' ) as mock_init :
28
+ with unittest .mock .patch (" ydb.aio.connection.Connection.__init__" ) as mock_init :
28
29
# Setup the mock to return None (as __init__ does)
29
30
mock_init .return_value = None
30
31
31
32
# Mock connection_ready method
32
- with unittest .mock .patch (' ydb.aio.connection.Connection.connection_ready' ) as mock_ready :
33
+ with unittest .mock .patch (" ydb.aio.connection.Connection.connection_ready" ) as mock_ready :
33
34
# Create event loop if there isn't one currently
34
35
loop = asyncio .new_event_loop ()
35
36
asyncio .set_event_loop (loop )
@@ -42,27 +43,29 @@ def mock_aio_connection():
42
43
43
44
def create_mock_discovery_resolver (path ):
44
45
"""Create a mock discovery resolver that raises exception if called."""
46
+
45
47
def _mock_fixture ():
46
48
with unittest .mock .patch (path ) as mock_resolve :
47
49
# Configure mock to throw an exception if called
48
50
mock_resolve .side_effect = Exception ("Discovery should not be executed when discovery is disabled" )
49
51
yield mock_resolve
52
+
50
53
return _mock_fixture
51
54
52
55
53
56
# Mock discovery resolvers to verify no discovery requests are made
54
- mock_discovery_resolver = pytest .fixture (create_mock_discovery_resolver ('ydb.resolver.DiscoveryEndpointsResolver.context_resolve' ))
55
- mock_aio_discovery_resolver = pytest .fixture (create_mock_discovery_resolver ('ydb.aio.resolver.DiscoveryEndpointsResolver.resolve' ))
57
+ mock_discovery_resolver = pytest .fixture (
58
+ create_mock_discovery_resolver ("ydb.resolver.DiscoveryEndpointsResolver.context_resolve" )
59
+ )
60
+ mock_aio_discovery_resolver = pytest .fixture (
61
+ create_mock_discovery_resolver ("ydb.aio.resolver.DiscoveryEndpointsResolver.resolve" )
62
+ )
56
63
57
64
58
65
# Basic unit tests for DriverConfig
59
66
def test_driver_config_has_disable_discovery_option (endpoint , database ):
60
67
"""Test that DriverConfig has the disable_discovery option."""
61
- config = ydb .DriverConfig (
62
- endpoint = endpoint ,
63
- database = database ,
64
- disable_discovery = True
65
- )
68
+ config = ydb .DriverConfig (endpoint = endpoint , database = database , disable_discovery = True )
66
69
assert hasattr (config , "disable_discovery" )
67
70
assert config .disable_discovery is True
68
71
@@ -110,9 +113,11 @@ def create_completed_future():
110
113
111
114
112
115
# Mock tests for synchronous driver
113
- def test_sync_driver_discovery_disabled_mock (driver_config_disabled_discovery , mock_connection , mock_discovery_resolver ):
116
+ def test_sync_driver_discovery_disabled_mock (
117
+ driver_config_disabled_discovery , mock_connection , mock_discovery_resolver
118
+ ):
114
119
"""Test that when disable_discovery=True, the discovery thread is not started and resolver is not called (mock)."""
115
- with unittest .mock .patch (' ydb.pool.Discovery' ) as mock_discovery_class :
120
+ with unittest .mock .patch (" ydb.pool.Discovery" ) as mock_discovery_class :
116
121
driver = ydb .Driver (driver_config = driver_config_disabled_discovery )
117
122
118
123
try :
@@ -129,15 +134,17 @@ def test_sync_driver_discovery_disabled_mock(driver_config_disabled_discovery, m
129
134
pass # Expected exception, we just want to ensure no discovery occurs
130
135
131
136
# Verify the mock wasn't called
132
- assert not mock_discovery_resolver .called , "Discovery resolver should not be called when discovery is disabled"
137
+ assert (
138
+ not mock_discovery_resolver .called
139
+ ), "Discovery resolver should not be called when discovery is disabled"
133
140
finally :
134
141
# Clean up
135
142
driver .stop ()
136
143
137
144
138
145
def test_sync_driver_discovery_enabled_mock (driver_config_enabled_discovery , mock_connection ):
139
146
"""Test that when disable_discovery=False, the discovery thread is started (mock)."""
140
- with unittest .mock .patch (' ydb.pool.Discovery' ) as mock_discovery_class :
147
+ with unittest .mock .patch (" ydb.pool.Discovery" ) as mock_discovery_class :
141
148
mock_discovery_instance = unittest .mock .MagicMock ()
142
149
mock_discovery_class .return_value = mock_discovery_instance
143
150
@@ -158,51 +165,53 @@ def setup_async_driver_mocks():
158
165
mocks = {}
159
166
160
167
# Create mock for Discovery class
161
- discovery_patcher = unittest .mock .patch (' ydb.aio.pool.Discovery' )
162
- mocks [' mock_discovery_class' ] = discovery_patcher .start ()
168
+ discovery_patcher = unittest .mock .patch (" ydb.aio.pool.Discovery" )
169
+ mocks [" mock_discovery_class" ] = discovery_patcher .start ()
163
170
164
171
# Mock the event loop
165
- loop_patcher = unittest .mock .patch (' asyncio.get_event_loop' )
172
+ loop_patcher = unittest .mock .patch (" asyncio.get_event_loop" )
166
173
mock_loop = loop_patcher .start ()
167
174
mock_loop_instance = unittest .mock .MagicMock ()
168
175
mock_loop .return_value = mock_loop_instance
169
176
mock_loop_instance .create_task .return_value = unittest .mock .MagicMock ()
170
- mocks [' mock_loop' ] = mock_loop
177
+ mocks [" mock_loop" ] = mock_loop
171
178
172
179
# Mock the connection pool stop method
173
- stop_patcher = unittest .mock .patch (' ydb.aio.pool.ConnectionPool.stop' )
180
+ stop_patcher = unittest .mock .patch (" ydb.aio.pool.ConnectionPool.stop" )
174
181
mock_stop = stop_patcher .start ()
175
182
mock_stop .return_value = create_completed_future ()
176
- mocks [' mock_stop' ] = mock_stop
183
+ mocks [" mock_stop" ] = mock_stop
177
184
178
185
# Add cleanup for all patchers
179
- mocks [' patchers' ] = [discovery_patcher , loop_patcher , stop_patcher ]
186
+ mocks [" patchers" ] = [discovery_patcher , loop_patcher , stop_patcher ]
180
187
181
188
return mocks
182
189
183
190
184
191
def teardown_async_mocks (mocks ):
185
192
"""Clean up all mock patchers."""
186
- for patcher in mocks [' patchers' ]:
193
+ for patcher in mocks [" patchers" ]:
187
194
patcher .stop ()
188
195
189
196
190
197
# Mock tests for asynchronous driver
191
198
@pytest .mark .asyncio
192
- async def test_aio_driver_discovery_disabled_mock (driver_config_disabled_discovery , mock_aio_connection , mock_aio_discovery_resolver ):
199
+ async def test_aio_driver_discovery_disabled_mock (
200
+ driver_config_disabled_discovery , mock_aio_connection , mock_aio_discovery_resolver
201
+ ):
193
202
"""Test that when disable_discovery=True, the discovery is not created and resolver is not called (mock)."""
194
203
mocks = setup_async_driver_mocks ()
195
204
196
205
try :
197
206
# Mock the pool's call method to prevent unhandled exceptions
198
- with unittest .mock .patch (' ydb.aio.pool.ConnectionPool.__call__' ) as mock_call :
207
+ with unittest .mock .patch (" ydb.aio.pool.ConnectionPool.__call__" ) as mock_call :
199
208
mock_call .return_value = create_future_with_error ()
200
209
201
210
driver = ydb .aio .Driver (driver_config = driver_config_disabled_discovery )
202
211
203
212
try :
204
213
# Check that the discovery class was not instantiated
205
- mocks [' mock_discovery_class' ].assert_not_called ()
214
+ mocks [" mock_discovery_class" ].assert_not_called ()
206
215
207
216
# Check that discovery is disabled in debug details
208
217
assert_discovery_disabled (driver )
@@ -219,7 +228,9 @@ async def test_aio_driver_discovery_disabled_mock(driver_config_disabled_discove
219
228
pass # Other exceptions are expected as we're using mocks
220
229
221
230
# Verify the mock wasn't called
222
- assert not mock_aio_discovery_resolver .called , "Discovery resolver should not be called when discovery is disabled"
231
+ assert (
232
+ not mock_aio_discovery_resolver .called
233
+ ), "Discovery resolver should not be called when discovery is disabled"
223
234
finally :
224
235
# The stop method is already mocked, so we don't need to await it
225
236
pass
@@ -234,13 +245,13 @@ async def test_aio_driver_discovery_enabled_mock(driver_config_enabled_discovery
234
245
235
246
try :
236
247
mock_discovery_instance = unittest .mock .MagicMock ()
237
- mocks [' mock_discovery_class' ].return_value = mock_discovery_instance
248
+ mocks [" mock_discovery_class" ].return_value = mock_discovery_instance
238
249
239
250
driver = ydb .aio .Driver (driver_config = driver_config_enabled_discovery )
240
251
241
252
try :
242
253
# Check that the discovery class was instantiated
243
- mocks [' mock_discovery_class' ].assert_called_once ()
254
+ mocks [" mock_discovery_class" ].assert_called_once ()
244
255
finally :
245
256
# The stop method is already mocked, so we don't need to await it
246
257
pass
@@ -265,6 +276,7 @@ def test_integration_disable_discovery(driver_config_disabled_discovery):
265
276
266
277
# Try to execute a simple query to ensure it works with discovery disabled
267
278
with ydb .SessionPool (driver ) as pool :
279
+
268
280
def query_callback (session ):
269
281
result_sets = session .transaction ().execute (TEST_QUERY , commit_tx = True )
270
282
assert len (result_sets ) == 1
@@ -297,4 +309,4 @@ async def query_callback(session):
297
309
finally :
298
310
await session_pool .stop ()
299
311
finally :
300
- await driver .stop (timeout = 10 )
312
+ await driver .stop (timeout = 10 )
0 commit comments