|
|
|
@ -12,8 +12,10 @@ def test_optional_imports_broken_installation(monkeypatch: pytest.MonkeyPatch) - |
|
|
|
real_import_module = importlib.import_module |
|
|
|
|
|
|
|
def fake_import_module(name: str, package: str | None = None) -> Any: |
|
|
|
if name in ("ujson", "orjson"): |
|
|
|
raise ImportError(f"simulated binary/load failure for {name}") |
|
|
|
if name == "ujson": |
|
|
|
raise ImportError("simulated binary/load failure for ujson") # pragma: no cover |
|
|
|
if name == "orjson": |
|
|
|
raise ImportError("simulated binary/load failure for orjson") # pragma: no cover |
|
|
|
return real_import_module(name, package) |
|
|
|
|
|
|
|
monkeypatch.setattr(importlib, "import_module", fake_import_module) |
|
|
|
@ -23,6 +25,9 @@ def test_optional_imports_broken_installation(monkeypatch: pytest.MonkeyPatch) - |
|
|
|
# Force a reload to ensure the module initialization runs with our monkeypatch |
|
|
|
try: |
|
|
|
importlib.reload(fastapi.responses) |
|
|
|
# Verify that the fallback worked and they are now None |
|
|
|
assert fastapi.responses.ujson is None |
|
|
|
assert fastapi.responses.orjson is None |
|
|
|
finally: |
|
|
|
# Revert the monkeypatch manually early so we can restore the module |
|
|
|
monkeypatch.undo() |
|
|
|
|