def it_flattens_nested_dicts_with_lists(self): data = {'top': {'list': ['a', 'b', 'c']}} assert to_nested_php_args(data) == { 'top[list][0]': 'a', 'top[list][1]': 'b', 'top[list][2]': 'c', }
def test_lists(self): data = {'top': {'list': ['a', 'b', 'c']}} result = utils.to_nested_php_args(data) self.assertEqual(result, { 'top[list][0]': 'a', 'top[list][1]': 'b', 'top[list][2]': 'c', })
def test_accepts_list_of_tuples(self): data = [('top', {'list': ['a', 'b', 'c']})] result = utils.to_nested_php_args(data) self.assertEqual(result, [ ('top[list][2]', 'c'), ('top[list][0]', 'a'), ('top[list][1]', 'b'), ])
def _encode_data(data): """URL encode data.""" data = data if data is not None else {} data = to_nested_php_args(data) files = dict([(key, value) for key, value in data.items() if isinstance(value, file_type)]) for fname in files: del data[fname] return data, files or None, None
def test_nested_dicts(self): data = { 'dict': { 'muffin': { 'taco': { 'puffin': 'value' } } } } result = utils.to_nested_php_args(data) self.assertEqual(result, {'dict[muffin][taco][puffin]': 'value'})
def it_flattens_multiple_levels_of_nested_dicts(self): data = { 'dict': { 'muffin': { 'taco': { 'puffin': 'value', }, }, }, } assert to_nested_php_args(data) == { 'dict[muffin][taco][puffin]': 'value', }
def _encode_data(self, data): """URL encode data.""" data = data if data is not None else {} data = to_nested_php_args(data) return urlencode_unicode(data)
def it_flattens_nested_dict(self): data = {'dict': {'key': 'value'}} assert to_nested_php_args(data) == {'dict[key]': 'value'}
def it_raises_type_error_on_bad_data_types(self): x = object() with pytest.raises(TypeError) as e: to_nested_php_args(x) assert str(e.value) == "expected dict or list, got {0}".format(type(x))
def it_flattens_nested_lists_of_tuples(self, data, result): assert sorted(to_nested_php_args(data)) == result
def test_single_level_dict(self): data = {'dict': {'key': 'value'}} result = utils.to_nested_php_args(data) self.assertEqual(result, {'dict[key]': 'value'})