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-01-13 13:38 +0000

1import importlib 1abcde

2import os 1abcde

3from pathlib import Path 1abcde

4 

5import pytest 1abcde

6from fastapi.testclient import TestClient 1abcde

7 

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

9 

10 

11@pytest.fixture( 1abcde

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): 1abcde

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

23 

24 client = TestClient(mod.app) 1abcde

25 return client 1abcde

26 

27 

28def test(client: TestClient): 1abcde

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

30 if log.is_file(): 1fghij

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

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

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

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

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

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