-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathtest_testing.py
89 lines (77 loc) · 3.09 KB
/
test_testing.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import pytest
from inline_snapshot import snapshot
import logfire
from logfire.testing import CaptureLogfire, TestExporter, TimeGenerator
def test_reset_exported_spans(exporter: TestExporter) -> None:
assert len(exporter.exported_spans) == 0
logfire.info('First log!')
assert len(exporter.exported_spans) == 1
assert exporter.exported_spans[0].name == 'First log!'
logfire.info('Second log!')
assert len(exporter.exported_spans) == 2
assert exporter.exported_spans[1].name == 'Second log!'
exporter.clear()
assert len(exporter.exported_spans) == 0
logfire.info('Third log!')
assert len(exporter.exported_spans) == 1
assert exporter.exported_spans[0].name == 'Third log!'
def test_capfire_fixture(capfire: CaptureLogfire) -> None:
with pytest.raises(Exception):
with logfire.span('a span!'):
logfire.info('a log!')
raise Exception('an exception!')
exporter = capfire.exporter
assert exporter.exported_spans_as_dict() == snapshot(
[
{
'name': 'a log!',
'context': {'trace_id': 1, 'span_id': 3, 'is_remote': False},
'parent': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
'start_time': 2000000000,
'end_time': 2000000000,
'attributes': {
'logfire.span_type': 'log',
'logfire.level_num': 9,
'logfire.msg_template': 'a log!',
'logfire.msg': 'a log!',
'code.filepath': 'test_testing.py',
'code.function': 'test_capfire_fixture',
'code.lineno': 123,
},
},
{
'name': 'a span!',
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
'parent': None,
'start_time': 1000000000,
'end_time': 4000000000,
'attributes': {
'code.filepath': 'test_testing.py',
'code.function': 'test_capfire_fixture',
'code.lineno': 123,
'logfire.msg_template': 'a span!',
'logfire.msg': 'a span!',
'logfire.span_type': 'span',
'logfire.level_num': 17,
},
'events': [
{
'name': 'exception',
'timestamp': 3000000000,
'attributes': {
'exception.type': 'Exception',
'exception.message': 'an exception!',
'exception.stacktrace': 'Exception: an exception!',
'exception.escaped': 'True',
'logfire.exception_first_recorded': True,
},
}
],
},
]
)
def test_time_generator():
t = TimeGenerator()
assert t() == 1000000000
assert t() == 2000000000
assert repr(t) == 'TimeGenerator(ns_time=2000000000)'