def test_db_insert_stock_by_user_id(self): user_stock_pref_obj = UserStockPrefDAL() print( "Executing Test Case : Insert Stock Preferences By User ID : {} : {} " .format(self.user_id, self.update_key)) update_exception, update_status = user_stock_pref_obj.update_stock_preference_for_user( user_id=self.user_id, stock_key=self.update_key) self.assertEqual(update_exception, None)
def test_db_fetch_users_by_stock_key(self): user_stock_pref_obj = UserStockPrefDAL() print( "Executing Test Case : Fetch Stock Preferences By Stock Key : {}". format(self.stock_key)) select_exception, user_prefs = user_stock_pref_obj.get_users_by_stock_preference( stock_key=self.stock_key) pprint(user_prefs) self.assertEqual(select_exception, None)
def test_db_fetch_stock_pref_by_user_id(self): user_stock_pref_obj = UserStockPrefDAL() print("Executing Test Case : Fetch Stock Preferences By User ID: {}". format(self.user_id)) select_exception, stock_prefs = user_stock_pref_obj.get_stock_preferences_by_user_id( user_id=self.user_id) pprint(stock_prefs) self.assertEqual(stock_prefs['userID'], self.user_id) self.assertEqual(select_exception, None)
def test_db_get_all_stock_preferences(self): user_stock_pref_obj = UserStockPrefDAL() print("Executing Test Case : Fetch All Users Stock Keys") select_exception, all_stock_keys = user_stock_pref_obj.get_all_stock_preferences( ) pprint(all_stock_keys) self.assertEqual(select_exception, None) self.assertGreater(all_stock_keys.__len__(), 0, msg='Some records received')
def start_stock_tickers(self): # Fetch all the stocks that users have chosen. while True: user_stock_pref_dal_obj = UserStockPrefDAL() stock_exception, available_stocks = user_stock_pref_dal_obj.get_all_stock_preferences( ) for stock in available_stocks: stock_key = stock # Start FetchStock Threads stock_worker_obj = StockWorker() ft_stock_thread = threading.Thread( target=stock_worker_obj.fetch_stock_price, args=(stock_key, )) ft_stock_thread.daemon = True ft_stock_thread.start() time.sleep(60)
def update_stock_preference(self, user_name): """ Updates stock preferences for a give user --- tags: - Stocks responses: 201: description: Returns updated preferences """ user_stock_pref_dal_obj = UserStockPrefDAL() tech_stock_quote_dal_obj = TechStockQuotesDAL() request_stock_pref = request.get_json() if request_stock_pref is None: raise JSONNotFoundException stock_company_title = request_stock_pref['stock_company'] # Fetch the stock key fetch_exception, stock_key = tech_stock_quote_dal_obj.get_stock_quote_by_company_title( company_title=stock_company_title) if stock_key is None: raise ResourceNotFoundException(resource_id=stock_company_title) update_exception, update_status = user_stock_pref_dal_obj.update_stock_preference_for_user( user_id=user_name, stock_key=stock_key) if update_exception is not None: raise InternalServerError if update_status is False: raise InternalServerError fetch_exception, updated_stock_pref = user_stock_pref_dal_obj.get_stock_preferences_by_user_id( user_id=user_name) if fetch_exception is not None: raise InternalServerException(exception_message=fetch_exception) return jsonify(updated_stock_pref)
class StockPublishPayloadToDict: def __init__(self): self.user_stock_pref_dal = UserStockPrefDAL() pass def get_stock_payload_to_publish(self, stock_payload): stocks_to_publish_dict = dict() if type(stock_payload) is StockModel: stock_key = stock_payload.stock_unit # Step 1: Get the list of Users that prefer a Stock select_exception, stock_preference_users = self.user_stock_pref_dal.get_users_by_stock_preference(stock_key) if select_exception: # TO DO: Raise a custom exception here ! pass stock_temp_user_index = 0 for user in stock_preference_users: # Step 2: Iterate through the Users in the list and fetch their Publish URL stock_to_publish_dict = dict() stock_to_publish_dict['publish_queue'] = self.get_user_publish_queue(user_id=user) stock_to_publish_dict['stock_unit'] = stock_payload.stock_unit stock_to_publish_dict['stock_title'] = stock_payload.stock_title stock_to_publish_dict['stock_price'] = stock_payload.stock_price stock_to_publish_dict['stock_deviation'] = stock_payload.stock_deviation stock_to_publish_dict['stock_deviation_status'] = stock_payload.stock_deviation_status stock_to_publish_dict['stock_equity'] = stock_payload.stock_equity stock_to_publish_dict['stock_last_update_time'] = stock_payload.stock_last_update_time stock_to_publish_dict['stock_52wkrange'] = stock_payload.stock_52wkrange stock_to_publish_dict['stock_open'] = stock_payload.stock_open stock_to_publish_dict['stock_market_cap'] = stock_payload.stock_market_cap stock_to_publish_dict['stock_prev_close'] = stock_payload.stock_prev_close stock_to_publish_dict['stock_peratio_tte'] = stock_payload.stock_peratio_tte stocks_to_publish_dict[stock_temp_user_index] = stock_to_publish_dict stock_temp_user_index += 1 return stocks_to_publish_dict def get_user_publish_queue(self, user_id): # This function does not belong here, should be a part of DAL user_topic_mgr_instance = UserTopicManager() user_topic = user_topic_mgr_instance.get_user_topic_endpoint(user_id=user_id) return user_topic
def __init__(self): self.user_stock_pref_dal = UserStockPrefDAL() pass