boltons/tests/test_jsonutils.py

32 lines
959 B
Python

# -*- coding: utf-8 -*-
import os
from boltons.jsonutils import (JSONLIterator,
DEFAULT_BLOCKSIZE,
reverse_iter_lines)
CUR_PATH = os.path.dirname(os.path.abspath(__file__))
NEWLINES_DATA_PATH = CUR_PATH + '/newlines_test_data.txt'
JSONL_DATA_PATH = CUR_PATH + '/jsonl_test_data.txt'
def _test_reverse_iter_lines(filename, blocksize=DEFAULT_BLOCKSIZE):
fo = open(filename)
reference = fo.read()
fo.seek(0, os.SEEK_END)
rev_lines = list(reverse_iter_lines(fo, blocksize))
assert '\n'.join(rev_lines[::-1]) == reference
def test_reverse_iter_lines():
for blocksize in (1, 4, 11, 4096):
_test_reverse_iter_lines(NEWLINES_DATA_PATH, blocksize)
def test_jsonl_iterator():
ref = [{u'4': 4}, {u'3': 3}, {u'2': 2}, {u'1': 1}, {}]
jsonl_iter = JSONLIterator(open(JSONL_DATA_PATH), reverse=True)
jsonl_list = list(jsonl_iter)
assert jsonl_list == ref