Files
exchange-rates/tests/test_api.py
T
JiriUhlir e39f52f027 Dalsi banky, prehled ve Swaggeru, /status a prazdne promenne
- zdroje rb, unicredit a air
- nahore ve Swaggeru prehled nabizenych a nenabizenych bank s duvody
- GET /status s prehledem vypadku stahovani u jednotlivych bank
- prazdna promenna prostredi znamena vychozi hodnotu
- popis stahovani v documentation/stahovani.md
2026-11-04 15:33:21 +01:00

117 lines
3.5 KiB
Python

"""TestClient is used without a context manager, so no background download runs."""
from dataclasses import replace
from datetime import UTC, date, datetime, timedelta
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from app import main
from app.models import Rate, RateSheet
from app.storage import read_sheet, write_sheet
def make_sheet(fetched_at: datetime) -> RateSheet:
return RateSheet(
source="cnb",
source_name="Česká národní banka",
source_url="https://example.test/rates",
fetched_at=fetched_at,
valid_date=date(2026, 11, 4),
rates=[
Rate(
currency="USD",
buy=21.57,
sell=21.57,
middle=21.57,
buy_sell_origin="computed",
middle_origin="fetched",
)
],
)
@pytest.fixture
def client(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> TestClient:
monkeypatch.setattr(main, "settings", replace(main.settings, data_dir=tmp_path))
return TestClient(main.app)
def test_health_answers_without_any_data(client: TestClient) -> None:
response = client.get("/health")
assert response.status_code == 200
assert response.json() == {"status": "ok"}
def test_documentation_lists_offered_and_not_offered_banks(client: TestClient) -> None:
description = client.get("/openapi.json").json()["info"]["description"]
assert "`/cnb`" in description
assert "`/moneta`" in description
assert "Revolut" in description
assert "Česká spořitelna" in description
def test_unknown_source_is_not_found(client: TestClient) -> None:
assert client.get("/nonexistent").status_code == 404
def test_known_source_without_data_is_unavailable(client: TestClient) -> None:
assert client.get("/cnb").status_code == 503
def test_recent_data_is_returned_as_fresh(client: TestClient) -> None:
write_sheet(main.settings.data_dir, make_sheet(datetime.now(UTC)))
response = client.get("/cnb")
assert response.status_code == 200
body = response.json()
assert body["stale"] is False
assert body["rates"] == [
{
"currency": "USD",
"buy": 21.57,
"sell": 21.57,
"middle": 21.57,
"buy_sell_origin": "computed",
"middle_origin": "fetched",
}
]
assert body["valid_date"] == "2026-11-04"
assert body["base"] == "CZK"
def test_base_parameter_converts_the_response(client: TestClient) -> None:
write_sheet(main.settings.data_dir, make_sheet(datetime.now(UTC)))
body = client.get("/cnb", params={"base": "USD"}).json()
assert body["base"] == "USD"
assert [rate["currency"] for rate in body["rates"]] == ["CZK"]
assert body["rates"][0]["middle_origin"] == "computed"
def test_base_currency_missing_in_source_is_not_found(client: TestClient) -> None:
write_sheet(main.settings.data_dir, make_sheet(datetime.now(UTC)))
assert client.get("/cnb", params={"base": "EUR"}).status_code == 404
def test_old_data_is_returned_as_stale(client: TestClient) -> None:
age = timedelta(minutes=main.settings.refresh_interval_minutes * 3 + 1)
write_sheet(main.settings.data_dir, make_sheet(datetime.now(UTC) - age))
assert client.get("/cnb").json()["stale"] is True
def test_storage_round_trip_leaves_no_temp_files(tmp_path: Path) -> None:
sheet = make_sheet(datetime.now(UTC))
write_sheet(tmp_path, sheet)
assert read_sheet(tmp_path, "cnb") == sheet
assert [path.name for path in tmp_path.iterdir()] == ["cnb.json"]