def test_get_all_activity(self): # First add some transactions, balances and eventlogs, and make sure the ActivityLogs are set Fixture1.settings_event1() Fixture1.transaction_event1() Fixture1.populate_balance1() # 2 Activity lines ActivityLogEvent.get(Event.APPROVE_SELECTED_SETTINGS) ActivityLogEvent.get(Event.GOAL_BALANCE_CALCULATED) ActivityLogEvent.get(Event.GOAL_DEPOSIT_EXECUTED) url = '/api/v1/clients/{}/activity'.format( Fixture1.personal_account1().primary_owner.id) self.client.force_authenticate( user=Fixture1.personal_account1().primary_owner.user) response = self.client.get(url) self.assertEqual(len(response.data), 4) self.assertEqual( response.data[3], { 'goal': 1, 'account': 1, 'time': 946684800, 'type': ActivityLogEvent.get( Event.APPROVE_SELECTED_SETTINGS).activity_log.id }) # Setting change self.assertEqual( response.data[2], { 'balance': 0.0, 'time': 978220800, 'type': ActivityLogEvent.get( Event.GOAL_BALANCE_CALCULATED).activity_log.id }) # Balance self.assertEqual( response.data[1], { 'balance': 3000.0, 'time': 978307200, 'type': ActivityLogEvent.get( Event.GOAL_BALANCE_CALCULATED).activity_log.id }) # Balance self.assertEqual( response.data[0], { 'data': [3000.0], 'goal': 1, 'account': 1, 'time': 978307200, 'type': ActivityLogEvent.get( Event.GOAL_DEPOSIT_EXECUTED).activity_log.id }) # Deposit
def test_get_no_activity(self): url = '/api/v1/clients/{}/activity'.format( Fixture1.personal_account1().primary_owner.id) self.client.force_authenticate( user=Fixture1.personal_account1().primary_owner.user) response = self.client.get(url) self.assertEqual(response.data, [])
def test_account_must_be_confirmed(self): account = Fixture1.personal_account1() account.confirmed = False account.save() self.assertFalse(account.confirmed) with self.assertRaises(ValidationError) as e: goal = Goal.objects.create(account=Fixture1.personal_account1(), name='goal1', type=Fixture1.goal_type1(), portfolio_set=Fixture1.portfolioset1(), selected_settings=Fixture1.settings1())
def test_change_account_cash(self): goal1 = Fixture1.goal1() account = Fixture1.personal_account1() account.all_goals.return_value = [goal1] #no difference account.cash_balance = 0 ib_account = account.ib_account ib_account_cash[ib_account.ib_account] = 0 difference = reconcile_cash_client_account(account) self.assertNotAlmostEqual(0, difference) self.assertEqual(ib_account.ib_account, IB_ACC_1) #deposit - transferred to account.cash_balance ib_account_cash[ib_account.ib_account] = 1100 reconcile_cash_client_account(account) self.assertNotAlmostEqual(0, account.cash_balance) #withdrawal - from account.cash_balance ib_account_cash[ib_account.ib_account] = 900 reconcile_cash_client_account(account) self.assertAlmostEqual(900, account.cash_balance) #exception - sum of goal cash balances > ib_account_cash goal1.cash_balance = 1000 goal1.save() account.cash_balance = 100 ib_account_cash[ib_account.ib_account] = 900 with self.assertRaises(Exception) as cm: reconcile_cash_client_account(account) self.assertTrue(ib_account.ib_account in cm.exception.args[0])
def test_update_account(self): url = '/api/v1/accounts/' + str(Fixture1.personal_account1().id) test_name = 'Holy Pingalicious Test Account' self.assertNotEqual(Fixture1.personal_account1().account_name, test_name) data = { 'account_name': test_name, } old_count = ClientAccount.objects.count() self.client.force_authenticate(user=Fixture1.client1_user()) response = self.client.put(url, data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(ClientAccount.objects.count(), old_count) # No extra account created self.assertTrue('id' in response.data) # Correct response serializer used self.assertEqual(response.data['account_name'], test_name) # New value returned self.assertEqual(Fixture1.personal_account1().account_name, test_name) # Value in db actually changed
def test_update_account(self): account = ClientAccountFactory.create() url = '/api/v1/accounts/' + str(account.id) test_name = 'Holy Pingalicious Test Account' account_number = '1234512345' self.assertNotEqual(account.account_name, test_name) sa1 = SecurityAnswerFactory.create(user=account.primary_owner.user, question='question one') sa2 = SecurityAnswerFactory.create(user=account.primary_owner.user, question='question two') data = { 'account_name': test_name, } self.client.force_authenticate(user=account.primary_owner.user) response = self.client.put(url, data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) data = { 'account_name': test_name, 'question_one': sa1.pk, 'answer_one': 'test', 'question_two': sa2.pk, 'answer_two': 'test', 'account_number': account_number, } old_count = ClientAccount.objects.count() response = self.client.put(url, data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(ClientAccount.objects.count(), old_count) # No extra account created self.assertTrue('id' in response.data) # Correct response serializer used self.assertEqual(response.data['account_name'], test_name) # New value returned self.assertEqual(response.data['account_number'], account.account_number ) # account number can't be modified on update. self.assertEqual(Fixture1.personal_account1().account_name, test_name) # Value in db actually changed
def setUp(self): self.bonds_type = InvestmentType.Standard.BONDS.get() self.stocks_type = InvestmentType.Standard.STOCKS.get() self.con = Mock(BaseBroker) self.con.connect.return_value = True #self.con.requesting_market_depth.return_value = False self.market_data = dict() self.market_data['GOOG'] = MarketDepth() single_level = SingleLevelMarketDepth() single_level.bid = 1 single_level.ask = 2 single_level.bid_volume = 50 single_level.ask_volume = 100 self.market_data['GOOG'].add_level(0, single_level) Fixture1.personal_account1() Fixture1.personal_account2() request1 = MarketOrderRequest.objects.create( state=MarketOrderRequest.State.APPROVED.value, account=Fixture1.personal_account1()) request2 = MarketOrderRequest.objects.create( state=MarketOrderRequest.State.APPROVED.value, account=Fixture1.personal_account2()) Fixture1.ib_account1() Fixture1.ib_account2() params = { 'reason': ExecutionRequest.Reason.DRIFT.value, 'goal': Fixture1.goal1(), 'asset': Fixture1.fund3(), 'volume': 5, 'order': request1 } ExecutionRequest.objects.get_or_create(id=1, defaults=params) params = { 'reason': ExecutionRequest.Reason.DRIFT.value, 'goal': Fixture1.goal1(), 'asset': Fixture1.fund4(), 'volume': 5, 'order': request1 } ExecutionRequest.objects.get_or_create(id=2, defaults=params) #Client2 params = { 'reason': ExecutionRequest.Reason.DRIFT.value, 'goal': Fixture1.goal2(), 'asset': Fixture1.fund3(), 'volume': 10, 'order': request2 } ExecutionRequest.objects.get_or_create(id=3, defaults=params) params = { 'reason': ExecutionRequest.Reason.DRIFT.value, 'goal': Fixture1.goal2(), 'asset': Fixture1.fund4(), 'volume': 10, 'order': request2 } ExecutionRequest.objects.get_or_create(id=4, defaults=params)
def test_account_must_be_confirmed(self): account = Fixture1.personal_account1()
def test_performance_history(self): goal = GoalFactory.create() prices = ( (Fixture1.fund1(), '20160101', 10), (Fixture1.fund1(), '20160102', 10.5), (Fixture1.fund1(), '20160103', 10.6), (Fixture1.fund1(), '20160104', 10.3), (Fixture1.fund1(), '20160105', 10.1), (Fixture1.fund1(), '20160106', 9.9), (Fixture1.fund1(), '20160107', 10.5), (Fixture1.fund2(), '20160101', 50), (Fixture1.fund2(), '20160102', 51), (Fixture1.fund2(), '20160103', 53), (Fixture1.fund2(), '20160104', 53.5), (Fixture1.fund2(), '20160105', 51), (Fixture1.fund2(), '20160106', 52), (Fixture1.fund2(), '20160107', 51.5), ) Fixture1.set_prices(prices) order_details = ( (Fixture1.personal_account1(), MarketOrderRequest.State.COMPLETE), (Fixture1.personal_account1(), MarketOrderRequest.State.COMPLETE), (Fixture1.personal_account1(), MarketOrderRequest.State.COMPLETE), (Fixture1.personal_account1(), MarketOrderRequest.State.COMPLETE), (Fixture1.personal_account1(), MarketOrderRequest.State.COMPLETE), (Fixture1.personal_account1(), MarketOrderRequest.State.COMPLETE), ) orders = Fixture1.add_orders(order_details) execution_details = ( (Fixture1.fund1(), orders[0], 3, 10.51, -75, '20160102'), (Fixture1.fund1(), orders[0], 4, 10.515, -75.05, '20160102'), (Fixture1.fund1(), orders[1], -1, 10.29, 10, '20160104'), (Fixture1.fund2(), orders[2], 2, 53.49, -110, '20160104'), (Fixture1.fund2(), orders[2], 8, 53.5, -430, '20160104'), (Fixture1.fund1(), orders[3], -3, 10.05, 30, '20160105'), (Fixture1.fund2(), orders[4], -3, 50.05, 145, '20160105'), (Fixture1.fund2(), orders[4], -2, 50.04, 98, '20160105'), (Fixture1.fund2(), orders[5], -5, 52, 255, '20160106'), ) executions = Fixture1.add_executions(execution_details) execution_requests = Fixture1.add_execution_requests( goal, execution_details, executions) # We distribute the entire executions to one goal. distributions = ( (executions[0], 3, Fixture1.goal1()), (executions[1], 4, Fixture1.goal1()), (executions[2], -1, Fixture1.goal1()), (executions[3], 2, Fixture1.goal1()), (executions[4], 8, Fixture1.goal1()), (executions[5], -3, Fixture1.goal1()), (executions[6], -3, Fixture1.goal1()), (executions[7], -2, Fixture1.goal1()), (executions[8], -5, Fixture1.goal1()), ) Fixture1.add_execution_distributions(distributions, execution_requests) url = '/api/v1/goals/{}/performance-history'.format( Fixture1.goal1().id) self.client.force_authenticate(user=Fixture1.client1().user) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data[0], (16802, 0)) # 20160102 self.assertEqual(response.data[1], (16803, Decimal('0.009524'))) self.assertEqual(response.data[2], (16804, Decimal('-0.028302'))) self.assertEqual(response.data[3], (16805, Decimal('-0.043901'))) self.assertEqual(response.data[4], (16806, Decimal('-0.019802'))) self.assertEqual(response.data[5], (16807, Decimal('0.060606'))) self.assertEqual(response.data[6], (16808, 0))