1
1
from pathlib import Path
2
2
3
3
import pytest
4
- from fastapi_cli .discover import get_import_string
4
+ from fastapi_cli .discover import get_import_string_and_app
5
5
from fastapi_cli .exceptions import FastAPICLIException
6
6
from pytest import CaptureFixture
7
7
12
12
13
13
def test_package_app_root (capsys : CaptureFixture [str ]) -> None :
14
14
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" ))
16
16
assert import_string == "package.mod.app:app"
17
17
18
18
captured = capsys .readouterr ()
@@ -40,7 +40,7 @@ def test_package_app_root(capsys: CaptureFixture[str]) -> None:
40
40
41
41
def test_package_api_root (capsys : CaptureFixture [str ]) -> None :
42
42
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" ))
44
44
assert import_string == "package.mod.api:api"
45
45
46
46
captured = capsys .readouterr ()
@@ -68,7 +68,7 @@ def test_package_api_root(capsys: CaptureFixture[str]) -> None:
68
68
69
69
def test_package_other_root (capsys : CaptureFixture [str ]) -> None :
70
70
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" ))
72
72
assert import_string == "package.mod.other:first_other"
73
73
74
74
captured = capsys .readouterr ()
@@ -96,7 +96,7 @@ def test_package_other_root(capsys: CaptureFixture[str]) -> None:
96
96
97
97
def test_package_app_mod (capsys : CaptureFixture [str ]) -> None :
98
98
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" ))
100
100
assert import_string == "package.mod.app:app"
101
101
102
102
captured = capsys .readouterr ()
@@ -124,7 +124,7 @@ def test_package_app_mod(capsys: CaptureFixture[str]) -> None:
124
124
125
125
def test_package_api_mod (capsys : CaptureFixture [str ]) -> None :
126
126
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" ))
128
128
assert import_string == "package.mod.api:api"
129
129
130
130
captured = capsys .readouterr ()
@@ -152,7 +152,7 @@ def test_package_api_mod(capsys: CaptureFixture[str]) -> None:
152
152
153
153
def test_package_other_mod (capsys : CaptureFixture [str ]) -> None :
154
154
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" ))
156
156
assert import_string == "package.mod.other:first_other"
157
157
158
158
captured = capsys .readouterr ()
@@ -180,7 +180,9 @@ def test_package_other_mod(capsys: CaptureFixture[str]) -> None:
180
180
181
181
def test_package_app_above (capsys : CaptureFixture [str ]) -> None :
182
182
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
+ )
184
186
assert import_string == "package.mod.app:app"
185
187
186
188
captured = capsys .readouterr ()
@@ -208,7 +210,9 @@ def test_package_app_above(capsys: CaptureFixture[str]) -> None:
208
210
209
211
def test_package_api_parent (capsys : CaptureFixture [str ]) -> None :
210
212
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
+ )
212
216
assert import_string == "package.mod.api:api"
213
217
214
218
captured = capsys .readouterr ()
@@ -236,7 +240,9 @@ def test_package_api_parent(capsys: CaptureFixture[str]) -> None:
236
240
237
241
def test_package_other_parent (capsys : CaptureFixture [str ]) -> None :
238
242
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
+ )
240
246
assert import_string == "package.mod.other:first_other"
241
247
242
248
captured = capsys .readouterr ()
@@ -264,7 +270,7 @@ def test_package_other_parent(capsys: CaptureFixture[str]) -> None:
264
270
265
271
def test_package_mod_init_inside (capsys : CaptureFixture [str ]) -> None :
266
272
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" ))
268
274
assert import_string == "package.mod:app"
269
275
270
276
captured = capsys .readouterr ()
@@ -291,7 +297,7 @@ def test_package_mod_init_inside(capsys: CaptureFixture[str]) -> None:
291
297
292
298
def test_package_mod_dir (capsys : CaptureFixture [str ]) -> None :
293
299
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" ))
295
301
assert import_string == "package.mod:app"
296
302
297
303
captured = capsys .readouterr ()
@@ -318,7 +324,7 @@ def test_package_mod_dir(capsys: CaptureFixture[str]) -> None:
318
324
319
325
def test_package_init_inside (capsys : CaptureFixture [str ]) -> None :
320
326
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" ))
322
328
assert import_string == "package:app"
323
329
324
330
captured = capsys .readouterr ()
@@ -343,7 +349,7 @@ def test_package_init_inside(capsys: CaptureFixture[str]) -> None:
343
349
344
350
def test_package_dir_inside_package (capsys : CaptureFixture [str ]) -> None :
345
351
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 ("../" ))
347
353
assert import_string == "package:app"
348
354
349
355
captured = capsys .readouterr ()
@@ -368,7 +374,7 @@ def test_package_dir_inside_package(capsys: CaptureFixture[str]) -> None:
368
374
369
375
def test_package_dir_above_package (capsys : CaptureFixture [str ]) -> None :
370
376
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" ))
372
378
assert import_string == "package:app"
373
379
374
380
captured = capsys .readouterr ()
@@ -393,7 +399,7 @@ def test_package_dir_above_package(capsys: CaptureFixture[str]) -> None:
393
399
394
400
def test_package_dir_explicit_app (capsys : CaptureFixture [str ]) -> None :
395
401
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" )
397
403
assert import_string == "package:api"
398
404
399
405
captured = capsys .readouterr ()
@@ -420,7 +426,7 @@ def test_broken_package_dir(capsys: CaptureFixture[str]) -> None:
420
426
with changing_dir (assets_path ):
421
427
# TODO (when deprecating Python 3.8): remove ValueError
422
428
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" ))
424
430
425
431
captured = capsys .readouterr ()
426
432
assert "Import error:" in captured .out
@@ -430,7 +436,7 @@ def test_broken_package_dir(capsys: CaptureFixture[str]) -> None:
430
436
def test_package_dir_no_app () -> None :
431
437
with changing_dir (assets_path ):
432
438
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" ))
434
440
assert (
435
441
"Could not find FastAPI app in module, try using --app" in e .value .args [0 ]
436
442
)
@@ -439,7 +445,7 @@ def test_package_dir_no_app() -> None:
439
445
def test_package_dir_object_not_an_app () -> None :
440
446
with changing_dir (assets_path ):
441
447
with pytest .raises (FastAPICLIException ) as e :
442
- get_import_string (
448
+ get_import_string_and_app (
443
449
path = Path ("package/core/utils.py" ), app_name = "get_hello_world"
444
450
)
445
451
assert (
@@ -451,5 +457,5 @@ def test_package_dir_object_not_an_app() -> None:
451
457
def test_package_dir_object_app_name_not_found () -> None :
452
458
with changing_dir (assets_path ):
453
459
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" )
455
461
assert "Could not find app name non_existent_app in package" in e .value .args [0 ]
0 commit comments