def test_complex(self): assert_that( flatten({ 'users': [ { 'name': 'gena', 'surname': 'chibisov', 'groups': ['admin', 'superuser'] }, { 'name': 'vova', 'surname': 'ivanov', 'groups': ['user'] }, { 'name': 'ivan', 'surname': 'petrov', 'groups': [] }, ], 'groups': { 'admin': { 'name': 'Admin', 'grants': ['remove', 'create'] }, 'superuser': { 'name': 'Super user', 'grants': ['all'] }, 'user': { 'name': 'User', 'grants': [] } } }), equal_to({ 'users[0].name': 'gena', 'users[0].surname': 'chibisov', 'users[0].groups[0]': 'admin', 'users[0].groups[1]': 'superuser', 'users[1].name': 'vova', 'users[1].surname': 'ivanov', 'users[1].groups[0]': 'user', 'users[2].name': 'ivan', 'users[2].surname': 'petrov', 'users[2].groups': [], 'groups.admin.name': 'Admin', 'groups.admin.grants[0]': 'remove', 'groups.admin.grants[1]': 'create', 'groups.superuser.name': 'Super user', 'groups.superuser.grants[0]': 'all', 'groups.user.name': 'User', 'groups.user.grants': [], }) )
def get_payload(params, definitions): if not params: return params_has_files = False final_data = {} for key, value in params.items(): with_files, prepared_data = prepare_payload(value) if with_files: params_has_files = True payload_property = definitions.get(key).get('payload_property') if payload_property: final_data[payload_property] = prepared_data else: final_data = prepared_data if params_has_files: if isinstance(final_data, FILE_TYPE): final_data = { 'file': final_data } else: final_data = flatten(final_data) for key, value in final_data.items(): if not isinstance(value, FILE_TYPE): final_data[key] = (None, json.dumps(value)) return { 'type': 'files' if with_files else 'json', 'data': final_data }
def get_payload(params, definitions): if not params: return params_has_files = False final_data = {} for key, value in params.items(): with_files, prepared_data = prepare_payload(value) if with_files: params_has_files = True payload_property = definitions.get(key).get('payload_property') if payload_property: final_data[payload_property] = prepared_data else: final_data = prepared_data if params_has_files: if isinstance(final_data, FILE_TYPE): final_data = {'file': final_data} else: final_data = flatten(final_data) for key, value in final_data.items(): if not isinstance(value, FILE_TYPE): final_data[key] = (None, json.dumps(value)) return {'type': 'files' if with_files else 'json', 'data': final_data}
def test_params_with_files(self): params = { 'data': { 'user': { 'name': 'gena' }, 'avatar': { 'big': open( os.path.join(self.CONFIG['FILES_PATH'], '1.txt') ) } } } expected_data = flatten(prepare_payload(params['data'])[1]) expected_data['user.name'] = ( None, json.dumps(expected_data['user.name']) ) assert_that( get_payload(params, self.definitions), equal_to({ 'type': 'files', 'data': expected_data }) )
def test_with_payload_params(self): class ResourceMethod(ResourceMethodBase): method = 'get' params = { 'data': { 'dest': 'payload' }, } instance = self.get_instance(ResourceMethod) assert_that( instance.get_context({ 'data': { 'user': { 'name': 'gena' } }, }), has_entry('json', {'user': { 'name': 'gena' }})) # with files data = { 'user': { 'name': 'gena', 'avatar': open(os.path.join(self.CONFIG['FILES_PATH'], '1.txt')) } } expected = flatten(data) expected['user.name'] = (None, json.dumps(expected['user.name'])) assert_that(instance.get_context({ 'data': data, }), has_entry('files', expected))
def test_tuple_with_simple_values(self): assert_that( flatten(('gena', 'vova')), equal_to({ '[0]': 'gena', '[1]': 'vova', }) )
def test_dict_with_list_values(self): assert_that( flatten({ 'users': ['gena', 'vova'] }), equal_to({ 'users[0]': 'gena', 'users[1]': 'vova', }) )
def test_dict_with_dict_values(self): assert_that( flatten({ 'user': { 'name': 'gena', 'surname': 'chibisov' } }), equal_to({ 'user.name': 'gena', 'user.surname': 'chibisov', }) )
def test_with_payload_params(self): class ResourceMethod(ResourceMethodBase): method = 'get' params = { 'data': { 'dest': 'payload' }, } instance = self.get_instance(ResourceMethod) assert_that( instance.get_context({ 'data': { 'user': { 'name': 'gena' } }, }), has_entry( 'json', { 'user': { 'name': 'gena' } } ) ) # with files data = { 'user': { 'name': 'gena', 'avatar': open( os.path.join(self.CONFIG['FILES_PATH'], '1.txt') ) } } expected = flatten(data) expected['user.name'] = ( None, json.dumps(expected['user.name']) ) assert_that( instance.get_context({ 'data': data, }), has_entry('files', expected) )
def test_params_with_files(self): params = { 'data': { 'user': { 'name': 'gena' }, 'avatar': { 'big': open(os.path.join(self.CONFIG['FILES_PATH'], '1.txt')) } } } expected_data = flatten(prepare_payload(params['data'])[1]) expected_data['user.name'] = (None, json.dumps(expected_data['user.name'])) assert_that(get_payload(params, self.definitions), equal_to({ 'type': 'files', 'data': expected_data }))
def test_list_with_list_value(self): assert_that( flatten({ 'users': [ [ 'gena', 'vova' ], [ 'max', 'oleg' ] ] }), equal_to({ 'users[0][0]': 'gena', 'users[0][1]': 'vova', 'users[1][0]': 'max', 'users[1][1]': 'oleg', }) )
def test_list_with_dict_value(self): assert_that( flatten({ 'users': [ { 'name': 'gena', 'surname': 'chibisov' }, { 'name': 'vova', 'surname': 'ivanov' } ] }), equal_to({ 'users[0].name': 'gena', 'users[0].surname': 'chibisov', 'users[1].name': 'vova', 'users[1].surname': 'ivanov', }) )
def test_none(self): assert_that( flatten(None), equal_to(None) )
def test_empty_dict(self): assert_that( flatten({}), equal_to({}) )
def test_empty_list(self): assert_that( flatten([]), equal_to([]) )
def test_empty_tuple(self): assert_that( flatten(tuple()), equal_to(tuple()) )
def test_string(self): assert_that( flatten('hello'), equal_to('hello') )
def test_dict_with_simple_key(self): assert_that( flatten({'name': 'gena', 'surname': 'chibisov'}), equal_to({'name': 'gena', 'surname': 'chibisov'}) )