def insert_test_data(): """Inserts some sample data for testing.""" app = create_app(__name__) with app.app_context(): user = User.create( family_name='Byeon', given_name='Sumin', email='*****@*****.**', ignore_if_exists=True) account_checking = create_account( AccountType.checking, 'Shinhan', 'checking', user) account_stock = create_account( AccountType.investment, 'Mirae Asset', 'stock', user) asset_krw = create_asset(AssetType.currency, 'KRW', 'Korean Won') create_asset(AssetType.currency, 'USD', 'United States Dollar') for _ in insert_stock_assets(): pass create_asset(AssetType.security, 'KR5223941018', 'KB S&P500') create_asset(AssetType.security, 'KR5229221225', '이스트스프링차이나') portfolio = Portfolio() portfolio.base_asset = asset_krw portfolio.add_accounts(account_checking, account_stock)
def insert_test_data(): """Inserts some sample data for testing.""" app = create_app(__name__) with app.app_context(): user = User.create( family_name="Byeon", given_name="Sumin", email="*****@*****.**", ignore_if_exists=True, ) account_checking = create_account(AccountType.checking, "Shinhan", "checking", user) account_stock = create_account(AccountType.investment, "Mirae Asset", "stock", user) asset_krw = create_asset(AssetType.currency, "KRW", "Korean Won") create_asset(AssetType.currency, "USD", "United States Dollar") for _ in insert_stock_assets(): pass create_asset(AssetType.security, "KR5223941018", "KB S&P500") create_asset(AssetType.security, "KR5229221225", "이스트스프링차이나") portfolio = Portfolio() portfolio.base_asset = asset_krw portfolio.add_accounts(account_checking, account_stock)
def test_portfolio_balance(account_checking, account_savings, account_sp500, asset_krw, asset_sp500): """Ensures a portfolio, which is essentially a collection of accounts, calculates its balance correctly. """ portfolio = Portfolio() portfolio.base_asset = asset_krw portfolio.add_accounts(account_checking, account_savings, account_sp500) assert portfolio.balance(parse_date('2016-05-20')) == {} deposit(account_checking, asset_krw, 1500, parse_date('2016-05-01')) deposit(account_savings, asset_krw, 3000, parse_date('2016-05-01')) deposit(account_sp500, asset_sp500, 120, parse_date('2016-05-01')) assert portfolio.balance(parse_date('2016-05-20')) \ == {asset_krw: 4500, asset_sp500: 120} deposit(account_savings, asset_krw, 4000, parse_date('2016-05-02')) deposit(account_savings, asset_krw, 5000, parse_date('2016-05-03')) assert portfolio.balance(parse_date('2016-05-20')) \ == {asset_krw: 13500, asset_sp500: 120} balance_adjustment(account_savings, asset_krw, 10000, parse_date('2016-05-04')) assert portfolio.balance(parse_date('2016-05-20')) \ == {asset_krw: 11500, asset_sp500: 120} db.session.delete(portfolio) db.session.commit()
def test_portfolio(account_hf, asset_hf1, account_checking, asset_krw): portfolio = Portfolio() portfolio.base_asset = asset_krw portfolio.add_accounts(account_hf, account_checking) deposit(account_checking, asset_krw, 500000, parse_date("2015-12-04")) with Transaction.create() as t: deposit(account_checking, asset_krw, -500000, parse_date("2015-12-04"), t) deposit(account_hf, asset_hf1, 1, parse_date("2015-12-04"), t) # The net asset value shall not be available at this point with pytest.raises(AssetValueUnavailableException): net_worth = portfolio.net_worth(evaluated_at=parse_date("2015-12-04"), granularity=Granularity.day) # Initial asset value AssetValue.create( evaluated_at=parse_date("2015-12-04"), asset=asset_hf1, base_asset=asset_krw, granularity=Granularity.day, close=500000, ) net_worth = portfolio.net_worth(evaluated_at=parse_date("2015-12-04"), granularity=Granularity.day) assert 500000 == net_worth # 1st payment interest, tax, returned = 3923, 740, 30930 deposit(account_checking, asset_krw, returned, parse_date("2016-01-08")) # Remaining principle value after the 1st payment AssetValue.create( evaluated_at=parse_date("2016-01-08"), asset=asset_hf1, base_asset=asset_krw, granularity=Granularity.day, close=472253, ) net_worth = portfolio.net_worth(evaluated_at=parse_date("2016-01-08"), granularity=Granularity.day) assert 500000 + (interest - tax) == net_worth # 2nd payment deposit(account_checking, asset_krw, 25016, parse_date("2016-02-05")) # Remaining principle value after the 2nd payment AssetValue.create( evaluated_at=parse_date("2016-02-05"), asset=asset_hf1, base_asset=asset_krw, granularity=Granularity.day, close=450195, ) db.session.delete(portfolio) db.session.commit()
def test_portfolio(account_hf, asset_hf1, account_checking, asset_krw): portfolio = Portfolio() portfolio.base_asset = asset_krw portfolio.add_accounts(account_hf, account_checking) with Transaction.create() as t: Record.create( created_at=parse_date('2015-12-04'), transaction=t, account=account_checking, asset=asset_krw, quantity=500000) Record.create( created_at=parse_date('2015-12-04'), transaction=t, account=account_checking, asset=asset_krw, quantity=-500000) Record.create( created_at=parse_date('2015-12-04'), transaction=t, account=account_hf, asset=asset_hf1, quantity=1) # The net asset value shall not be available at this point with pytest.raises(AssetValueUnavailableException): net_worth = portfolio.net_worth(evaluated_at=parse_date('2015-12-04'), granularity=Granularity.day) # Initial asset value AssetValue.create( evaluated_at=parse_date('2015-12-04'), asset=asset_hf1, base_asset=asset_krw, granularity=Granularity.day, close=500000) net_worth = portfolio.net_worth(evaluated_at=parse_date('2015-12-04'), granularity=Granularity.day) assert 500000 == net_worth # 1st payment interest, tax, returned = 3923, 740, 30930 with Transaction.create() as t: Record.create( created_at=parse_date('2016-01-08'), transaction=t, account=account_checking, asset=asset_krw, quantity=returned) # Remaining principle value after the 1st payment AssetValue.create( evaluated_at=parse_date('2016-01-08'), asset=asset_hf1, base_asset=asset_krw, granularity=Granularity.day, close=472253) net_worth = portfolio.net_worth(evaluated_at=parse_date('2016-01-08'), granularity=Granularity.day) assert 500000 + (interest - tax) == net_worth # 2nd payment with Transaction.create() as t: Record.create( created_at=parse_date('2016-02-05'), transaction=t, account=account_checking, asset=asset_krw, quantity=25016) # Remaining principle value after the 2nd payment AssetValue.create( evaluated_at=parse_date('2016-02-05'), asset=asset_hf1, base_asset=asset_krw, granularity=Granularity.day, close=450195) db.session.delete(portfolio) db.session.commit()
def test_portfolio_balance(account_checking, account_savings, account_sp500, asset_krw, asset_sp500): portfolio = Portfolio() portfolio.base_asset = asset_krw portfolio.add_accounts(account_checking, account_savings, account_sp500) assert portfolio.balance(parse_date('2016-05-20')) == {} Record.create(created_at=parse_date('2016-05-01'), account=account_checking, asset=asset_krw, quantity=1500) Record.create(created_at=parse_date('2016-05-01'), account=account_savings, asset=asset_krw, quantity=3000) Record.create(created_at=parse_date('2016-05-01'), account=account_sp500, asset=asset_sp500, quantity=120) assert portfolio.balance(parse_date('2016-05-20')) \ == {asset_krw: 4500, asset_sp500: 120} Record.create(created_at=parse_date('2016-05-02'), account=account_savings, asset=asset_krw, quantity=4000) Record.create(created_at=parse_date('2016-05-03'), account=account_savings, asset=asset_krw, quantity=5000) assert portfolio.balance(parse_date('2016-05-20')) \ == {asset_krw: 13500, asset_sp500: 120} Record.create(created_at=parse_date('2016-05-04'), account=account_savings, asset=asset_krw, quantity=10000, type=RecordType.balance_adjustment) assert portfolio.balance(parse_date('2016-05-20')) \ == {asset_krw: 11500, asset_sp500: 120} db.session.delete(portfolio) db.session.commit()
def portfolio(request, db, asset_krw, account_checking, account_sp500): p = Portfolio.create(base_asset=asset_krw) p.add_accounts(account_checking, account_sp500) def teardown(): # NOTE: The following statement is necessary because the scope of # `asset_krw` is a module, whereas the scope of `p` is a function. p.base_asset = None db.session.delete(p) db.session.commit() request.addfinalizer(teardown) return p
def test_portfolio_balance(account_checking, account_savings, account_sp500, asset_krw, asset_sp500): portfolio = Portfolio() portfolio.base_asset = asset_krw portfolio.add_accounts(account_checking, account_savings, account_sp500) assert portfolio.balance(parse_date('2016-05-20')) == {} Record.create( created_at=parse_date('2016-05-01'), account=account_checking, asset=asset_krw, quantity=1500) Record.create( created_at=parse_date('2016-05-01'), account=account_savings, asset=asset_krw, quantity=3000) Record.create( created_at=parse_date('2016-05-01'), account=account_sp500, asset=asset_sp500, quantity=120) assert portfolio.balance(parse_date('2016-05-20')) \ == {asset_krw: 4500, asset_sp500: 120} Record.create( created_at=parse_date('2016-05-02'), account=account_savings, asset=asset_krw, quantity=4000) Record.create( created_at=parse_date('2016-05-03'), account=account_savings, asset=asset_krw, quantity=5000) assert portfolio.balance(parse_date('2016-05-20')) \ == {asset_krw: 13500, asset_sp500: 120} Record.create( created_at=parse_date('2016-05-04'), account=account_savings, asset=asset_krw, quantity=10000, type=RecordType.balance_adjustment) assert portfolio.balance(parse_date('2016-05-20')) \ == {asset_krw: 11500, asset_sp500: 120} db.session.delete(portfolio) db.session.commit()
def test_portfolio(account_hf, asset_hf1, account_checking, asset_krw): portfolio = Portfolio() portfolio.base_asset = asset_krw portfolio.add_accounts(account_hf, account_checking) with Transaction.create() as t: Record.create(created_at=parse_date('2015-12-04'), transaction=t, account=account_checking, asset=asset_krw, quantity=500000) Record.create(created_at=parse_date('2015-12-04'), transaction=t, account=account_checking, asset=asset_krw, quantity=-500000) Record.create(created_at=parse_date('2015-12-04'), transaction=t, account=account_hf, asset=asset_hf1, quantity=1) # The net asset value shall not be available at this point with pytest.raises(AssetValueUnavailableException): net_worth = portfolio.net_worth(evaluated_at=parse_date('2015-12-04'), granularity=Granularity.day) # Initial asset value AssetValue.create(evaluated_at=parse_date('2015-12-04'), asset=asset_hf1, base_asset=asset_krw, granularity=Granularity.day, close=500000) net_worth = portfolio.net_worth(evaluated_at=parse_date('2015-12-04'), granularity=Granularity.day) assert 500000 == net_worth # 1st payment interest, tax, returned = 3923, 740, 30930 with Transaction.create() as t: Record.create(created_at=parse_date('2016-01-08'), transaction=t, account=account_checking, asset=asset_krw, quantity=returned) # Remaining principle value after the 1st payment AssetValue.create(evaluated_at=parse_date('2016-01-08'), asset=asset_hf1, base_asset=asset_krw, granularity=Granularity.day, close=472253) net_worth = portfolio.net_worth(evaluated_at=parse_date('2016-01-08'), granularity=Granularity.day) assert 500000 + (interest - tax) == net_worth # 2nd payment with Transaction.create() as t: Record.create(created_at=parse_date('2016-02-05'), transaction=t, account=account_checking, asset=asset_krw, quantity=25016) # Remaining principle value after the 2nd payment AssetValue.create(evaluated_at=parse_date('2016-02-05'), asset=asset_hf1, base_asset=asset_krw, granularity=Granularity.day, close=450195) db.session.delete(portfolio) db.session.commit()