Files
o2o-weather/backend/tests/test_resilience.py
T

49 lines
2.1 KiB
Python

import pytest
from httpx import ASGITransport, AsyncClient
import services.air_service as air_service
import services.season_service as season_service
import services.sky_service as sky_service
import services.warning_service as warning_service
import services.weather_service as weather_service
import services.week_service as week_service
from router.router import app
from services.external.data_go_kr import DataGoKrRequestFailed
from services.external.open_meteo import OpenMeteoRequestFailed
REGION = "1114000000"
ENDPOINTS = ["/v1/feed", "/v1/weather/today", "/v1/weather/now", "/v1/sky", "/v1/week", "/v1/index/list", "/v1/season/week"]
SOURCES = {
"vilage": (weather_service, "fetch_vilage", DataGoKrRequestFailed),
"ultra": (weather_service, "fetch_ultra", DataGoKrRequestFailed),
"air": (air_service, "fetch_realtime", DataGoKrRequestFailed),
"warning": (warning_service, "fetch_warning_events", DataGoKrRequestFailed),
"clouds": (sky_service, "fetch_clouds", OpenMeteoRequestFailed),
"mid": (weather_service, "fetch_mid", DataGoKrRequestFailed),
"holiday": (week_service, "fetch_holidays", DataGoKrRequestFailed),
"festival": (week_service, "fetch_festivals", DataGoKrRequestFailed),
"solar_term": (season_service, "fetch_solar_terms", DataGoKrRequestFailed),
}
def _break(monkeypatch, source: str, mode: str) -> None:
module, name, exc = SOURCES[source]
async def fail(*_a, **_k):
raise exc(f"{source} down")
async def empty(*_a, **_k):
return ({}, {}) if source == "mid" else []
monkeypatch.setattr(module, name, {"fail": fail, "empty": empty}[mode])
@pytest.mark.anyio
@pytest.mark.parametrize("mode", ["fail", "empty"])
@pytest.mark.parametrize("source", list(SOURCES))
async def test_every_screen_survives_one_broken_source(offline, monkeypatch, source, mode):
_break(monkeypatch, source, mode)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
codes = {path: (await client.get(path, params={"region": REGION})).status_code for path in ENDPOINTS}
assert all(c == 200 for c in codes.values()), codes