11from pathlib import Path
22
33import pytest
4- from fastapi_cli .discover import get_import_string
4+ from fastapi_cli .discover import get_import_string_and_app
55from fastapi_cli .exceptions import FastAPICLIException
66from pytest import CaptureFixture
77
1212
1313def test_package_app_root (capsys : CaptureFixture [str ]) -> None :
1414 with changing_dir (assets_path ):
15- import_string = get_import_string (path = Path ("package/mod/app.py" ))
15+ import_string = get_import_string_and_app (path = Path ("package/mod/app.py" ))
1616 assert import_string == "package.mod.app:app"
1717
1818 captured = capsys .readouterr ()
@@ -40,7 +40,7 @@ def test_package_app_root(capsys: CaptureFixture[str]) -> None:
4040
4141def test_package_api_root (capsys : CaptureFixture [str ]) -> None :
4242 with changing_dir (assets_path ):
43- import_string = get_import_string (path = Path ("package/mod/api.py" ))
43+ import_string = get_import_string_and_app (path = Path ("package/mod/api.py" ))
4444 assert import_string == "package.mod.api:api"
4545
4646 captured = capsys .readouterr ()
@@ -68,7 +68,7 @@ def test_package_api_root(capsys: CaptureFixture[str]) -> None:
6868
6969def test_package_other_root (capsys : CaptureFixture [str ]) -> None :
7070 with changing_dir (assets_path ):
71- import_string = get_import_string (path = Path ("package/mod/other.py" ))
71+ import_string = get_import_string_and_app (path = Path ("package/mod/other.py" ))
7272 assert import_string == "package.mod.other:first_other"
7373
7474 captured = capsys .readouterr ()
@@ -96,7 +96,7 @@ def test_package_other_root(capsys: CaptureFixture[str]) -> None:
9696
9797def test_package_app_mod (capsys : CaptureFixture [str ]) -> None :
9898 with changing_dir (assets_path / "package/mod" ):
99- import_string = get_import_string (path = Path ("app.py" ))
99+ import_string = get_import_string_and_app (path = Path ("app.py" ))
100100 assert import_string == "package.mod.app:app"
101101
102102 captured = capsys .readouterr ()
@@ -124,7 +124,7 @@ def test_package_app_mod(capsys: CaptureFixture[str]) -> None:
124124
125125def test_package_api_mod (capsys : CaptureFixture [str ]) -> None :
126126 with changing_dir (assets_path / "package/mod" ):
127- import_string = get_import_string (path = Path ("api.py" ))
127+ import_string = get_import_string_and_app (path = Path ("api.py" ))
128128 assert import_string == "package.mod.api:api"
129129
130130 captured = capsys .readouterr ()
@@ -152,7 +152,7 @@ def test_package_api_mod(capsys: CaptureFixture[str]) -> None:
152152
153153def test_package_other_mod (capsys : CaptureFixture [str ]) -> None :
154154 with changing_dir (assets_path / "package/mod" ):
155- import_string = get_import_string (path = Path ("other.py" ))
155+ import_string = get_import_string_and_app (path = Path ("other.py" ))
156156 assert import_string == "package.mod.other:first_other"
157157
158158 captured = capsys .readouterr ()
@@ -180,7 +180,9 @@ def test_package_other_mod(capsys: CaptureFixture[str]) -> None:
180180
181181def test_package_app_above (capsys : CaptureFixture [str ]) -> None :
182182 with changing_dir (assets_path .parent ):
183- import_string = get_import_string (path = Path ("assets/package/mod/app.py" ))
183+ import_string = get_import_string_and_app (
184+ path = Path ("assets/package/mod/app.py" )
185+ )
184186 assert import_string == "package.mod.app:app"
185187
186188 captured = capsys .readouterr ()
@@ -208,7 +210,9 @@ def test_package_app_above(capsys: CaptureFixture[str]) -> None:
208210
209211def test_package_api_parent (capsys : CaptureFixture [str ]) -> None :
210212 with changing_dir (assets_path .parent ):
211- import_string = get_import_string (path = Path ("assets/package/mod/api.py" ))
213+ import_string = get_import_string_and_app (
214+ path = Path ("assets/package/mod/api.py" )
215+ )
212216 assert import_string == "package.mod.api:api"
213217
214218 captured = capsys .readouterr ()
@@ -236,7 +240,9 @@ def test_package_api_parent(capsys: CaptureFixture[str]) -> None:
236240
237241def test_package_other_parent (capsys : CaptureFixture [str ]) -> None :
238242 with changing_dir (assets_path .parent ):
239- import_string = get_import_string (path = Path ("assets/package/mod/other.py" ))
243+ import_string = get_import_string_and_app (
244+ path = Path ("assets/package/mod/other.py" )
245+ )
240246 assert import_string == "package.mod.other:first_other"
241247
242248 captured = capsys .readouterr ()
@@ -264,7 +270,7 @@ def test_package_other_parent(capsys: CaptureFixture[str]) -> None:
264270
265271def test_package_mod_init_inside (capsys : CaptureFixture [str ]) -> None :
266272 with changing_dir (assets_path / "package/mod" ):
267- import_string = get_import_string (path = Path ("__init__.py" ))
273+ import_string = get_import_string_and_app (path = Path ("__init__.py" ))
268274 assert import_string == "package.mod:app"
269275
270276 captured = capsys .readouterr ()
@@ -291,7 +297,7 @@ def test_package_mod_init_inside(capsys: CaptureFixture[str]) -> None:
291297
292298def test_package_mod_dir (capsys : CaptureFixture [str ]) -> None :
293299 with changing_dir (assets_path ):
294- import_string = get_import_string (path = Path ("package/mod" ))
300+ import_string = get_import_string_and_app (path = Path ("package/mod" ))
295301 assert import_string == "package.mod:app"
296302
297303 captured = capsys .readouterr ()
@@ -318,7 +324,7 @@ def test_package_mod_dir(capsys: CaptureFixture[str]) -> None:
318324
319325def test_package_init_inside (capsys : CaptureFixture [str ]) -> None :
320326 with changing_dir (assets_path / "package" ):
321- import_string = get_import_string (path = Path ("__init__.py" ))
327+ import_string = get_import_string_and_app (path = Path ("__init__.py" ))
322328 assert import_string == "package:app"
323329
324330 captured = capsys .readouterr ()
@@ -343,7 +349,7 @@ def test_package_init_inside(capsys: CaptureFixture[str]) -> None:
343349
344350def test_package_dir_inside_package (capsys : CaptureFixture [str ]) -> None :
345351 with changing_dir (assets_path / "package/mod" ):
346- import_string = get_import_string (path = Path ("../" ))
352+ import_string = get_import_string_and_app (path = Path ("../" ))
347353 assert import_string == "package:app"
348354
349355 captured = capsys .readouterr ()
@@ -368,7 +374,7 @@ def test_package_dir_inside_package(capsys: CaptureFixture[str]) -> None:
368374
369375def test_package_dir_above_package (capsys : CaptureFixture [str ]) -> None :
370376 with changing_dir (assets_path .parent ):
371- import_string = get_import_string (path = Path ("assets/package" ))
377+ import_string = get_import_string_and_app (path = Path ("assets/package" ))
372378 assert import_string == "package:app"
373379
374380 captured = capsys .readouterr ()
@@ -393,7 +399,7 @@ def test_package_dir_above_package(capsys: CaptureFixture[str]) -> None:
393399
394400def test_package_dir_explicit_app (capsys : CaptureFixture [str ]) -> None :
395401 with changing_dir (assets_path ):
396- import_string = get_import_string (path = Path ("package" ), app_name = "api" )
402+ import_string = get_import_string_and_app (path = Path ("package" ), app_name = "api" )
397403 assert import_string == "package:api"
398404
399405 captured = capsys .readouterr ()
@@ -420,7 +426,7 @@ def test_broken_package_dir(capsys: CaptureFixture[str]) -> None:
420426 with changing_dir (assets_path ):
421427 # TODO (when deprecating Python 3.8): remove ValueError
422428 with pytest .raises ((ImportError , ValueError )):
423- get_import_string (path = Path ("broken_package/mod/app.py" ))
429+ get_import_string_and_app (path = Path ("broken_package/mod/app.py" ))
424430
425431 captured = capsys .readouterr ()
426432 assert "Import error:" in captured .out
@@ -430,7 +436,7 @@ def test_broken_package_dir(capsys: CaptureFixture[str]) -> None:
430436def test_package_dir_no_app () -> None :
431437 with changing_dir (assets_path ):
432438 with pytest .raises (FastAPICLIException ) as e :
433- get_import_string (path = Path ("package/core/utils.py" ))
439+ get_import_string_and_app (path = Path ("package/core/utils.py" ))
434440 assert (
435441 "Could not find FastAPI app in module, try using --app" in e .value .args [0 ]
436442 )
@@ -439,7 +445,7 @@ def test_package_dir_no_app() -> None:
439445def test_package_dir_object_not_an_app () -> None :
440446 with changing_dir (assets_path ):
441447 with pytest .raises (FastAPICLIException ) as e :
442- get_import_string (
448+ get_import_string_and_app (
443449 path = Path ("package/core/utils.py" ), app_name = "get_hello_world"
444450 )
445451 assert (
@@ -451,5 +457,5 @@ def test_package_dir_object_not_an_app() -> None:
451457def test_package_dir_object_app_name_not_found () -> None :
452458 with changing_dir (assets_path ):
453459 with pytest .raises (FastAPICLIException ) as e :
454- get_import_string (path = Path ("package" ), app_name = "non_existent_app" )
460+ get_import_string_and_app (path = Path ("package" ), app_name = "non_existent_app" )
455461 assert "Could not find app name non_existent_app in package" in e .value .args [0 ]
0 commit comments