def test_empty_line(): text = '{"a": 1}\n{"a": 2}\n\n' # extra endline with tmpfile('.json') as fn: with open(fn, 'w') as f: f.write(text) j = JSONLines(fn) assert len(convert(list, j)) == 2
def test_append_jsonlines(): with tmpfile('json') as fn: j = JSONLines(fn) append(j, dat) with open(j.path) as f: lines = f.readlines() assert len(lines) == 2 assert 'Alice' in lines[0] assert 'Bob' in lines[1]
def test_write_gzip_lines(): with tmpfile('json.gz') as fn: j = JSONLines(fn) append(j, dat) f = gzip.open(fn) line = next(f) f.close() assert line.decode('utf-8').strip() == str(json.dumps(dat[0]))
def test_read_gzip_lines(): with tmpfile('json.gz') as fn: f = gzip.open(fn, 'wb') for item in dat: s = json.dumps(item).encode('utf-8') f.write(s) f.write(b'\n') f.close() js = JSONLines(fn) assert convert(list, js) == dat
def test_datetimes(): from odo import into import numpy as np data = [{ 'a': 1, 'dt': datetime.datetime(2001, 1, 1) }, { 'a': 2, 'dt': datetime.datetime(2002, 2, 2) }] with tmpfile('json') as fn: j = JSONLines(fn) append(j, data) assert str(into(np.ndarray, j)) == str(into(np.ndarray, data))
def test_tuples_to_json(): ds = dshape('var * {a: int, b: int}') with tmpfile('json') as fn: j = JSON(fn) append(j, [(1, 2), (10, 20)], dshape=ds) with open(fn) as f: assert '"a": 1' in f.read() with tmpfile('json') as fn: j = JSONLines(fn) append(j, [(1, 2), (10, 20)], dshape=ds) with open(fn) as f: assert '"a": 1' in f.read()
def test_discover_jsonlines(): with jsonlines_file(dat) as fn: j = JSONLines(fn) assert discover(j) == discover(dat)
def test_convert_jsonlines(): with jsonlines_file(dat) as fn: j = JSONLines(fn) assert convert(list, j) == dat