def testBuy_and_sell(self): print("""\n========================= UNIT TEST testBuy_and_sell() This test will ALWAYS say Insufficient Funds or Shares for Buy and Sell, respectively by design. Testing graceceful error/exception handling \n""") account_data = Account(username="******", balance=20000000, first_name="John", last_name="Smith", email_address="*****@*****.**") account_data.set_password_hash("password") account_data.save() ticker = view.ticker_info() trade = view.buy_sell_choice() if trade not in ("1", "2"): view.bad_choice_input() return elif trade == "1": volume = float(view.enter_buy_info()) with self.assertRaises(InsufficientFundsError, msg="should raise InsufficientFundsError"): view.insufficient_funds() account_data.buy(ticker, 1000000) elif trade == "2": volume = float(view.enter_sell_info()) with self.assertRaises(InsufficientSharesError, msg="should raise InsufficientSharesError"): view.insufficient_shares() account_data.sell(ticker, 1000000)
def testCreate_account_and_admin_login(self): print("""\n========================= UNIT TEST testCreate_account_and_admin_login() !!!Wait for menu to load first!!!... <<Press 8: ACCOUNT POSITIONS>> and then <<Press 9: GRANT ADMIN ACCESS to username "ms2020">> and then <<Press 7 and 3 to quit>> and proceed to next test !!!Wait for menu to load first!!!...\n""") account_data = Account(username="******", balance=20000000, first_name="John", last_name="Smith", email_address="*****@*****.**", admin=1) account_data.set_password_hash("password") account_data.save() account_data2 = Account(username="******", balance=20000000, first_name="Mary", last_name="Sue", email_address="*****@*****.**", admin=0) account_data2.set_password_hash("password") account_data2.save() account_data.buy("STOK", 100) account_data.buy("A33A", 300) account_data.sell("STOK", 50) account_data.buy("AAPL", 999) account_data2.buy("AAPL", 100) account_data2.sell("AAPL", 100) account_data2.buy("GS", 200) account_data2.sell("GS", 100) account_data2.buy("MS", 200) account_data2.sell("MS", 100) js2020 = Account.get_from_username("js2020") account_data = Account.login(js2020.username, "password") self.assertIsNotNone(account_data, msg="login() should return account data") controller.main_menu(account_data) ms2020 = Account.get_from_username("ms2020") account_data2 = Account.login(ms2020.username, "password") self.assertEqual(account_data2.admin, 1, msg="control should make this account an admin")
def testReview_trade_history(self): print("""\n========================= UNIT TEST testReview_trade_history()\n""") account_data = Account(username="******", balance=20000000, first_name="John", last_name="Smith", email_address="*****@*****.**") account_data.set_password_hash("password") account_data.save() account_data.buy("STOK", 100) account_data.buy("P2P", 200) account_data.buy("A33A", 300) account_data.sell("P2P", 100) account_data.sell("STOK", 50) trades = account_data.get_trades() view.trades_info(trades)
def testSell(self): caroline = Account(username="******", password_hash="password", balance=10000, first_name="Caroline", last_name="Grabowski", email="*****@*****.**") caroline.save() # Tests sell for a position with SUFFICIENT shares a33a_position = Position(ticker="A33A", shares=200, account_id=caroline.id) a33a_position.save() caroline.sell(ticker="A33A", volume=100) check_a33a = Position.from_account_id_and_ticker( account_id=caroline.id, ticker="A33A") self.assertEqual(check_a33a.shares, 100, "sell() should decrease position shares") # Tests sell for a position with INSUFFICIENT shares stok_position = Position(ticker="STOK", shares=100, account_id=caroline.id) stok_position.save() with self.assertRaises( InsufficientSharesError, msg="sell() should raise InsufficicentSharesError"): caroline.sell(ticker="STOK", volume=200) # Tests sell trade for a position that doesn't exist with self.assertRaises( InsufficientSharesError, msg="sell() should raise InsufficicentSharesError"): caroline.sell(ticker="P2P", volume=202) # Test bad ticker symbol with self.assertRaises(NoSuchTickerError, msg="sell() should raise NoSUchTickerError"): caroline.sell(ticker="xyz1234", volume=100)