Ejemplo n.º 1
0
def calc_error_helper(self, message, expected_result):
    from UnitTesting.calc_error import calc_error

    if version_info[0] == 2 or version_info[1] < 4:

        from testfixtures import LogCapture

        tuple_list = []

        for string in message:
            string_tuple = string.split(':', 2)
            string_tuple[0], string_tuple[1] = string_tuple[1], string_tuple[0]
            tuple_list.append(tuple(string_tuple))

        tuple_tuple = tuple(tuple_list)

        with LogCapture() as logger:
            self.assertTrue(expected_result == calc_error(self))
        logger.check(*tuple_tuple)

    else:

        with self.assertLogs(level='DEBUG') as logger:
            self.assertEqual(expected_result, calc_error(self))
        self.assertEqual(logger.output, message)
Ejemplo n.º 2
0
    def test_calc_error(self):

        from UnitTesting.calc_error import calc_error
        from datetime import date

        mp.dps = precision

        self.module_name = 'TestModule'
        self.trusted_values_dict_name = 'TestModule__globals'

        self.calculated_dict = {}
        self.trusted_values_dict_entry = {}
        self.assertEqual(True, calc_error(self))

        self.calculated_dict = {'a': mpf('1.0')}
        self.trusted_values_dict_entry = {}
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'ERROR:root: TestModule: Calculated dictionary and trusted dictionary have different variables.',
            'ERROR:root: Calculated Dictionary variables not in Trusted Dictionary: '
            + "['a']"
        ]
        calc_error_helper(self, message, False)

        self.calculated_dict = {'a': mpf('1.0'), 'b': mpf('2.0')}
        self.trusted_values_dict_entry = {}
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'ERROR:root: TestModule: Calculated dictionary and trusted dictionary have different variables.',
            'ERROR:root: Calculated Dictionary variables not in Trusted Dictionary: '
            + "['a', 'b']"
        ]
        calc_error_helper(self, message, False)

        self.calculated_dict = {'a': mpf('1.0')}
        self.trusted_values_dict_entry = {'b': mpf('2.0')}
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'ERROR:root: TestModule: Calculated dictionary and trusted dictionary have different variables.',
            'ERROR:root: Calculated Dictionary variables not in Trusted Dictionary: '
            + "['a']",
            'ERROR:root: Trusted Dictionary variables not in Calculated Dictionary: '
            + "['b']"
        ]
        calc_error_helper(self, message, False)

        self.calculated_dict = {'a': mpf('2.0'), 'b': mpf('3.0')}
        self.trusted_values_dict_entry = {'c': mpf('1.0')}
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'ERROR:root: TestModule: Calculated dictionary and trusted dictionary have different variables.',
            'ERROR:root: Calculated Dictionary variables not in Trusted Dictionary: '
            + "['a', 'b']",
            'ERROR:root: Trusted Dictionary variables not in Calculated Dictionary: '
            + "['c']"
        ]
        calc_error_helper(self, message, False)

        self.calculated_dict = {'a': mpf('2.0'), 'b': mpf('3.0')}
        self.trusted_values_dict_entry = {'a': mpf('1.0'), 'c': mpf('4.0')}
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'ERROR:root: TestModule: Calculated dictionary and trusted dictionary have different variables.',
            'ERROR:root: Calculated Dictionary variables not in Trusted Dictionary: '
            + "['b']",
            'ERROR:root: Trusted Dictionary variables not in Calculated Dictionary: '
            + "['c']"
        ]
        calc_error_helper(self, message, False)

        self.calculated_dict = {'a': mpf('1.0'), 'b': mpf('2.0')}
        self.trusted_values_dict_entry = {'c': mpf('3.0'), 'd': mpf('4.0')}
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'ERROR:root: TestModule: Calculated dictionary and trusted dictionary have different variables.',
            'ERROR:root: Calculated Dictionary variables not in Trusted Dictionary: '
            + "['a', 'b']",
            'ERROR:root: Trusted Dictionary variables not in Calculated Dictionary: '
            + "['c', 'd']"
        ]
        calc_error_helper(self, message, False)

        self.calculated_dict = {'b': mpf('1.0'), 'a': mpf('2.0')}
        self.trusted_values_dict_entry = {'c': mpf('3.0')}
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'ERROR:root: TestModule: Calculated dictionary and trusted dictionary have different variables.',
            'ERROR:root: Calculated Dictionary variables not in Trusted Dictionary: '
            + "['a', 'b']",
            'ERROR:root: Trusted Dictionary variables not in Calculated Dictionary: '
            + "['c']"
        ]
        calc_error_helper(self, message, False)

        self.calculated_dict = {'a': mpf('1.0')}
        self.trusted_values_dict_entry = {'a': mpf('1.0')}
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'DEBUG:root: ...Success: same variables in both dicts.\n',
            'DEBUG:root: Comparing all calculated and trusted values...',
            'DEBUG:root:' + '\nTestModule: ' + 'a' + ': Calculated: ' + '1.0' +
            '\nTestModule: ' + 'a' + ': Trusted:    ' + '1.0' + '\n',
            'DEBUG:root: ...Success: all variables identical.\n'
        ]
        calc_error_helper(self, message, True)

        self.calculated_dict = {'a': mpf('1.0'), 'b': mpf('2.0')}
        self.trusted_values_dict_entry = {'a': mpf('1.0'), 'b': mpf('2.0')}
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'DEBUG:root: ...Success: same variables in both dicts.\n',
            'DEBUG:root: Comparing all calculated and trusted values...',
            'DEBUG:root:' + '\nTestModule: ' + 'a' + ': Calculated: ' + '1.0' +
            '\nTestModule: ' + 'a' + ': Trusted:    ' + '1.0' + '\n',
            'DEBUG:root:' + '\nTestModule: ' + 'b' + ': Calculated: ' + '2.0' +
            '\nTestModule: ' + 'b' + ': Trusted:    ' + '2.0' + '\n',
            'DEBUG:root: ...Success: all variables identical.\n'
        ]
        calc_error_helper(self, message, True)

        self.calculated_dict = {'a': mpf('1.0'), 'b': mpf('2.0')}
        self.trusted_values_dict_entry = {'b': mpf('2.0'), 'a': mpf('1.0')}
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'DEBUG:root: ...Success: same variables in both dicts.\n',
            'DEBUG:root: Comparing all calculated and trusted values...',
            'DEBUG:root:' + '\nTestModule: ' + 'a' + ': Calculated: ' + '1.0' +
            '\nTestModule: ' + 'a' + ': Trusted:    ' + '1.0' + '\n',
            'DEBUG:root:' + '\nTestModule: ' + 'b' + ': Calculated: ' + '2.0' +
            '\nTestModule: ' + 'b' + ': Trusted:    ' + '2.0' + '\n',
            'DEBUG:root: ...Success: all variables identical.\n'
        ]
        calc_error_helper(self, message, True)

        self.calculated_dict = {'b': mpf('2.0'), 'a': mpf('1.0')}
        self.trusted_values_dict_entry = {'b': mpf('2.0'), 'a': mpf('1.0')}
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'DEBUG:root: ...Success: same variables in both dicts.\n',
            'DEBUG:root: Comparing all calculated and trusted values...',
            'DEBUG:root:' + '\nTestModule: ' + 'a' + ': Calculated: ' + '1.0' +
            '\nTestModule: ' + 'a' + ': Trusted:    ' + '1.0' + '\n',
            'DEBUG:root:' + '\nTestModule: ' + 'b' + ': Calculated: ' + '2.0' +
            '\nTestModule: ' + 'b' + ': Trusted:    ' + '2.0' + '\n',
            'DEBUG:root: ...Success: all variables identical.\n'
        ]
        calc_error_helper(self, message, True)

        self.calculated_dict = {'a': mpf('1.0')}
        self.trusted_values_dict_entry = {'a': mpf('2.0')}
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'DEBUG:root: ...Success: same variables in both dicts.\n',
            'DEBUG:root: Comparing all calculated and trusted values...',
            'DEBUG:root:' + '\nTestModule: ' + 'a' + ': Calculated: ' + '1.0' +
            '\nTestModule: ' + 'a' + ': Trusted:    ' + '2.0' + '\n',
            'ERROR:root:' + '''
\nVariable(s) {} in module TestModule failed. Please check values.
If you are confident that the newly calculated values are correct, comment out the old trusted values for
TestModule__globals in your trusted_values_dict and copy the following code between the ##### into your trusted_values_dict.
Make sure to fill out the TODO comment describing why the values had to be changed. Then re-run test script.

#####

# Generated on: {}
# Reason for changing values: TODO
trusted_values_dict['TestModule__globals'] = {}

#####
'''.format("['a']", str(date.today()), "{'a': mpf('1.0')}")
        ]
        calc_error_helper(self, message, False)

        ## Broken test: FAILS WITH precision > 30
        # self.calculated_dict = {'a': mpf('1.0')}
        # self.trusted_values_dict_entry = {'a': mpf('1.00000000000000010000000000000')}
        # message = ['DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
        #            'DEBUG:root: ...Success: same variables in both dicts.\n',
        #            'DEBUG:root: Comparing all calculated and trusted values...',
        #            'DEBUG:root:' + '\nTestModule: ' + 'a' + ': Calculated: ' + '1.0' + '\nTestModule: ' + 'a' + ': Trusted:    ' + '1.0000000000000001' + '\n',
        #            'DEBUG:root: ...Success: all variables identical.\n'
        #            ]
        # calc_error_helper(self, message, True)

        ## Broken test: FAILS WITH precision > 30
        # self.calculated_dict = {'a': mpf('0.0')}
        # self.trusted_values_dict_entry = {'a': mpf('0.0000000000000001')}
        # message = ['DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
        #            'DEBUG:root: ...Success: same variables in both dicts.\n',
        #            'DEBUG:root: Comparing all calculated and trusted values...',
        #            'DEBUG:root:' + '\nTestModule: ' + 'a' + ': Calculated: ' + '0.0' + '\nTestModule: ' + 'a' + ': Trusted:    ' + '1.0e-16' + '\n',
        #            'DEBUG:root: ...Success: all variables identical.\n'
        #            ]
        # calc_error_helper(self, message, True)

        self.calculated_dict = {'b': mpf('0.0')}
        self.trusted_values_dict_entry = {'b': mpf('0.000000000000001')}
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'DEBUG:root: ...Success: same variables in both dicts.\n',
            'DEBUG:root: Comparing all calculated and trusted values...',
            'DEBUG:root:' + '\nTestModule: ' + 'b' + ': Calculated: ' + '0.0' +
            '\nTestModule: ' + 'b' + ': Trusted:    ' + '1.0e-15' + '\n',
            'ERROR:root:' + '''
\nVariable(s) {} in module TestModule failed. Please check values.
If you are confident that the newly calculated values are correct, comment out the old trusted values for
TestModule__globals in your trusted_values_dict and copy the following code between the ##### into your trusted_values_dict.
Make sure to fill out the TODO comment describing why the values had to be changed. Then re-run test script.

#####

# Generated on: {}
# Reason for changing values: TODO
trusted_values_dict['TestModule__globals'] = {}

#####
'''.format("['b']", str(date.today()), "{'b': mpf('0.0')}")
        ]
        calc_error_helper(self, message, False)

        ## Broken test: FAILS WITH precision > 30
        # self.calculated_dict = {'a': mpf('0.0000000000000001')}
        # self.trusted_values_dict_entry = {'a': mpf('0.0')}
        # message = ['DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
        #            'DEBUG:root: ...Success: same variables in both dicts.\n',
        #            'DEBUG:root: Comparing all calculated and trusted values...',
        #            'DEBUG:root:' + '\nTestModule: ' + 'a' + ': Calculated: ' + '1.0e-16' + '\nTestModule: ' + 'a' + ': Trusted:    ' + '0.0' + '\n',
        #            'DEBUG:root: ...Success: all variables identical.\n'
        #            ]
        # calc_error_helper(self, message, True)

        self.calculated_dict = {'alpha': mpf('0.000000000000001')}
        self.trusted_values_dict_entry = {'alpha': mpf('0.0')}
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'DEBUG:root: ...Success: same variables in both dicts.\n',
            'DEBUG:root: Comparing all calculated and trusted values...',
            'DEBUG:root:' + '\nTestModule: ' + 'alpha' + ': Calculated: ' +
            '1.0e-15' + '\nTestModule: ' + 'alpha' + ': Trusted:    ' + '0.0' +
            '\n', 'ERROR:root:' + '''
\nVariable(s) {} in module TestModule failed. Please check values.
If you are confident that the newly calculated values are correct, comment out the old trusted values for
TestModule__globals in your trusted_values_dict and copy the following code between the ##### into your trusted_values_dict.
Make sure to fill out the TODO comment describing why the values had to be changed. Then re-run test script.

#####

# Generated on: {}
# Reason for changing values: TODO
trusted_values_dict['TestModule__globals'] = {}

#####
'''.format("['alpha']", str(date.today()), "{'alpha': mpf('1.0e-15')}")
        ]
        calc_error_helper(self, message, False)

        self.calculated_dict = {'f': mpf('123.012345678901234567890123456')}
        self.trusted_values_dict_entry = {
            'f': mpf('123.012345678901234567890123456')
        }
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'DEBUG:root: ...Success: same variables in both dicts.\n',
            'DEBUG:root: Comparing all calculated and trusted values...',
            'DEBUG:root:' + '\nTestModule: ' + 'f' + ': Calculated: ' +
            '123.012345678901234567890123456' + '\nTestModule: ' + 'f' +
            ': Trusted:    ' + '123.012345678901234567890123456' + '\n',
            'DEBUG:root: ...Success: all variables identical.\n'
        ]
        calc_error_helper(self, message, True)

        self.calculated_dict = {'f': mpf('123.0123456781012345678901')}
        self.trusted_values_dict_entry = {
            'f': mpf('123.0123456789012345678901')
        }
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'DEBUG:root: ...Success: same variables in both dicts.\n',
            'DEBUG:root: Comparing all calculated and trusted values...',
            'DEBUG:root:' + '\nTestModule: ' + 'f' + ': Calculated: ' +
            '123.0123456781012345678901' + '\nTestModule: ' + 'f' +
            ': Trusted:    ' + '123.0123456789012345678901' + '\n',
            'ERROR:root:' + '''
\nVariable(s) {} in module TestModule failed. Please check values.
If you are confident that the newly calculated values are correct, comment out the old trusted values for
TestModule__globals in your trusted_values_dict and copy the following code between the ##### into your trusted_values_dict.
Make sure to fill out the TODO comment describing why the values had to be changed. Then re-run test script.

#####

# Generated on: {}
# Reason for changing values: TODO
trusted_values_dict['TestModule__globals'] = {}

#####
'''.format("['f']", str(date.today()),
           "{'f': mpf('123.0123456781012345678901')}")
        ]
        calc_error_helper(self, message, False)

        self.calculated_dict = {'a': mpf('0.0'), 'b': mpf('1.0')}
        self.trusted_values_dict_entry = {'a': mpf('0.1'), 'b': mpf('1.0')}
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'DEBUG:root: ...Success: same variables in both dicts.\n',
            'DEBUG:root: Comparing all calculated and trusted values...',
            'DEBUG:root:' + '\nTestModule: ' + 'a' + ': Calculated: ' + '0.0' +
            '\nTestModule: ' + 'a' + ': Trusted:    ' + '0.1' + '\n',
            'DEBUG:root:' + '\nTestModule: ' + 'b' + ': Calculated: ' + '1.0' +
            '\nTestModule: ' + 'b' + ': Trusted:    ' + '1.0' + '\n',
            'ERROR:root:' + '''
\nVariable(s) {} in module TestModule failed. Please check values.
If you are confident that the newly calculated values are correct, comment out the old trusted values for
TestModule__globals in your trusted_values_dict and copy the following code between the ##### into your trusted_values_dict.
Make sure to fill out the TODO comment describing why the values had to be changed. Then re-run test script.

#####

# Generated on: {}
# Reason for changing values: TODO
trusted_values_dict['TestModule__globals'] = {}

#####
'''.format("['a']", str(date.today()), "{'a': mpf('0.0'), 'b': mpf('1.0')}")
        ]
        calc_error_helper(self, message, False)

        self.calculated_dict = {'a': mpf('0.0'), 'b': mpf('1.1')}
        self.trusted_values_dict_entry = {'a': mpf('0.1'), 'b': mpf('1.0')}
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'DEBUG:root: ...Success: same variables in both dicts.\n',
            'DEBUG:root: Comparing all calculated and trusted values...',
            'DEBUG:root:' + '\nTestModule: ' + 'a' + ': Calculated: ' + '0.0' +
            '\nTestModule: ' + 'a' + ': Trusted:    ' + '0.1' + '\n',
            'DEBUG:root:' + '\nTestModule: ' + 'b' + ': Calculated: ' + '1.1' +
            '\nTestModule: ' + 'b' + ': Trusted:    ' + '1.0' + '\n',
            'ERROR:root:' + '''
\nVariable(s) {} in module TestModule failed. Please check values.
If you are confident that the newly calculated values are correct, comment out the old trusted values for
TestModule__globals in your trusted_values_dict and copy the following code between the ##### into your trusted_values_dict.
Make sure to fill out the TODO comment describing why the values had to be changed. Then re-run test script.

#####

# Generated on: {}
# Reason for changing values: TODO
trusted_values_dict['TestModule__globals'] = {}

#####
'''.format("['a', 'b']", str(date.today()),
           "{'a': mpf('0.0'), 'b': mpf('1.1')}")
        ]
        calc_error_helper(self, message, False)

        self.calculated_dict = {'a': mpf('0.0'), 'b': mpf('1.1')}
        self.trusted_values_dict_entry = {'a': mpf('0.0'), 'b': mpf('1.0')}
        message = [
            'DEBUG:root: Checking that calculated and trusted dicts contain the same variables...',
            'DEBUG:root: ...Success: same variables in both dicts.\n',
            'DEBUG:root: Comparing all calculated and trusted values...',
            'DEBUG:root:' + '\nTestModule: ' + 'a' + ': Calculated: ' + '0.0' +
            '\nTestModule: ' + 'a' + ': Trusted:    ' + '0.0' + '\n',
            'DEBUG:root:' + '\nTestModule: ' + 'b' + ': Calculated: ' + '1.1' +
            '\nTestModule: ' + 'b' + ': Trusted:    ' + '1.0' + '\n',
            'ERROR:root:' + '''
\nVariable(s) {} in module TestModule failed. Please check values.
If you are confident that the newly calculated values are correct, comment out the old trusted values for
TestModule__globals in your trusted_values_dict and copy the following code between the ##### into your trusted_values_dict.
Make sure to fill out the TODO comment describing why the values had to be changed. Then re-run test script.

#####

# Generated on: {}
# Reason for changing values: TODO
trusted_values_dict['TestModule__globals'] = {}

#####
'''.format("['b']", str(date.today()), "{'a': mpf('0.0'), 'b': mpf('1.1')}")
        ]
        calc_error_helper(self, message, False)

        logging.info(' All calc_error tests passed.')
Ejemplo n.º 3
0
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()
Ejemplo n.º 5
0
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.')