# # ------------------------------------------------------------------------ import unittest import logUtils.log_utils as log_utils from config_utils_test import ConfigUtilsTest from db_utils_test import DBUtilsTest from geartype_retriever_test import GearTypeRetrieverTest from geartype_retrieved_dao_test import GearTypeRetrievedDAOTest from geartype_dao_test import GearTypeDAOTest if __name__ == '__main__': try: # Initialize both loggers so that the logging works during tests geartype_logger = log_utils.init_root_logger( logger_name="gearstack_rbt", log_path="../log/geartype_rbt.log", log_level="DEBUG") gear_logger = log_utils.init_root_logger( logger_name="gear_rbt_logger", log_path="../log/gear_rbt.log", log_level="DEBUG") # Define list of test case classes to run tests = [ ConfigUtilsTest, DBUtilsTest, GearTypeRetrieverTest, GearTypeRetrievedDAOTest, GearTypeDAOTest ] suites_list = [] # Initialize test loader
def main(argv): # Gather command line args env = None log_level = None usage = 'geartype_rbt_runner.py --env <local | dev | prod> --log_level <info | debug | warning | error>' try: opts, args = getopt.getopt(argv, "h", ["env=", "log_level="]) except getopt.GetoptError: print('An invalid argument was specified!') sys.exit(2) for opt, arg in opts: if opt == '-h': print(usage) sys.exit(0) elif opt == '--env': env = arg elif opt == '--log_level': log_level = arg # Validate command line args try: # env arg if env.lower() not in ('local', 'dev', 'prod'): print('An invalid value {0} was specified for arg [--env]'.format( env)) print(usage) sys.exit(2) # log_level arg if log_level.lower() not in ('info', 'debug', 'warning', 'error', 'critical'): print('An invalid value {0} was specified for arg [--log_level]'. format(log_level)) print(usage) sys.exit(2) except AttributeError: print('An invalid argument was specified!') print(usage) sys.exit(2) # Create logger and log options specified logger = log_utils.init_root_logger(logger_name="gearstack_rbt", log_path='../log/geartype_rbt.log', log_level=log_level) logger.info('Gearstack Gear Type Bot') logger.info('Environment: {0}'.format(env.upper())) logger.info('Log Level: {0}'.format(log_level.upper())) # Fetch configuration data for specified environment bot_configs = config_utils.fetch_bot_config(env) types = config_utils.fetch_data_config('cat_data_keys') # Instantiate retriever object type_retriever = GearTypeRetriever( amazon_config=bot_configs['amazon_config'], bot_db_configs=bot_configs['db_config']['bot_db_props'], app_db_configs=bot_configs['db_config']['app_db_props']) # Iterate through types list and persist try: for type in types: nodes_for_type = config_utils.fetch_data_config(config_name=type) type_retriever.save_geartypes(node_ids=nodes_for_type) logger.info('Finished persisting gear types!') except Exception: logger.exception('An error occurred during persistence of gear types!') finally: log_utils.close_logger(logger)
print rec_id # Fetch gear instance by id fetched_rec = self.dao.fetch_gear_by_id(rec_id) self.assertIsNotNone(fetched_rec) self.assertEquals(fetched_rec.name, 'Test Gear') # Fetch gear instance by name fetched_rec = self.dao.fetch_gear_by_name('Test Gear') self.assertIsNotNone(fetched_rec) # Delete gear instance self.dao.delete_gear(rec_id) fetched_rec = self.dao.fetch_gear_by_id(rec_id) self.assertIsNone(fetched_rec) """ Execute tests """ if __name__ == '__main__': # Initialize a root logger here so logging works during tests root_logger = log_utils.init_root_logger(logger_name="gear_rbt_logger", log_path="../log/gear_rbt.log", log_level='DEBUG') try: # Create test suite and execute suite = unittest.TestLoader().loadTestsFromTestCase(GearDAOTest) unittest.TextTestRunner(verbosity=2).run(suite) finally: if root_logger: log_utils.close_logger(root_logger)
def main(argv): # Gather command line args env = None log_level = None browse_node = None usage = "gear_rbt_runner.py --env <local|dev|prod> --log_level <info|debug|warning|error> " \ "--browse_node <browse_node_id>" try: opts, args = getopt.getopt(argv, "h", ["env=", "log_level=", "browse_node="]) except getopt.GetoptError: print('An invalid argument was specified!') sys.exit(2) for opt, arg in opts: if opt == '-h': print(usage) sys.exit(0) elif opt == '--env': env = arg elif opt == '--log_level': log_level = arg elif opt == '--browse_node': browse_node = arg # Validate command line args try: # env arg if env.lower() not in ('local', 'dev', 'prod'): print('An invalid value {0} was specified for arg [--env]'.format( env)) print(usage) sys.exit(2) # log_level arg if log_level.lower() not in ('info', 'debug', 'warning', 'error', 'critical'): print('An invalid value {0} was specified for arg [--log_level]'. format(log_level)) print(usage) sys.exit(2) # browse_node arg browse_node = int(browse_node) except (AttributeError, ValueError): print('An invalid argument was specified!') print(usage) sys.exit(2) # Create logger and log options specified logger = log_utils.init_root_logger(logger_name="gear_rbt_logger", log_path='../log/gear_rbt.log', log_level=log_level) logger.info('Gearstack Gear Bot') logger.info('Environment: {0}'.format(env.upper())) logger.info('Log Level: {0}'.format(log_level.upper())) logger.info('Amazon Browse Node: {0}'.format(browse_node)) # Fetch configuration data for specified environment bot_configs = config_utils.fetch_bot_config(env) # Instantiate gear retriever instance gear_retriever = GearRetriever( amazon_config=bot_configs['amazon_config'], app_db_config=bot_configs['db_config']['app_db_props'], exclusion_keys=config_utils.fetch_data_config('excluded_keywords')) # Loop through nodes_to_search list and execute amazon API search # and item save try: fetched_items = gear_retriever.search_items(browse_node_id=browse_node) gear_retriever.save_gear(item_list=fetched_items, browse_node=browse_node) logger.info("Finished DB population for browse node {0}!!!".format( browse_node)) except Exception: logger.exception( 'Gear Retriever bot encountered an exception during execution!') finally: log_utils.close_logger(logger)