def test_compare_data_frames(self): """Test a function to compare data frames.""" df1 = load_expected_case(1)[0] df2 = load_expected_case(1)[0] df3 = load_expected_case(2)[0] self.assertTrue(compare_data_frames(df1, df2)) with self.assertRaises(AssertionError): compare_data_frames(df1, df3)
def run_case_without_some_parameters(self, case_num, specific_params=None, special_case=None): """Run a test case deleting some parameters. Args: case_num: The test case number to run. """ test_wb = get_orig_cases_path(case_num) params = load_parameters_case(case_num) exp_dfs = load_expected_case(case_num, special_case) params.remove_non_critical() if specific_params: for specific_param, value in specific_params.items(): params[specific_param] = value # change safe_mode to True, for complete test in safe_mode (very slow) safe_mode = False series = XlSeries(test_wb) test_dfs = series.get_data_frames(params, safe_mode=safe_mode) # get them always into a list if type(test_dfs) != list: test_dfs = [test_dfs] if type(exp_dfs) != list: exp_dfs = [exp_dfs] for test_df, exp_df in zip(test_dfs, exp_dfs): self.assertTrue(compare_data_frames(test_df, exp_df))
def run_case_with_parameters(self, case_num, specific_params=None, special_case=None): """Run a test case with parameters using ParameterDiscovery strategy. Args: case_num (int): The test case number to run. """ test_wb = load_original_case(case_num) params = load_parameters_case(case_num) params["data_ends"] = None exp_dfs = load_expected_case(case_num, special_case) if specific_params: for specific_param, value in specific_params.items(): params[specific_param] = value # get dfs from the strategy series = XlSeries(test_wb) test_dfs = series.get_data_frames(params) if type(test_dfs) != list: test_dfs = [test_dfs] if type(exp_dfs) != list: exp_dfs = [exp_dfs] for test_df, exp_df in zip(test_dfs, exp_dfs): print(test_df.columns, exp_df.columns) self.assertTrue(compare_data_frames(test_df, exp_df))
def _get_data_frames(cls, ws, params, safe_mode): """Extract time data series and return them as data frames.""" # FIRST: discover missing parameters generating attempts attempts = cls._discover_parameters(ws, params) # there is only one attempt, probably the user passed all the params if len(attempts) == 1: params = attempts[0] # SECOND: clean the data cls._clean_data(ws, params) # THIRD: get the data from a cleaned worksheet dfs = cls._get_data(ws, params) return (dfs, params) # there is multiple combinations of parameters to try else: results = [] for params_attempt in attempts: ws_temp = make_ws_copy(ws) try: # SECOND: clean the data cls._clean_data(ws_temp, params_attempt) # THIRD: get the data from a cleaned worksheet dfs = cls._get_data(ws_temp, params_attempt) # don't return a list with only one element if type(dfs) == list and len(dfs) == 1: dfs = dfs[0] if (type(params_attempt) == list and len(params_attempt) == 1): params_attempt = params_attempt[0] results.append((dfs, params_attempt)) # stops with the first successful result if not safe_mode: break except: continue # remove duplicates unique_results = [] for res in results: repeated = False # first result will not be repeated! for unique_res in unique_results: repeated = True for df_a, df_b in zip(res[0], unique_res[0]): try: compare_data_frames(df_a, df_b) except AssertionError: repeated = False if repeated: break # if True or not repeated: if not repeated: unique_results.append(res) # return results if len(unique_results) == 0: raise Exception(""" File couldn't be parsed with provided parameters: {} Last attempt was: {} """.format(repr(params), repr(params_attempt))) elif len(unique_results) == 1: return unique_results[0] else: print("There is more than one result with given parameters.") dfs = [res[0] for res in unique_results] params = [res[1] for res in unique_results] return (dfs, params_attempt)