|
| 1 | +import contextlib |
| 2 | +import json |
| 3 | +import sys |
| 4 | +import unittest.mock |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import bokeh |
| 8 | +import pytest |
| 9 | +from bokeh.server.tornado import BokehTornado |
| 10 | +from bokeh_fastapi import BokehFastAPI |
| 11 | + |
| 12 | + |
| 13 | +def cache_path() -> Path: |
| 14 | + folder = Path(__file__).parents[3] / ".bokeh_fastapi_cache" |
| 15 | + folder.mkdir(parents=True, exist_ok=True) |
| 16 | + name = f"python-{sys.version_info[0]}.{sys.version_info[1]}-bokeh-{bokeh.__version__}.json" |
| 17 | + return folder / name |
| 18 | + |
| 19 | + |
| 20 | +TESTS = [] |
| 21 | +PATCHES = {} |
| 22 | + |
| 23 | + |
| 24 | +def update_required_patches(modules): |
| 25 | + global PATCHES |
| 26 | + for module in modules: |
| 27 | + if module in PATCHES: |
| 28 | + continue |
| 29 | + |
| 30 | + for name, obj in module.__dict__.items(): |
| 31 | + if isinstance(obj, type) and issubclass(obj, BokehTornado): |
| 32 | + PATCHES[module.__name__] = name |
| 33 | + break |
| 34 | + |
| 35 | + |
| 36 | +class BokehFastAPICompat(BokehFastAPI): |
| 37 | + def __init__(self, *args, **kwargs): |
| 38 | + kwargs["websocket_origins"] = kwargs.pop("extra_websocket_origins") |
| 39 | + kwargs.pop("absolute_url", None) |
| 40 | + kwargs.pop("index", None) |
| 41 | + kwargs.pop("websocket_max_message_size_bytes", None) |
| 42 | + kwargs.pop("extra_patterns", None) |
| 43 | + super().__init__(*args, **kwargs) |
| 44 | + |
| 45 | + def initialize(self, *args, **kwargs): |
| 46 | + pass |
| 47 | + |
| 48 | + def start(self, *args, **kwargs): |
| 49 | + pass |
| 50 | + |
| 51 | + def __call__(self, *args, **kwargs): |
| 52 | + pass |
| 53 | + |
| 54 | + |
| 55 | +@pytest.hookimpl(wrapper=True) |
| 56 | +def pytest_collection_modifyitems(config, items): |
| 57 | + path = cache_path() |
| 58 | + if path.exists(): |
| 59 | + with open(cache_path()) as file: |
| 60 | + cache = json.load(file) |
| 61 | + |
| 62 | + tests = set(cache["tests"]) |
| 63 | + select = [] |
| 64 | + deselect = [] |
| 65 | + for item in items: |
| 66 | + (select if item.nodeid in tests else deselect).append(item) |
| 67 | + items[:] = select |
| 68 | + config.hook.pytest_deselected(items=deselect) |
| 69 | + |
| 70 | + for module_name, obj_name in cache["patches"].items(): |
| 71 | + unittest.mock.patch( |
| 72 | + f"{module_name}.{obj_name}", new=BokehFastAPICompat |
| 73 | + ).start() |
| 74 | + else: |
| 75 | + update_required_patches({item.module for item in items}) |
| 76 | + |
| 77 | + return (yield) |
| 78 | + |
| 79 | + |
| 80 | +def pytest_terminal_summary(): |
| 81 | + path = cache_path() |
| 82 | + if not path.exists(): |
| 83 | + with open(path, "w") as file: |
| 84 | + json.dump({"patches": PATCHES, "tests": TESTS}, file, indent=2) |
| 85 | + |
| 86 | + |
| 87 | +@pytest.fixture(autouse=True) |
| 88 | +def detect_bokeh_tornado_usage(request): |
| 89 | + update_required_patches( |
| 90 | + [ |
| 91 | + module |
| 92 | + for name, module in sys.modules.items() |
| 93 | + if (name == "bokeh" or name.startswith("bokeh.")) |
| 94 | + and name != "bokeh.server.tornado" |
| 95 | + ] |
| 96 | + ) |
| 97 | + |
| 98 | + with contextlib.ExitStack() as stack: |
| 99 | + spies = [ |
| 100 | + stack.enter_context( |
| 101 | + unittest.mock.patch(f"{module_name}.{obj_name}", wraps=BokehTornado) |
| 102 | + ) |
| 103 | + for module_name, obj_name in PATCHES.items() |
| 104 | + ] |
| 105 | + |
| 106 | + yield |
| 107 | + |
| 108 | + global TESTS |
| 109 | + if any(spy.called for spy in spies): |
| 110 | + TESTS.append(request.node.nodeid) |
0 commit comments