File size: 1,051 Bytes
e48c905
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import asyncio
import unittest
from contextlib import redirect_stdout
from io import StringIO
from unittest.mock import patch

import httpx
import d1_client


class _RaisingClient:
    async def __aenter__(self):
        return self

    async def __aexit__(self, exc_type, exc, tb):
        return False

    async def post(self, *args, **kwargs):
        raise httpx.ReadTimeout('')


class D1ClientDiagnosticsTests(unittest.TestCase):
    def test_timeout_log_includes_exception_type(self):
        output = StringIO()
        with patch.object(d1_client, 'ACCOUNT_ID', 'acct'), \
             patch.object(d1_client, 'DATABASE_ID', 'db'), \
             patch.object(d1_client, 'API_TOKEN', 'token'), \
             patch.object(d1_client.httpx, 'AsyncClient', return_value=_RaisingClient()), \
             redirect_stdout(output):
            result = asyncio.run(d1_client.execute_sql('SELECT 1'))

        self.assertEqual(result, [])
        self.assertIn('ReadTimeout', output.getvalue())


if __name__ == '__main__':
    unittest.main()