from os import SEEK_SET
from random import choice, seed
from string import ascii_uppercase, digits

import fastavro
from fastavro.compat import BytesIO

letters = ascii_uppercase + digits
id_size = 100

seed('str_py3')  # Repeatable results


def gen_id():
    return ''.join(choice(letters) for _ in range(id_size))


keys = ['first', 'second', 'third', 'fourth']

testdata = [dict((key, gen_id()) for key in keys) for _ in range(50)]

schema = {
    "fields": [{'name': key, 'type': 'string'} for key in keys],
    "namespace": "namespace",
    "name": "zerobyte",
    "type": "record"
}


def test_str_py3():
    buf = BytesIO()
    fastavro.writer(buf, schema, testdata)

    buf.seek(0, SEEK_SET)
    for i, rec in enumerate(fastavro.iter_avro(buf), 1):
        pass

    size = len(testdata)

    assert i == size, 'bad number of records'
    assert rec == testdata[-1], 'bad last record'

if __name__ == '__main__':
    test_str_py3()

# -*- coding: utf-8 -*-
"""Python3 string tests for fastavro"""

from __future__ import absolute_import

from os import SEEK_SET
from random import choice, seed
from string import ascii_uppercase, digits

try:
    from cStringIO import StringIO as BytesIO
except ImportError:
    from io import BytesIO

import fastavro


letters = ascii_uppercase + digits
id_size = 100

seed('str_py3')  # Repeatable results


def gen_id():
    return ''.join(choice(letters) for _ in range(id_size))


keys = ['first', 'second', 'third', 'fourth']

testdata = [dict((key, gen_id()) for key in keys) for _ in range(50)]

schema = {
    "fields": [{'name': key, 'type': 'string'} for key in keys],
    "namespace": "namespace",
    "name": "zerobyte",
    "type": "record"
}


def test_str_py3():
    buf = BytesIO()
    fastavro.writer(buf, schema, testdata)

    buf.seek(0, SEEK_SET)
    for i, rec in enumerate(fastavro.iter_avro(buf), 1):
        pass

    size = len(testdata)

    assert i == size, 'bad number of records'
    assert rec == testdata[-1], 'bad last record'


if __name__ == '__main__':
    test_str_py3()

Test files shouldn't import 'fastavro.compat'. Just import BytesIO manually.