def test_confirmed(self): """Test if the remove from watchlist dialog confirmed works. Flow, there is a stock in users watchlist, user asks to remove it but denies it afterwards.""" # Setup: check if there is stock in users watchlist with self.app.app_context(): watchlist = Watchlist.get_users_tickers(test_user_id) self.assertIn(test_add_stock, watchlist) # Step 1 ask to remove stock (stock present) res = self._remove_from_watchlist(test_add_stock) # Assert self.assertEqual(res.status_code, 200) self.assertIn(RESPONSE_intent_remove_from_watchlist_ask_confirmation, str(res.data)) # Step 2 Confirm removing the stock # Prepare request request = json.dumps(intent_remove_from_watchlist_confirm()) # Execute res = self.client().post('/api/', data=request, content_type='application/json') # Assert self.assertEqual(res.status_code, 200) self.assertIn( RESPONSE_intent_remove_from_watchlist_confirmed.format( test_add_stock), str(res.data)) # Check if stock was removed from watchlist with self.app.app_context(): watchlist = Watchlist.get_users_tickers(test_user_id) self.assertNotIn(test_add_stock, watchlist)
def _handle_dialog_add_started(request): """ Check if the provided ticker is supported or is not already in watchlist, if not, ask for confirmation. :type request AlexaRequest """ print("LOG-d: dialogState STARTED") # Check if ticker is provided try: ticker = _check_valid_ticker_provided(request) except AttributeError as e: logger.exception("No valid ticker provided") message = strings.INTENT_ADDED_TO_WATCHLIST_FAIL return ResponseBuilder.create_response(request, message=message) \ .with_reprompt(strings.INTENT_GENERAL_REPROMPT) # Ask user to confirm ticker add message = strings.INTENT_ADD_TO_WATCHLIST_ASK_CONFIRMATION.format(ticker) # Check if ticker not already in Watchlist user_id = request.get_user_id() watchlist_tickers = Watchlist.get_users_tickers(user_id) for ticker_in_watchlist in watchlist_tickers: if ticker == ticker_in_watchlist: message = strings.INTENT_ADDED_TO_WATCHLIST_EXISTS.format(ticker) return ResponseBuilder.create_response(request, message) \ .with_dialog_confirm_intent()
def test_watchlist_empty(self): """Test removing stock from watchlist when watchlist is empty.""" # Setup: check the target stock is not in users watchlist with self.app.app_context(): watchlist = Watchlist.get_users_tickers(test_user_id) self.assertNotIn(test_stock_1_ticker, watchlist) # Step 1 ask to remove stock (stock not present) res = self._remove_from_watchlist(test_stock_1_ticker) # Assert self.assertEqual(res.status_code, 200) self.assertIn(RESPONSE_intent_remove_from_watchlist_not_there, str(res.data)) pass
def add_to_watchlist_dialog_deny(self): # Setup request = json.dumps(intent_add_to_watchlist_deny()) # Execute res = self.client().post('/api/', data=request, content_type='application/json') # Assert 1st step self.assertEqual(res.status_code, 200) self.assertIn(RESPONSE_intent_add_to_watchlist_denied, str(res.data)) # check if stock not in watchlist just yet with self.app.app_context(): watchlist = Watchlist.get_users_tickers(test_user_id) for item in watchlist: self.assertNotEqual(item, test_add_stock)
def handle_report_stock_watchlist(request): """ Generate response to intent type ReportStockWatchlistIntent reporting the current portfolio performance. :type request AlexaRequest :return: JSON response including the performance report. """ user_id = request.get_user_id() # Query DB for watchlist data ticker_list = Watchlist.get_users_tickers(user_id) changes = _get_stocks_24h_change(ticker_list) if len(ticker_list) > 2: max_idx = _find_biggest_value(changes) gainer_ticker = ticker_list.pop(max_idx) gainer_value = changes.pop(max_idx) min_idx = _find_smallest_value(changes) loser_ticker = ticker_list.pop(min_idx) loser_value = changes.pop(min_idx) gainer_message = strings.INTENT_WATCHLIST_REPORT_TOP_STOCK.format( Ticker2Name.ticker_to_name(gainer_ticker), _get_movement_direction(gainer_value), abs(gainer_value)) loser_message = strings.INTENT_WATCHLIST_REPORT_WORST_STOCK.format( Ticker2Name.ticker_to_name(loser_ticker), _get_movement_direction(loser_value), abs(loser_value)) message = gainer_message + loser_message + _build_report_msg(ticker_list, changes) elif len(ticker_list) == 2 or len(ticker_list) == 1: message = _build_report_msg(ticker_list, changes) else: message = strings.INTENT_WATCHLIST_EMPTY_MSG reprompt_message = strings.INTENT_GENERAL_REPROMPT return ResponseBuilder.create_response(request, message=message) \ .with_reprompt(reprompt_message)