def test_filter_false(): t1 = { "falsey_dict": {}, "falsey_list": [], "falsey_string": "", "is_false": False, "a": None, "b": 2, "c": { "d": "herm", "e": None, "falsey_dict": {}, "falsey_list": [], "falsey_string": "", "is_false": False, }, } result = JsonObj(t1).filter_false() assert result == JsonObj( **{ "b": 2, "c": { "d": "herm", "e": None, "falsey_dict": {}, "falsey_list": [], "falsey_string": "", "is_false": False, }, })
def test_protected_attrs_slash_members(): j = JsonObj() j.key = 'value' j['12'] = 'twelve' with pytest.raises(ValueError): j.items = [1, 2, 3, 4] j['items'] = [1, 2, 3, 4] j_items = j.items assert j.items != [1, 2, 3, 4] assert j['items'] == [1, 2, 3, 4]
def test_dataclass_stringify(): a = JsonObj(**{"a": 'c', "herm": 123}) from dataclasses import dataclass @dataclass() class DataThing: n: int s: str data = DataThing(n=1, s='stringy') data_string = JSON.stringify(data) assert data_string == '{"n":1,"s":"stringy"}' a.data = data nested_str = JSON.stringify(a) assert nested_str == '{"a":"c","herm":123,"data":{"n":1,"s":"stringy"}}'
def test_dot_list_keys() -> None: d = JsonObj(d1) expected = [ "id", "code", "subd.a", "subd.b.herm", "type", "bars", "n", "date_str", "float_here", "complex", "profile_id", "state", ] assert set([tuple(el.split('.')) for el in expected]) == set(d.dot_keys_list())
def test_filter_falsey_recursive(): d = { "falsey_dict": {}, "falsey_list": [], "falsey_string": "", "is_false": False, "a": None, "b": 2, "c": { "d": "herm", "e": None, "falsey_dict": {}, "falsey_list": [], "falsey_string": "", "is_false": False, }, } result = JsonObj(d).filter_false(recursive=True) assert result == JsonObj(**{"b": 2, "c": {"d": "herm"}})
def test_dot_list_keys_sorted() -> None: d = JsonObj(d1) expected = [ "id", "code", "subd.a", "subd.b.herm", "type", "bars", "n", "date_str", "float_here", "complex", "profile_id", "state", ] print(d.dot_keys_list(sort_keys=True)) assert [tuple(el.split('.')) for el in sorted(expected)] == d.dot_keys_list(sort_keys=True)
def test_dot_iter_keys() -> None: d = JsonObj(d1) expected = [ "id", "code", "subd.a", "subd.b.herm", "type", "bars", "n", "date_str", "float_here", "complex", "profile_id", "state", ] print(set(d.dot_keys())) dot_keys_set = set(d.dot_keys()) expected_tuples_set = set(tuple(el.split('.')) for el in expected) assert dot_keys_set == expected_tuples_set
def test_dictainer_basic_unpacking() -> None: thing = Thingy({"a": 1, "b": 2, "c": 3, "d": ["list"]}) thing2 = Thingy({ "a": 1, "b": 2, "c": 3, "d": ["different list"], "a": 234 }) assert thing.a == thing["a"] assert {**thing} == {"a": 1, "b": 2, "c": 3, "d": ["list"]} assert {**thing2} == {"a": 234, "b": 2, "c": 3, "d": ["different list"]} merged = {**thing, **thing2} assert merged["a"] == 234 merged_dictainer = JsonObj({**thing, **thing2}) assert {**merged_dictainer} == merged
def test_json_dict_reject_non_string_key(): t1 = {1: None, 2: 2} with pytest.raises(ValueError): jd = JsonObj(t1)
def test_dotlookup_dont_exist() -> None: d = JsonObj(d1) with pytest.raises(KeyError): dot_lookup = d["subd.a.hermhermherm.ohno"]
def test_dotlookup_no_dots() -> None: d = JsonObj(d1) dot_get = d.n dot_lookup = d.dot_lookup("n") assert dot_lookup == dot_get
def test_dotlookup() -> None: d = JsonObj(d1) dot_get = d.subd.a dot_lookup = d.dot_lookup("subd.a") assert dot_lookup == dot_get
def test_dot_items(): jd: JsonObj = JSON(data) expected = [ (('id', ), 1), (('code', ), None), (('subd', 'a'), 23), (('subd', 'b', 'herm'), 2), (('type', ), 'foo'), (('root', 'string_list'), ['one', 'two', 'octopus', 'what_is_up']), (('root', 'mixed_dict', 'num'), 123), (('root', 'mixed_dict', 'obj', 'k'), 'v'), (('root', 'mixed_dict', 'list'), ['s', 123, JsonObj(**{'k': 'v'})]), ( ('bars', ), [ JsonObj(**{'id': 6934900}), JsonObj(**{'id': 6934977}), JsonObj(**{'id': 6934992}), JsonObj(**{'id': 6934993}), JsonObj(**{'id': 6935014}), ], ), (('n', ), 10), (('date_str', ), '2013-07-08 00:00:00'), (('float_here', ), 0.454545), ( ('complex', ), [ JsonObj(**{ 'id': 83865, 'goal': Decimal('2.000000'), 'state': 'active' }) ], ), (('profile_id', ), None), (('state', ), 'active'), ] expected_strings = [ ('id', 1), ('code', None), ('subd.a', 23), ('subd.b.herm', 2), ('type', 'foo'), ('root.string_list', ['one', 'two', 'octopus', 'what_is_up']), ('root.mixed_dict.num', 123), ('root.mixed_dict.obj.k', 'v'), ('root.mixed_dict.list', ['s', 123, JsonObj(**{'k': 'v'})]), ( 'bars', [ JsonObj(**{'id': 6934900}), JsonObj(**{'id': 6934977}), JsonObj(**{'id': 6934992}), JsonObj(**{'id': 6934993}), JsonObj(**{'id': 6935014}), ], ), ('n', 10), ('date_str', '2013-07-08 00:00:00'), ('float_here', 0.454545), ( 'complex', [ JsonObj(**{ 'id': 83865, 'goal': Decimal('2.000000'), 'state': 'active' }) ], ), ('profile_id', None), ('state', 'active'), ] dkl = jd.dot_items_list() assert expected == dkl # from time import time # ta = time() # # for i in range(10): # for i in range(100): # a = list(jd.dot_items_chain()) # tb = time() # for i in range(100): # b = list(jd.dot_items_chain2()) # tc = time() # for i in range(100): # c = list(jd.dot_items()) # td = time() # print('yielding', tc-tb,'-- chain: ', tb-ta, '-- og: ', td-tc) expected_json_str = JSON.stringify(expected, sort_keys=True, pretty=True) output_json_str = JSON.stringify(dkl, sort_keys=True, pretty=True) assert output_json_str == expected_json_str try: from deepdiff import DeepDiff print('deepdiff', DeepDiff(expected, dkl)) assert not DeepDiff(expected, dkl) except ModuleNotFoundError as mnfe: print(mnfe)
def test_number_keys(): j = JsonObj() j.key = 'value' j['12'] = 'twelve' assert j['12'] == 'twelve'
def test_cycle_stringify(): a = JsonObj(**{"a": 'c', "herm": 123}) b = JsonObj(**{"c": 'c', 'd': a}) a.circle = b with pytest.raises((TypeError, ValueError)): json_str = a.to_json()
def test_cycle_eject(): a = JsonObj(**{"a": 'c', "herm": 123}) b = JsonObj(**{"c": 'c', 'd': a}) a.circle = b astring = str(a) assert isinstance(astring, str)