xonsh/tests/test_lazyjson.py

179 lines
4.3 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2015-05-03 15:53:26 -05:00
"""Tests lazy json functionality."""
from __future__ import unicode_literals, print_function
2015-05-03 18:25:13 -05:00
from io import StringIO
2015-05-03 15:53:26 -05:00
2016-06-22 23:04:25 +03:00
import pytest
# assert_equal.__self__.maxDiff = None
2015-05-03 15:53:26 -05:00
2016-06-16 00:24:35 -04:00
from xonsh.lazyjson import index, ljdump, LazyJSON, LJNode
2015-05-03 15:53:26 -05:00
def test_index_int():
2015-05-03 17:57:21 -05:00
exp = {'offsets': 0, 'sizes': 2}
2015-05-03 15:53:26 -05:00
s, obs = index(42)
2016-06-22 23:04:25 +03:00
assert exp == obs
2015-05-03 15:53:26 -05:00
def test_index_str():
2015-05-03 17:57:21 -05:00
exp = {'offsets': 0, 'sizes': 7}
2015-05-03 15:53:26 -05:00
s, obs = index('wakka')
2016-06-22 23:04:25 +03:00
assert exp == obs
2015-05-03 15:53:26 -05:00
def test_index_list_ints():
2015-05-03 17:57:21 -05:00
exp = {'offsets': [1, 4, 0], 'sizes': [1, 2, 8]}
2015-05-03 15:53:26 -05:00
s, obs = index([1, 42])
2016-06-22 23:04:25 +03:00
assert exp == obs
2015-05-03 15:53:26 -05:00
2015-05-03 16:05:11 -05:00
def test_index_list_str():
2015-05-03 17:57:21 -05:00
exp = {'offsets': [1, 10, 0], 'sizes': [7, 8, 20]}
2015-05-03 16:05:11 -05:00
s, obs = index(['wakka', 'jawaka'])
2016-06-22 23:04:25 +03:00
assert exp == obs
2015-05-03 16:05:11 -05:00
def test_index_list_str_int():
2015-05-03 17:57:21 -05:00
exp = {'offsets': [1, 10, 0], 'sizes': [7, 2, 14]}
2015-05-03 16:05:11 -05:00
s, obs = index(['wakka', 42])
2016-06-22 23:04:25 +03:00
assert exp == obs
2015-05-03 16:05:11 -05:00
def test_index_list_int_str():
2015-05-03 17:57:21 -05:00
exp = {'offsets': [1, 5, 14, 0], 'sizes': [2, 7, 8, 24]}
2015-05-03 16:05:11 -05:00
s, obs = index([42, 'wakka', 'jawaka'])
2016-06-22 23:04:25 +03:00
assert exp == obs
2015-05-03 16:05:11 -05:00
2015-05-03 17:57:21 -05:00
def test_index_dict_int():
2015-07-29 23:58:25 +02:00
exp = {'offsets': {'wakka': 10, '__total__': 0},
2015-05-03 17:57:21 -05:00
'sizes': {'wakka': 2, '__total__': 14}}
s, obs = index({'wakka': 42})
2016-06-22 23:04:25 +03:00
assert exp == obs
2015-05-03 17:57:21 -05:00
def test_index_dict_str():
2015-07-29 23:58:25 +02:00
exp = {'offsets': {'wakka': 10, '__total__': 0},
2015-05-03 17:57:21 -05:00
'sizes': {'wakka': 8, '__total__': 20}}
s, obs = index({'wakka': 'jawaka'})
2016-06-22 23:04:25 +03:00
assert exp == obs
2015-05-03 17:57:21 -05:00
def test_index_dict_dict_int():
exp = {'offsets': {'wakka': {'jawaka': 21, '__total__': 10},
'__total__': 0,
},
2015-07-29 23:58:25 +02:00
'sizes': {'wakka': {'jawaka': 2, '__total__': 15},
2015-05-03 17:57:21 -05:00
'__total__': 27}
}
s, obs = index({'wakka': {'jawaka': 42}})
2016-06-22 23:04:25 +03:00
assert exp == obs
2015-05-03 16:05:11 -05:00
2015-05-03 18:25:13 -05:00
def test_lazy_load_index():
f = StringIO()
2016-06-16 00:24:35 -04:00
ljdump({'wakka': 42}, f)
2015-05-03 18:25:13 -05:00
f.seek(0)
lj = LazyJSON(f)
2016-06-22 23:04:25 +03:00
assert {'wakka': 10, '__total__': 0} == lj.offsets
assert {'wakka': 2, '__total__': 14} == lj.sizes
2015-05-03 15:53:26 -05:00
2015-05-03 19:57:57 -05:00
def test_lazy_int():
f = StringIO()
2016-06-16 00:24:35 -04:00
ljdump(42, f)
2015-05-03 19:57:57 -05:00
f.seek(0)
lj = LazyJSON(f)
2016-06-22 23:04:25 +03:00
assert 42 == lj.load()
2015-05-03 19:57:57 -05:00
def test_lazy_str():
f = StringIO()
2016-06-16 00:24:35 -04:00
ljdump('wakka', f)
2015-05-03 19:57:57 -05:00
f.seek(0)
lj = LazyJSON(f)
2016-06-22 23:04:25 +03:00
assert 'wakka' == lj.load()
2015-05-03 19:57:57 -05:00
2015-08-05 19:43:30 -05:00
def test_lazy_list_empty():
x = []
f = StringIO()
2016-06-16 00:24:35 -04:00
ljdump(x, f)
2015-08-05 19:43:30 -05:00
f.seek(0)
lj = LazyJSON(f)
2016-06-22 23:04:25 +03:00
assert 0 == len(lj)
assert x == lj.load()
2015-08-05 19:43:30 -05:00
2015-05-03 19:57:57 -05:00
def test_lazy_list_ints():
x = [0, 1, 6, 28, 496, 8128]
f = StringIO()
2016-06-16 00:24:35 -04:00
ljdump(x, f)
2015-05-03 19:57:57 -05:00
f.seek(0)
lj = LazyJSON(f)
2016-06-22 23:04:25 +03:00
assert 28 == lj[3]
assert x[:2:-2] == lj[:2:-2]
assert x == [_ for _ in lj]
assert x == lj.load()
2015-08-05 19:43:30 -05:00
2015-05-03 19:57:57 -05:00
def test_lazy_list_ints():
x = [0, 1, 6, 28, 496, 8128]
f = StringIO()
2016-06-16 00:24:35 -04:00
ljdump(x, f)
2015-05-03 19:57:57 -05:00
f.seek(0)
lj = LazyJSON(f)
2016-06-22 23:04:25 +03:00
assert 28 == lj[3]
assert x[:2:-2] == lj[:2:-2]
assert x == [_ for _ in lj]
assert x == lj.load()
2015-05-03 19:57:57 -05:00
def test_lazy_list_str():
x = ['I', 'have', 'seen', 'the', 'wind', 'blow']
f = StringIO()
2016-06-16 00:24:35 -04:00
ljdump(x, f)
2015-05-03 19:57:57 -05:00
f.seek(0)
lj = LazyJSON(f)
2016-06-22 23:04:25 +03:00
assert 'the' == lj[3]
assert x[:2:-2] == lj[:2:-2]
assert x == [_ for _ in lj]
assert x == lj.load()
2015-05-03 19:57:57 -05:00
def test_lazy_list_ints():
x = [0, 1, 6, 28, 496, 8128]
f = StringIO()
2016-06-16 00:24:35 -04:00
ljdump(x, f)
2015-05-03 19:57:57 -05:00
f.seek(0)
lj = LazyJSON(f)
2016-06-22 23:04:25 +03:00
assert 28 == lj[3]
assert x[:2:-2] == lj[:2:-2]
assert x == [_ for _ in lj]
assert x == lj.load()
2015-05-03 19:57:57 -05:00
def test_lazy_list_list_ints():
x = [[0, 1], [6, 28], [496, 8128]]
f = StringIO()
2016-06-16 00:24:35 -04:00
ljdump(x, f)
2015-05-03 19:57:57 -05:00
f.seek(0)
lj = LazyJSON(f)
2016-06-22 23:04:25 +03:00
assert isinstance(lj[1], LJNode)
assert 28 == lj[1][1]
assert [6 == 28], lj[1].load()
assert x == lj.load()
2015-05-03 19:57:57 -05:00
2015-08-05 19:43:30 -05:00
def test_lazy_dict_empty():
x = {}
f = StringIO()
2016-06-16 00:24:35 -04:00
ljdump(x, f)
2015-08-05 19:43:30 -05:00
f.seek(0)
lj = LazyJSON(f)
2016-06-22 23:04:25 +03:00
assert 0 == len(lj)
assert x == lj.load()
2015-08-05 19:43:30 -05:00
2015-05-03 19:57:57 -05:00
def test_lazy_dict():
f = StringIO()
2016-06-16 00:24:35 -04:00
ljdump({'wakka': 42}, f)
2015-05-03 19:57:57 -05:00
f.seek(0)
lj = LazyJSON(f)
2016-06-22 23:04:25 +03:00
assert ['wakka'] == list(lj.keys())
assert 42 == lj['wakka']
assert 1 == len(lj)
assert {'wakka': 42} == lj.load()
2015-05-03 19:57:57 -05:00
2015-05-03 20:09:26 -05:00
def test_lazy_dict_dict_int():
x = {'wakka': {'jawaka': 42}}
f = StringIO()
2016-06-16 00:24:35 -04:00
ljdump(x, f)
2015-05-03 20:09:26 -05:00
f.seek(0)
lj = LazyJSON(f)
2016-06-22 23:04:25 +03:00
assert ['wakka'] == list(lj.keys())
assert isinstance(lj['wakka'], LJNode)
assert 42 == lj['wakka']['jawaka']
assert 1 == len(lj)
assert x == lj.load()