mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-16 18:56:02 +00:00
test: format test suite
This commit is contained in:
@@ -1 +1 @@
|
||||
"""Core module unit tests."""
|
||||
"""Core module unit tests."""
|
||||
|
||||
@@ -4,6 +4,7 @@ Tests cover:
|
||||
- _get_positive_int_config() validation
|
||||
- _get_positive_float_config() validation
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import Mock
|
||||
@@ -188,4 +189,4 @@ class TestGetPositiveFloatConfig:
|
||||
result = app._get_positive_float_config('not-a-number', default=1.5, name='test.config')
|
||||
|
||||
assert result == 1.5
|
||||
mock_logger.warning.assert_called_once()
|
||||
mock_logger.warning.assert_called_once()
|
||||
|
||||
@@ -27,6 +27,7 @@ class TestCheckDeps:
|
||||
from langbot.pkg.core.bootutils.deps import check_deps
|
||||
|
||||
import asyncio
|
||||
|
||||
result = asyncio.get_event_loop().run_until_complete(check_deps())
|
||||
|
||||
assert result == []
|
||||
@@ -46,6 +47,7 @@ class TestCheckDeps:
|
||||
from langbot.pkg.core.bootutils.deps import check_deps
|
||||
|
||||
import asyncio
|
||||
|
||||
result = asyncio.get_event_loop().run_until_complete(check_deps())
|
||||
|
||||
assert 'requests' in result
|
||||
@@ -61,6 +63,7 @@ class TestCheckDeps:
|
||||
from langbot.pkg.core.bootutils.deps import check_deps, required_deps
|
||||
|
||||
import asyncio
|
||||
|
||||
result = asyncio.get_event_loop().run_until_complete(check_deps())
|
||||
|
||||
# Should include all required_deps keys
|
||||
@@ -107,6 +110,7 @@ class TestPrecheckPluginDeps:
|
||||
with patch('os.path.exists', return_value=False):
|
||||
with patch('langbot.pkg.core.bootutils.deps.pkgmgr.install_requirements') as mock_install:
|
||||
import asyncio
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(precheck_plugin_deps())
|
||||
|
||||
mock_install.assert_not_called()
|
||||
@@ -129,6 +133,7 @@ class TestPrecheckPluginDeps:
|
||||
with patch('os.listdir', side_effect=mock_listdir):
|
||||
with patch('langbot.pkg.core.bootutils.deps.pkgmgr.install_requirements') as mock_install:
|
||||
import asyncio
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(precheck_plugin_deps())
|
||||
|
||||
mock_install.assert_called_once_with('plugins/plugin1/requirements.txt', extra_params=[])
|
||||
|
||||
@@ -7,6 +7,7 @@ Tests cover:
|
||||
- Dict type skipping
|
||||
- Missing key creation
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
@@ -248,15 +249,8 @@ class TestApplyEnvOverridesToConfig:
|
||||
"""Test multiple env vars applied in order."""
|
||||
load_config = get_load_config_module()
|
||||
|
||||
cfg = {
|
||||
'system': {'name': 'default', 'enable': True},
|
||||
'concurrency': {'pipeline': 5}
|
||||
}
|
||||
env = {
|
||||
'SYSTEM__NAME': 'custom',
|
||||
'SYSTEM__ENABLE': 'false',
|
||||
'CONCURRENCY__PIPELINE': '10'
|
||||
}
|
||||
cfg = {'system': {'name': 'default', 'enable': True}, 'concurrency': {'pipeline': 5}}
|
||||
env = {'SYSTEM__NAME': 'custom', 'SYSTEM__ENABLE': 'false', 'CONCURRENCY__PIPELINE': '10'}
|
||||
|
||||
with patch.dict(os.environ, env, clear=True):
|
||||
result = load_config._apply_env_overrides_to_config(cfg)
|
||||
@@ -287,4 +281,4 @@ class TestApplyEnvOverridesToConfig:
|
||||
with patch.dict(os.environ, env, clear=True):
|
||||
result = load_config._apply_env_overrides_to_config(cfg)
|
||||
|
||||
assert result['api']['extra_webhook_prefix'] == 'https://extra.example.com'
|
||||
assert result['api']['extra_webhook_prefix'] == 'https://extra.example.com'
|
||||
|
||||
@@ -175,4 +175,4 @@ class TestPreregisteredStages:
|
||||
pass
|
||||
|
||||
for key in preregistered_stages:
|
||||
assert isinstance(key, str)
|
||||
assert isinstance(key, str)
|
||||
|
||||
@@ -7,6 +7,7 @@ Tests cover:
|
||||
|
||||
Note: Uses import_isolation to break circular import chains.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
@@ -19,15 +20,17 @@ from typing import Generator
|
||||
|
||||
class MockLifecycleControlScopeEnum:
|
||||
"""Mock enum value for LifecycleControlScope with .value attribute."""
|
||||
|
||||
def __init__(self, value: str):
|
||||
self.value = value
|
||||
|
||||
def __repr__(self):
|
||||
return f"LifecycleControlScope.{self.value.upper()}"
|
||||
return f'LifecycleControlScope.{self.value.upper()}'
|
||||
|
||||
|
||||
class MockLifecycleControlScope:
|
||||
"""Mock enum for LifecycleControlScope."""
|
||||
|
||||
APPLICATION = MockLifecycleControlScopeEnum('application')
|
||||
PLATFORM = MockLifecycleControlScopeEnum('platform')
|
||||
PIPELINE = MockLifecycleControlScopeEnum('pipeline')
|
||||
@@ -40,17 +43,17 @@ def isolated_taskmgr_import() -> Generator[None, None, None]:
|
||||
# Mock modules that cause circular imports
|
||||
mock_entities = MagicMock()
|
||||
mock_entities.LifecycleControlScope = MockLifecycleControlScope
|
||||
|
||||
|
||||
mock_app = MagicMock()
|
||||
|
||||
|
||||
mock_importutil = MagicMock()
|
||||
mock_importutil.import_modules_in_pkg = lambda pkg: None
|
||||
mock_importutil.import_modules_in_pkgs = lambda pkgs: None
|
||||
|
||||
|
||||
mock_http_controller = MagicMock()
|
||||
|
||||
|
||||
mock_rag_mgr = MagicMock()
|
||||
|
||||
|
||||
mocks = {
|
||||
'langbot.pkg.core.entities': mock_entities,
|
||||
'langbot.pkg.core.app': mock_app,
|
||||
@@ -58,26 +61,26 @@ def isolated_taskmgr_import() -> Generator[None, None, None]:
|
||||
'langbot.pkg.rag.knowledge.kbmgr': mock_rag_mgr,
|
||||
'langbot.pkg.utils.importutil': mock_importutil,
|
||||
}
|
||||
|
||||
|
||||
# Save original state
|
||||
saved = {}
|
||||
for name in mocks:
|
||||
if name in sys.modules:
|
||||
saved[name] = sys.modules[name]
|
||||
|
||||
|
||||
# Clear taskmgr to force re-import
|
||||
taskmgr_name = 'langbot.pkg.core.taskmgr'
|
||||
if taskmgr_name in sys.modules:
|
||||
saved[taskmgr_name] = sys.modules[taskmgr_name]
|
||||
|
||||
|
||||
try:
|
||||
# Apply mocks
|
||||
for name, module in mocks.items():
|
||||
sys.modules[name] = module
|
||||
|
||||
|
||||
# Clear taskmgr
|
||||
sys.modules.pop(taskmgr_name, None)
|
||||
|
||||
|
||||
yield
|
||||
finally:
|
||||
# Restore
|
||||
@@ -86,7 +89,7 @@ def isolated_taskmgr_import() -> Generator[None, None, None]:
|
||||
sys.modules[name] = saved[name]
|
||||
else:
|
||||
sys.modules.pop(name, None)
|
||||
|
||||
|
||||
if taskmgr_name in saved:
|
||||
sys.modules[taskmgr_name] = saved[taskmgr_name]
|
||||
else:
|
||||
@@ -97,6 +100,7 @@ def get_taskmgr_classes():
|
||||
"""Get TaskContext, TaskWrapper, AsyncTaskManager classes."""
|
||||
with isolated_taskmgr_import():
|
||||
from langbot.pkg.core.taskmgr import TaskContext, TaskWrapper, AsyncTaskManager
|
||||
|
||||
return TaskContext, TaskWrapper, AsyncTaskManager
|
||||
|
||||
|
||||
@@ -194,9 +198,10 @@ class TestTaskContext:
|
||||
"""Test TaskContext.placeholder() returns singleton."""
|
||||
with isolated_taskmgr_import():
|
||||
from langbot.pkg.core.taskmgr import TaskContext
|
||||
|
||||
|
||||
# Reset global placeholder
|
||||
import langbot.pkg.core.taskmgr as taskmgr_module
|
||||
|
||||
taskmgr_module.placeholder_context = None
|
||||
|
||||
ctx1 = TaskContext.placeholder()
|
||||
@@ -269,7 +274,8 @@ class TestTaskWrapper:
|
||||
return 'result'
|
||||
|
||||
wrapper = TaskWrapper(
|
||||
mock_app, immediate_coro(),
|
||||
mock_app,
|
||||
immediate_coro(),
|
||||
name='test_task',
|
||||
label='Test Task',
|
||||
)
|
||||
@@ -414,7 +420,7 @@ class TestAsyncTaskManager:
|
||||
async def test_cancel_by_scope(self):
|
||||
"""Test cancel_by_scope cancels matching tasks."""
|
||||
_, _, AsyncTaskManager = get_taskmgr_classes()
|
||||
|
||||
|
||||
mock_app = create_mock_app()
|
||||
manager = AsyncTaskManager(mock_app)
|
||||
|
||||
@@ -422,16 +428,10 @@ class TestAsyncTaskManager:
|
||||
await asyncio.sleep(10)
|
||||
|
||||
# Create task with APPLICATION scope
|
||||
w1 = manager.create_task(
|
||||
long_coro(),
|
||||
scopes=[MockLifecycleControlScope.APPLICATION]
|
||||
)
|
||||
w1 = manager.create_task(long_coro(), scopes=[MockLifecycleControlScope.APPLICATION])
|
||||
|
||||
# Create task with different scope
|
||||
w2 = manager.create_task(
|
||||
long_coro(),
|
||||
scopes=[MockLifecycleControlScope.PIPELINE]
|
||||
)
|
||||
w2 = manager.create_task(long_coro(), scopes=[MockLifecycleControlScope.PIPELINE])
|
||||
|
||||
manager.cancel_by_scope(MockLifecycleControlScope.APPLICATION)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user