def process_test_case(self, tmp_tests_dir, test_case): test_name = test_case['test_name'] query = test_case.get('query_python', None) if query is None: return input_table_path = test_case['input_table_path'] query = query.replace('###UT_TESTS_DIR###', script_dir) input_table_path = os.path.join(script_dir, input_table_path) expected_output_table_path = test_case.get( 'expected_output_table_path', None) if expected_output_table_path is not None: expected_output_table_path = os.path.join( script_dir, expected_output_table_path) expected_md5 = calc_file_md5(expected_output_table_path) output_file_name = os.path.basename(expected_output_table_path) actual_output_table_path = os.path.join(tmp_tests_dir, output_file_name) else: actual_output_table_path = os.path.join(tmp_tests_dir, 'expected_empty_file') expected_error = test_case.get('expected_error', None) expected_warnings = test_case.get('expected_warnings', []) delim = test_case['csv_separator'] policy = test_case['csv_policy'] encoding = test_case['csv_encoding'] output_format = test_case.get('output_format', 'input') out_delim, out_policy = ( delim, policy ) if output_format == 'input' else csv_utils.interpret_named_csv_format( output_format) error_info, warnings = rbql_csv.csv_run(query, input_table_path, delim, policy, actual_output_table_path, out_delim, out_policy, encoding) self.assertTrue((expected_error is not None) == (error_info is not None), 'Inside json test: {}'.format(test_name)) if expected_error is not None: self.assertTrue(error_info['message'].find(expected_error) != -1, 'Inside json test: {}'.format(test_name)) else: actual_md5 = calc_file_md5(actual_output_table_path) self.assertTrue( expected_md5 == actual_md5, 'md5 missmatch. Expected table: {}, Actual table: {}'.format( expected_output_table_path, actual_output_table_path)) warnings = sorted(normalize_warnings(warnings)) expected_warnings = sorted(expected_warnings) self.assertEqual(expected_warnings, warnings, 'Inside json test: {}'.format(test_name))
def execute_python(src_table_path, rb_script_path, encoding, input_delim, input_policy, out_delim, out_policy, dst_table_path): query = codecs.open(rb_script_path, encoding=encoding).read() error_info, warnings = rbql_csv.csv_run(query, src_table_path, input_delim, input_policy, dst_table_path, out_delim, out_policy, encoding) if error_info is None: warning_report = '\n'.join(warnings) vim_interface.set_vim_variable('psv_warning_report', warning_report) vim_interface.set_vim_variable('psv_query_status', 'OK') else: vim_interface.report_error_to_vim(error_info['type'], error_info['message'])
def process_test_case(self, tmp_tests_dir, test_case): test_name = test_case['test_name'] minimal_python_version = float(test_case.get('minimal_python_version', 2.7)) if python_version < minimal_python_version: print('Skipping {}: python version must be at least {}. Interpreter version is {}'.format(test_name, minimal_python_version, python_version)) return query = test_case.get('query_python', None) if query is None: return debug_mode = test_case.get('debug_mode', False) randomly_replace_var_names = test_case.get('randomly_replace_var_names', True) input_table_path = test_case['input_table_path'] query = query.replace('###UT_TESTS_DIR###', script_dir) if randomly_replace_var_names: query = randomly_replace_columns_dictionary_style(query) input_table_path = os.path.join(script_dir, input_table_path) expected_output_table_path = test_case.get('expected_output_table_path', None) if expected_output_table_path is not None: expected_output_table_path = os.path.join(script_dir, expected_output_table_path) expected_md5 = calc_file_md5(expected_output_table_path) output_file_name = os.path.basename(expected_output_table_path) actual_output_table_path = os.path.join(tmp_tests_dir, output_file_name) else: actual_output_table_path = os.path.join(tmp_tests_dir, 'expected_empty_file') expected_error = test_case.get('expected_error', None) expected_warnings = test_case.get('expected_warnings', []) delim = test_case['csv_separator'] policy = test_case['csv_policy'] encoding = test_case['csv_encoding'] output_format = test_case.get('output_format', 'input') out_delim, out_policy = (delim, policy) if output_format == 'input' else rbql_csv.interpret_named_csv_format(output_format) if debug_mode: rbql_csv.set_debug_mode() error_info, warnings = rbql_csv.csv_run(query, input_table_path, delim, policy, actual_output_table_path, out_delim, out_policy, encoding) self.assertTrue((expected_error is not None) == (error_info is not None), 'Inside json test: "{}". Expected error: {}, error_info: {}'.format(test_name, expected_error, error_info)) if expected_error is not None: self.assertTrue(error_info['message'].find(expected_error) != -1, 'Inside json test: "{}", Expected error: "{}", Actual error: "{}"'.format(test_name, expected_error, error_info['message'])) else: actual_md5 = calc_file_md5(actual_output_table_path) self.assertTrue(expected_md5 == actual_md5, 'md5 missmatch. Expected table: {}, Actual table: {}'.format(expected_output_table_path, actual_output_table_path)) warnings = sorted(normalize_warnings(warnings)) expected_warnings = sorted(expected_warnings) self.assertEqual(expected_warnings, warnings, 'Inside json test: "{}"'.format(test_name))
import rbql from rbql import rbql_csv user_query = 'SELECT a1, int(a2) % 1000 WHERE a3 != "USA" LIMIT 5' error_info, warnings = rbql_csv.csv_run(user_query, 'input.csv', ',', 'quoted', 'output.csv', ',', 'quoted', 'utf-8') if error_info is None: print(open('output.csv').read()) else: print('Error: {}: {}'.format(error_info['type'], error_info['message']))