Simplify tests for background_tasks (#13166)

This commit is contained in:
Alejandra 2025-01-08 19:23:42 +00:00 committed by GitHub
parent 144f09ea14
commit 44adb29ce1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 21 additions and 86 deletions

View File

@ -1,14 +1,31 @@
import importlib
import os
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from docs_src.background_tasks.tutorial002 import app
client = TestClient(app)
from ...utils import needs_py39, needs_py310
def test():
@pytest.fixture(
name="client",
params=[
"tutorial002",
pytest.param("tutorial002_py310", marks=needs_py310),
"tutorial002_an",
pytest.param("tutorial002_an_py39", marks=needs_py39),
pytest.param("tutorial002_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.background_tasks.{request.param}")
client = TestClient(mod.app)
return client
def test(client: TestClient):
log = Path("log.txt")
if log.is_file():
os.remove(log) # pragma: no cover

View File

@ -1,19 +0,0 @@
import os
from pathlib import Path
from fastapi.testclient import TestClient
from docs_src.background_tasks.tutorial002_an import app
client = TestClient(app)
def test():
log = Path("log.txt")
if log.is_file():
os.remove(log) # pragma: no cover
response = client.post("/send-notification/foo@example.com?q=some-query")
assert response.status_code == 200, response.text
assert response.json() == {"message": "Message sent"}
with open("./log.txt") as f:
assert "found query: some-query\nmessage to foo@example.com" in f.read()

View File

@ -1,21 +0,0 @@
import os
from pathlib import Path
from fastapi.testclient import TestClient
from ...utils import needs_py310
@needs_py310
def test():
from docs_src.background_tasks.tutorial002_an_py310 import app
client = TestClient(app)
log = Path("log.txt")
if log.is_file():
os.remove(log) # pragma: no cover
response = client.post("/send-notification/foo@example.com?q=some-query")
assert response.status_code == 200, response.text
assert response.json() == {"message": "Message sent"}
with open("./log.txt") as f:
assert "found query: some-query\nmessage to foo@example.com" in f.read()

View File

@ -1,21 +0,0 @@
import os
from pathlib import Path
from fastapi.testclient import TestClient
from ...utils import needs_py39
@needs_py39
def test():
from docs_src.background_tasks.tutorial002_an_py39 import app
client = TestClient(app)
log = Path("log.txt")
if log.is_file():
os.remove(log) # pragma: no cover
response = client.post("/send-notification/foo@example.com?q=some-query")
assert response.status_code == 200, response.text
assert response.json() == {"message": "Message sent"}
with open("./log.txt") as f:
assert "found query: some-query\nmessage to foo@example.com" in f.read()

View File

@ -1,21 +0,0 @@
import os
from pathlib import Path
from fastapi.testclient import TestClient
from ...utils import needs_py310
@needs_py310
def test():
from docs_src.background_tasks.tutorial002_py310 import app
client = TestClient(app)
log = Path("log.txt")
if log.is_file():
os.remove(log) # pragma: no cover
response = client.post("/send-notification/foo@example.com?q=some-query")
assert response.status_code == 200, response.text
assert response.json() == {"message": "Message sent"}
with open("./log.txt") as f:
assert "found query: some-query\nmessage to foo@example.com" in f.read()