Files
daily_stock_analysis/tests/test_threadless_test_client.py
mumu 3b5ef16144 feat: harden AlphaSift data source routing (#1680)
* feat: harden alphasift data source routing

* fix(review-feedback-1680): Keep the sample AlphaSift pin in sync and Avoid circuit-breaking

* fix(review-feedback-1680): Defer half-open health checks until a source is attempted and Treat

* fix(review-feedback-1680): fix only bypasses this branch for fetchers that opt into allow empty

* fix(review-feedback-1680): Avoid accepting truncated Tencent histories

* fix(review-feedback-1680): 修正 .env.example 的 AlphaSift 快照源默认说明,并避免在 docs/CHANGELOG.md

* fix(review-feedback-1680): src/config.py、src/services/alphasift service.py

* fix(review-feedback-1680): requirements.txt、src/config.py、.env.example、src/services/alphasift

* fix(review-feedback-1680): 修复 TencentFetcher 对显式历史窗口的处理问题

* fix(review-feedback-1680): Check the cap was hit before rejecting Tencent history

* fix(review-feedback-1680): Use Tencent's hyphenated date format and Convert Tencent daily volume

* fix(review-feedback-1680): Accept the executor timeout argument and preserve capped windows with

* fix: limit empty daily data opt-in behavior

* docs: restore AlphaSift 3.22 changelog entry
2026-06-14 18:47:41 +08:00

99 lines
2.9 KiB
Python

# -*- coding: utf-8 -*-
"""Regression tests for the local TestClient compatibility shim."""
import asyncio
from contextvars import ContextVar
import anyio.to_thread
from fastapi import FastAPI, Request, Response
from fastapi.testclient import TestClient as FastAPITestClient
def test_threadless_test_client_preserves_cookies_between_requests() -> None:
app = FastAPI()
@app.post("/login")
def login(response: Response) -> dict[str, bool]:
response.set_cookie("dsa_session", "session-token")
return {"ok": True}
@app.get("/protected")
def protected(request: Request) -> dict[str, str | None]:
return {"session": request.cookies.get("dsa_session")}
client = FastAPITestClient(app)
assert client.post("/login").status_code == 200
assert client.get("/protected").json() == {"session": "session-token"}
def test_threadless_test_client_lifespan_runs_once_per_context() -> None:
app = FastAPI()
lifecycle_calls = {"startup": 0, "shutdown": 0, "requests": 0}
@app.on_event("startup")
async def _on_startup() -> None:
lifecycle_calls["startup"] += 1
@app.on_event("shutdown")
async def _on_shutdown() -> None:
lifecycle_calls["shutdown"] += 1
@app.get("/ping")
def ping() -> dict[str, str]:
lifecycle_calls["requests"] += 1
return {"ok": "pong"}
with FastAPITestClient(app) as client:
assert client.get("/ping").json() == {"ok": "pong"}
assert client.get("/ping").json() == {"ok": "pong"}
assert lifecycle_calls["startup"] == 1
assert lifecycle_calls["shutdown"] == 1
assert lifecycle_calls["requests"] == 2
def test_threadless_test_client_does_not_run_lifespan_without_context() -> None:
app = FastAPI()
lifecycle_calls = {"startup": 0, "shutdown": 0, "requests": 0}
@app.on_event("startup")
async def _on_startup() -> None:
lifecycle_calls["startup"] += 1
@app.on_event("shutdown")
async def _on_shutdown() -> None:
lifecycle_calls["shutdown"] += 1
@app.get("/ping")
def ping() -> dict[str, str]:
lifecycle_calls["requests"] += 1
return {"ok": "pong"}
client = FastAPITestClient(app)
assert client.get("/ping").json() == {"ok": "pong"}
assert client.get("/ping").json() == {"ok": "pong"}
assert lifecycle_calls["startup"] == 0
assert lifecycle_calls["shutdown"] == 0
assert lifecycle_calls["requests"] == 2
def test_anyio_to_thread_shim_preserves_contextvars() -> None:
request_id = ContextVar("request_id", default="")
request_id.set("req-123")
async def read_from_worker() -> str:
return await anyio.to_thread.run_sync(request_id.get)
assert anyio.run(read_from_worker) == "req-123"
def test_shutdown_default_executor_shim_accepts_timeout_argument() -> None:
loop = asyncio.new_event_loop()
try:
loop.run_until_complete(loop.shutdown_default_executor(timeout=0.01))
finally:
loop.close()