def test_get_account_transactions(account_with_transaction, client): app = account_with_transaction stock_2 = dict(transaction_type=StockTransactionType.buy, stock_symbol="VCN.TO", cost_per_unit=3333, quantity=1600, trade_fee=995, trade_date=datetime.date(2016, 4, 26), account_id=2, user_id=1) with app.app_context(): db.session.add(InvestmentAccount(**investment_account_2)) db.session.add(InvestmentAccount(**investment_account_3)) db.session.add(StockTransaction(**stock_2)) db.session.add( StockTransaction(transaction_type=StockTransactionType.buy, stock_symbol="VCN.TO", cost_per_unit=3000, quantity=600, trade_fee=995, trade_date=datetime.date(2016, 4, 26), account_id=3, user_id=2)) db.session.commit() response = client.get('/investment_account/transactions?account_id=2') accounts = json.loads(response.data) assert len(accounts) == 1 for k, v in accounts[0].items(): if k == 'id': continue assert v == accounts[0][k]
def test_get_existing_account(investment_account_setup, client): with investment_account_setup.app_context(): db.session.add(InvestmentAccount(**investment_account_2)) db.session.add(InvestmentAccount(**investment_account_3)) db.session.commit() response = client.get('/investment_account/all') accounts = json.loads(response.data) assert len(accounts) == 2
def test_stats_two_transaction_diff_user_book_value(investment_account_setup, client): app = investment_account_setup with app.app_context(): db.session.add(InvestmentAccount(**investment_account_2)) db.session.add(InvestmentAccount(**investment_account_3)) db.session.add(StockTransaction(**stock_transaction_1)) stock_account_2 = stock_transaction_2.copy() stock_account_2['user_id'] = 2 stock_account_2['account_id'] = 3 db.session.add(StockTransaction(**stock_account_2)) db.session.commit() response = client.get('/transaction/stats') json_data = json.loads(response.data) assert json_data['book_cost'] == "$3,150.99"
def investment_account_setup(auth_app_user_1): auth_app = auth_app_user_1 try: with auth_app.app_context(): with open('tests/resources/stock_price.csv', 'r') as csv_file: csv_iterator = csv.reader(csv_file) csv_iterator.__next__() # ignore the headers for row in csv_iterator: db.session.add( StockPrice( **dict(stock_symbol=row[0], price_date=date.fromisoformat(row[1]), close_price=int(row[2])))) db.session.add(InvestmentAccount(**investment_account_1)) db.session.commit() yield auth_app except Exception as e: logging.error(traceback.format_exc()) logging.error(e) assert False finally: with auth_app.app_context(): StockTransaction.query.delete() InvestmentAccount.query.delete() db.session.commit()
def test_get_empty_account_acb(investment_account_setup, client): app = investment_account_setup with app.app_context(): db.session.add(InvestmentAccount(**investment_account_2)) db.session.commit() response = client.get('/investment_account/2/acb') json_data = json.loads(response.data) assert json_data['adjust_cost_base'] == {}
def test_stats_multi_stock_user_market_value(investment_account_setup, client): app = investment_account_setup with app.app_context(): db.session.add(InvestmentAccount(**investment_account_2)) db.session.add(InvestmentAccount(**investment_account_3)) db.session.add(StockTransaction(**stock_transaction_1)) stock_account_2 = stock_transaction_2.copy() stock_account_2['user_id'] = 2 stock_account_2['account_id'] = 3 db.session.add(StockTransaction(**stock_account_2)) db.session.commit() response = client.get('/transaction/stats') json_data = json.loads(response.data) assert json_data['market_value']['total'] == "$3,312.00" breakdown = json_data['market_value']['breakdown'] assert len(breakdown) == 1 assert breakdown['VCN.TO']["formatted_value"] == "$3,312.00" assert breakdown['VCN.TO']['percent'] == '100.0%'
def investment_account_setup(auth_app_user_1): auth_app = auth_app_user_1 try: with auth_app.app_context(): db.session.add(InvestmentAccount(**investment_account_1)) db.session.commit() yield auth_app except Exception as e: assert False finally: with auth_app.app_context(): InvestmentAccount.query.delete() db.session.commit()
def test_get_one_stock_acb(investment_account_setup, client): app = investment_account_setup with app.app_context(): db.session.add(InvestmentAccount(**investment_account_2)) db.session.add(StockTransaction(**stock_transaction_4)) db.session.add(StockTransaction(**stock_transaction_5)) db.session.add(StockTransaction(**stock_transaction_6)) db.session.commit() response = client.get('/investment_account/2/acb') json_data = json.loads(response.data) acbs = json_data['adjust_cost_base'] assert len(acbs) == 1 assert acbs['Bagel'] == '$5,054.08'
def create_investment_account(): try: json_data = apply_user_id(json.loads(request.data)) if 'id' in json_data: del json_data['id'] investment_account = InvestmentAccount(**json_data) db.session.add(investment_account) db.session.commit() return jsonify(dict(investment_account)) except Exception as e: logging.error(e) logging.error(traceback.format_exc()) db.session.rollback() return jsonify(None)
def update_investment_account(id): """ Updates the values of the investment account with the values provided in request data """ try: json_data = apply_user_id(json.loads(request.data)) if 'id' in json_data: del json_data['id'] investment_account = InvestmentAccount(**json_data) db.session.query(InvestmentAccount) \ .filter((InvestmentAccount.id == id) & \ (InvestmentAccount.user_id == current_user.id)) \ .update(json_data) db.session.commit() return jsonify(json_data) except Exception as e: logging.error(e) logging.error(traceback.format_exc()) db.session.rollback() return jsonify(None)
def test_get_multi_stock_acb(investment_account_setup, client): app = investment_account_setup with app.app_context(): db.session.add(InvestmentAccount(**investment_account_2)) s1 = stock_transaction_1.copy() s1['account_id'] = 2 s2 = stock_transaction_2.copy() s2['account_id'] = 2 db.session.add(StockTransaction(**s1)) db.session.add(StockTransaction(**s2)) db.session.add(StockTransaction(**stock_transaction_4)) db.session.add(StockTransaction(**stock_transaction_5)) db.session.add(StockTransaction(**stock_transaction_6)) db.session.commit() response = client.get('/investment_account/2/acb') json_data = json.loads(response.data) acbs = json_data['adjust_cost_base'] assert len(acbs) == 3 assert acbs['Bagel'] == '$5,054.08' assert acbs['VAB.TO'] == '$5,211.99' assert acbs['VCN.TO'] == '$3,150.99'