-
Notifications
You must be signed in to change notification settings - Fork 415
/
Copy pathtest_logging.py
51 lines (38 loc) · 1.51 KB
/
test_logging.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
import asyncio
from asyncpg import _testbase as tb
from asyncpg import exceptions
class LogCollector:
def __init__(self):
self.records = []
def __call__(self, record):
self.records.append(record)
class TestQueryLogging(tb.ConnectedTestCase):
async def test_logging_context(self):
queries = asyncio.Queue()
def query_saver(record):
queries.put_nowait(record)
log = LogCollector()
with self.con.query_logger(query_saver):
self.assertEqual(len(self.con._query_loggers), 1)
await self.con.execute("SELECT 1")
with self.con.query_logger(log):
self.assertEqual(len(self.con._query_loggers), 2)
await self.con.execute("SELECT 2")
r1 = await queries.get()
r2 = await queries.get()
self.assertEqual(r1.query, "SELECT 1")
self.assertEqual(r2.query, "SELECT 2")
self.assertEqual(len(log.records), 1)
self.assertEqual(log.records[0].query, "SELECT 2")
self.assertEqual(len(self.con._query_loggers), 0)
async def test_error_logging(self):
log = LogCollector()
with self.con.query_logger(log):
with self.assertRaises(exceptions.UndefinedColumnError):
await self.con.execute("SELECT x")
await asyncio.sleep(0) # wait for logging
self.assertEqual(len(log.records), 1)
self.assertEqual(
type(log.records[0].exception),
exceptions.UndefinedColumnError
)