-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest_record.py
958 lines (813 loc) · 27.7 KB
/
test_record.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
import importlib
import inspect
import os
import sys
from unittest.mock import patch
import pytest
from flow.record import (
RECORD_VERSION,
GroupedRecord,
Record,
RecordDescriptor,
RecordPacker,
RecordPrinter,
RecordReader,
RecordWriter,
extend_record,
fieldtypes,
record_stream,
)
from flow.record.base import (
ignore_fields_for_comparison,
merge_record_descriptors,
normalize_fieldname,
set_ignored_fields_for_comparison,
)
from flow.record.exceptions import RecordDescriptorError
from flow.record.stream import RecordFieldRewriter
def test_record_creation():
TestRecord = RecordDescriptor(
"test/record",
[
("string", "url"),
("string", "query"),
],
)
# No arguments defaults to None
r = TestRecord()
assert r.url is None
assert r.query is None
# Keyword arguments
r = TestRecord(url="foo", query="bar")
assert r.url == "foo"
assert r.query == "bar"
# Positional arguments
r = TestRecord("foo", "bar")
assert r.url == "foo"
assert r.query == "bar"
# Single keyword argument
r = TestRecord(query="foo")
assert r.query == "foo"
assert r.url is None
def test_record_version(tmpdir):
path = "jsonfile://{}".format(tmpdir.join("test.jsonl").strpath)
writer = RecordWriter(path)
packer = RecordPacker()
TestRecord = RecordDescriptor(
"test/record",
[
("string", "hello"),
("string", "world"),
],
)
r1 = TestRecord(hello="hello", world="world")
writer.write(r1)
data = packer.pack(r1)
u1 = packer.unpack(data)
print(repr(u1._desc))
assert u1.hello == r1.hello
assert u1.world == r1.world
# change the order
TestRecord = RecordDescriptor(
"test/record",
[
("string", "world"),
("string", "hello"),
],
)
r2 = TestRecord(hello="hello", world="world")
writer.write(r2)
data = packer.pack(r2)
u2 = packer.unpack(data)
assert u2.hello == r2.hello
assert u2.world == r2.world
print(repr(u2._desc))
# change fieldtypes
TestRecord = RecordDescriptor(
"test/record",
[
("varint", "world"),
("string", "hello"),
],
)
r3 = TestRecord(hello="hello", world=42)
writer.write(r3)
data = packer.pack(r3)
u3 = packer.unpack(data)
writer.flush()
assert u3._desc.identifier == r3._desc.identifier
assert u1._desc.identifier != u3._desc.identifier
assert u2._desc.identifier != u3._desc.identifier
assert u3.hello == r3.hello
assert u3.world == r3.world
reader = RecordReader(path)
rec = [r for r in reader]
assert len(rec) == 3
assert u3._desc.identifier == rec[2]._desc.identifier
assert u1._desc.identifier != rec[2]._desc.identifier
assert u2._desc.identifier != rec[2]._desc.identifier
assert u3.hello == rec[2].hello
assert u3.world == rec[2].world
def test_grouped_record():
TestRecord = RecordDescriptor(
"test/record",
[
("string", "hello"),
("string", "world"),
("uint32", "count"),
],
)
WQMetaRecord = RecordDescriptor(
"wq/meta",
[
("string", "assignee"),
("string", "profile"),
("string", "hello"),
],
)
test_record = TestRecord("a", "b", 12345)
meta_record = WQMetaRecord("me", "this is a test", "other hello")
grouped = GroupedRecord("grouped/wq", [test_record, meta_record])
assert grouped.hello == "a"
assert grouped.world == "b"
assert grouped.count == 12345
assert grouped.assignee == "me"
assert grouped.profile == "this is a test"
grouped.profile = "omg"
grouped.hello = "new value"
assert grouped.hello == "new value"
assert grouped.profile == "omg"
assert grouped.records[0].hello == "new value"
assert grouped.records[1].hello == "other hello"
grouped.records[1].hello = "testing"
assert grouped.hello != "testing"
assert grouped.hello == "new value"
assert grouped.records[1].hello == "testing"
assert len(grouped.records) == 2
# test grouped._asdict
rdict = grouped._asdict()
assert set(["hello", "world", "count", "assignee", "profile", "hello"]) <= set(rdict)
rdict = grouped._asdict(fields=["profile", "count", "_generated"])
assert set(["profile", "count", "_generated"]) == set(rdict)
assert rdict["profile"] == "omg"
assert rdict["count"] == 12345
def test_grouped_records_packing(tmpdir):
RecordA = RecordDescriptor(
"test/a",
[
("string", "a_string"),
("string", "common"),
("uint32", "a_count"),
],
)
RecordB = RecordDescriptor(
"test/b",
[
("string", "b_string"),
("string", "common"),
("uint32", "b_count"),
],
)
a = RecordA("hello", "world", 12345, _source="TheBadInternet", _classification="CLASSIFIED")
b = RecordB("good", "bye", 54321, _source="TheGoodInternet", _classification="TLP.WHITE")
assert isinstance(a, Record)
assert not isinstance(a, GroupedRecord)
grouped = GroupedRecord("grouped/ab", [a, b])
assert isinstance(grouped, (Record, GroupedRecord))
assert [(f.typename, f.name) for f in grouped._desc.fields.values()] == [
("string", "a_string"),
("string", "common"),
("uint32", "a_count"),
("string", "b_string"),
("uint32", "b_count"),
]
path = tmpdir.join("grouped.records").strpath
writer = RecordWriter(path)
writer.write(grouped)
writer.write(grouped)
writer.write(grouped)
writer.write(grouped)
writer.write(grouped)
writer.flush()
reader = RecordReader(path)
record = next(iter(reader))
# grouped record tests
assert isinstance(record, Record)
assert isinstance(record, GroupedRecord)
assert record.common == "world" # first 'key' has precendence
assert record.name == "grouped/ab"
assert record.a_string == "hello"
assert record.a_count == 12345
assert record.b_count == 54321
assert record.b_string == "good"
assert record._source == "TheBadInternet"
assert record._classification == "CLASSIFIED"
# access 'common' on second record directly
assert record.records[1].common == "bye"
# access raw records directly
assert len(record.records) == 2
assert record.records[0]._desc.name == "test/a"
assert record.records[1]._desc.name == "test/b"
# test using selectors
reader = RecordReader(path, selector="r.a_count == 12345")
assert len(list(iter(reader))) == 5
reader = RecordReader(path, selector="r.common == 'bye'")
assert len(list(iter(reader))) == 0
reader = RecordReader(path, selector="r.common == 'world'")
assert len(list(iter(reader))) == 5
def test_record_reserved_fieldname():
with pytest.raises(RecordDescriptorError):
RecordDescriptor(
"test/a",
[
("string", "_classification"),
("string", "_source"),
("uint32", "_generated"),
],
)
def test_record_printer_stdout(capsys):
Record = RecordDescriptor(
"test/a",
[
("string", "a_string"),
("string", "common"),
("uint32", "a_count"),
],
)
record = Record("hello", "world", 10)
# fake capsys to be a tty.
def isatty():
return True
capsys._capture.out.tmpfile.isatty = isatty
writer = RecordPrinter(getattr(sys.stdout, "buffer", sys.stdout))
writer.write(record)
out, err = capsys.readouterr()
expected = "<test/a a_string='hello' common='world' a_count=10>\n"
assert out == expected
def test_record_printer_stdout_surrogateescape(capsys):
Record = RecordDescriptor(
"test/a",
[
("string", "name"),
],
)
record = Record(b"R\xc3\xa9\xeamy")
# fake capsys to be a tty.
def isatty():
return True
capsys._capture.out.tmpfile.isatty = isatty
writer = RecordPrinter(getattr(sys.stdout, "buffer", sys.stdout))
writer.write(record)
out, err = capsys.readouterr()
expected = "<test/a name='Ré\\udceamy'>\n"
assert out == expected
def test_record_field_limit():
count = 1337
fields = [("uint32", "field_{}".format(i)) for i in range(count)]
values = dict([("field_{}".format(i), i) for i in range(count)])
Record = RecordDescriptor("test/limit", fields)
record = Record(**values)
for i in range(count):
assert getattr(record, "field_{}".format(i)) == i
# test kwarg init
record = Record(field_404=12345)
assert record.field_404 == 12345
assert record.field_0 is None
# test arg init
record = Record(200, 302, 404)
assert record.field_0 == 200
assert record.field_1 == 302
assert record.field_2 == 404
assert record.field_404 is None
# test arg + kwarg init
record = Record(200, 302, 404, field_502=502)
assert record.field_0 == 200
assert record.field_1 == 302
assert record.field_2 == 404
assert record.field_3 is None
assert record.field_502 == 502
def test_record_internal_version():
Record = RecordDescriptor(
"test/a",
[
("string", "a_string"),
("string", "common"),
("uint32", "a_count"),
],
)
record = Record("hello", "world", 10)
assert record._version == RECORD_VERSION
record = Record("hello", "world", 10, _version=1337)
assert record._version == RECORD_VERSION
def test_record_reserved_keyword():
Record = RecordDescriptor(
"test/a",
[
("string", "from"),
("string", "and"),
("uint32", "or"),
("uint32", "normal"),
],
)
init = Record.recordType.__init__
sig = inspect.signature(init)
params = list(sig.parameters.values())
assert init.__code__.co_argcount == 1
assert len(params) == 3
assert params[1].name == "args"
assert params[1].kind == params[1].VAR_POSITIONAL
assert params[2].name == "kwargs"
assert params[2].kind == params[2].VAR_KEYWORD
r = Record("hello", "world", 1337, 10)
assert getattr(r, "from") == "hello"
assert getattr(r, "and") == "world"
assert getattr(r, "or") == 1337
assert r.normal == 10
r = Record("some", "missing", normal=5)
assert getattr(r, "from") == "some"
assert getattr(r, "and") == "missing"
assert getattr(r, "or") is None
assert r.normal == 5
r = Record("from_value", **{"and": "dict", "or": 7331, "normal": 3})
assert getattr(r, "from") == "from_value"
assert getattr(r, "and") == "dict"
assert getattr(r, "or") == 7331
assert r.normal == 3
Record = RecordDescriptor(
"test/a",
[
("uint32", "normal"),
],
)
init = Record.recordType.__init__
sig = inspect.signature(init)
params = list(sig.parameters.values())
assert init.__code__.co_argcount == 6
assert len(params) == 6
assert params[1].name == "normal"
assert params[1].kind == params[1].POSITIONAL_OR_KEYWORD
assert params[1].default is None
assert params[2].name == "_source"
assert params[2].kind == params[2].POSITIONAL_OR_KEYWORD
assert params[2].default is None
assert params[3].name == "_classification"
assert params[3].kind == params[3].POSITIONAL_OR_KEYWORD
assert params[3].default is None
assert params[4].name == "_generated"
assert params[4].kind == params[4].POSITIONAL_OR_KEYWORD
assert params[4].default is None
assert params[5].name == "_version"
assert params[5].kind == params[5].POSITIONAL_OR_KEYWORD
assert params[5].default is None
Record = RecordDescriptor(
"test/a",
[
("uint32", "self"),
("uint32", "cls"),
],
)
r = Record(1, 2)
assert r.self == 1
assert r.cls == 2
def test_record_stream(tmp_path):
Record = RecordDescriptor(
"test/counter",
[
("uint32", "counter"),
("string", "tag"),
],
)
datasets = [
tmp_path / "dataset1.records",
tmp_path / "dataset2.records.gz",
]
for ds in datasets:
writer = RecordWriter(str(ds))
for i in range(100):
writer.write(Record(i, tag=ds.name))
writer.close()
datasets = [str(ds) for ds in datasets]
assert len(list(record_stream(datasets))) == len(datasets) * 100
assert len(list(record_stream(datasets, "r.counter == 42"))) == len(datasets)
def test_record_replace():
TestRecord = RecordDescriptor(
"test/record",
[
("uint32", "index"),
("string", "foo"),
],
)
t = TestRecord(1, "hello")
assert t.index == 1
assert t.foo == "hello"
t2 = t._replace(foo="bar", index=1337)
assert t2.foo == "bar"
assert t2.index == 1337
t3 = t._replace()
assert t3.index == 1
assert t3.foo == "hello"
assert t3._source == t._source
assert t3._generated == t._generated
assert t3._version == t._version
t4 = t2._replace(foo="test", _source="pytest")
assert t4.index == 1337
assert t4.foo == "test"
assert t4._source == "pytest"
assert t4._generated == t2._generated
with pytest.raises(ValueError) as excinfo:
t._replace(foobar="keyword does not exist")
excinfo.match(".*Got unexpected field names:.*foobar.*")
def test_record_init_from_record():
TestRecord = RecordDescriptor(
"test/record",
[
("uint32", "index"),
("string", "foo"),
],
)
t = TestRecord(1, "hello")
assert t.index == 1
assert t.foo == "hello"
TestRecord2 = TestRecord.extend(
[
("string", "bar"),
("uint32", "test"),
]
)
t2 = TestRecord2.init_from_record(t)
assert t2.index == 1
assert t2.foo == "hello"
assert t2.bar is None
assert t2.test is None
t2.bar = "bar"
t2.test = 3
assert t2.bar == "bar"
assert t2.test == 3
TestRecord3 = RecordDescriptor(
"test/record3",
[
("string", "test"),
("uint32", "count"),
],
)
with pytest.raises(TypeError):
t3 = TestRecord3.init_from_record(t2, raise_unknown=True)
# explicit raise_unknown=False
t3 = TestRecord3.init_from_record(t2, raise_unknown=False)
assert t3.test == "3"
assert t3.count is None
# default should not raise either
t3 = TestRecord3.init_from_record(t2)
assert t3.test == "3"
assert t3.count is None
def test_record_asdict():
Record = RecordDescriptor(
"test/a",
[
("string", "a_string"),
("string", "common"),
("uint32", "a_count"),
],
)
record = Record("hello", "world", 1337)
rdict = record._asdict()
assert rdict.get("a_string") == "hello"
assert rdict.get("common") == "world"
assert rdict.get("a_count") == 1337
assert set(rdict) == set(["a_string", "common", "a_count", "_source", "_generated", "_version", "_classification"])
rdict = record._asdict(fields=["common", "_source", "a_string"])
assert set(rdict) == set(["a_string", "common", "_source"])
rdict = record._asdict(exclude=["a_count", "_source", "_generated", "_version"])
assert set(rdict) == set(["a_string", "common", "_classification"])
rdict = record._asdict(fields=["common", "_source", "a_string"], exclude=["common"])
assert set(rdict) == set(["a_string", "_source"])
def test_recordfield_rewriter_expression():
rewriter = RecordFieldRewriter(expression="upper_a = a_string.upper(); count_times_10 = a_count * 10")
Record = RecordDescriptor(
"test/a",
[
("string", "a_string"),
("string", "common"),
("uint32", "a_count"),
],
)
record = Record("hello", "world", 1337)
new_record = rewriter.rewrite(record)
assert new_record.a_string == "hello"
assert new_record.common == "world"
assert new_record.a_count == 1337
assert new_record.upper_a == "HELLO"
assert new_record.count_times_10 == 1337 * 10
def test_recordfield_rewriter_fields():
rewriter = RecordFieldRewriter(fields=["a_count"])
Record = RecordDescriptor(
"test/a",
[
("string", "a_string"),
("string", "common"),
("uint32", "a_count"),
],
)
record = Record("hello", "world", 1337)
new_record = rewriter.rewrite(record)
assert hasattr(new_record, "a_count")
assert not hasattr(new_record, "a_string")
assert not hasattr(new_record, "common")
def test_extend_record():
TestRecord = RecordDescriptor(
"test/record",
[
("string", "url"),
("string", "query"),
],
)
FooRecord = RecordDescriptor(
"test/foo",
[
("varint", "foo"),
("bytes", "query"),
("bytes", "bar"),
],
)
HelloRecord = RecordDescriptor(
"test/hello",
[
("string", "hello"),
("string", "world"),
("string", "url"),
],
)
a = TestRecord("http://flow.record", "myquery")
b = FooRecord(12345, b"FOO", b"BAR")
c = HelloRecord("hello", "world", "http://hello.world")
new = extend_record(a, [b, c])
assert new._desc == RecordDescriptor(
"test/record",
[
("string", "url"),
("string", "query"),
("varint", "foo"),
("bytes", "bar"),
("string", "hello"),
("string", "world"),
],
)
assert new.url == "http://flow.record"
assert new.query == "myquery"
assert new.foo == 12345
assert new.bar == b"BAR"
assert new.hello == "hello"
assert new.world == "world"
new = extend_record(a, [b, c], replace=True)
assert new._desc == RecordDescriptor(
"test/record",
[
("string", "url"),
("bytes", "query"),
("varint", "foo"),
("bytes", "bar"),
("string", "hello"),
("string", "world"),
],
)
assert new.url == "http://hello.world"
assert new.query == b"FOO"
assert new.foo == 12345
assert new.bar == b"BAR"
assert new.hello == "hello"
assert new.world == "world"
def test_extend_record_with_replace():
TestRecord = RecordDescriptor(
"test/record",
[
("string", "ip"),
("uint16", "port"),
("string", "data"),
("string", "note"),
],
)
ReplaceRecord = RecordDescriptor(
"test/foo",
[
("net.ipaddress", "ip"),
("net.tcp.Port", "port"),
("bytes", "data"),
("string", "location"),
],
)
a = TestRecord("10.13.13.17", 80, "HTTP/1.1 200 OK\r\n", "webserver")
b = ReplaceRecord(
ip=a.ip,
port=a.port,
data=a.data.encode(),
location="DMZ",
)
new = extend_record(a, [b], replace=False)
assert new.ip == "10.13.13.17"
assert new.port == 80
assert new.data == "HTTP/1.1 200 OK\r\n"
assert new.note == "webserver"
assert new.location == "DMZ"
assert isinstance(new.ip, str)
assert isinstance(new.port, int)
assert isinstance(new.data, str)
assert isinstance(new.note, str)
assert isinstance(new.location, str)
assert new._desc.name == "test/record"
assert "<test/record " in repr(new)
new = extend_record(a, [b], replace=True, name="test/replaced")
assert new.ip == "10.13.13.17"
assert new.port == 80
assert new.data == b"HTTP/1.1 200 OK\r\n"
assert new.note == "webserver"
assert new.location == "DMZ"
assert isinstance(new.ip, fieldtypes.net.ipaddress)
assert isinstance(new.port, fieldtypes.net.tcp.Port)
assert isinstance(new.data, bytes)
assert isinstance(new.note, str)
assert isinstance(new.location, str)
assert new._desc.name == "test/replaced"
assert "<test/replaced " in repr(new)
def test_extend_record_cache():
TestRecord = RecordDescriptor(
"test/record_a",
[
("net.ipaddress", "ip"),
("uint16", "port"),
("bytes", "data"),
],
)
NoteRecord = RecordDescriptor(
"test/record_b",
[
("string", "note"),
],
)
start_info = merge_record_descriptors.cache_info()
a = TestRecord("1.2.3.4", 80, b"hello")
b = NoteRecord("webserver")
new = extend_record(a, [b], name="test/record")
assert new._desc.name == "test/record"
assert new.ip == "1.2.3.4"
assert new.port == 80
assert new.data == b"hello"
assert new.note == "webserver"
# check cache, w should have a miss
info1 = merge_record_descriptors.cache_info()
assert info1.misses == start_info.misses + 1
assert info1.hits == start_info.hits
a = TestRecord("8.8.8.8", 53, b"world")
b = NoteRecord("dns server")
new = extend_record(a, [b], name="test/record")
assert new._desc.name == "test/record"
assert new.ip == "8.8.8.8"
assert new.port == 53
assert new.data == b"world"
assert new.note == "dns server"
# check cache, we should now have a hit and miss unchanged
info2 = merge_record_descriptors.cache_info()
assert info2.misses == info1.misses
assert info2.hits == start_info.hits + 1
def test_merge_record_descriptor_name():
TestRecord = RecordDescriptor(
"test/ip_record",
[
("net.ipaddress", "ip"),
("uint16", "port"),
("bytes", "data"),
],
)
NoteRecord = RecordDescriptor(
"test/note_record",
[
("string", "note"),
],
)
descriptors = (TestRecord, NoteRecord)
MergedRecord = merge_record_descriptors(descriptors, name="test/merged")
assert MergedRecord.name == "test/merged"
record = MergedRecord()
assert record._desc.name == "test/merged"
MergedRecord = merge_record_descriptors(descriptors)
assert MergedRecord.name == "test/ip_record"
record = MergedRecord()
assert record._desc.name == "test/ip_record"
def test_normalize_fieldname():
assert normalize_fieldname("hello") == "hello"
assert normalize_fieldname("my-variable-name-with-dashes") == "my_variable_name_with_dashes"
assert normalize_fieldname("_my_name_starting_with_underscore") == "x__my_name_starting_with_underscore"
assert normalize_fieldname("1337") == "x_1337"
assert normalize_fieldname("my name with spaces") == "my_name_with_spaces"
assert normalize_fieldname("my name (with) parentheses") == "my_name__with__parentheses"
assert normalize_fieldname("_generated") == "_generated"
assert normalize_fieldname("_source") == "_source"
def test_compare_global_variable():
TestRecord = RecordDescriptor(
"test/record",
[
("string", "firstname"),
("string", "lastname"),
],
)
same_same = TestRecord(firstname="James", lastname="Bond")
but_different = TestRecord(firstname="Ethan", lastname="Hunt")
but_still_same = TestRecord(firstname="Andrew", lastname="Bond")
records = [same_same, but_different, but_still_same]
assert same_same != but_still_same
set_ignored_fields_for_comparison({"_generated", "firstname"})
assert same_same == but_still_same
assert same_same != but_different
assert len(set(records)) == 2
def test_compare_environment_variable():
with patch.dict(os.environ), patch.dict(sys.modules):
os.environ["FLOW_RECORD_IGNORE"] = "_generated,lastname"
# Force a re-import of flow.record so the global variable gets re-initialized based on the environment variable
keys = [key for key in sys.modules if key == "flow" or "flow." in key]
for key in keys:
del sys.modules[key]
importlib.import_module("flow.record")
from flow.record import IGNORE_FIELDS_FOR_COMPARISON, RecordDescriptor
assert IGNORE_FIELDS_FOR_COMPARISON == {"_generated", "lastname"}
TestRecord = RecordDescriptor(
"test/record",
[
("string", "firstname"),
("string", "lastname"),
],
)
same_same = TestRecord(firstname="John", lastname="Rambo")
but_different = TestRecord(firstname="Johnny", lastname="English")
but_still_same = TestRecord(firstname="John", lastname="McClane")
records = [same_same, but_different, but_still_same]
assert same_same == but_still_same
assert same_same != but_different
assert len(set(records)) == 2
def test_ignore_fields_for_comparision_contextmanager():
TestRecord = RecordDescriptor(
"test/record",
[
("string", "firstname"),
("string", "lastname"),
("string", "movie"),
],
)
records = [
TestRecord("John", "Rambo", "First Blood"),
TestRecord("John", "Rambo", "Rambo: First Blood Part II"),
TestRecord("John", "Rambo", "Rambo III"),
TestRecord("John", "Rambo", "Rambo"),
TestRecord("John", "Rambo", "Rambo: Last Blood"),
TestRecord("Johnny", "English", "Johnny English"),
TestRecord("Johnny", "English", "Johnny English Strikes Again"),
TestRecord("John", "McClane", "Die Hard"),
TestRecord("John", "McClane", "Die Hard 2"),
TestRecord("John", "McClane", "Die Hard with a Vengeance"),
TestRecord("John", "McClane", "Live Free or Die Hard"),
TestRecord("John", "McClane", "A Good Day to Die Hard"),
TestRecord("John", "Wick", "John Wick"),
TestRecord("John", "Wick", "John Wick: Chapter 2"),
TestRecord("John", "Wick", "John Wick: Chapter 3 - Parabellum"),
]
with ignore_fields_for_comparison({"_generated", "lastname", "movie"}):
# unique by firstname
first_name_records = set(records)
firstnames = [first_name.firstname for first_name in first_name_records]
assert len(first_name_records) == 2
assert firstnames.count("John") == 1
assert firstnames.count("Johnny") == 1
assert records[0] == records[1]
# test nested context manager
with ignore_fields_for_comparison({"_generated", "firstname", "movie"}):
# unique by lastname
last_name_records = set(records)
lastnames = [last_name.lastname for last_name in last_name_records]
assert len(lastnames) == 4
assert lastnames.count("Rambo") == 1
assert lastnames.count("English") == 1
assert lastnames.count("McClane") == 1
assert lastnames.count("Wick") == 1
assert records[0] == records[1]
# test if the contextmanager properly resets the ignored fields
with ignore_fields_for_comparison({"_generated", "movie"}):
# unique by firstname + lastname
main_character_records = set(records)
main_characters = [f"{r.firstname} {r.lastname}" for r in main_character_records]
assert len(main_characters) == 4
assert main_characters.count("John Rambo") == 1
assert main_characters.count("Johnny English") == 1
assert main_characters.count("John McClane") == 1
assert main_characters.count("John Wick") == 1
assert records[0] == records[1]
# test reset again
assert len(set(records)) == len(records)
assert records[0] != records[1]
def test_list_field_type_hashing():
TestRecord = RecordDescriptor(
"test/record",
[
("string[]", "stringlist"),
("dictlist", "dictlist"),
],
)
test_record = TestRecord(stringlist=["a", "b", "c", "d"], dictlist=[{"a": "b", "c": "d"}, {"foo": "bar"}])
assert isinstance(hash(test_record), int)