Spaces:
Sleeping
Sleeping
Commit ·
e48c905
1
Parent(s): 1fce18e
diagnose D1 timeout class
Browse files- d1_client.py +1 -1
- test_d1_client.py +37 -0
d1_client.py
CHANGED
|
@@ -43,5 +43,5 @@ async def execute_sql(sql: str, params: Optional[list[Any]] = None) -> list[dict
|
|
| 43 |
return []
|
| 44 |
return data["result"][0].get("results", [])
|
| 45 |
except Exception as e:
|
| 46 |
-
print(f"🔴 D1 Connection Error: {e}")
|
| 47 |
return []
|
|
|
|
| 43 |
return []
|
| 44 |
return data["result"][0].get("results", [])
|
| 45 |
except Exception as e:
|
| 46 |
+
print(f"🔴 D1 Connection Error [{type(e).__name__}]: {e}")
|
| 47 |
return []
|
test_d1_client.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import unittest
|
| 3 |
+
from contextlib import redirect_stdout
|
| 4 |
+
from io import StringIO
|
| 5 |
+
from unittest.mock import patch
|
| 6 |
+
|
| 7 |
+
import httpx
|
| 8 |
+
import d1_client
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class _RaisingClient:
|
| 12 |
+
async def __aenter__(self):
|
| 13 |
+
return self
|
| 14 |
+
|
| 15 |
+
async def __aexit__(self, exc_type, exc, tb):
|
| 16 |
+
return False
|
| 17 |
+
|
| 18 |
+
async def post(self, *args, **kwargs):
|
| 19 |
+
raise httpx.ReadTimeout('')
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class D1ClientDiagnosticsTests(unittest.TestCase):
|
| 23 |
+
def test_timeout_log_includes_exception_type(self):
|
| 24 |
+
output = StringIO()
|
| 25 |
+
with patch.object(d1_client, 'ACCOUNT_ID', 'acct'), \
|
| 26 |
+
patch.object(d1_client, 'DATABASE_ID', 'db'), \
|
| 27 |
+
patch.object(d1_client, 'API_TOKEN', 'token'), \
|
| 28 |
+
patch.object(d1_client.httpx, 'AsyncClient', return_value=_RaisingClient()), \
|
| 29 |
+
redirect_stdout(output):
|
| 30 |
+
result = asyncio.run(d1_client.execute_sql('SELECT 1'))
|
| 31 |
+
|
| 32 |
+
self.assertEqual(result, [])
|
| 33 |
+
self.assertIn('ReadTimeout', output.getvalue())
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
if __name__ == '__main__':
|
| 37 |
+
unittest.main()
|