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 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)