xonsh/tests/test_lazyjson.py

185 lines
4.6 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
import nose
2015-05-03 19:57:57 -05:00
from nose.tools import assert_equal, assert_is_instance
2015-05-03 17:57:21 -05:00
assert_equal.__self__.maxDiff = None
2015-05-03 15:53:26 -05:00
2015-05-03 19:57:57 -05:00
from xonsh.lazyjson import index, dump, LazyJSON, Node
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)
assert_equal(exp, obs)
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')
assert_equal(exp, obs)
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])
assert_equal(exp, obs)
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'])
assert_equal(exp, obs)
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])
assert_equal(exp, obs)
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'])
assert_equal(exp, obs)
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})
assert_equal(exp, obs)
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'})
assert_equal(exp, obs)
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}})
assert_equal(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()
dump({'wakka': 42}, f)
f.seek(0)
lj = LazyJSON(f)
assert_equal({'wakka': 10, '__total__': 0}, lj.offsets)
assert_equal({'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()
dump(42, f)
f.seek(0)
lj = LazyJSON(f)
assert_equal(42, lj.load())
def test_lazy_str():
f = StringIO()
dump('wakka', f)
f.seek(0)
lj = LazyJSON(f)
assert_equal('wakka', lj.load())
2015-08-05 19:43:30 -05:00
def test_lazy_list_empty():
x = []
f = StringIO()
dump(x, f)
f.seek(0)
lj = LazyJSON(f)
assert_equal(0, len(lj))
assert_equal(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()
dump(x, f)
f.seek(0)
lj = LazyJSON(f)
assert_equal(28, lj[3])
assert_equal(x[:2:-2], lj[:2:-2])
assert_equal(x, [_ for _ in lj])
assert_equal(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()
dump(x, f)
f.seek(0)
lj = LazyJSON(f)
assert_equal(28, lj[3])
assert_equal(x[:2:-2], lj[:2:-2])
assert_equal(x, [_ for _ in lj])
assert_equal(x, lj.load())
def test_lazy_list_str():
x = ['I', 'have', 'seen', 'the', 'wind', 'blow']
f = StringIO()
dump(x, f)
f.seek(0)
lj = LazyJSON(f)
assert_equal('the', lj[3])
assert_equal(x[:2:-2], lj[:2:-2])
assert_equal(x, [_ for _ in lj])
assert_equal(x, lj.load())
def test_lazy_list_ints():
x = [0, 1, 6, 28, 496, 8128]
f = StringIO()
dump(x, f)
f.seek(0)
lj = LazyJSON(f)
assert_equal(28, lj[3])
assert_equal(x[:2:-2], lj[:2:-2])
assert_equal(x, [_ for _ in lj])
assert_equal(x, lj.load())
def test_lazy_list_list_ints():
x = [[0, 1], [6, 28], [496, 8128]]
f = StringIO()
dump(x, f)
f.seek(0)
lj = LazyJSON(f)
assert_is_instance(lj[1], Node)
assert_equal(28, lj[1][1])
assert_equal([6, 28], lj[1].load())
assert_equal(x, lj.load())
2015-08-05 19:43:30 -05:00
def test_lazy_dict_empty():
x = {}
f = StringIO()
dump(x, f)
f.seek(0)
lj = LazyJSON(f)
assert_equal(0, len(lj))
assert_equal(x, lj.load())
2015-05-03 19:57:57 -05:00
def test_lazy_dict():
f = StringIO()
dump({'wakka': 42}, f)
f.seek(0)
lj = LazyJSON(f)
assert_equal(['wakka'], list(lj.keys()))
assert_equal(42, lj['wakka'])
assert_equal(1, len(lj))
assert_equal({'wakka': 42}, lj.load())
2015-05-03 20:09:26 -05:00
def test_lazy_dict_dict_int():
x = {'wakka': {'jawaka': 42}}
f = StringIO()
dump(x, f)
f.seek(0)
lj = LazyJSON(f)
assert_equal(['wakka'], list(lj.keys()))
assert_is_instance(lj['wakka'], Node)
assert_equal(42, lj['wakka']['jawaka'])
assert_equal(1, len(lj))
assert_equal(x, lj.load())
2015-05-03 19:57:57 -05:00
2015-05-03 15:53:26 -05:00
if __name__ == '__main__':
nose.runmodule()