def _add_accounts(self, day_of_week=None): db.session.add_all([ # user account with some subscriptions Account( email='*****@*****.**', email_events=[ EmailEvent( account_email='*****@*****.**', time_of_day=time(12), day_of_week=day_of_week, subreddits=Subreddit.query.filter( Subreddit.name.in_( ['aviation', 'spacex', 'running'])).all(), ) ], ), # deactivated account Account( email='*****@*****.**', active=False, email_events=[ EmailEvent( account_email='*****@*****.**', time_of_day=time(12), day_of_week=day_of_week, subreddits=Subreddit.query.filter( Subreddit.name.in_(['programming', 'askreddit'])).all(), ) ], ), # account that already received their email for today Account( email='*****@*****.**', last_email=datetime.utcnow() - timedelta(minutes=10), email_events=[ EmailEvent( account_email='*****@*****.**', time_of_day=time(12), day_of_week=day_of_week, subreddits=Subreddit.query.filter( Subreddit.name.in_(['analog', 'finance'])).all(), ) ], ), # account expecting different interval from the rest Account( email='*****@*****.**', email_events=[ EmailEvent( account_email='*****@*****.**', time_of_day=time(12), day_of_week=6 if day_of_week is None else None, subreddits=Subreddit.query.filter( Subreddit.name.in_( ['aviation', 'spacex', 'running'])).all(), ) ], ), ])
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 populate(): Role.insert_roles() user = Account(password='******', email='*****@*****.**', confirmed=True) user2 = Account(password='******', email='*****@*****.**', confirmed=True) new_category = Category(name='Kategoria1') new_category2 = Category(name='Kategoria2') new_product = Product(name='Produkt1', description='Opis1', weight=1.99, price=12.99, category=new_category, seller=user) new_product2 = Product(name='Produkt2', description='Opis2', weight=20, price=100, category=new_category2, seller=user2) db.session.add_all([user, user2, new_product, new_product2, new_category, new_category2]) db.session.commit()
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 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 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_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 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(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 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 testOnePosition(self): account = Account(username="******", password_hash="password", balance=50000) self.assertEqual(account.username, "Justin") self.assertEqual(account.password_hash, "password") self.assertEqual(account.balance, 50000) self.assertIsInstance(account, Account)
def testOnePosition(self): account = Account(username="******", password_hash="abdoulname", balance=20000) self.assertEqual(account.username, "Abdoul") self.assertEqual(account.password_hash, "abdoulname") self.assertEqual(account.balance, 20000) self.assertIsInstance(account, Account)
def setUpClass(cls): cls.app = create_app('testing') cls.app_context = cls.app.app_context() cls.app_context.push() db.create_all() Role.insert_roles() cls.acc = Account(password='******', email='*****@*****.**') db.session.add(cls.acc) db.session.commit()
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 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 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 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 testIs_admin(self): alex = Account(username="******", password_hash="password", balance=20000000, first_name="Alex", last_name="C", email="*****@*****.**", admin=1) alex.save() self.assertEqual(alex.admin, 1, msg="account should be admin account") caroline = Account(username="******", password_hash="password", balance=10000, first_name="Caroline", last_name="Grabowski", email="*****@*****.**") caroline.save() self.assertEqual(caroline.admin, 0, msg="account should not be admin account")
def testSave(self): account = Account(username="******", password_hash="password", balance=50000) account.save() self.assertEqual(account.pk, 2) self.assertEqual(account.username, "Justin") self.assertEqual(account.password_hash, "password") self.assertEqual(account.balance, 50000) self.assertIsInstance(account, Account) account = Account(pk=2, username="******", password_hash="password1", balance=25000) account.save() self.assertEqual(account.pk, 2) self.assertEqual(account.username, "Justin") self.assertEqual(account.password_hash, "password1") self.assertEqual(account.balance, 25000) self.assertIsInstance(account, Account)
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 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 testAll(self): caroline = Account(username="******", password_hash="password", balance=10000, first_name="Caroline", last_name="Grabowski", email="*****@*****.**") caroline.save() alex = Account(username="******", password_hash="password", balance=20000, first_name="Alex", last_name="C", email="*****@*****.**") alex.save() all_data = Account.all() # self.assertEqual(len(all_data), 2, "all data should return all rows of data") self.assertEqual(all_data[0].first_name, "Caroline", "all function should return all account data") self.assertEqual(all_data[1].first_name, "Alex", "all function should return all account data")
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 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 setUp(self): super().setUp() self.client = app.test_client() self.account = Account( email='*****@*****.**', email_events=[ EmailEvent( account_email='*****@*****.**', time_of_day=time(12), ) ], ) db.session.add(self.account) db.session.commit()