Coverage for tests/test_utils_single_file.py: 100%

36 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-11 09:18 +0000

1from pathlib import Path 1abcdefghijklmn

2 

3import pytest 1abcdefghijklmn

4from pytest import CaptureFixture 1abcdefghijklmn

5 

6from fastapi_cli.discover import get_import_data 1abcdefghijklmn

7from fastapi_cli.exceptions import FastAPICLIException 1abcdefghijklmn

8 

9from .utils import changing_dir 1abcdefghijklmn

10 

11assets_path = Path(__file__).parent / "assets" 1abcdefghijklmn

12 

13 

14def test_single_file_app(capsys: CaptureFixture[str]) -> None: 1abcdefghijklmn

15 with changing_dir(assets_path): 1opqrstuvwxyzAB

16 import_data = get_import_data(path=Path("single_file_app.py")) 1opqrstuvwxyzAB

17 

18 assert import_data.import_string == "single_file_app:app" 1opqrstuvwxyzAB

19 

20 assert import_data.module_data.extra_sys_path == assets_path 1opqrstuvwxyzAB

21 assert import_data.module_data.module_import_str == "single_file_app" 1opqrstuvwxyzAB

22 

23 

24def test_single_file_api(capsys: CaptureFixture[str]) -> None: 1abcdefghijklmn

25 with changing_dir(assets_path): 1CDEFGHIJKLMNOP

26 import_data = get_import_data(path=Path("single_file_api.py")) 1CDEFGHIJKLMNOP

27 

28 assert import_data.import_string == "single_file_api:api" 1CDEFGHIJKLMNOP

29 

30 assert import_data.module_data.extra_sys_path == assets_path 1CDEFGHIJKLMNOP

31 assert import_data.module_data.module_import_str == "single_file_api" 1CDEFGHIJKLMNOP

32 

33 

34def test_single_file_other(capsys: CaptureFixture[str]) -> None: 1abcdefghijklmn

35 with changing_dir(assets_path): 1QRSTUVWXYZ0123

36 import_data = get_import_data(path=Path("single_file_other.py")) 1QRSTUVWXYZ0123

37 

38 assert import_data.import_string == "single_file_other:first_other" 1QRSTUVWXYZ0123

39 

40 assert import_data.module_data.extra_sys_path == assets_path 1QRSTUVWXYZ0123

41 assert import_data.module_data.module_import_str == "single_file_other" 1QRSTUVWXYZ0123

42 

43 

44def test_single_file_explicit_object(capsys: CaptureFixture[str]) -> None: 1abcdefghijklmn

45 with changing_dir(assets_path): 1456789!#$%'()*

46 import_data = get_import_data( 1456789!#$%'()*

47 path=Path("single_file_app.py"), app_name="second_other" 

48 ) 

49 

50 assert import_data.import_string == "single_file_app:second_other" 1456789!#$%'()*

51 

52 assert import_data.module_data.extra_sys_path == assets_path 1456789!#$%'()*

53 assert import_data.module_data.module_import_str == "single_file_app" 1456789!#$%'()*

54 

55 

56def test_single_non_existing_file() -> None: 1abcdefghijklmn

57 with changing_dir(assets_path): 1+,-./:;=?@[]^_

58 with pytest.raises(FastAPICLIException) as e: 1+,-./:;=?@[]^_

59 get_import_data(path=assets_path / "non_existing.py") 1+,-./:;=?@[]^_

60 assert "Path does not exist" in e.value.args[0] 1+,-./:;=?@[]^_