def test_valid_dict(self): recommendation_set_dict = { "set_id": "1430b59a-5b79-11ea-8e96-acbc329ef75f", "creation_date": "2020-09-01T04:56:57.612693+00:00", "valid_from": "2019-08-01", "valid_to": "2019-08-31", "price_date": "2019-09-01", "strategy_name": "PRICE_DISPERSION", "security_type": "US Equities", "securities_set": [{ "ticker_symbol": "GE", "price": 123.45 }, { "ticker_symbol": "INTC", "price": 123.45 }, { "ticker_symbol": "AAPL", "price": 123.45 }] } SecurityRecommendationSet.from_dict(recommendation_set_dict)
def test_invalid_dict_2(self): recommendation_set_dict = { "set_id": "1430b59a-5b79-11ea-8e96-acbc329ef75f", "creation_date": "2020-09-01T04:56:57.612693+00:00", "valid_from": "2019-08-01", "price_date": "2019-09-01", "strategy_name": "PRICE_DISPERSION", "security_type": "US Equities" } with self.assertRaises(ValidationError): SecurityRecommendationSet.from_dict(recommendation_set_dict)
def test_update_portfolio_too_small(self): security_recommendation = SecurityRecommendationSet.from_dict( self.sr_dict) portfolio = Portfolio() portfolio.create_empty_portfolio(security_recommendation) with self.assertRaises(ValidationError): portfolio_mgr_svc.update_portfolio( portfolio, security_recommendation, 0)
def test_create_empty_portfolio_valid(self): with patch.object(intrinio_data, 'get_latest_close_price', return_value=('2019-08-31', 123.45)): recommendation_set = SecurityRecommendationSet.from_dict( self.sr_dict) portfolio = Portfolio() portfolio.create_empty_portfolio(recommendation_set)
def test_create_empty_portfolio_invalid_intrinio_response(self): with patch.object(intrinio_data, 'get_latest_close_price', return_value=('aaaa', 123.45)): recommendation_set = SecurityRecommendationSet.from_dict( self.sr_dict) with self.assertRaises(ValidationError): portfolio = Portfolio() portfolio.create_empty_portfolio(recommendation_set)
def test_create_empty_portfolio_no_prices(self): with patch.object(intrinio_data, 'get_latest_close_price', side_effect=DataError("test exception", None)): recommendation_set = SecurityRecommendationSet.from_dict( self.sr_dict) with self.assertRaises(DataError): portfolio = Portfolio() portfolio.create_empty_portfolio(recommendation_set)
def test_publish_current_returns(self): security_recommendation = SecurityRecommendationSet.from_dict( self.sr_dict) portfolio = Portfolio() portfolio.create_empty_portfolio(security_recommendation) (new_p, updated) = portfolio_mgr_svc.update_portfolio( portfolio, security_recommendation, 1) with patch.object(aws_service_wrapper, 'sns_publish_notification', side_effect=AWSError("", None)), \ patch.object(aws_service_wrapper, 'cf_read_export_value', return_value="some_value"): with self.assertRaises(AWSError): portfolio_mgr_svc.publish_current_returns(new_p, updated, 'sa')
def test_update_portfolio_too_big(self): security_recommendation = SecurityRecommendationSet.from_dict( self.sr_dict) portfolio = Portfolio() portfolio.create_empty_portfolio(security_recommendation) (new_p, updated) = portfolio_mgr_svc.update_portfolio( portfolio, security_recommendation, 100) ''' ensure that portfolio contains all securitie from the recommendation set ''' self.assertTrue(updated) self.assertEqual(len(new_p.model['current_portfolio'][ 'securities']), len(security_recommendation.model['securities_set'])) self.assertEqual(len(new_p.model['securities_set']), 0)
def test_update_portfolio_new_recommendation(self): sr_mod = deepcopy(self.sr_dict) sr_mod['set_id'] = 'different_set' security_recommendation = SecurityRecommendationSet.from_dict(sr_mod) portfolio = Portfolio.from_dict(self.portfolio_dict) (new_p, updated) = portfolio_mgr_svc.update_portfolio( portfolio, security_recommendation, 1) ''' ensure that 1) portfolio is updated 2) set id are being set properly ''' self.assertTrue(updated) self.assertEqual(new_p.model['set_id'], security_recommendation.model['set_id']) self.assertNotEqual(new_p.model['set_id'], self.sr_dict['set_id'])
def test_update_portfolio_empty_portfolio(self): security_recommendation = SecurityRecommendationSet.from_dict( self.sr_dict) portfolio = Portfolio() portfolio.create_empty_portfolio(security_recommendation) (new_p, updated) = portfolio_mgr_svc.update_portfolio( portfolio, security_recommendation, 1) ''' ensure that 1) portfolio is updated 2) portfolio contains 1 security 3) portfolio contains nothing in the security set ''' self.assertTrue(updated) self.assertEqual( len(new_p.model['current_portfolio']['securities']), 1) self.assertEqual(len(new_p.model['securities_set']), len( security_recommendation.model['securities_set']) - 1)
def test_portfolio_not_empty(self): with patch.object(intrinio_data, 'get_latest_close_price', return_value=('2019-08-31', 123.45)): recommendation_set = SecurityRecommendationSet.from_dict( self.sr_dict) portfolio = Portfolio() portfolio.create_empty_portfolio(recommendation_set) portfolio.model['current_portfolio'] = { 'securities': [{ "ticker_symbol": "ABC", "purchase_date": "2019-09-01T02:34:12.876012+00:00", "purchase_price": 100, "current_price": 200, "trade_state": "FILLED", "order_id": None }] } self.assertFalse(portfolio.is_empty())
def test_invalid_dict_1(self): with self.assertRaises(ValidationError): SecurityRecommendationSet.from_dict({'x': 'y'})