Esempio n. 1
0
def first_time_print_helper(self, message='', write=False):
    from UnitTesting.first_time_print import first_time_print

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

        from testfixtures import LogCapture
        with LogCapture() as logger:
            first_time_print(self, write)
        logger.check(('root', 'ERROR', message))

    else:

        with self.assertLogs(level='DEBUG') as logger:
            first_time_print(self, write)
        self.assertEqual(logger.output, ['ERROR:root:' + message])
Esempio n. 2
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()
Esempio n. 4
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.')