-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest_avro.py
64 lines (48 loc) · 1.73 KB
/
test_avro.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
from io import BytesIO
import pytest
from flow.record import RecordDescriptor, RecordReader
from flow.record.adapter.avro import AvroReader, AvroWriter
from flow.record.base import HAS_AVRO
def generate_records(amount):
TestRecordWithFooBar = RecordDescriptor(
"test/record",
[
("string", "name"),
("string", "foo"),
("string", "bar"),
],
)
for i in range(amount):
yield TestRecordWithFooBar(name=f"record{i}", foo="bar", bar="baz")
def test_writing_reading_avrofile(tmp_path):
if not HAS_AVRO:
pytest.skip("fastavro module not installed")
avro_path = tmp_path / "test.avro"
out = AvroWriter(avro_path)
for rec in generate_records(100):
out.write(rec)
out.close()
reader = AvroReader(avro_path)
for index, rec in enumerate(reader):
assert rec.name == f"record{index}"
assert rec.foo == "bar"
assert rec.bar == "baz"
def test_avrostream_filelike_object(tmp_path):
if not HAS_AVRO:
pytest.skip("fastavro module not installed")
avro_path = tmp_path / "test.avro"
out = AvroWriter(avro_path)
for rec in generate_records(100):
out.write(rec)
out.close()
with open(avro_path, "rb") as avro_file:
avro_buffer = avro_file.read()
avro_io = BytesIO(avro_buffer)
reader = RecordReader(fileobj=avro_io)
# The record reader should automatically have created an 'AvroReader' to handle the Avro Record Stream
assert isinstance(reader, AvroReader)
# Verify if selector worked and records are the same
for index, rec in enumerate(reader):
assert rec.name == f"record{index}"
assert rec.foo == "bar"
assert rec.bar == "baz"