|
| 1 | +"""Tests for the Deribit volatility surface loader.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +from pathlib import Path |
| 7 | +from typing import Any, AsyncIterator |
| 8 | +from unittest.mock import AsyncMock, patch |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from quantflow.data.deribit import Deribit |
| 13 | + |
| 14 | +FIXTURES = Path(__file__).parent / "fixtures" |
| 15 | + |
| 16 | + |
| 17 | +def load_fixture(name: str) -> list[dict]: |
| 18 | + return json.loads((FIXTURES / name).read_text()) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def futures() -> list[dict]: |
| 23 | + return load_fixture("deribit_futures.json") |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def options() -> list[dict]: |
| 28 | + return load_fixture("deribit_options.json") |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def instruments() -> list[dict]: |
| 33 | + return load_fixture("deribit_instruments.json") |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +async def deribit_cli( |
| 38 | + futures: list[dict], options: list[dict], instruments: list[dict] |
| 39 | +) -> AsyncIterator[Deribit]: |
| 40 | + async def get_path(path: str, **kw: Any) -> list[dict]: |
| 41 | + params = kw.get("params", {}) |
| 42 | + if path == "public/get_instruments": |
| 43 | + return instruments |
| 44 | + if path == "public/get_book_summary_by_currency": |
| 45 | + return futures if params.get("kind") == "future" else options |
| 46 | + raise ValueError(f"Unexpected path: {path}") |
| 47 | + |
| 48 | + with patch.object(Deribit, "get_path", AsyncMock(side_effect=get_path)): |
| 49 | + async with Deribit() as cli: |
| 50 | + yield cli |
| 51 | + |
| 52 | + |
| 53 | +async def test_loader_loads_known_options(deribit_cli: Deribit) -> None: |
| 54 | + """Options present in both book summary and instruments are loaded.""" |
| 55 | + loader = await deribit_cli.volatility_surface_loader("btc") |
| 56 | + surface = loader.surface() |
| 57 | + # fixture has 2 strikes (70000 C+P, 75000 C) all on one maturity |
| 58 | + total_strikes = sum(len(m.strikes) for m in surface.maturities) |
| 59 | + assert total_strikes == 2 |
| 60 | + |
| 61 | + |
| 62 | +async def test_loader_skips_option_missing_from_instruments( |
| 63 | + deribit_cli: Deribit, options: list[dict] |
| 64 | +) -> None: |
| 65 | + """Options absent from the instruments list are silently skipped.""" |
| 66 | + ghost = "BTC-10APR26-67500-P" |
| 67 | + assert any(o["instrument_name"] == ghost for o in options) |
| 68 | + |
| 69 | + loader = await deribit_cli.volatility_surface_loader("btc") |
| 70 | + surface = loader.surface() |
| 71 | + all_strikes = { |
| 72 | + strike.strike for mat in surface.maturities for strike in mat.strikes |
| 73 | + } |
| 74 | + assert 67500 not in all_strikes |
| 75 | + |
| 76 | + |
| 77 | +async def test_loader_skips_future_missing_from_instruments( |
| 78 | + deribit_cli: Deribit, futures: list[dict] |
| 79 | +) -> None: |
| 80 | + """Futures absent from the instruments list are silently skipped.""" |
| 81 | + assert any(f["instrument_name"] == "BTC-GHOST-26" for f in futures) |
| 82 | + |
| 83 | + loader = await deribit_cli.volatility_surface_loader("btc") |
| 84 | + assert loader is not None |
0 commit comments