| 1 |
#!/usr/bin/env python3 |
| 2 |
# -*- mode: python -*- |
| 3 |
# -*- coding: utf-8 -*- |
| 4 |
|
| 5 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 6 |
# or more contributor license agreements. See the NOTICE file |
| 7 |
# distributed with this work for additional information |
| 8 |
# regarding copyright ownership. The ASF licenses this file |
| 9 |
# to you under the Apache License, Version 2.0 (the |
| 10 |
# "License"); you may not use this file except in compliance |
| 11 |
# with the License. You may obtain a copy of the License at |
| 12 |
# |
| 13 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 14 |
# |
| 15 |
# Unless required by applicable law or agreed to in writing, software |
| 16 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 17 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 |
# See the License for the specific language governing permissions and |
| 19 |
# limitations under the License. |
| 20 |
|
| 21 |
import logging |
| 22 |
import os |
| 23 |
import tempfile |
| 24 |
import unittest |
| 25 |
|
| 26 |
from avro import datafile |
| 27 |
from avro import io |
| 28 |
from avro import schema |
| 29 |
|
| 30 |
|
| 31 |
def GetInteropSchema(): |
| 32 |
test_dir = os.path.dirname(os.path.abspath(__file__)) |
| 33 |
schema_json_path = os.path.join(test_dir, 'interop.avsc') |
| 34 |
with open(schema_json_path, 'r') as f: |
| 35 |
schema_json = f.read() |
| 36 |
return schema.Parse(schema_json) |
| 37 |
|
| 38 |
|
| 39 |
INTEROP_SCHEMA = GetInteropSchema() |
| 40 |
INTEROP_DATUM = { |
| 41 |
'intField': 12, |
| 42 |
'longField': 15234324, |
| 43 |
'stringField': 'hey', |
| 44 |
'boolField': True, |
| 45 |
'floatField': 1234.0, |
| 46 |
'doubleField': -1234.0, |
| 47 |
'bytesField': b'12312adf', |
| 48 |
'nullField': None, |
| 49 |
'arrayField': [5.0, 0.0, 12.0], |
| 50 |
'mapField': {'a': {'label': 'a'}, 'bee': {'label': 'cee'}}, |
| 51 |
'unionField': 12.0, |
| 52 |
'enumField': 'C', |
| 53 |
'fixedField': b'1019181716151413', |
| 54 |
'recordField': { |
| 55 |
'label': 'blah', |
| 56 |
'children': [{'label': 'inner', 'children': []}], |
| 57 |
}, |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
def WriteDataFile(path, datum, schema): |
| 62 |
datum_writer = io.DatumWriter() |
| 63 |
with open(path, 'wb') as writer: |
| 64 |
# NB: not using compression |
| 65 |
with datafile.DataFileWriter(writer, datum_writer, schema) as dfw: |
| 66 |
dfw.append(datum) |
| 67 |
|
| 68 |
|
| 69 |
class TestDataFileInterop(unittest.TestCase): |
| 70 |
def testInterop(self): |
| 71 |
with tempfile.NamedTemporaryFile() as temp_path: |
| 72 |
WriteDataFile(temp_path.name, INTEROP_DATUM, INTEROP_SCHEMA) |
| 73 |
|
| 74 |
# read data in binary from file |
| 75 |
datum_reader = io.DatumReader() |
| 76 |
with open(temp_path.name, 'rb') as reader: |
| 77 |
dfr = datafile.DataFileReader(reader, datum_reader) |
| 78 |
for datum in dfr: |
| 79 |
self.assertEqual(INTEROP_DATUM, datum) |
| 80 |
|
| 81 |
|
| 82 |
if __name__ == '__main__': |
| 83 |
raise Exception('Use run_tests.py') |