/
test_aliases.py
119 lines (95 loc) · 2.7 KB
/
test_aliases.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
import fastavro
from fastavro.six import MemoryIO
import pytest
def roundtrip(schema, records, new_schema):
new_file = MemoryIO()
fastavro.writer(new_file, schema, records)
new_file.seek(0)
reader = fastavro.reader(new_file, new_schema)
new_records = list(reader)
return new_records
def test_aliases_not_present():
schema = {
"type": "record",
"name": "test_aliases_not_present",
"fields": [{
"name": "test",
"type": "double"
}]
}
new_schema = {
"type": "record",
"name": "test_aliases_not_present_new",
"fields": [
{"name": "newtest", "type": "double", "aliases": ["testX"]},
]
}
records = [{"test": 1.2}]
with pytest.raises(fastavro.read.SchemaResolutionError):
roundtrip(schema, records, new_schema)
def test_incompatible_aliases():
schema = {
"type": "record",
"name": "test_incompatible_aliases",
"fields": [{
"name": "test",
"type": "double"
}]
}
new_schema = {
"type": "record",
"name": "test_incompatible_aliases_new",
"fields": [
{"name": "newtest", "type": "int", "aliases": ["test"]},
]
}
records = [{"test": 1.2}]
with pytest.raises(fastavro.read.SchemaResolutionError):
roundtrip(schema, records, new_schema)
def test_aliases_in_reader_schema():
schema = {
"type": "record",
"name": "test_aliases_in_reader_schema",
"fields": [{
"name": "test",
"type": "int"
}]
}
new_schema = {
"type": "record",
"name": "test_aliases_in_reader_schema_new",
"fields": [{
"name": "newtest",
"type": "int",
"aliases": ["test"]
}]
}
records = [{"test": 1}]
assert roundtrip(schema, records, new_schema) == [{"newtest": 1}]
def test_aliases_with_default_value_and_field_added():
"""https://github.com/fastavro/fastavro/issues/225"""
schema = {
"type": "record",
"name": "test_aliases_with_default_value",
"fields": [{
"name": "test",
"type": "int"
}]
}
new_schema = {
"type": "record",
"name": "test_aliases_with_default_value",
"fields": [{
"name": "newtest",
"type": "int",
"default": 0,
"aliases": ["test"]
}, {
"name": "test2",
"type": "int",
"default": 100
}]
}
records = [{"test": 1}]
new_records = roundtrip(schema, records, new_schema)
assert new_records == [{"newtest": 1, "test2": 100}]