def check_update_cases(self, test_cases): for original, expr_str, value, expected in test_cases: print('parse(%r).update(%r, %r) =?= %r' % (expr_str, original, value, expected)) expr = parse(expr_str) actual = expr.update(original, value) assert actual == expected
def check_update_cases(self, test_cases): for original, expr_str, value, expected in test_cases: logger.debug('parse(%r).update(%r, %r) =?= %r' % (expr_str, original, value, expected)) expr = parse(expr_str) actual = expr.update(original, value) assert actual == expected
def get_exactly_one_jsonpath_match(jsonpath, jsondata, custom_error_msg): jsonpath_expr = parser.parse(jsonpath) matches = jsonpath_expr.find(jsondata) if len(matches) != 1: raise exception.BadRequest( custom_error_msg.format(jsonpath, jsondata)) return matches[0].value
def get_exactly_one_jsonpath_match( jsonpath, jsondata, custom_error_msg): jsonpath_expr = parser.parse(jsonpath) matches = jsonpath_expr.find(jsondata) if len(matches) != 1: raise exception.BadRequest( custom_error_msg.format(jsonpath, jsondata)) return matches[0].value
def check_cases(self, test_cases): # Note that just manually building an AST would avoid this dep and isolate the tests, but that would suck a bit # Also, we coerce iterables, etc, into the desired target type for string, data, target in test_cases: print('parse("%s").find(%s) =?= %s' % (string, data, target)) result = parse(string).find(data) if isinstance(target, list): assert [r.value for r in result] == target elif isinstance(target, set): assert set([r.value for r in result]) == target else: assert result.value == target
def update_method( table_name=table_name, table_info=table_info): try: full_path = self._api_endpoint.rstrip( '/') + '/' + table_info['api_path'].lstrip('/') result = self._session.get(full_path).json() # FIXME: generalize to other verbs? jsonpath_expr = parser.parse(table_info['jsonpath']) ingest_data = [match.value for match in jsonpath_expr.find(result)] self.state[table_name] = set( [json.dumps(item, sort_keys=True) for item in ingest_data]) except BaseException: LOG.exception('Exception occurred while updating ' 'table %s.%s from: URL %s', self.name, table_name, full_path)
def update_method(table_name=table_name, table_info=table_info): try: full_path = self._api_endpoint.rstrip( '/') + '/' + table_info['api_path'].lstrip('/') result = self._session.get(full_path).json() # FIXME: generalize to other verbs? jsonpath_expr = parser.parse(table_info['jsonpath']) ingest_data = [ match.value for match in jsonpath_expr.find(result) ] self.state[table_name] = set([ json.dumps(item, sort_keys=True) for item in ingest_data ]) except BaseException: LOG.exception( 'Exception occurred while updating ' 'table %s.%s from: URL %s', self.name, table_name, full_path)
def check_include_cases(self, test_cases): for original, string, expected in test_cases: logging.debug('parse("%s").include(%s) =?= %s' % (string, original, expected)) actual = parse(string).include(original) assert actual == expected
def json_path_extract(self, json_data, xpath): an_expr = parser.parse(xpath) get_data = an_expr.find(json.loads(json_data)) result = [match.value for match in get_data] return result
def get_data_for_key(self, row): depend_data = self.data.get_depend_key(row) response_data = self.run_dependent() json_exe = parse(depend_data) madle = json_exe.find(response_data) return [math.value for math in madle][0] #增强的for循环
def get_case_line_data(self, case_id): rows_data = self.opera_excel.get_rows_data(case_id) return rows_data #执行依赖测试,获取结果 def run_dependent(self): run_method = RunMethod() row_num = self.opera_excel.get_row_num(self.case_id) request_data = self.data.get_data_form_json(row_num) header = self.data.is_header(row_num) method = self.data.get_request_method(row_num) url = self.data.get_request_url(row_num) res = run_method.run_main(method, url, request_data, header) return json.loads(res) #根据依赖的key去获取执行依赖测试case的响应,然后返回 def get_data_for_key(self, row): depend_data = self.data.get_depend_key(row) response_data = self.run_dependent() json_exe = parse(depend_data) madle = json_exe.find(response_data) return [math.value for math in madle][0] #增强的for循环 if __name__ == "__main__": order = {'data': {'out_trade_no': 234927537432095}} res = "data.out_trade_no" json_exe = parse(res) madle = json_exe.find(order) print([math.value for math in madle[0]])