Coverage for tests/test_discover.py: 100%

41 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

4 

5from fastapi_cli.discover import ( 1abcdefghijkl

6 ImportData, 

7 get_import_data_from_import_string, 

8) 

9from fastapi_cli.exceptions import FastAPICLIException 1abcdefghijkl

10 

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

12 

13 

14def test_get_import_data_from_import_string_valid() -> None: 1abcdefghijkl

15 result = get_import_data_from_import_string("module.submodule:app", False) 1mnopqrstuvwx

16 

17 assert isinstance(result, ImportData) 1mnopqrstuvwx

18 assert result.app_name == "app" 1mnopqrstuvwx

19 assert result.import_string == "module.submodule:app" 1mnopqrstuvwx

20 assert result.module_data.module_import_str == "module.submodule" 1mnopqrstuvwx

21 assert result.module_data.extra_sys_path == Path(".").resolve() 1mnopqrstuvwx

22 assert result.module_data.module_paths == [] 1mnopqrstuvwx

23 assert result.module_config_source == "entrypoint-option" 1mnopqrstuvwx

24 assert result.app_name_config_source == "entrypoint-option" 1mnopqrstuvwx

25 

26 

27def test_get_import_data_from_import_string_pyproject_valid() -> None: 1abcdefghijkl

28 result = get_import_data_from_import_string("module.submodule:app", True) 1yzABCDEFGHIJ

29 

30 assert isinstance(result, ImportData) 1yzABCDEFGHIJ

31 assert result.app_name == "app" 1yzABCDEFGHIJ

32 assert result.import_string == "module.submodule:app" 1yzABCDEFGHIJ

33 assert result.module_data.module_import_str == "module.submodule" 1yzABCDEFGHIJ

34 assert result.module_data.extra_sys_path == Path(".").resolve() 1yzABCDEFGHIJ

35 assert result.module_data.module_paths == [] 1yzABCDEFGHIJ

36 assert result.module_config_source == "entrypoint-pyproject" 1yzABCDEFGHIJ

37 assert result.app_name_config_source == "entrypoint-pyproject" 1yzABCDEFGHIJ

38 

39 

40def test_get_import_data_from_import_string_missing_colon() -> None: 1abcdefghijkl

41 with pytest.raises(FastAPICLIException) as exc_info: 1KLMNOPQRSTUV

42 get_import_data_from_import_string("module.submodule", False) 1KLMNOPQRSTUV

43 

44 assert "Import string must be in the format module.submodule:app_name" in str( 1KLMNOPQRSTUV

45 exc_info.value 

46 ) 

47 

48 

49def test_get_import_data_from_import_string_missing_app() -> None: 1abcdefghijkl

50 with pytest.raises(FastAPICLIException) as exc_info: 1WXYZ01234567

51 get_import_data_from_import_string("module.submodule:", False) 1WXYZ01234567

52 

53 assert "Import string must be in the format module.submodule:app_name" in str( 1WXYZ01234567

54 exc_info.value 

55 ) 

56 

57 

58def test_get_import_data_from_import_string_missing_module() -> None: 1abcdefghijkl

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

60 get_import_data_from_import_string(":app", False) 189!#$%'()*+,

61 

62 assert "Import string must be in the format module.submodule:app_name" in str( 189!#$%'()*+,

63 exc_info.value 

64 ) 

65 

66 

67def test_get_import_data_from_import_string_empty() -> None: 1abcdefghijkl

68 with pytest.raises(FastAPICLIException) as exc_info: 1-./:;=?@[]^_

69 get_import_data_from_import_string("", False) 1-./:;=?@[]^_

70 assert "Import string must be in the format module.submodule:app_name" in str( 1-./:;=?@[]^_

71 exc_info.value 

72 )