def positions_sub_menu(pk): retrieve_bal = Account(pk=pk) views.generic_msg("Your current balance = {}".format( retrieve_bal.get_account().balance)) while True: position_choice = views.position_menu() if position_choice is None: #Bad input views.generic_msg( "Please enter a number that corresponds to a stated option") elif position_choice == 3: #Exit break elif position_choice == 1: #Retrieve and display a given position ticker = views.get_input("Please enter a Ticker Symbol") user_position = Account(pk=pk) position = user_position.get_position_for(ticker) valuation = Position() getval = valuation.current_value(ticker, position.shares) views.show_positions(position, getval) elif position_choice == 2: #Retrieve and display all positions user_positions = Account(pk=pk) positions = user_positions.get_positions() for position in positions: valuation = Position() getval = valuation.current_value(position.ticker, position.shares) views.show_positions(position, getval)
class MyTestCase(unittest.TestCase): """ Simple Test """ def setUp(self): """ Hook method for setting up the test fixture before exercising it. """ self.account_data = {"id": "1", "name": "test"} mock_data_interface = Mock() mock_data_interface.get_id.return_value = '1' mock_data_interface.get_name.return_value = 'test' mock_data_interface.get.side_effect = AccountException() self.account = Account(mock_data_interface) def test_account_returns_data_for_id(self): """ Simple Test for ID """ self.assertEqual(self.account_data["id"], self.account.get_account_id()) def test_account_returns_data_for_name(self): """ Simple Test for NAME """ self.assertEqual(self.account_data["name"], self.account.get_account_name()) def test_account_when_connect_exception_raised(self): """ Raises Exception """ self.assertEqual("Connection error occurred", self.account.get_account())
def test_account_conn_exception(self): mock_data_interface = Mock() mock_data_interface.get.side_effect = ConnectionError() # mock_data_interface.wibble_list = ['wibble', 'wobble'] account = Account(mock_data_interface) self.assertEqual("Connection error occurred. Try Again.", account.get_account(1))
def balance(api_key): if Account.api_authenticate(api_key) == None: msg = "Invalid login credentials, pls retry" else: pk = Account.api_authenticate(api_key).pk retrieve_bal = Account(pk=pk) msg = "Your current balance = {}".format(retrieve_bal.get_account().balance) return jsonify({'message':msg})
def viewapikey(name, password): if not Account.login(name, password): msg = "Invalid login credentials, pls retry" else: pk = Account.login(name, password).pk retrieve_ak = Account(pk=pk) msg = "Your API Key = {}".format(retrieve_ak.get_account().api_key) return jsonify({'message': msg})
def viewbalance(name, password): if not Account.login(name, password): msg = "Invalid login credentials, pls retry" else: pk = Account.login(name, password).pk retrieve_bal = Account(pk=pk) msg = "Your current balance = {}".format( retrieve_bal.get_account().balance) return jsonify({'message': msg})
def test_get_account_returns_data_for_id_1(self): account_data = {"id": "1", "name": "test"} # we can Mock external dependencies ssuch as services, API endpoints etc. mock_data_interface = Mock() mock_data_interface.get.return_value = account_data # use the mock as we instantiate an Account account = Account(mock_data_interface) # now make a unittetst asertion # wrong = {"id":"2", "name":"test"} # this fails the test! # self.assertDictEqual( wrong, account.get_account(1) ) self.assertDictEqual(account_data, account.get_account(1)) # this passes the test
def test_account_returns_data_for_id_1(self): account_data = dict(id=1, name="data") mock_data_interface = Mock() mock_data_interface.get.return_value = account_data account = Account(mock_data_interface) self.assertDictEqual(account_data, account.get_account(1))
def test_account_when_connect_exception_raised(self): mock_data_interface = Mock() mock_data_interface.get.side_effect = ConnectionError() account = Account(mock_data_interface) self.assertEqual("Connection error occured. Try Again.", account.get_account(1))
def test_account_returns_data_for_id_1(self): account_data = {"id": "1", "name": "test"} mock_data_interface = Mock() mock_data_interface.get.return_value = account_data account = Account(mock_data_interface) self.assertDictEqual(account_data, account.get_account(1))
def test_account_when_connect_exception_raised(self): mock_data_interface = Mock() mock_data_interface.get.side_effect = ConnectionError() account = Account(mock_data_interface) self.assertEqual("Connection error occurred. Try Again.", account.get_account(1))
def test_account_returns_data_for_id_1(self): accountData = {"id": "1", "name": "test"} mockDataIF = Mock() mockDataIF.get.return_value = accountData account = Account(mockDataIF) self.assertDictEqual(accountData, account.get_account(1))
def test_account_when_connect_exception_raised(self): mockDataIF = Mock() mockDataIF.get.return_value = "Connection error occurred. Try Again." account = Account(mockDataIF) self.assertEqual("Connection error occurred. Try Again.", account.get_account(1))