-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
59 lines (47 loc) · 1.7 KB
/
conftest.py
File metadata and controls
59 lines (47 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
Root conftest.py — pre-stubs external/infrastructure modules so that
services/zkillboard unit tests can be collected without a live database,
Redis, or Telegram service.
This conftest is loaded by pytest before any test collection begins,
ensuring that module-level instantiations in services/zkillboard/ (e.g.
RedisStateManager()) never attempt real connections.
"""
import sys
from unittest.mock import MagicMock
def _stub_if_missing(module_name: str) -> None:
"""Install a MagicMock stub only if the module cannot be imported for real."""
if module_name in sys.modules:
return
try:
__import__(module_name)
except (ModuleNotFoundError, ImportError):
sys.modules[module_name] = MagicMock()
# --- src.* stubs (no such package outside Docker) ---
_src_modules = [
"src",
"src.database",
"src.route_service",
"src.telegram_service",
"src.auth",
"src.core",
"src.core.config",
]
for _mod in _src_modules:
_stub_if_missing(_mod)
# --- Redis: stub Redis.ping() so state_manager module-level init passes ---
try:
import redis as _redis_pkg # real redis is installed
_orig_redis_cls = _redis_pkg.Redis
class _MockRedis(_orig_redis_cls):
"""Drop-in that skips real network connection in test environments."""
def __init__(self, *args, **kwargs):
# Don't call super().__init__ — avoid real socket creation
pass
def ping(self):
return True
_redis_pkg.Redis = _MockRedis
except ImportError:
# redis not installed at all — stub the whole package
_redis_pkg = MagicMock()
sys.modules.setdefault("redis", _redis_pkg)
sys.modules.setdefault("redis.asyncio", MagicMock())