Skip to content

Commit

Permalink
test: test __repr__ for Portfolio and EfficientFrontierReb
Browse files Browse the repository at this point in the history
  • Loading branch information
chilango74 committed Aug 16, 2021
1 parent 24cf9ef commit d7055b5
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 183 deletions.
343 changes: 172 additions & 171 deletions examples/05 efficient frontier multi-period.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion okama/asset_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AssetList(ListMaker):

def __repr__(self):
dic = {
"symbols": self.symbols,
"assets": self.symbols,
"currency": self._currency.ticker,
"first_date": self.first_date.strftime("%Y-%m"),
"last_date": self.last_date.strftime("%Y-%m"),
Expand Down
8 changes: 4 additions & 4 deletions okama/frontier/multi_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ def __init__(self,
def __repr__(self):
dic = {
'symbols': self.symbols,
'currency': self.currency.ticker,
'first date': self.first_date.strftime("%Y-%m"),
'currency': self._currency.ticker,
'first_date': self.first_date.strftime("%Y-%m"),
'last_date': self.last_date.strftime("%Y-%m"),
'period length': self._pl_txt,
'rebalancing period': self.reb_period,
'period_length': self._pl_txt,
'rebalancing_period': self.reb_period,
'inflation': self.inflation if hasattr(self, 'inflation') else 'None',
}
return repr(pd.Series(dic))
Expand Down
8 changes: 4 additions & 4 deletions okama/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ def __init__(
def __repr__(self):
dic = {
"symbol": self.symbol,
"symbols": self.symbols,
"assets": self.symbols,
"weights": self.weights,
"rebalancing period": self.rebalancing_period,
"rebalancing_period": self.rebalancing_period,
"currency": self.currency,
"inflation": self.inflation if hasattr(self, "inflation") else "None",
"first date": self.first_date.strftime("%Y-%m"),
"first_date": self.first_date.strftime("%Y-%m"),
"last_date": self.last_date.strftime("%Y-%m"),
"period length": self._pl_txt,
"period_length": self._pl_txt,
}
return repr(pd.Series(dic))

Expand Down
5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def init_plots():
return ok.Plots(assets=['RUB.FX', 'EUR.FX', 'MCFTR.INDX'], ccy='RUB', first_date='2010-01', last_date='2020-01')


# Efficient Frontier
# Efficient Frontier Single Period
@pytest.fixture(scope='module')
def init_efficient_frontier_values():
return dict(
Expand All @@ -148,7 +148,8 @@ def init_efficient_frontier_bounds(init_efficient_frontier_values):
return ok.EfficientFrontier(**init_efficient_frontier_values, bounds=bounds)


# Efficient Frontier Multi-Period
@pytest.fixture(scope='module')
def init_efficient_frontier_reb():
ls = ['SPY.US', 'GLD.US']
return ok.EfficientFrontierReb(assets=ls, ccy='RUB', first_date='2019-01', last_date='2020-02', n_points=3)
return ok.EfficientFrontierReb(assets=ls, ccy='RUB', first_date='2019-01', last_date='2020-02', n_points=3, verbose=False)
2 changes: 1 addition & 1 deletion tests/test_asset_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_asset_list_init_failing():
class TestAssetList:
def test_repr(self):
value = pd.Series(dict(
symbols="[pf1.PF, RUB.FX, MCFTR.INDX]",
assets="[pf1.PF, RUB.FX, MCFTR.INDX]",
currency="USD",
first_date="2019-02",
last_date="2020-01",
Expand Down
15 changes: 15 additions & 0 deletions tests/test_frontier_reb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import numpy as np
from numpy.testing import assert_allclose

import pandas as pd

import okama as ok


Expand All @@ -15,6 +17,19 @@ def test_init_efficient_frontier_reb():
ok.EfficientFrontierReb(assets=['MCFTR.INDX'])


def test_repr(init_efficient_frontier_reb):
value = pd.Series(dict(
symbols="[SPY.US, GLD.US]",
currency="RUB",
first_date="2019-01",
last_date="2020-02",
period_length="1 years, 2 months",
rebalancing_period="year",
inflation="RUB.INFL",
))
assert repr(init_efficient_frontier_reb) == repr(value)


@mark.rebalance
@mark.frontier
def test_gmv_annual_weights(init_efficient_frontier_reb):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@
from .conftest import data_folder


def test_repr(portfolio_rebalanced_year):
value = pd.Series(dict(
symbol="pf1.PF",
assets="[RUB.FX, MCFTR.INDX]",
weights="[0.5, 0.5]",
rebalancing_period="year",
currency="RUB",
inflation="RUB.INFL",
first_date="2015-01",
last_date="2020-01",
period_length="5 years, 1 months",
))
assert repr(portfolio_rebalanced_year) == repr(value)


def test_symbol_failing(portfolio_rebalanced_year):
with pytest.raises(ValueError, match='portfolio symbol must be a string ending with ".PF" namespace.'):
portfolio_rebalanced_year.symbol = 1
Expand Down

0 comments on commit d7055b5

Please sign in to comment.