Coverage for tests / test_tutorial / test_background_tasks / test_tutorial002.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1import importlib 1abcd

2import os 1abcd

3from pathlib import Path 1abcd

4 

5import pytest 1abcd

6from fastapi.testclient import TestClient 1abcd

7 

8from tests.utils import needs_py310, workdir_lock 1abcd

9 

10 

11@pytest.fixture( 1abcd

12 name="client", 

13 params=[ 

14 pytest.param("tutorial002_py310", marks=needs_py310), 

15 pytest.param("tutorial002_an_py310", marks=needs_py310), 

16 ], 

17) 

18def get_client(request: pytest.FixtureRequest): 1abcd

19 mod = importlib.import_module(f"docs_src.background_tasks.{request.param}") 1lmnopqr

20 

21 client = TestClient(mod.app) 1lmnopqr

22 return client 1lmnopqr

23 

24 

25@workdir_lock 1abcd

26def test(client: TestClient): 1abcd

27 log = Path("log.txt") 1efghijk

28 if log.is_file(): 1efghijk

29 os.remove(log) # pragma: no cover 1efghijk

30 response = client.post("/send-notification/[email protected]?q=some-query") 1efghijk

31 assert response.status_code == 200, response.text 1efghijk

32 assert response.json() == {"message": "Message sent"} 1efghijk

33 with open("./log.txt") as f: 1efghijk

34 assert "found query: some-query\nmessage to [email protected]" in f.read() 1efghijk