Skip to content

Commit

Permalink
fix: Broken test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
suminb committed May 23, 2024
1 parent edc31d3 commit 56b9f1c
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 117 deletions.
119 changes: 119 additions & 0 deletions tests/ext/test_ext_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import pytest

from finance.ext.models import Portfolio


def assert_equals_dict_of_float(d1: dict, d2: dict):
assert d1.keys() == d2.keys()
for k in d1.keys():
assert abs(d1[k] - d2[k]) < 1e-6


def test_portfolio():
pf = Portfolio.load_from_file("tests/samples/portfolio1.yml", {})
pf.evaluate_inventory()
assert pf.inventory == {"QQQ": 6, "SCHD": 70, "SCHH": 10, "TLT": 15}


# TODO: Diversify test scenarios
inventory = {
"SPY": 50,
"TLT": 45,
"ARKW": 500,
"REMX": 100,
"_USD": 10000,
}
current_prices = {
"SPY": 436.04,
"TLT": 86.2,
"ARKW": 52.64,
"REMX": 63.25,
"GDX": 28.95,
"_USD": 1,
}
target = {
"SPY": 4,
"TLT": 3,
"GDX": 2,
"ARKW": 1,
"_USD": 1,
}
p1 = Portfolio(current_prices, target, [])
p1.inventory = inventory


def test_portfolio_asset_values():
assert_equals_dict_of_float(
p1.asset_values,
{
"SPY": 21802.0,
"TLT": 3879.0,
"ARKW": 26320.0,
"REMX": 6325.0,
"_USD": 10000,
},
)


def test_portfolio_net_asset_value():
assert p1.net_asset_value == 68326


def test_portfolio_current_weights():
assert_equals_dict_of_float(
p1.current_weights,
{
"ARKW": 0.3852120715393847,
"REMX": 0.09257091004888329,
"SPY": 0.3190879021163247,
"TLT": 0.056771946257647164,
"_USD": 0.14635717003776014,
},
)


def test_portfolio_calc_diff():
assert_equals_dict_of_float(
p1.calc_diff(),
{
"SPY": -0.04454846152003894,
"ARKW": 0.2943029806302938,
"TLT": -0.21595532646962554,
"GDX": -0.18181818181818182,
"REMX": 0.09257091004888329,
"_USD": 0.055448079128669225,
},
)


@pytest.mark.skip
def test_portfolio_make_rebalancing_plan():
assert_equals_dict_of_float(
p1.make_rebalancing_plan(),
{
"SPY": 6,
"ARKW": -382,
"TLT": 171,
"GDX": 429,
"REMX": -100,
},
)


@pytest.mark.skip
def test_portfolio_apply_plan():
plan = p1.make_rebalancing_plan()
p1.apply_plan(plan, parse_dt("2023-01-03"), parse_dt("2023-01-05"), {})
assert_equals_dict_of_float(
p1.inventory,
{
"SPY": 56,
"TLT": 216,
"ARKW": 118,
"REMX": 0,
"GDX": 429,
"_USD": 6657.49,
},
)
expected_usd = (target["_USD"] / sum(target.values())) * p1.net_asset_value
assert 0.9 < p1.inventory["_USD"] / expected_usd < 1.1
7 changes: 0 additions & 7 deletions tests/ext/test_models.py

This file was deleted.

111 changes: 1 addition & 110 deletions tests/ext/test_warehouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@

import pytest

from finance.ext.warehouse import Portfolio, make_combination_indices


def assert_equals_dict_of_float(d1: dict, d2: dict):
assert d1.keys() == d2.keys()
for k in d1.keys():
assert abs(d1[k] - d2[k]) < 1e-6
from finance.ext.warehouse import make_combination_indices


@pytest.mark.parametrize(
Expand All @@ -36,106 +30,3 @@ def parse_dt(str_dt, tz=pytz.timezone("America/New_York")):
return datetime.strptime(str_dt + " 00:00:00", "%Y-%m-%d %H:%M:%S").replace(
tzinfo=tz
)


# TODO: Diversify test scenarios
inventory = {
"SPY": 50,
"TLT": 45,
"ARKW": 500,
"REMX": 100,
"_USD": 10000,
}
current_prices = {
"SPY": 436.04,
"TLT": 86.2,
"ARKW": 52.64,
"REMX": 63.25,
"GDX": 28.95,
"_USD": 1,
}
target = {
"SPY": 4,
"TLT": 3,
"GDX": 2,
"ARKW": 1,
"_USD": 1,
}
p1 = Portfolio(inventory, current_prices, target)


def test_portfolio_asset_values():
assert_equals_dict_of_float(
p1.asset_values,
{
"SPY": 21802.0,
"TLT": 3879.0,
"ARKW": 26320.0,
"REMX": 6325.0,
"_USD": 10000,
},
)


def test_portfolio_net_asset_value():
assert p1.net_asset_value == 68326


def test_portfolio_current_weights():
assert_equals_dict_of_float(
p1.current_weights,
{
"ARKW": 0.3852120715393847,
"REMX": 0.09257091004888329,
"SPY": 0.3190879021163247,
"TLT": 0.056771946257647164,
"_USD": 0.14635717003776014,
},
)


def test_portfolio_calc_diff():
assert_equals_dict_of_float(
p1.calc_diff(),
{
"SPY": -0.04454846152003894,
"ARKW": 0.2943029806302938,
"TLT": -0.21595532646962554,
"GDX": -0.18181818181818182,
"REMX": 0.09257091004888329,
"_USD": 0.055448079128669225,
},
)


@pytest.mark.skip
def test_portfolio_make_rebalancing_plan():
assert_equals_dict_of_float(
p1.make_rebalancing_plan(),
{
"SPY": 6,
"ARKW": -382,
"TLT": 171,
"GDX": 429,
"REMX": -100,
},
)


@pytest.mark.skip
def test_portfolio_apply_plan():
plan = p1.make_rebalancing_plan()
p1.apply_plan(plan, parse_dt("2023-01-03"), parse_dt("2023-01-05"), {})
assert_equals_dict_of_float(
p1.inventory,
{
"SPY": 56,
"TLT": 216,
"ARKW": 118,
"REMX": 0,
"GDX": 429,
"_USD": 6657.49,
},
)
expected_usd = (target["_USD"] / sum(target.values())) * p1.net_asset_value
assert 0.9 < p1.inventory["_USD"] / expected_usd < 1.1

0 comments on commit 56b9f1c

Please sign in to comment.