def test_depth_deepness(): o = jdic({'a':{'b':{'c':{'d':1}}}, 'e':True}) assert o['a.b.c'].depth() == 3 assert o.deepness() == 3 assert [m.value for m in o.browse(depth = 3)] == [1] assert [m.value for m in o.leaves(depth = 3)] == [1] assert [m.value for m in o.leaves(maxdepth = 2)] == [True] assert [m.value for m in o.leaves(maxdepth = 3, sort = True)] == [1, True] assert o.depth() == 0 o = jdic({}) assert o.deepness() == 0
def new(asdict = False, schema = None, serializer = None, driver = None): o = { 1 : 'éàè', 2 : Exception('TestSerialize', 'Just a test'), 3 : { None : 0 }, None : { "None" }, 'a' : None, 'b' : 0.1, 'c' : 5.0, 'd' : { 'da' : 'db' }, 'e' : { 'ea' : { 'eb' : 'ec' } }, 'f' : { 'fa' : { 'fab' : 'fac' }, 'fb' : { 'fbc' : 'fbd' } }, 'g' : { 'a' : { 'b' : [1,2,3] }, 'b' : { 'c' : [4,5,6] } }, 'h' : { 'a' : { 'b' : [[1,2],[3,4],[5,6]] }, 'b' : { 'c' : [[None, "Ok"], [-1, 1.0], [{'d' : 'e'}]] } }, 'i' : [1,2,3], 'j' : [{'a':1}, {'a':2}, {'b':2}, {'c':3}], 'k' : [[{'a':1}, {'b':2}], [{'c':3}]], 'l' : [[[{'a':1}, {'b':2}]], [[{'c':3}, None]]], 'm' : { 'a' : [ { 'b' : [ { 'c' : 3 }, None ] } ] } } if asdict: return o return jdic(o, schema = schema, serializer = serializer, driver = driver)
def test_diff_patch(): o = new() p = new() o['l.0.0'].append(7777) assert o != p d = o.diff(p) assert d == [[['l', 0, 0, 2]]] o.patch(d) assert o != p o = o.patch(d) assert o == p o['l.0.0'].append(7777) assert o != p d = p.diff(o) assert d == [[['l', 0, 0, 2], 7777]] p = p.patch(d) assert o['l.0.0'] == p['l.0.0'] assert o == p p = [1,2,3] o = jdic({'a':1}) o = o.patch(o.diff(p)) assert isinstance(o, JdicSequence) assert o == p p = None assert o.patch(o.diff(p)) == None
def test_nb_leaves(): o = jdic({ 'a':1, 'b': {'c':2}, 'c': [{'d':3}, 4], 'f': [[{'g':5},6],7] }) assert o.nb_leaves() == 7 del(o['a']) assert o.nb_leaves() == 6 o['f']['0.0'].update({'h':8}) assert o.nb_leaves() == 7 assert o == { 'b': {'c':2}, 'c': [{'d':3}, 4], 'f': [[{'g':5, 'h':8},6],7] }
def test_leaves(): o = jdic({ 'a':1, 'b': {'c':2}, 'c': [{'d':3}, 4], 'f': [[{'g':5},6],7] }) assert [r.path for r in o.leaves(sort = True)] == ['a', 'b.c', 'c.0.d', 'c.1', 'f.0.0.g', 'f.0.1', 'f.1'] assert [r.key for r in o.leaves(sort = True)] == ['a', 'c', 'd', 1, 'g', 1, 1] parent_paths = [r.parent_path for r in o.leaves(sort = True)] assert parent_paths == ['', 'b', 'c.0', 'c', 'f.0.0', 'f.0', 'f'] parents = [r.parent for r in o.leaves(sort = True)] assert parents[1] == {'c':2} assert parents[0] == o depths = [r.depth for r in o.leaves(sort = True)] assert depths == [0, 1, 2, 1, 3, 2, 1] values = [r.value for r in o.leaves(sort = True)] assert values == [1, 2, 3, 4, 5, 6, 7]
def test_match(): o = jdic({'e':[{'f':'4'}]}) assert o.match({'e.f': { '$exists': True }}) == True o = jdic({'e':[[0, {'f':'4'}]]}) assert o.match({'e.f': { '$exists': True }}) == True assert o.match({'e.f': { '$exists': False }}) == False assert o.match({'e.g': { '$exists': False }}) == True assert o.match({'e.f': { '$exists': True }}) == True assert o.match({'e.a': { '$exists': True }}) == False assert o.match({'a.e': { '$exists': True }}) == False o = jdic({'e':[{'f':'4'}]}) assert o.match({'e.f':'4'}) == True assert o.match({'e.f':5}) == False assert o.match({'e.f': { '$ne' : 3 }}) == True assert o.match({'e.f': { '$gt' : 3 }}) == False o = jdic({'e':{'f':4}}) assert o.match({'e.f': { '$gt' : 3 }}) == True assert o.match({'e.f': { '$lt' : 5 }}) == True assert o.match({'e.f': { '$lt' : 3 }}) == False assert o.match({'e.f': { '$gt' : '3' }}) == False assert o.match({'e.f': { '$gte' : '3' }}) == False assert o.match({'e.f': { '$gte' : 3 }}) == True assert o.match({'e.z': { '$gte' : 3 }}) == False o = jdic({'e':4}) assert o.match({'f':'5'}) == False assert o.match({'e':4}) == True o = jdic([{'e':4}]) assert o.match({'e':4}) == True assert o.match({'e':'4'}) == False assert o.match({'f':'5'}) == False assert o.match({'0.e':4}) == True o = jdic({'e':[0, 1, {'f':1}]}) assert o.match({'e':{'$in':[0]}}) == True assert o.match({'e':{'$in':[0, 555]}}) == True assert o.match({'e':{'$in':[555, 556]}}) == False assert o.match({'e.2':{'$in':[{'f':1}]}}) == True assert o.match({'e.1':{'$in':[{'f':1}]}}) == False assert o.match({'e.1':{'$nin':[{'f':2}]}}) == True assert o.match({'e.2':{'$nin':[{'f':2}]}}) == True assert o.match({'e.2':{'$ne':None}}) == True assert o.match({'e.2':{'$gt':None}}) == False
def test_merge(): o = jdic({ 'a' : 1, 'b' : 2}) o.merge({'a':2,'b':3}) assert o == {'a':2, 'b':3} o.merge({'a':[1,2]}) assert o == {'a':[1,2], 'b':3}