|
| 1 | +import nanobook |
| 2 | + |
| 3 | + |
| 4 | +def _checks_by_name(report): |
| 5 | + return {check["name"]: check for check in report} |
| 6 | + |
| 7 | + |
| 8 | +def test_risk_engine_accepts_new_cap_defaults(): |
| 9 | + risk = nanobook.RiskEngine( |
| 10 | + max_order_value_cents=10_000, |
| 11 | + max_batch_value_cents=10_000, |
| 12 | + ) |
| 13 | + report = risk.check_order("AAPL", "buy", 50, 200, 100_000_000, []) |
| 14 | + assert isinstance(report, list) |
| 15 | + assert len(report) > 0 |
| 16 | + |
| 17 | + |
| 18 | +def test_risk_order_value_boundary(): |
| 19 | + risk = nanobook.RiskEngine( |
| 20 | + max_order_value_cents=10_000, |
| 21 | + max_position_pct=1.0, |
| 22 | + max_batch_value_cents=100_000_000, |
| 23 | + ) |
| 24 | + report = risk.check_order("AAPL", "buy", 50, 200, 100_000_000, []) |
| 25 | + checks = _checks_by_name(report) |
| 26 | + assert checks["Max order value"]["status"] == "PASS" |
| 27 | + |
| 28 | + fail_report = risk.check_order("AAPL", "buy", 51, 200, 100_000_000, []) |
| 29 | + fail_checks = _checks_by_name(fail_report) |
| 30 | + assert fail_checks["Max order value"]["status"] == "FAIL" |
| 31 | + |
| 32 | + |
| 33 | +def test_risk_batch_report_includes_cap_checks(): |
| 34 | + risk = nanobook.RiskEngine( |
| 35 | + max_batch_value_cents=10_000, |
| 36 | + max_order_value_cents=10_000, |
| 37 | + max_position_pct=1.0, |
| 38 | + ) |
| 39 | + report = risk.check_batch( |
| 40 | + orders=[("AAPL", "buy", 100, 100), ("MSFT", "buy", 0, 0)], |
| 41 | + equity_cents=100_000_000, |
| 42 | + positions=[], |
| 43 | + target_weights=[("AAPL", 0.5), ("MSFT", 0.5)], |
| 44 | + ) |
| 45 | + checks = _checks_by_name(report) |
| 46 | + assert "Max batch value" not in checks |
| 47 | + assert "Max order value" not in checks |
| 48 | + |
| 49 | + fail_report = risk.check_batch( |
| 50 | + orders=[("AAPL", "buy", 30, 400), ("MSFT", "buy", 30, 400)], |
| 51 | + equity_cents=100_000_000, |
| 52 | + positions=[], |
| 53 | + target_weights=[("AAPL", 0.5), ("MSFT", 0.5)], |
| 54 | + ) |
| 55 | + fail_checks = _checks_by_name(fail_report) |
| 56 | + assert fail_checks["Max batch value"]["status"] == "FAIL" |
0 commit comments