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

19 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1import importlib 1abcdefg

2import os 1abcdefg

3from pathlib import Path 1abcdefg

4 

5import pytest 1abcdefg

6from fastapi.testclient import TestClient 1abcdefg

7 

8from ...utils import needs_py39, needs_py310 1abcdefg

9 

10 

11@pytest.fixture( 1abcdefg

12 name="client", 

13 params=[ 

14 "tutorial002", 

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

16 "tutorial002_an", 

17 pytest.param("tutorial002_an_py39", marks=needs_py39), 

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

19 ], 

20) 

21def get_client(request: pytest.FixtureRequest): 1abcdefg

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

23 

24 client = TestClient(mod.app) 1abcdefg

25 return client 1abcdefg

26 

27 

28def test(client: TestClient): 1abcdefg

29 log = Path("log.txt") 1hijklmn

30 if log.is_file(): 1hijklmn

31 os.remove(log) # pragma: no cover 1hijklmn

32 response = client.post("/send-notification/foo@example.com?q=some-query") 1hijklmn

33 assert response.status_code == 200, response.text 1hijklmn

34 assert response.json() == {"message": "Message sent"} 1hijklmn

35 with open("./log.txt") as f: 1hijklmn

36 assert "found query: some-query\nmessage to foo@example.com" in f.read() 1hijklmn