Skip to content

Commit b35e0e3

Browse files
committed
added tests for the routers import
1 parent 1e17ca4 commit b35e0e3

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

tests/test_routers_import.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import builtins
2+
import importlib
3+
4+
import pytest
5+
6+
7+
@pytest.fixture(autouse=True)
8+
def restore_sys_modules(monkeypatch):
9+
builtins.__import__
10+
yield
11+
monkeypatch.undo()
12+
13+
14+
def test_all_missing(monkeypatch):
15+
modules_to_fail = {
16+
"fastopenapi.routers.falcon",
17+
"fastopenapi.routers.flask",
18+
"fastopenapi.routers.quart",
19+
"fastopenapi.routers.sanic",
20+
"fastopenapi.routers.starlette",
21+
}
22+
orig_import = builtins.__import__
23+
24+
def fake_import(name, globals=None, locals=None, fromlist=(), level=0):
25+
if name in modules_to_fail:
26+
raise ModuleNotFoundError(f"No module named '{name}'")
27+
return orig_import(name, globals, locals, fromlist, level)
28+
29+
monkeypatch.setattr(builtins, "__import__", fake_import)
30+
import fastopenapi.routers as routers
31+
32+
importlib.reload(routers)
33+
from fastopenapi.routers import MissingRouter
34+
35+
assert routers.FalconRouter is MissingRouter
36+
assert routers.FlaskRouter is MissingRouter
37+
assert routers.QuartRouter is MissingRouter
38+
assert routers.SanicRouter is MissingRouter
39+
assert routers.StarletteRouter is MissingRouter
40+
41+
with pytest.raises(ImportError, match="This framework is not installed."):
42+
routers.FalconRouter()
43+
44+
45+
def test_all_variable():
46+
import fastopenapi.routers as routers
47+
48+
expected = [
49+
"FalconRouter",
50+
"FlaskRouter",
51+
"QuartRouter",
52+
"SanicRouter",
53+
"StarletteRouter",
54+
]
55+
assert routers.__all__ == expected

0 commit comments

Comments
 (0)