def test_from_pk(self): result = Account.from_pk(jimid) self.assertIsInstance(result, Account, "from_pk returns an instance of an account") self.assertEqual(result.email, "*****@*****.**") self.assertIsNone(Account.from_pk(0), "from_pk returns None for bad pk")
def test_buy(self): #buy is trade withe positive quantity acct = Account.from_pk(jimid) result = acct.trade("aapl", 3, 100) #result is mv of trade result2 = acct.trade("pton", 1, 100) trade_hist = Trade.from_account_and_ticker(jimid, "aapl") trade_hist2 = Trade.from_account_and_ticker(jimid, "pton") pos_test = Position.from_account_and_ticker(jimid, "aapl") quant = pos_test.total_quantity self.assertAlmostEqual(result, 300, "mv = px * quantity") self.assertAlmostEqual( acct.balance, 600, "balance started at 1000 trades cost 400 so new bal should be 600") self.assertEqual( len(trade_hist), 2, "there shouold only be 2 trades with aapl one trade here and one from seed" ) self.assertEqual( len(trade_hist2), 1, "there shouold only be 1 trade with pton from here and one from seed" ) self.assertAlmostEqual( quant, 8, "seed gave 5 shs of aapl and this test bought 3 total position is now 8" ) with self.assertRaises( InsufficientFundsError ): #"tring buy shs that cost more then your balance causes an error" result3 = acct.trade("pton", 500, 100)
def test_login_attempt(self): result = Account.from_pk(jimid) result.set_password("password") result.save() test = Account.login_attempt("JimmyLove", "password") self.assertIsNotNone(test, "login should work with the username & password")
def test_sell(self): #sell is the same function, trade, as buy with negtive quantity # it has opposite effect on cash balance and position acct = Account.from_pk(jimid) result = acct.trade("aapl", -2, 100) #result is mv of trade pos_test = Position.from_account_and_ticker(jimid, "aapl") quant = pos_test.total_quantity trade_hist = Trade.from_account_and_ticker(jimid, "aapl") self.assertAlmostEqual(result, 200, "mv = quantity * px") self.assertAlmostEqual( quant, 3, "seed gave 5 shs of aapl and this test sold 2; total position is now 3" ) self.assertAlmostEqual(acct.balance, 1200, "started with 1000 and sell give +200") self.assertEqual( len(trade_hist), 2, "there shouold only be 2 trades with aapl one trade here and one from seed" ) with self.assertRaises( InsufficientSharesError ): #"tring sell shs that you don't have causes an error" result3 = acct.trade("pton", -10, 100) with self.assertRaises( InsufficientSharesError ): #"tring sell more shs than you have causes an error" result3 = acct.trade("aapl", -20, 100)
def test_update(self): #Tests Save - update function result = Account.from_pk(jimid) result.balance = 100 result.save() result = Account.all() #using the all fucntion to count rows in DB self.assertEqual( result[0].balance, 100, "all func populates attributes, checking updated first for row[0] / pk1" )