-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest_multi_timestamp.py
149 lines (122 loc) · 4.14 KB
/
test_multi_timestamp.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from datetime import datetime, timedelta, timezone
from flow.record import RecordDescriptor, iter_timestamped_records
from flow.record.base import merge_record_descriptors
UTC = timezone.utc
def test_multi_timestamp():
TestRecord = RecordDescriptor(
"test/record",
[
("datetime", "ctime"),
("datetime", "atime"),
("string", "data"),
],
)
test_record = TestRecord(
ctime=datetime(2020, 1, 1, 1, 1, 1),
atime=datetime(2022, 11, 22, 13, 37, 37),
data="test",
)
ts_records = list(iter_timestamped_records(test_record))
for rec in ts_records:
assert rec.ctime == datetime(2020, 1, 1, 1, 1, 1, tzinfo=UTC)
assert rec.atime == datetime(2022, 11, 22, 13, 37, 37, tzinfo=UTC)
assert rec.data == "test"
assert ts_records[0].ts == datetime(2020, 1, 1, 1, 1, 1, tzinfo=UTC)
assert ts_records[0].ts_description == "ctime"
assert ts_records[1].ts == datetime(2022, 11, 22, 13, 37, 37, tzinfo=UTC)
assert ts_records[1].ts_description == "atime"
def test_multi_timestamp_no_datetime():
TestRecord = RecordDescriptor(
"test/record",
[
("string", "data"),
],
)
test_record = TestRecord(data="test")
ts_records = list(iter_timestamped_records(test_record))
assert len(ts_records) == 1
assert ts_records[0].data == "test"
def test_multi_timestamp_single_datetime():
TestRecord = RecordDescriptor(
"test/record",
[
("datetime", "ctime"),
("string", "data"),
],
)
test_record = TestRecord(
ctime=datetime(2020, 1, 1, 1, 1, 1),
data="test",
)
ts_records = list(iter_timestamped_records(test_record))
assert len(ts_records) == 1
assert ts_records[0].ts == test_record.ctime
assert ts_records[0].ts_description == "ctime"
def test_multi_timestamp_ts_fieldname():
TestRecord = RecordDescriptor(
"test/record",
[
("datetime", "ts"),
("string", "data"),
],
)
test_record = TestRecord(
ts=datetime(2020, 1, 1, 1, 1, 1),
data="test",
)
ts_records = list(iter_timestamped_records(test_record))
assert len(ts_records) == 1
assert ts_records[0].ts == test_record.ts
assert ts_records[0].ts_description == "ts"
def test_multi_timestamp_timezone():
TestRecord = RecordDescriptor(
"test/record",
[
("datetime", "ts"),
("string", "data"),
],
)
correct_ts = datetime(2023, 12, 31, 13, 37, 1, 123456, tzinfo=UTC)
ts_notations = [
correct_ts,
"2023-12-31T13:37:01.123456Z",
]
for i, ts_notation in enumerate(ts_notations):
test_record = TestRecord(
ts=ts_notation,
data=f"record with timezone ({str(i)})",
)
ts_records = list(iter_timestamped_records(test_record))
assert len(ts_records) == 1
assert ts_records[0].ts == correct_ts
assert ts_records[0].ts_description == "ts"
def test_multi_timestamp_descriptor_cache():
TestRecord = RecordDescriptor(
"test/record",
[
("datetime", "ctime"),
("datetime", "atime"),
("varint", "count"),
("string", "data"),
],
)
merge_record_descriptors.cache_clear()
for i in range(10):
test_record = TestRecord(
ctime=datetime.now(UTC) + timedelta(hours=69),
atime=datetime.now(UTC) + timedelta(hours=420),
count=i,
data=f"test {i}",
)
for record in iter_timestamped_records(test_record):
assert record.data == f"test {i}"
assert record.count == i
assert record.ctime == test_record.ctime
assert record.atime == test_record.atime
assert hasattr(record, "ts")
assert hasattr(record, "ts_description")
tsfield = record.ts_description
assert record.ts == getattr(test_record, tsfield)
cache_info = merge_record_descriptors.cache_info()
assert cache_info.misses == 2
assert cache_info.hits == 18