Skip to content

Commit 013db20

Browse files
committed
using prepared stmts and context managers in tests
1 parent 087e711 commit 013db20

File tree

3 files changed

+69
-17
lines changed

3 files changed

+69
-17
lines changed

BUILD_TEST_PROGRESS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@
2626
- black: ✅ 105 files would be left unchanged
2727
- isort: ✅ All checks passed
2828
- mypy: ✅ Success: no issues found in 12 source files
29+
30+
#### 3. ✅ `make test-unit` - Completed
31+
- Fixed hanging tests by correcting mock future callback patterns:
32+
- test_schema_changes.py::test_concurrent_ddl_operations
33+
- test_session_edge_cases.py::test_execute_batch_statement
34+
- Key fix: Use `asyncio.get_running_loop().call_soon()` to delay callbacks
35+
- All 560 unit tests passing with coverage report generated

tests/unit/test_schema_changes.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,46 @@ def add_callbacks(callback=None, errback=None):
5151
future.clear_callbacks = Mock()
5252
return future
5353

54+
def _create_mock_future(self, result=None, error=None):
55+
"""Create a properly configured mock future that simulates driver behavior."""
56+
future = Mock()
57+
58+
# Store callbacks
59+
callbacks = []
60+
errbacks = []
61+
62+
def add_callbacks(callback=None, errback=None):
63+
if callback:
64+
callbacks.append(callback)
65+
if errback:
66+
errbacks.append(errback)
67+
68+
# Delay the callback execution to allow AsyncResultHandler to set up properly
69+
def execute_callback():
70+
if error:
71+
if errback:
72+
errback(error)
73+
else:
74+
if callback and result is not None:
75+
# For successful results, pass rows
76+
rows = getattr(result, "rows", [])
77+
callback(rows)
78+
79+
# Schedule callback for next event loop iteration
80+
try:
81+
loop = asyncio.get_running_loop()
82+
loop.call_soon(execute_callback)
83+
except RuntimeError:
84+
# No event loop, execute immediately
85+
execute_callback()
86+
87+
future.add_callbacks = add_callbacks
88+
future.has_more_pages = False
89+
future.timeout = None
90+
future.clear_callbacks = Mock()
91+
92+
return future
93+
5494
@pytest.mark.asyncio
5595
async def test_create_table_already_exists(self, mock_session):
5696
"""Test handling of AlreadyExists errors during schema changes."""
@@ -115,14 +155,10 @@ def execute_async_side_effect(*args, **kwargs):
115155
query = args[0] if args else kwargs.get("query", "")
116156
ddl_operations.append(query)
117157

118-
# Create a successful future
119-
future = Mock()
120-
future.result = Mock(return_value=Mock(one=Mock(return_value=None)))
121-
future.add_callbacks = Mock()
122-
future.has_more_pages = False
123-
future.timeout = None
124-
future.clear_callbacks = Mock()
125-
return future
158+
# Use the same pattern as test_session_edge_cases
159+
result = Mock()
160+
result.rows = [] # DDL operations return no rows
161+
return self._create_mock_future(result=result)
126162

127163
mock_session.execute_async.side_effect = execute_async_side_effect
128164

tests/unit/test_session_edge_cases.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,24 @@ def add_callbacks(callback=None, errback=None):
3434
if errback:
3535
errbacks.append(errback)
3636

37-
# Immediately call the appropriate callback
38-
if error:
39-
if errback:
40-
errback(error)
41-
else:
42-
if callback and result is not None:
43-
# For successful results, pass rows
44-
rows = getattr(result, "rows", [])
45-
callback(rows)
37+
# Delay the callback execution to allow AsyncResultHandler to set up properly
38+
def execute_callback():
39+
if error:
40+
if errback:
41+
errback(error)
42+
else:
43+
if callback and result is not None:
44+
# For successful results, pass rows
45+
rows = getattr(result, "rows", [])
46+
callback(rows)
47+
48+
# Schedule callback for next event loop iteration
49+
try:
50+
loop = asyncio.get_running_loop()
51+
loop.call_soon(execute_callback)
52+
except RuntimeError:
53+
# No event loop, execute immediately
54+
execute_callback()
4655

4756
future.add_callbacks = add_callbacks
4857
future.has_more_pages = False

0 commit comments

Comments
 (0)