def seed(dbpath=DBPATH): ORM.dbpath = dbpath mike_bloom = Account(username='******', balance=10000.00) mike_bloom.set_password('password') mike_bloom.save() # trade for a purchase of 10 shares yesterday # trade for a sell of 5 shares today tsla_position = Position(ticker='tsla', shares=5, accounts_pk=mike_bloom.pk) tsla_position.save() ms_position = Position(ticker='ms', shares=10, accounts_pk=mike_bloom.pk) ms_position.save() tsla_trade = Trade( ticker='tsla', volume=5, price=95.20, time=time.time(), accounts_pk=mike_bloom.pk, ) tsla_trade.save() ms_trade = Trade( ticker='ms', volume=10, price=25.50, time=time.time(), accounts_pk=mike_bloom.pk, ) ms_trade.save()
def testSaveUpdate(self): caroline = Account(username="******", password_hash="password", balance=10000, first_name="Caroline", last_name="Grabowski", email="*****@*****.**") caroline.save() caroline_id = caroline.id caroline2 = Account.from_id(caroline_id) caroline2.username = "******" caroline2.balance = 20000 caroline2.first_name = "Caro" caroline2.last_name = "Grabo" caroline2.save() self.assertEqual(caroline2.id, caroline_id, "update should not change ID number") caroline3 = Account.from_id(caroline_id) self.assertEqual(caroline3.username, "cgrabow16", "update should update username") self.assertEqual(caroline3.balance, 20000, "update should update balance") self.assertEqual(caroline3.first_name, "Caro", "update should update name") self.assertEqual(caroline3.last_name, "Grabo", "update should update name")
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 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 test_select_all(self): new_acc = Account(username="******",password_hash="1234",balance=10000) new_acc2 = Account(username="******",password_hash="4321",balance=20000) new_acc.save() new_acc2.save() acc_list = Account.all_from_where_clause() self.assertEqual(len(acc_list), 2)
def seed(dbpath=DBPATH): ORM.dbpath = dbpath mike_bloom = Account(username='******', balance=10000.00) mike_bloom.set_password('password') mike_bloom.api_key = '00000' mike_bloom.save() buy_trade = Trade(accounts_pk=mike_bloom.pk, ticker='tsla', volume=10, price=100.0) sell_trade = Trade(accounts_pk=mike_bloom.pk, ticker='tsla', volume=-5, price=200.0) buy_trade.save() sell_trade.save() tsla_position = Position(ticker='tsla', shares=5, accounts_pk=mike_bloom.pk) appl_position = Position(ticker='aapl', shares=5, accounts_pk=mike_bloom.pk) tsla_position.save() appl_position.save()
def testApiAuthenticate(self): account = Account(pk=2, username="******", password_hash="password", balance=50000) api_key = account.generate_api_key() account.save() test = account.api_authenticate(api_key) self.assertAlmostEqual(account, test)
def testAPIKey(self): account = Account(pk=2, username="******", password_hash="password", balance=50000) api_key = account.generate_api_key() account.save() test = Account.one_from_where_clause("WHERE api_key=?", (account.api_key, )) self.assertEqual(test, api_key)
def create_account(): data = request.get_json() if data: # email, password, balance = views.create_account() new_account = Account(None, data['email'], data['password'], data['first_name'], data['last_name']) new_account.api_key = generate_key() new_account.save() return jsonify({'status': 'success', 'api_key': new_account.api_key}) return jsonify({"error": "invalid data"})
def test_key_creation(self): #Create new account record incl. api_key and save to DB new_user = Account(username="******") new_user.set_password("test_password") new_user.create_api_key() new_user.save() #Retrieve new account record and evaluate api_key pop'd pk = new_user.pk same_user = Account.one_from_pk(pk) self.assertEqual(len(same_user.api_key), 15, "api key populated")
def createaccount(): if not request.json or 'username' not in request.json or 'password' not in request.json: return jsonify(BADREQUEST), 400 account = Account() r = request.json account.username = r['username'] account.set_password(r['password']) account.set_apikey() account.balance = 0.00 account.save() return jsonify({"username": account.username, "apikey": account.api_key})
def seed(dbpath=DBPATH): ORM.dbpath = dbpath abdoul = Account(username="******", pin="3212", balance=2000.00) abdoul.save() # abdoul.generate_api_key() # abdoul.save() # print(abdoul.api_key)
def testLogin(self): account3 = Account(username="******", pin="1234", balance=20.00) api = account3.generate_api_key() account3.save() get_api = account3.get_api() account4 = Account.api_authenticate(get_api) self.assertEqual(account4.username, "mike_bloom") self.assertEqual(account4.pin, "1234") self.assertEqual(account4.balance, 20.00)
def testNewAccount(self): account1 = Account() self.assertEqual(account1.tablename, "accounts") self.assertIsInstance(account1, Account) self.assertEqual(account1.fields, ["username", "pin", "balance", "api_key"]) account2 = Account(username="******", pin="4456", balance="1200.00") api = account2.generate_api_key() account2.save() get_api = account2.get_api() self.assertEqual(api, get_api)
def testCheck_balance(self): print("""\n========================= UNIT TEST testCheck_balance()\n""") account_data = Account(username="******", balance=20000000, first_name="John", last_name="Smith", email_address="*****@*****.**") account_data.set_password_hash("password") account_data.save() view.give_balance(account_data.balance)
def testFromId(self): caroline = Account(username="******", password_hash="password", balance=10000, first_name="Caroline", last_name="Grabowski", email="*****@*****.**") caroline.save() caroline_id = caroline.id caroline2 = Account.from_id(caroline_id) self.assertEqual(caroline2.first_name, "Caroline") alex = Account.from_id(10340923950399) self.assertIsNone(alex, "from_id returns None for nonexistent row")
def seed(dbpath=DBPATH): ORM.dbpath = dbpath print("DATABASE: ", dbpath) mike_bloom = Account(username='******', balance=10000.00) mike_bloom.set_password("password") print(mike_bloom.dbpath) mike_bloom.save() tsla_position = Position(ticker='tsla', shares=5, account_pk=mike_bloom.pk) tsla_position.save() fake_trade = Trade(ticker='tsla', quantity=2, type=1) fake_trade.save()
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 create(): if not request.json or 'username' not in request.json or 'password_hash' not in request.json: return jsonify(BAD_REQUEST), 401 account = Account(username=request.json['username'], password_hash=request.json['password_hash']) hashed_pw = Account.set_password(account, request.json['password_hash']) account.set_api_key() account.save() token = encodeAuthToken(account.pk) return jsonify({ 'status': 'success', 'auth_token': str(token), 'api_key': account.api_key })
def testSaveInsert(self): caroline = Account(username="******", password_hash="password", balance=10000, first_name="Caroline", last_name="Grabowski", email="*****@*****.**") caroline.save() self.assertIsNotNone(caroline.id, "save should set an id value for new input") with sqlite3.connect(DBPATH) as connection: cursor = connection.cursor() SQL = "SELECT * FROM accounts WHERE username='******';" cursor.execute(SQL) rows = cursor.fetchall() self.assertEqual(len(rows), 1, "save should create 1 new row in the database")
def testDeposit_funds(self): print("""\n========================= UNIT TEST testDeposit_funds()\n""") account_data = Account(username="******", balance=20000000, first_name="John", last_name="Smith", email_address="*****@*****.**") account_data.set_password_hash("password") account_data.save() view.give_balance(account_data.balance) amount = round(float(view.enter_deposit_info()), 2) account_data.balance += amount account_data.save() view.give_balance(account_data.balance)
def test_save_and_pk_load(self): user = Account(username="******") user.save() self.assertIsInstance(user.pk, int, 'save sets pk') pk = user.pk same_user = Account.one_from_pk(pk) self.assertIsInstance(same_user, Account, "one_from_pk loads an Account object") self.assertEqual(same_user.username, "Samuel", "save creates database row") same_user.username = "******" same_user.save() same_again = Account.one_from_pk(pk) self.assertEqual(same_again.username, "Sam", "save updates an existing row")
def testPasswordHash(self): # TEST that set_password_hash actually returns a hashed password caroline = Account(username="******", balance=10000, first_name="Caroline", last_name="Grabowski", email="*****@*****.**") caroline.save() caroline.set_password_hash("password") self.assertIsNotNone(caroline.password_hash, "password_hash should return a hashed password") # TEST that set_password_hash actually hashed password correctly # it worked due to grit, not skill self.assertTrue( bcrypt.checkpw("password".encode(), caroline.password_hash), msg= "set_password hash() should set self.hashed_password to encrypted password" )
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 testLookup_stock_prices(self): print("""\n========================= UNIT TEST testLookup_stock_prices()\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() try: stock_quote = account_data.stock_quotes(ticker) view.stock_info(ticker, stock_quote) except NoSuchTickerError: view.bad_ticker()
def test_save_and_pk_load(self): user = Account(username='******') user.save() self.assertIsInstance(user.pk, int, 'save sets pk') pk = user.pk same_user = Account.one_from_pk(pk) self.assertIsInstance(same_user, Account, 'one_from_pk loads Account object') self.assertEqual(same_user.username, 'Samuel', 'save creates database row') same_user.username = '******' same_user.save() same_again = Account.one_from_pk(pk) self.assertEqual(same_again.username, 'Sam', 'save updates an existing row')
def test_update(self): new_acc = Account(username="******",password_hash="1234",balance=10000) new_acc2 = Account(username="******",password_hash="4321",balance=20000) new_acc.save() new_acc2.save() new_acc.username = "******" new_acc2.username = "******" new_acc.save() new_acc2.save() item1 = Account.one_from_pk(1) item2 = Account.one_from_pk(2) self.assertEqual(item1.username, "test") self.assertEqual(item2.username, "test2")
def testSave(self): account = Account(username="******", password_hash="abdoulname", balance=20000) account.save() self.assertEqual(account.pk, 2) self.assertEqual(account.username, "Abdoul") self.assertEqual(account.password_hash, "abdoulname") self.assertEqual(account.balance, 20000) self.assertIsInstance(account, Account) account = Account(pk=2, username="******", password_hash="abdoulname", balance=25000) account.save() self.assertEqual(account.pk, 2) self.assertEqual(account.username, "Abdoul") self.assertEqual(account.password_hash, "abdoulname") self.assertEqual(account.balance, 25000) self.assertIsInstance(account, Account)
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 testCreate_account_and_nonadmin_login(self): print("""\n========================= UNIT TEST testCreate_account_and_nonadmin_login() <<Press 7 and 3 to quit>> and proceed to next test\n""") account_data = Account(username="******", balance=20000000, first_name="John", last_name="Smith", email_address="*****@*****.**", admin=0) account_data.set_password_hash("password") account_data.save() js2020 = Account.get_from_username("js2020") account_data = Account.login(js2020.username, "password") print("TRCR test_controller.py account_data\n", account_data) self.assertIsNotNone(account_data, msg="login() should return account data") controller.main_menu(account_data)