|
| 1 | +"""An unresolvable futures contract must raise inside the SDK's hierarchy (#122). |
| 2 | +
|
| 3 | +`normalize_futures_slug` raised a builtin `ValueError`, which is not an |
| 4 | +`OilPriceAPIError`. That predates #111, but #111 turned 18 live catalog codes |
| 5 | +from "resolved to a DIFFERENT instrument" into "raises" -- the right call -- so |
| 6 | +user code shaped like |
| 7 | +
|
| 8 | + try: |
| 9 | + curve = client.futures.curve(code) |
| 10 | + except OilPriceAPIError: |
| 11 | + fall_back() |
| 12 | +
|
| 13 | +went from a silently wrong answer to an uncaught crash. A refusal is only |
| 14 | +recoverable if it is catchable through the documented base class. |
| 15 | +""" |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from oilpriceapi import exceptions |
| 20 | +from oilpriceapi.exceptions import OilPriceAPIError, ValidationError |
| 21 | +from oilpriceapi.resources._futures_slug import normalize_futures_slug |
| 22 | +from oilpriceapi.resources.futures import FuturesResource |
| 23 | + |
| 24 | +# The catalog codes #111 moved from "wrong instrument" to "raises". |
| 25 | +REWRITTEN_BY_111 = [ |
| 26 | + "LNG_NW_EUROPE_EUR", |
| 27 | + "WTI_MIDLAND_USD", |
| 28 | + "BRENT_CRUDE_USD", |
| 29 | + "GASOIL_USD", |
| 30 | + "JKM_LNG_USD", |
| 31 | +] |
| 32 | + |
| 33 | + |
| 34 | +class _FakeClient: |
| 35 | + def request(self, **kwargs): # pragma: no cover - never reached |
| 36 | + raise AssertionError("a refused contract must not reach the transport") |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize("code", REWRITTEN_BY_111) |
| 40 | +def test_normalize_raises_inside_the_sdk_hierarchy(code): |
| 41 | + with pytest.raises(OilPriceAPIError): |
| 42 | + normalize_futures_slug(code) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize("code", REWRITTEN_BY_111) |
| 46 | +def test_it_is_a_validation_error_carrying_the_contract(code): |
| 47 | + with pytest.raises(exceptions.FuturesContractError) as excinfo: |
| 48 | + normalize_futures_slug(code) |
| 49 | + |
| 50 | + error = excinfo.value |
| 51 | + assert isinstance(error, ValidationError) |
| 52 | + assert error.field == "contract" |
| 53 | + assert error.value == code |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.parametrize("code", REWRITTEN_BY_111) |
| 57 | +def test_value_error_callers_still_catch_it(code): |
| 58 | + """Backwards compatible: code written against the old `raise ValueError`.""" |
| 59 | + with pytest.raises(ValueError): |
| 60 | + normalize_futures_slug(code) |
| 61 | + |
| 62 | + |
| 63 | +def test_the_helpful_message_survives_str(): |
| 64 | + """The message IS the remediation: it lists every valid slug and code.""" |
| 65 | + with pytest.raises(exceptions.FuturesContractError) as excinfo: |
| 66 | + normalize_futures_slug("WTI_MIDLAND_USD") |
| 67 | + |
| 68 | + rendered = str(excinfo.value) |
| 69 | + assert "Unknown futures contract/slug 'WTI_MIDLAND_USD'" in rendered |
| 70 | + assert "Pass a slug (" in rendered |
| 71 | + assert "brent" in rendered |
| 72 | + assert "CL" in rendered |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize("method", ["latest", "curve", "intraday", "historical", "ohlc"]) |
| 76 | +def test_public_futures_methods_do_not_escape_the_base_class(method): |
| 77 | + resource = FuturesResource(_FakeClient()) |
| 78 | + |
| 79 | + with pytest.raises(OilPriceAPIError): |
| 80 | + getattr(resource, method)("WTI_MIDLAND_USD") |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.parametrize("contract", ["brent", "wti", "CL", "BZ", "continuous/brent"]) |
| 84 | +def test_valid_contracts_are_untouched(contract): |
| 85 | + assert normalize_futures_slug(contract) |
0 commit comments