Skip to content

Commit

Permalink
validate caches on FXForwards
Browse files Browse the repository at this point in the history
  • Loading branch information
attack68 committed Dec 24, 2024
1 parent 298e212 commit 32e598a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/rateslib/curves/curves.py
Original file line number Diff line number Diff line change
Expand Up @@ -2924,6 +2924,8 @@ def __init__(
self.node_dates = [self.fx_forwards.immediate, self.terminal]

def __getitem__(self, date: datetime) -> DualTypes:
self.fx_forwards._validate_cache() # manually handle cache check

_1: DualTypes = self.fx_forwards._rate_with_path(self.pair, date, path=self.path)[0]
_2: DualTypes = self.fx_forwards.fx_rates_immediate._fx_array_el(
self.cash_idx, self.coll_idx
Expand Down
21 changes: 21 additions & 0 deletions python/tests/test_curves.py
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,27 @@ def test_typing_as_curve(self):
curve = fxf.curve("cad", "eur")
assert isinstance(curve, Curve)

def test_cache_is_validated_on_getitem(self):
fxr1 = FXRates({"usdeur": 0.95}, dt(2022, 1, 3))
fxr2 = FXRates({"usdcad": 1.1}, dt(2022, 1, 2))
fxf = FXForwards(
[fxr1, fxr2],
{
"usdusd": Curve({dt(2022, 1, 1): 1.0, dt(2022, 10, 1): 0.95}),
"eureur": Curve({dt(2022, 1, 1): 1.0, dt(2022, 10, 1): 1.0}),
"eurusd": Curve({dt(2022, 1, 1): 1.0, dt(2022, 10, 1): 0.99}),
"cadusd": Curve({dt(2022, 1, 1): 1.00, dt(2022, 10, 1): 0.97}),
"cadcad": Curve({dt(2022, 1, 1): 1.00, dt(2022, 10, 1): 0.969}),
},
)
curve = fxf.curve("cad", "eur")
fxr1.update({"usdeur": 100000000.0})
fxf.curve("eur", "eur")._set_node_vector([0.5], 1)
prior_id = fxf._cache_id
curve[dt(2022, 1, 9)]
new_id = fxf._cache_id
assert prior_id != new_id


class TestPlotCurve:
def test_plot_curve(self, curve) -> None:
Expand Down

0 comments on commit 32e598a

Please sign in to comment.