Coverage for tests/test_utils_single_file.py: 100%

36 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-12-05 11:33 +0000

1from pathlib import Path 1abcdefghijkl

2 

3import pytest 1abcdefghijkl

4from pytest import CaptureFixture 1abcdefghijkl

5 

6from fastapi_cli.discover import get_import_data 1abcdefghijkl

7from fastapi_cli.exceptions import FastAPICLIException 1abcdefghijkl

8 

9from .utils import changing_dir 1abcdefghijkl

10 

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

12 

13 

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

15 with changing_dir(assets_path): 1mnopqrstuvwx

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

17 

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

19 

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

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

22 

23 

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

25 with changing_dir(assets_path): 1yzABCDEFGHIJ

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

27 

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

29 

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

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

32 

33 

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

35 with changing_dir(assets_path): 1KLMNOPQRSTUV

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

37 

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

39 

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

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

42 

43 

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

45 with changing_dir(assets_path): 1WXYZ01234567

46 import_data = get_import_data( 1WXYZ01234567

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

48 ) 

49 

50 assert import_data.import_string == "single_file_app:second_other" 1WXYZ01234567

51 

52 assert import_data.module_data.extra_sys_path == assets_path 1WXYZ01234567

53 assert import_data.module_data.module_import_str == "single_file_app" 1WXYZ01234567

54 

55 

56def test_single_non_existing_file() -> None: 1abcdefghijkl

57 with changing_dir(assets_path): 189!#$%'()*+,

58 with pytest.raises(FastAPICLIException) as e: 189!#$%'()*+,

59 get_import_data(path=assets_path / "non_existing.py") 189!#$%'()*+,

60 assert "Path does not exist" in e.value.args[0] 189!#$%'()*+,