test some of the fastavro __main__ functionality
MODIFY
.travis.yml
.travis.yml
@@ -8,6 +8,6 @@ python:
- pypy
- pypy3
install:
- - pip install pytest flake8 check-manifest Cython
+ - pip install pytest flake8 check-manifest Cython ujson
script:
- ./run-tests.sh

MODIFY
appveyor.yml
appveyor.yml
@@ -12,7 +12,7 @@ environment:
- PYTHON: "C:\\Python36-x64"
install:
- - "%PYTHON%\\python.exe -m pip install pytest flake8 check-manifest Cython wheel"
+ - "%PYTHON%\\python.exe -m pip install pytest flake8 check-manifest Cython wheel ujson"
build: off

MODIFY
fastavro/__main__.py
fastavro/__main__.py
@@ -47,7 +47,7 @@ def main(argv=None):
args = parser.parse_args(argv[1:])
if args.codecs:
- print('\n'.join(sorted(avro._reader.BLOCK_READERS)))
+ print('\n'.join(sorted(avro.read.BLOCK_READERS)))
raise SystemExit
files = args.file or ['-']

MODIFY
fastavro/_six.pyx
fastavro/_six.pyx
@@ -30,9 +30,11 @@ if sys.version_info >= (3, 0):
def py3_json_dump(obj, indent):
if _HAS_UJSON:
+ if indent is None:
+ indent = 0
json.dump(obj, stdout, indent=indent)
else:
- json.dump(obj, stdout, indent=indent, encoding=_outenc)
+ json.dump(obj, stdout, indent=indent)
def py3_iterkeys(obj):
return obj.keys()
@@ -68,10 +70,13 @@ else: # Python 2x
_outenc = getattr(stdout, 'encoding', None) or _encoding
def py2_json_dump(obj, indent):
+ kwargs = {}
+ if indent is not None:
+ kwargs['indent'] = indent
if _HAS_UJSON:
- json.dump(obj, stdout, indent=indent)
+ json.dump(obj, stdout, **kwargs)
else:
- json.dump(obj, stdout, indent=indent, encoding=_outenc)
+ json.dump(obj, stdout, encoding=_outenc, **kwargs)
def py2_iterkeys(obj):
return obj.iterkeys()

MODIFY
fastavro/read.py
fastavro/read.py
@@ -15,9 +15,10 @@ is_avro = _read.is_avro
READERS = _read.READERS
LOGICAL_READERS = _read.LOGICAL_READERS
+BLOCK_READERS = _read.BLOCK_READERS
__all__ = [
'acquaint_schema', 'reader', 'schemaless_reader', 'read_data', 'is_avro',
'HEADER_SCHEMA', 'SYNC_SIZE', 'MAGIC', 'SchemaResolutionError',
- 'LOGICAL_READERS', 'READERS',
+ 'LOGICAL_READERS', 'READERS', 'BLOCK_READERS',
]

MODIFY
fastavro/six.py
fastavro/six.py
@@ -30,9 +30,11 @@ if sys.version_info >= (3, 0):
def py3_json_dump(obj, indent):
if _HAS_UJSON:
+ if indent is None:
+ indent = 0
json.dump(obj, stdout, indent=indent)
else:
- json.dump(obj, stdout, indent=indent, encoding=_outenc)
+ json.dump(obj, stdout, indent=indent)
def py3_iterkeys(obj):
return obj.keys()

ADD
tests/test_main_module.py
+from os.path import join, abspath, dirname
+
+import pytest
+import fastavro
+from fastavro.__main__ import main
+
+data_dir = join(abspath(dirname(__file__)), 'avro-files')
+
+
+def test_codecs():
+ argv = ['fastavro', '--codecs']
+ with pytest.raises(SystemExit):
+ ret_val = main(argv)
+ assert 'deflate' in ret_val
+ assert 'null' in ret_val
+
+
+def test_dump_with_ujson():
+ import ujson as ujson
+ fastavro.six._HAS_UJSON = True
+ fastavro.six.json = ujson
+
+ # without pretty option
+ argv = ['fastavro', join(data_dir, 'weather.avro')]
+ main(argv)
+
+ # with pretty option
+ argv = ['fastavro', '-p', join(data_dir, 'weather.avro')]
+ main(argv)
+
+
+def test_dump_with_standard_json():
+ import json as stdjson
+ fastavro.six._HAS_UJSON = False
+ fastavro.six.json = stdjson
+
+ # without pretty option
+ argv = ['fastavro', join(data_dir, 'weather.avro')]
+ main(argv)
+
+ # with pretty option
+ argv = ['fastavro', '-p', join(data_dir, 'weather.avro')]
+ main(argv)

test some of the fastavro __main__ functionality