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 testBuy(self): alex = Account(username="******", password_hash="password", balance=20000000, first_name="Alex", last_name="C", email="*****@*****.**") alex.save() # Tests buy for an already existing position stok_position = Position(ticker="STOK", shares=100, account_id=alex.id) stok_position.save() alex.buy(ticker="STOK", volume=100) check_stok = Position.from_account_id_and_ticker(account_id=alex.id, ticker="STOK") self.assertEqual(check_stok.shares, 200, msg="buy() should increase position shares") # Tests buy trade for a position that doesn't exist alex.buy(ticker="P2P", volume=101) check_p2p = Position.from_account_id_and_ticker(account_id=alex.id, ticker="P2P") self.assertEqual(check_p2p.shares, 101, "buy() should increase position shares") # Test bad ticker symbol with self.assertRaises(NoSuchTickerError, msg="buy() should raise NoSUchTickerError"): alex.buy(ticker="xyz1234", volume=100)
def testView_positions(self): print("""\n========================= UNIT TEST testView_positions()\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) positions = account_data.get_positions() view.positions_info(positions)
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)