1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
"""jdata.py test unit
To run the test, please run
import testjd
testjd.run()
Copyright (c) 2019 Qianqian Fang <q.fang at neu.edu>
"""
import unittest
import jdata as jd
import numpy as np
import collections
import json
class TestModule(unittest.TestCase):
def test_module(self):
data=collections.OrderedDict();
data['const']=[2.0, 1, True, False, None, float('nan'), float('-inf')];
data['shortarray']=[1,2,3];
data['a_complex']=1+2.0j;
data['object']=[[[1],[2],[3]],None, False];
data['a_typedarray']=np.asarray([9,9,9,9],dtype=np.uint8);
data['a_ndarray']=np.arange(1,10,dtype=np.int32).reshape(3,3);
data['a_biginteger']=9007199254740991;
data['a_map']={
float('nan'): 'one',
2: 'two',
"k": 'three'
};
print('== Original Python native data ==')
newdata=data.copy();
print(newdata);
print('== JData-annotated data ==')
print(jd.show(jd.encode(newdata),indent=4, default=jd.jsonfilter));
print('== JData-annotated data exported to JSON with zlib compression ==')
newdata=data.copy();
print(jd.show(jd.encode(newdata,{'compression':'zlib','base64':True}), indent=4, default=jd.jsonfilter));
print('== Decoding a JData-encoded data and printed in JSON format ==')
newdata=data.copy();
print(jd.show(jd.decode(jd.encode(newdata)), indent=4, default=jd.jsonfilter));
print('== Saving encoded data to test.json ==')
jd.save(data,'test.json')
print('== Loading data from test.json and decode ==')
newdata=jd.load('test.json')
print(jd.show(newdata, indent=4, default=jd.jsonfilter));
if __name__ == '__main__':
unittest.main()
|