def test_evaluate_globals(self): from UnitTesting.evaluate_globals import evaluate_globals import sympy as sp logging.getLogger().setLevel('CRITICAL') self.module = '' self.module_name = 'TestModule' self.initialization_string = '' self.function = '' self.global_list = [] self.assertRaises(AssertionError, evaluate_globals, self) self.module = 'random_string' self.module_name = 'TestModule' self.initialization_string = '' self.function = '' self.global_list = [] self.assertRaises(AssertionError, evaluate_globals, self) a, b, c = sp.symbols('a b c') self.module = 'module_for_testing' self.module_name = 'TestModule' self.initialization_string = '' self.function = 'function(create_gamma=False)' self.global_list = [] self.assertEqual({}, evaluate_globals(self)) self.module = 'module_for_testing' self.module_name = 'TestModule' self.initialization_string = '' self.function = 'function(create_gamma=False)' self.global_list = ['alpha'] self.assertEqual({'alpha': a + b + c}, evaluate_globals(self)) self.module = 'module_for_testing' self.module_name = 'TestModule' self.initialization_string = '' self.function = 'function(create_gamma=False)' self.global_list = ['alpha', 'betaU'] self.assertEqual( { 'alpha': a + b + c, 'betaU': [0, a**2 + 2 * b**2 + c**2, sp.sqrt(a + b)] }, evaluate_globals(self)) self.module = 'module_for_testing' self.module_name = 'TestModule' self.initialization_string = '' self.function = 'function(create_gamma=False)' self.global_list = ['gamma'] self.assertRaises(AttributeError, evaluate_globals, self) self.module = 'module_for_testing' self.module_name = 'TestModule' self.initialization_string = '' self.function = 'function(create_gamma=True)' self.global_list = ['alpha', 'betaU', 'gamma'] self.assertEqual( { 'alpha': a + b + c, 'betaU': [0, a**2 + 2 * b**2 + c**2, sp.sqrt(a + b)], 'gamma': sp.atan2(b, a) }, evaluate_globals(self)) self.module = 'module_for_testing' self.module_name = 'TestModule' self.initialization_string = '' self.function = 'function2(create_gamma=False)' self.global_list = ['alpha2'] self.assertRaises(NameError, evaluate_globals, self) self.module = 'module_for_testing' self.module_name = 'TestModule' self.initialization_string = '' self.function = 'function2(create_gamma=False)' self.global_list = ['betaU2'] self.assertRaises(NameError, evaluate_globals, self) a2, b2, c2 = sp.symbols('a2 b2 c2') self.module = 'module_for_testing' self.module_name = 'TestModule' self.initialization_string = 'import module_for_testing as tm\ntm.init_function2()\n' self.function = 'function2(create_gamma=False)' self.global_list = ['alpha2', 'betaU2'] self.assertEqual( { 'alpha2': a2 + b2 + c2, 'betaU2': [0, a2**2 + 2 * b2**2 + c2**2, sp.sqrt(a2 + b2)] }, evaluate_globals(self)) self.module = 'module_for_testing' self.module_name = 'TestModule' self.initialization_string = '' self.function = 'function2(create_gamma=False)' self.global_list = ['alpha2', 'betaU2'] self.assertEqual( { 'alpha2': a2 + b2 + c2, 'betaU2': [0, a2**2 + 2 * b2**2 + c2**2, sp.sqrt(a2 + b2)] }, evaluate_globals(self)) self.module = 'module_for_testing' self.module_name = 'TestModule' self.initialization_string = '' self.function = 'function2(create_gamma=False)' self.global_list = ['alpha2', 'betaU2', 'gamma2'] self.assertRaises(AttributeError, evaluate_globals, self) self.module = 'module_for_testing' self.module_name = 'TestModule' self.initialization_string = '' self.function = 'function2(create_gamma=True)' self.global_list = ['alpha2', 'betaU2', 'gamma2'] self.assertEqual( { 'alpha2': a2 + b2 + c2, 'betaU2': [0, a2**2 + 2 * b2**2 + c2**2, sp.sqrt(a2 + b2)], 'gamma2': sp.atan2(b2, a2) }, evaluate_globals(self)) logging.getLogger().setLevel('INFO') logging.info(' All evaluate_globals tests passed.')
def run_test(self): # Step 1: Setup logging.info(' Currently working on function ' + self.function + ' in module ' + self.module_name + '...\n') # Step 1.a: Set precision to the value defined in standard_constants mp.dps = precision # Step 1.b: Import trusted_values_dict from trusted_values_dict.py in self.path logging.info(' Importing trusted_values_dict...') self.trusted_values_dict = import_module( 'trusted_values_dict').trusted_values_dict logging.info(' ...Success: Imported trusted_values_dict.\n') # Step 1.c: Set boolean self.first_time based on existence of desired trusted_values_dict entry self.first_time = self.trusted_values_dict_name not in self.trusted_values_dict if self.first_time: logging.info( ' Proper entry not in trusted_values_dict -- ' 'this function in this module is being run for the first time.') # Step 1.e: Set trusted_values_dict_entry to its corresponding trusted_values_dict entry self.trusted_values_dict_entry = {} if self.first_time else self.trusted_values_dict[ self.trusted_values_dict_name] # Step 2: Calculation # Step 2.a: Call evaluate_globals which calls self.function and gets expressions for all globals in self.global_list logging.info(' Calling evaluate_globals...') self.variable_dict = evaluate_globals(self) logging.info(' ...Success: evaluate_globals ran without errors.\n') # Step 2.b: Call cse_simplify_and_evaluate_sympy_expressions to assign each variable in each expression a random # value and calculate the numerical result logging.info(' Calling cse_simplify_and_evaluate_sympy_expressions...') self.calculated_dict = cse_simplify_and_evaluate_sympy_expressions(self) logging.info( ' ...Success: cse_simplify_and_evaluate_sympy_expressions ran without errors.\n' ) # Step 3: Comparison if self.first_time: # Step 3.a: Print self.calculated_dict in a nice format and append it to trusted_values_dict logging.info( ' Calling first_time_print since it is being run for the first time...' ) first_time_print(self) logging.info( ' ...Success: first_time_print ran without errors. Automatically failing due to first_time.\n' ) self.assertTrue(False) else: # Step 3.b: Call calc_error to calculate the error between the trusted values and the calculated values logging.info(' Calling calc_error...') values_identical = calc_error(self) # If there is an error large enough, fail if not values_identical: self.assertTrue( values_identical, 'Variable(s) above have different calculated and trusted values. Follow ' 'instructions above.') # Otherwise, pass else: logging.info(' ...Success: calc_error ran without errors.\n')
def test_globals(self): self.module = 'GiRaFFEfood_HO.GiRaFFEfood_HO' self.module_name = 'GiRaFFEfood_HO' self.function = 'GiRaFFEfood_HO()' self.global_list = ['AD', 'ValenciavU'] self.initialization_string = """""" self.trusted_values_dict_name = 'GiRaFFEfood_HO__GiRaFFEfood_HO__globals' try: # Step 1.a: Initialize core Python/UnitTesting modules from UnitTesting.calc_error import calc_error from UnitTesting.evaluate_globals import evaluate_globals from UnitTesting.first_time_print import first_time_print from UnitTesting.cse_simplify_and_evaluate_sympy_expressions import cse_simplify_and_evaluate_sympy_expressions from UnitTesting.standard_constants import precision from mpmath import mp from importlib import import_module import logging logging.info(' Currently working on function ' + self.function + ' in module ' + self.module_name + '...\n') # Step 1.b: Set precision to the value defined in standard_constants mp.dps = precision # Step 1.c: Import trusted_values_dict from trusted_values_dict.py in self.path logging.info(' Importing trusted_values_dict...') self.trusted_values_dict = import_module('trusted_values_dict').trusted_values_dict logging.info(' ...Success: Imported trusted_values_dict.\n') # Step 1.d: Set boolean self.first_time based on existence of desired trusted_values_dict entry self.first_time = self.trusted_values_dict_name not in self.trusted_values_dict logging.debug(' First time: ' + str(self.first_time)) # Step 1.e: Set trusted_values_dict_entry to its corresponding trusted_values_dict entry self.trusted_values_dict_entry = {} if self.first_time else self.trusted_values_dict[self.trusted_values_dict_name] # Step 2: Calculation # Step 2.a: Call evaluate_globals which calls self.function and gets expressions for all globals in self.global_list logging.info(' Calling evaluate_globals...') self.variable_dict = evaluate_globals(self) logging.info(' ...Success: evaluate_globals ran without errors.\n') # Step 2.b: Call cse_simplify_and_evaluate_sympy_expressions to assign each variable in each expression a random # value and calculate the numerical result logging.info(' Calling cse_simplify_and_evaluate_sympy_expressions...') self.calculated_dict = cse_simplify_and_evaluate_sympy_expressions(self) logging.info(' ...Success: cse_simplify_and_evaluate_sympy_expressions ran without errors.\n') # Step 3: Comparison if self.first_time: # Step 3.a: Print self.calculated_dict in a nice format and append it to trusted_values_dict logging.info(' Calling first_time_print since it is being run for the first time...') first_time_print(self) logging.info(' ...Success: first_time_print ran without errors. Automatically failing due to first_time.\n') self.assertTrue(False) else: # Step 3.b: Call calc_error to calculate the error between the trusted values and the calculated values logging.info(' Calling calc_error...') values_identical = calc_error(self) # If there is an error large enough, fail if not values_identical: self.assertTrue(values_identical, 'Variable(s) above have different calculated and trusted values. Follow ' 'instructions above.') # Otherwise, pass else: logging.info(' ...Success: calc_error ran without errors.\n') # Something failed except AssertionError: pass # Nothing failed else: import os file = open(os.path.join(self.path, 'success.txt'), 'w') file.close()
def run_test(self, mod_dict, trusted_values_dict, path, locs): # Can't use empty dictionaries assert mod_dict != dict() mp.dps = precision # Determining if this is the first time the code is run based of the existence of trusted values first_times = is_first_time(mod_dict, trusted_values_dict) # Creating trusted dictionary based off names of modules in ModDict trusted_dict = create_trusted_globals_dict(mod_dict, trusted_values_dict, first_times) # Timing how long evaluate_globals takes t = time() # Creating dictionary of expressions for all modules in ModDict result_dict = evaluate_globals(mod_dict, locs) # Printing the time it took to run evaluate_globals logging.debug(str(time() - t) + ' seconds to run evaluate_globals') del mod_dict # # If it is the first time for at least one module, sort the module dictionary based on first_times. # # This makes it so the new modules are done last. This makes it easy to copy the necessary modules' code. # if True in first_times: # # https://stackoverflow.com/questions/13668393/python-sorting-two-lists # first_times, result_mods = (list(x) for x in zip(*sorted(zip(first_times, result_dict)))) # # temp_dict = dict() # # # Creates dictionary based on order of first_times # for mod in result_mods: # temp_dict[mod] = result_dict[mod] # # # Updates resultDict to be in this new order # result_dict = temp_dict # del temp_dict, result_mods # Looping through each module in resultDict for mod in result_dict: var_dict = result_dict[mod] first_time = first_times[mod] if not first_time: logging.info('Currently working on module ' + mod + '...') # Generating variable list and name list for module new_dict = expand_variable_dict(var_dict) # Timing how long list_to_value_list takes t = time() # Calculating numerical list for module value_dict = simplify_and_evaluate_sympy_expressions( new_dict, first_time) # Printing the time it took to run list_to_value_list logging.debug(str(time() - t) + ' seconds to run list_to_value_list') # If being run for the first time, print the code that must be copied into trusted_values_dict if first_time: first_time_print(mod, value_dict, path) # Otherwise, compare calculated values to trusted values else: # Calculates the error between mod_dict and trusted_dict[mod] for the current module values_identical = calc_error(mod, value_dict, trusted_dict[mod]) # If at least one value differs, print exit message and fail the unittest if not values_identical: self.assertTrue( values_identical, 'Variable above has different calculated and trusted values. Follow ' 'above instructions.') # If every value is the same, completed module. else: logging.info('Completed module ' + mod + ' with no errors.\n') self.assertTrue(values_identical) # If it's the first time for at least one module if True in first_times.values(): self.assertTrue( False, 'Automatically failing due to first time for at least one module. Please see above ' 'for the code to copy into your trusted_values_dict.')