import pytest
import quantrs
class TestQuantrsPythonBindings:
def test_day_count(self):
conventions = [
"ACT/365F",
"ACT/365",
"ACT/360",
"30/360US",
"30/360E",
"ACT/ACT ISDA",
"ACT/ACT ICMA",
]
for conv in conventions:
dc = quantrs.DayCount(conv)
year_frac = dc.year_fraction("2025-01-01", "2025-07-01")
days = dc.day_count("2025-01-01", "2025-07-01")
assert isinstance(year_frac, float)
assert isinstance(days, int)
assert year_frac > 0
assert days > 0
def test_bond_pricing(self):
bond = quantrs.ZeroCouponBond(1000.0, "2030-12-31")
dc = quantrs.DayCount("ACT/365F")
assert bond.face_value == 1000.0
assert bond.maturity == "2030-12-31"
yields = [0.01, 0.02, 0.03, 0.04, 0.05]
settlement = "2025-06-19"
prev_price = None
for ytm in yields:
price = bond.price(settlement, ytm, dc)
assert isinstance(price, float)
assert price > 0
assert price <= 1000.0
if prev_price is not None:
assert price < prev_price
prev_price = price
def test_convenience_functions(self):
result1 = quantrs.calculate_year_fraction(
"2025-01-01", "2025-07-01", "ACT/365F"
)
dc = quantrs.DayCount("ACT/365F")
result2 = dc.year_fraction("2025-01-01", "2025-07-01")
assert abs(result1 - result2) < 1e-10
def test_error_handling(self):
with pytest.raises(ValueError):
quantrs.DayCount("INVALID_CONVENTION")
with pytest.raises(ValueError):
dc = quantrs.DayCount("ACT/365F")
dc.year_fraction("invalid-date", "2025-07-01")
with pytest.raises(ValueError):
quantrs.ZeroCouponBond(1000.0, "invalid-date")
def test_repr_methods(self):
dc = quantrs.DayCount("ACT/365F")
dc_repr = repr(dc)
assert "DayCount" in dc_repr
bond = quantrs.ZeroCouponBond(1000.0, "2030-12-31")
bond_repr = repr(bond)
assert "ZeroCouponBond" in bond_repr
assert "1000" in bond_repr
assert "2030-12-31" in bond_repr
def test_performance(self):
import time
dc = quantrs.DayCount("ACT/365F")
bond = quantrs.ZeroCouponBond(1000.0, "2030-12-31")
settlement = "2025-06-19"
ytm = 0.04
n_iterations = 100
start_time = time.time()
for _ in range(n_iterations):
price = bond.price(settlement, ytm, dc)
end_time = time.time()
total_time = end_time - start_time
assert total_time < 1.0