userver: samples/testsuite-support/tests/test_logcapture.py
Loading...
Searching...
No Matches
samples/testsuite-support/tests/test_logcapture.py
1import pytest
2from pytest_userver.plugins import log_capture
3
4
5# /// [select]
6async def test_select(service_client):
7 async with service_client.capture_logs(log_level='INFO') as capture:
8 response = await service_client.get('/logcapture')
9 assert response.status == 200
10
11 records = capture.select(
12 text='Message to capture', link=response.headers['x-yarequestid'],
13 )
14 assert len(records) == 1, capture.select()
15 # /// [select]
16
17
18async def test_subscribe(service_client, mockserver):
19 async with service_client.capture_logs() as capture:
20
21 @capture.subscribe(
22 text='Message to capture', trace_id=mockserver.trace_id,
23 )
24 def log_event(link, **other):
25 pass
26
27 response = await service_client.get(
28 '/logcapture', headers={'x-yatraceid': mockserver.trace_id},
29 )
30 assert response.status == 200
31
32 call = await log_event.wait_call()
33 assert call['link'] == response.headers['x-yarequestid']
34
35
36async def test_select_error(service_client):
37 async with service_client.capture_logs(log_level='INFO') as capture:
38 response = await service_client.get('/logcapture')
39 assert response.status == 200
40
41 with pytest.raises(log_capture.IncorrectUsageError):
42 capture.select(
43 text='Message to capture',
44 link=response.headers['x-yarequestid'],
45 )
46
47
48async def test_subscribe_error(service_client, mockserver):
49 async with service_client.capture_logs() as capture:
50 response = await service_client.get(
51 '/logcapture', headers={'x-yatraceid': mockserver.trace_id},
52 )
53 assert response.status == 200
54
55 with pytest.raises(log_capture.IncorrectUsageError):
56
57 @capture.subscribe(
58 text='Message to capture', trace_id=mockserver.trace_id,
59 )
60 def log_event(link, **other):
61 pass