adding jsonutils tests

This commit is contained in:
Mahmoud Hashemi 2015-05-18 21:37:49 -07:00
parent a38fb7b029
commit 278e77d82d
4 changed files with 47 additions and 15 deletions

View File

@ -178,18 +178,4 @@ class JSONLIterator(object):
continue continue
return obj return obj
__next__ = next
if __name__ == '__main__':
def _test_reverse_iter_lines(filename, blocksize=DEFAULT_BLOCKSIZE):
# from cStringIO import StringIO
fo = open('_tmp_nl.txt')
reference = fo.read()
# fo = StringIO(reference)
fo.seek(0, os.SEEK_END)
rev_lines = list(reverse_iter_lines(fo, blocksize))
assert '\n'.join(rev_lines[::-1]) == reference
for blocksize in (1, 4, 11, 4096):
_test_reverse_iter_lines('_tmp_nl.txt', blocksize)
print(list(JSONLIterator(open('_tmp_jsonl.jsonl'), reverse=True)))

View File

@ -0,0 +1,5 @@
{}
{"1": 1}
{"2": 2}
{"3": 3}
{"4": 4}

View File

@ -0,0 +1,10 @@
a
b
c
d
e
f
g
hijklmnop
qrstuv
wxyz

31
tests/test_jsonutils.py Normal file
View File

@ -0,0 +1,31 @@
# -*- 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