-
Notifications
You must be signed in to change notification settings - Fork 0
/
typecheck.py
executable file
·99 lines (86 loc) · 2.87 KB
/
typecheck.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
#!/usr/bin/env python
# coding=utf-8
# Copyright (C) 2015 Wesley Tanaka
"""Experiments with avro
"""
import json
import StringIO
import avro.schema
from avro.datafile import DataFileReader, DataFileWriter
from avro.io import DatumReader, DatumWriter
from avro.io import AvroTypeException
def main():
known_schemas = avro.schema.Names()
with open("point.avsc", "rb") as fp:
point = avro.schema.make_avsc_object(json.loads(fp.read()), known_schemas)
with open("review.avsc", "rb") as fp:
place = avro.schema.make_avsc_object(json.loads(fp.read()), known_schemas)
with open("place.avsc", "rb") as fp:
place = avro.schema.make_avsc_object(json.loads(fp.read()), known_schemas)
output = StringIO.StringIO()
writer = DataFileWriter(output, DatumWriter(), point)
writer.append({'x': 1.5, 'y': 2.75})
writer.flush()
serialized = output.getvalue()
reader = DataFileReader(StringIO.StringIO(serialized), DatumReader())
deserialized = tuple(reader)[0]
assert deserialized['x'] == 1.5
assert deserialized['y'] == 2.75
reader.close()
writer.close()
try:
output = StringIO.StringIO()
writer = DataFileWriter(output, DatumWriter(), point)
writer.append({'x': 1.5})
assert False
except AvroTypeException as e:
pass
try:
output = StringIO.StringIO()
writer = DataFileWriter(output, DatumWriter(), point)
writer.append({'x': 1.5, 'y': "wtanaka.com"})
assert False
except AvroTypeException as e:
pass
output = StringIO.StringIO()
writer = DataFileWriter(output, DatumWriter(), place)
writer.append({
'name': 'wtanaka.com',
'location': {'x': 1.5, 'y': 2.75}
})
writer.flush()
serialized = output.getvalue()
reader = DataFileReader(StringIO.StringIO(serialized), DatumReader())
deserialized = tuple(reader)[0]
assert deserialized['location']['x'] == 1.5
assert deserialized['location']['y'] == 2.75
reader.close()
writer.close()
output = StringIO.StringIO()
writer = DataFileWriter(output, DatumWriter(), place)
writer.append({
'name': 'wtanaka.com',
'location': {'x': 1.5, 'y': 2.75},
'review': {'rating': 4, 'text': '4 stars would come again'},
})
writer.flush()
serialized = output.getvalue()
reader = DataFileReader(StringIO.StringIO(serialized), DatumReader())
deserialized = tuple(reader)[0]
assert deserialized['location']['x'] == 1.5
assert deserialized['location']['y'] == 2.75
reader.close()
writer.close()
try:
output = StringIO.StringIO()
writer = DataFileWriter(output, DatumWriter(), place)
writer.append({
'name': 'wtanaka.com',
'location': {'x': 1.5, 'y': 2.75},
'review': {'x': 1.5, 'y': 2.75},
})
assert False
except AvroTypeException as e:
pass
if __name__ == "__main__":
main()