88TEST_ERROR = "Test error"
99TEST_QUERY = "SELECT 1 + 2 AS sum"
1010
11+
1112@pytest .fixture
1213def mock_connection ():
1314 """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 :
1516 # Setup the mock to return a connection-like object
1617 mock_connection = unittest .mock .MagicMock ()
1718 # Use the endpoint fixture value via the function parameter
@@ -24,12 +25,12 @@ def mock_connection():
2425@pytest .fixture
2526def mock_aio_connection ():
2627 """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 :
2829 # Setup the mock to return None (as __init__ does)
2930 mock_init .return_value = None
3031
3132 # 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 :
3334 # Create event loop if there isn't one currently
3435 loop = asyncio .new_event_loop ()
3536 asyncio .set_event_loop (loop )
@@ -42,27 +43,29 @@ def mock_aio_connection():
4243
4344def create_mock_discovery_resolver (path ):
4445 """Create a mock discovery resolver that raises exception if called."""
46+
4547 def _mock_fixture ():
4648 with unittest .mock .patch (path ) as mock_resolve :
4749 # Configure mock to throw an exception if called
4850 mock_resolve .side_effect = Exception ("Discovery should not be executed when discovery is disabled" )
4951 yield mock_resolve
52+
5053 return _mock_fixture
5154
5255
5356# 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+ )
5663
5764
5865# Basic unit tests for DriverConfig
5966def test_driver_config_has_disable_discovery_option (endpoint , database ):
6067 """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 )
6669 assert hasattr (config , "disable_discovery" )
6770 assert config .disable_discovery is True
6871
@@ -110,9 +113,11 @@ def create_completed_future():
110113
111114
112115# 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+ ):
114119 """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 :
116121 driver = ydb .Driver (driver_config = driver_config_disabled_discovery )
117122
118123 try :
@@ -129,15 +134,17 @@ def test_sync_driver_discovery_disabled_mock(driver_config_disabled_discovery, m
129134 pass # Expected exception, we just want to ensure no discovery occurs
130135
131136 # 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"
133140 finally :
134141 # Clean up
135142 driver .stop ()
136143
137144
138145def test_sync_driver_discovery_enabled_mock (driver_config_enabled_discovery , mock_connection ):
139146 """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 :
141148 mock_discovery_instance = unittest .mock .MagicMock ()
142149 mock_discovery_class .return_value = mock_discovery_instance
143150
@@ -158,51 +165,53 @@ def setup_async_driver_mocks():
158165 mocks = {}
159166
160167 # 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 ()
163170
164171 # Mock the event loop
165- loop_patcher = unittest .mock .patch (' asyncio.get_event_loop' )
172+ loop_patcher = unittest .mock .patch (" asyncio.get_event_loop" )
166173 mock_loop = loop_patcher .start ()
167174 mock_loop_instance = unittest .mock .MagicMock ()
168175 mock_loop .return_value = mock_loop_instance
169176 mock_loop_instance .create_task .return_value = unittest .mock .MagicMock ()
170- mocks [' mock_loop' ] = mock_loop
177+ mocks [" mock_loop" ] = mock_loop
171178
172179 # 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" )
174181 mock_stop = stop_patcher .start ()
175182 mock_stop .return_value = create_completed_future ()
176- mocks [' mock_stop' ] = mock_stop
183+ mocks [" mock_stop" ] = mock_stop
177184
178185 # Add cleanup for all patchers
179- mocks [' patchers' ] = [discovery_patcher , loop_patcher , stop_patcher ]
186+ mocks [" patchers" ] = [discovery_patcher , loop_patcher , stop_patcher ]
180187
181188 return mocks
182189
183190
184191def teardown_async_mocks (mocks ):
185192 """Clean up all mock patchers."""
186- for patcher in mocks [' patchers' ]:
193+ for patcher in mocks [" patchers" ]:
187194 patcher .stop ()
188195
189196
190197# Mock tests for asynchronous driver
191198@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+ ):
193202 """Test that when disable_discovery=True, the discovery is not created and resolver is not called (mock)."""
194203 mocks = setup_async_driver_mocks ()
195204
196205 try :
197206 # 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 :
199208 mock_call .return_value = create_future_with_error ()
200209
201210 driver = ydb .aio .Driver (driver_config = driver_config_disabled_discovery )
202211
203212 try :
204213 # Check that the discovery class was not instantiated
205- mocks [' mock_discovery_class' ].assert_not_called ()
214+ mocks [" mock_discovery_class" ].assert_not_called ()
206215
207216 # Check that discovery is disabled in debug details
208217 assert_discovery_disabled (driver )
@@ -219,7 +228,9 @@ async def test_aio_driver_discovery_disabled_mock(driver_config_disabled_discove
219228 pass # Other exceptions are expected as we're using mocks
220229
221230 # 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"
223234 finally :
224235 # The stop method is already mocked, so we don't need to await it
225236 pass
@@ -234,13 +245,13 @@ async def test_aio_driver_discovery_enabled_mock(driver_config_enabled_discovery
234245
235246 try :
236247 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
238249
239250 driver = ydb .aio .Driver (driver_config = driver_config_enabled_discovery )
240251
241252 try :
242253 # Check that the discovery class was instantiated
243- mocks [' mock_discovery_class' ].assert_called_once ()
254+ mocks [" mock_discovery_class" ].assert_called_once ()
244255 finally :
245256 # The stop method is already mocked, so we don't need to await it
246257 pass
@@ -265,6 +276,7 @@ def test_integration_disable_discovery(driver_config_disabled_discovery):
265276
266277 # Try to execute a simple query to ensure it works with discovery disabled
267278 with ydb .SessionPool (driver ) as pool :
279+
268280 def query_callback (session ):
269281 result_sets = session .transaction ().execute (TEST_QUERY , commit_tx = True )
270282 assert len (result_sets ) == 1
@@ -297,4 +309,4 @@ async def query_callback(session):
297309 finally :
298310 await session_pool .stop ()
299311 finally :
300- await driver .stop (timeout = 10 )
312+ await driver .stop (timeout = 10 )
0 commit comments