feat(cbr): USD и CNY тянутся всегда, новая валюта — с начала леджера

Валюта, впервые появившаяся в ZenMoney между двумя запусками, раньше показывала «нет курса» до следующего синка. Для валюты без сохранённых курсов история берётся с начала леджера, а не от общего курсора.
This commit is contained in:
Dmitry
2026-09-19 21:54:51 +03:00
parent 4552efbf5d
commit bc5d5c0811
4 changed files with 101 additions and 29 deletions
+48 -13
View File
@@ -19,6 +19,7 @@ from fintracker.sources.cbr.sync import CbrSource
USD_ID = "R01235"
JPY_ID = "R01820"
CNY_ID = "R01375"
def settings() -> Settings:
@@ -39,7 +40,11 @@ def mock_cbr(mock_http, fixture_bytes, *, dynamic: dict[str, str] | None = None)
daily = mock_http.get(DAILY_URL).mock(
return_value=httpx.Response(200, content=fixture_bytes("cbr", "daily.xml"))
)
files = dynamic or {USD_ID: "dynamic_usd.xml", JPY_ID: "dynamic_jpy.xml"}
files = dynamic or {
USD_ID: "dynamic_usd.xml",
JPY_ID: "dynamic_jpy.xml",
CNY_ID: "dynamic_cny.xml",
}
def _dynamic(request: httpx.Request) -> httpx.Response:
cbr_id = request.url.params.get("VAL_NM_RQ", "")
@@ -51,17 +56,19 @@ def mock_cbr(mock_http, fixture_bytes, *, dynamic: dict[str, str] | None = None)
return daily, mock_http.get(DYNAMIC_URL).mock(side_effect=_dynamic)
async def test_only_currencies_in_use_are_requested(app, mock_http, fixture_bytes, run_sync):
async def test_currencies_in_use_are_requested_besides_the_always_quoted_pair(
app, mock_http, fixture_bytes, run_sync
):
await make_account(currency="RUB")
await make_account(currency="USD", source_id="usd-cash")
await make_txn(date(2026, 9, 1), outcome="100", outcome_currency="USD")
await make_account(currency="JPY", source_id="jpy-cash")
await make_txn(date(2026, 9, 1), outcome="100", outcome_currency="JPY")
_, dynamic = mock_cbr(mock_http, fixture_bytes)
result = await run_sync(CbrSource(), settings=settings())
requested = {params["VAL_NM_RQ"] for params in route_params(dynamic)}
assert requested == {USD_ID} # EUR and JPY are quoted by CBR but unused here
assert result.counts == {"currencies": 1, "rates": 3}
assert requested == {USD_ID, CNY_ID, JPY_ID} # EUR is quoted by CBR, but nobody uses it
assert result.counts["currencies"] == 3
assert result.warnings == []
assert result.changed is True
assert result.cursor_after is not None
@@ -88,13 +95,13 @@ async def test_currency_cbr_does_not_quote_is_a_warning(app, mock_http, fixture_
_, dynamic = mock_cbr(mock_http, fixture_bytes)
result = await run_sync(CbrSource(), settings=settings())
assert {params["VAL_NM_RQ"] for params in route_params(dynamic)} == {USD_ID}
assert {params["VAL_NM_RQ"] for params in route_params(dynamic)} == {USD_ID, CNY_ID}
assert sorted(result.warnings) == [
"BTC is not quoted by CBR — skipped (crypto or metal?)",
"XAU is not quoted by CBR — skipped (crypto or metal?)",
]
assert result.counts["currencies"] == 1
assert result.counts["rates"] == 3
assert result.counts["currencies"] == 2
assert result.counts["rates"] == 6
async def test_range_starts_before_the_first_transaction(app, mock_http, fixture_bytes, run_sync):
@@ -129,15 +136,43 @@ async def test_cursor_shortens_the_range_and_reruns_are_idempotent(
assert params["date_req1"] == expected_start.strftime("%d/%m/%Y")
async def test_no_foreign_currency_skips_the_network(app, mock_http, fixture_bytes, run_sync):
async def test_the_dollar_and_the_yuan_are_fetched_even_with_only_roubles(
app, mock_http, fixture_bytes, run_sync
):
"""A currency that shows up in ZenMoney between two runs must already have its rate."""
await make_account(currency="RUB")
daily, dynamic = mock_cbr(mock_http, fixture_bytes)
result = await run_sync(CbrSource(), settings=settings())
assert not daily.called and not dynamic.called
assert result.changed is False
assert result.counts == {"currencies": 0, "rates": 0}
assert daily.called
assert {params["VAL_NM_RQ"] for params in route_params(dynamic)} == {USD_ID, CNY_ID}
assert result.changed is True
assert result.counts == {"currencies": 2, "rates": 6}
rates = await stored_rates()
assert rates[("CNY", "2026-09-03")] == (1, Decimal("12.7654"))
async def test_a_currency_new_to_the_table_is_backfilled_from_the_start_of_the_ledger(
app, mock_http, fixture_bytes, run_sync
):
"""The cursor only knows what the other currencies have; a new one needs its whole past."""
first = today_local() - timedelta(days=10)
await make_account(currency="USD", source_id="usd")
await make_txn(first, outcome="10", outcome_currency="USD")
_, dynamic = mock_cbr(mock_http, fixture_bytes)
earlier = await run_sync(CbrSource(), settings=settings())
await make_account(currency="JPY", source_id="jpy")
await make_txn(first + timedelta(days=2), outcome="1000", outcome_currency="JPY")
seen = len(dynamic.calls)
await run_sync(CbrSource(), settings=settings(), cursor=earlier.cursor_after)
starts = {p["VAL_NM_RQ"]: p["date_req1"] for p in route_params(dynamic)[seen:]}
incremental = date.fromisoformat(earlier.cursor_after or "") - timedelta(days=3)
assert starts[JPY_ID] == (first - timedelta(days=7)).strftime("%d/%m/%Y")
assert starts[USD_ID] == incremental.strftime("%d/%m/%Y")
assert starts[CNY_ID] == incremental.strftime("%d/%m/%Y")
def test_long_ranges_are_chunked_by_year():