def test_both(self): _source = {"data": {"item": [1, 2, 3]}} _modified = compress(_source, node_delimiter="__", list_delimiter="*") _like_source = expand(_modified, node_delimiter="__", list_delimiter="*") assert _source == _like_source
def test_back_nested_mapping(self): @schema(strip_unknown=True) class CustomMapper: nickname: str = Options(rename="info.nickname") _int: int = Options(alias="int", rename="characteristics/0") _dex: int = Options(alias="dex", rename="characteristics/1") _str: int = Options(alias="str", rename="characteristics/2") _vit: int = Options(alias="vit", rename="characteristics/3") _in_data = { "nickname": "Killer777", "int": 7, "dex": 55, "str": 11, "vit": 44 } mapper = CustomMapper(**_in_data) assert expand(to_dict(mapper)) == { "info": { "nickname": "Killer777", }, "characteristics": [7, 55, 11, 44] }
def test_expand(self): _compressed = {"data__item*0": 1, "data__item*1": 2, "data__item*2": 3} _normal = {"data": {"item": [1, 2, 3]}} _ex_compressed = expand(_compressed, node_delimiter="__", list_delimiter="*") assert _normal == _ex_compressed
def get_data(self, request, method_handler, **kwargs): try: data = json.loads(request.body) data = expand(data) except JSONDecodeError: data = {} get_data = multi_dict_to_dict(request.GET) get_data = expand(get_data) data.update(get_data) post_data = multi_dict_to_dict(request.POST) post_data = expand(post_data) data.update(post_data) return data
def test_back_nested_mapping(): @schema(strip_unknown=True) class CustomMapper: nickname: str = Options(rename="info.nickname") _int: int = Options(alias="int", rename="characteristics/0") _dex: int = Options(alias="dex", rename="characteristics/1") _str: int = Options(alias="str", rename="characteristics/2") _vit: int = Options(alias="vit", rename="characteristics/3") _in_data = { "nickname": "Killer777", "int": 7, "dex": 55, "str": 11, "vit": 44 } mapper = CustomMapper(**_in_data) data = to_dict(mapper) # EXPAND BUG: need sort nodes keys = sorted(data.keys()) data = {key: data[key] for key in keys} assert expand(data) == { "info": { "nickname": "Killer777", }, "characteristics": [7, 55, 11, 44] }
def test_list_missing_index(self): data = { 'root.1.child/0.count': '0', 'root.1.child/1.count': '0', 'root.1.child/2.count': '0', 'root.1.child/4.count': '0', } assert expand(data) == { 'root': { '1': { 'child': [ { 'count': '0' }, { 'count': '0' }, { 'count': '0' }, {}, { 'count': '0' }, ] } } }
def test_dict_in_list(self): data = { 'root.1.child/0.count': '0', 'root.1.child/1.count': '0', 'root.1.child/2.count': '0', 'root.1.child/3.count': '0', } assert expand(data) == { 'root': { '1': { 'child': [ { 'count': '0' }, { 'count': '0' }, { 'count': '0' }, { 'count': '0' }, ] } } }
def test_value_in_list(self): data = { 'root.1.child/0': '0', 'root.1.child/1': '0', 'root.1.child/2': '0', 'root.1.child/3': '0', } assert expand(data) == {'root': {'1': {'child': ['0', '0', '0', '0']}}}
def test_matrix(self): _source = { 'matrix/0/0': 1, 'matrix/0/1': 2, 'matrix/0/2': 3, 'matrix/1/0': 4, 'matrix/1/1': 5, 'matrix/1/2': 6, 'matrix/2/0': 7, 'matrix/2/1': 8, 'matrix/2/2': 9, } assert expand(_source) == {'matrix': [[1, 2, 3], [4, 5, 6], [7, 8, 9]]}
def test_empty(self): _source = {"data": {"item": []}} _modified = compress(_source) _like_source = expand(_modified) assert _source == _like_source