From 278e77d82dc51a2bda83ca8dc8af4e80e56b5e4e Mon Sep 17 00:00:00 2001 From: Mahmoud Hashemi Date: Mon, 18 May 2015 21:37:49 -0700 Subject: [PATCH] adding jsonutils tests --- boltons/jsonutils.py | 16 +--------------- tests/jsonl_test_data.txt | 5 +++++ tests/newlines_test_data.txt | 10 ++++++++++ tests/test_jsonutils.py | 31 +++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 tests/jsonl_test_data.txt create mode 100644 tests/newlines_test_data.txt create mode 100644 tests/test_jsonutils.py diff --git a/boltons/jsonutils.py b/boltons/jsonutils.py index f1a7078..dbeca4d 100644 --- a/boltons/jsonutils.py +++ b/boltons/jsonutils.py @@ -178,18 +178,4 @@ class JSONLIterator(object): continue return obj - -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))) + __next__ = next diff --git a/tests/jsonl_test_data.txt b/tests/jsonl_test_data.txt new file mode 100644 index 0000000..5b8fd12 --- /dev/null +++ b/tests/jsonl_test_data.txt @@ -0,0 +1,5 @@ +{} +{"1": 1} +{"2": 2} +{"3": 3} +{"4": 4} diff --git a/tests/newlines_test_data.txt b/tests/newlines_test_data.txt new file mode 100644 index 0000000..268abe8 --- /dev/null +++ b/tests/newlines_test_data.txt @@ -0,0 +1,10 @@ +a +b +c +d +e +f +g +hijklmnop +qrstuv +wxyz diff --git a/tests/test_jsonutils.py b/tests/test_jsonutils.py new file mode 100644 index 0000000..21f43ac --- /dev/null +++ b/tests/test_jsonutils.py @@ -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