Files
LangBot/tests/unit_tests/utils/test_pkgmgr.py
huanghuoguoguo 3ba727f0e4 test: add 105 new unit tests for untested core functionality
Add comprehensive tests for B-class issues (core functionality untested):

Pipeline:
- test_pool.py: QueryPool ID generation, caching, async context (12 tests)
- test_ratelimit.py: Fixed timing-sensitive test tolerance
- test_pipelinemgr.py: Use real Pydantic StageProcessResult instead of Mock

Utils:
- test_version.py: Version comparison functions (20 tests)
- test_logcache.py: Log page management and retrieval (18 tests)
- test_httpclient.py: HTTP session pool management (10 tests)
- test_proxy.py: Proxy configuration from env and config (10 tests)
- test_image.py: URL parsing and base64 extraction (12 tests)
- test_pkgmgr.py: Pip command generation (8 tests)

Discover:
- test_engine.py: I18nString, Metadata, Component manifest (15 tests)

Test count: 1193 → 1298 (+105 tests)

Note: Some B-class issues cannot be tested due to circular import bugs
filed as GitHub issues #2175 (pipeline) and #2176 (persistence).
2026-05-16 10:13:15 +08:00

103 lines
3.6 KiB
Python

"""
Unit tests for package manager utilities.
Tests pip command generation without actual installation.
"""
from __future__ import annotations
import pytest
from unittest.mock import patch, Mock
from langbot.pkg.utils import pkgmgr
class TestPkgMgr:
"""Tests for package manager functions."""
def test_install_calls_pipmain(self):
"""install calls pipmain with correct arguments."""
with patch('langbot.pkg.utils.pkgmgr.pipmain') as mock_pipmain:
pkgmgr.install('requests')
mock_pipmain.assert_called_once_with(['install', 'requests'])
def test_install_with_version(self):
"""install handles package with version specifier."""
with patch('langbot.pkg.utils.pkgmgr.pipmain') as mock_pipmain:
pkgmgr.install('requests>=2.0.0')
mock_pipmain.assert_called_once_with(['install', 'requests>=2.0.0'])
def test_install_upgrade_calls_pipmain(self):
"""install_upgrade calls pipmain with upgrade and mirror."""
with patch('langbot.pkg.utils.pkgmgr.pipmain') as mock_pipmain:
pkgmgr.install_upgrade('requests')
expected_args = [
'install',
'--upgrade',
'requests',
'-i',
'https://pypi.tuna.tsinghua.edu.cn/simple',
'--trusted-host',
'pypi.tuna.tsinghua.edu.cn',
]
mock_pipmain.assert_called_once_with(expected_args)
def test_run_pip_with_params(self):
"""run_pip passes params to pipmain."""
with patch('langbot.pkg.utils.pkgmgr.pipmain') as mock_pipmain:
pkgmgr.run_pip(['list', '--outdated'])
mock_pipmain.assert_called_once_with(['list', '--outdated'])
def test_run_pip_empty_params(self):
"""run_pip handles empty params."""
with patch('langbot.pkg.utils.pkgmgr.pipmain') as mock_pipmain:
pkgmgr.run_pip([])
mock_pipmain.assert_called_once_with([])
def test_install_requirements_calls_pipmain(self):
"""install_requirements calls pipmain with requirements file."""
with patch('langbot.pkg.utils.pkgmgr.pipmain') as mock_pipmain:
pkgmgr.install_requirements('requirements.txt')
expected_args = [
'install',
'-r',
'requirements.txt',
'-i',
'https://pypi.tuna.tsinghua.edu.cn/simple',
'--trusted-host',
'pypi.tuna.tsinghua.edu.cn',
]
mock_pipmain.assert_called_once_with(expected_args)
def test_install_requirements_with_extra_params(self):
"""install_requirements handles extra params."""
with patch('langbot.pkg.utils.pkgmgr.pipmain') as mock_pipmain:
pkgmgr.install_requirements('requirements.txt', ['--no-cache-dir'])
expected_args = [
'install',
'-r',
'requirements.txt',
'-i',
'https://pypi.tuna.tsinghua.edu.cn/simple',
'--trusted-host',
'pypi.tuna.tsinghua.edu.cn',
'--no-cache-dir',
]
mock_pipmain.assert_called_once_with(expected_args)
def test_install_requirements_multiple_extra_params(self):
"""install_requirements handles multiple extra params."""
with patch('langbot.pkg.utils.pkgmgr.pipmain') as mock_pipmain:
pkgmgr.install_requirements('requirements.txt', ['--no-cache-dir', '--verbose'])
call_args = mock_pipmain.call_args[0][0]
assert '--no-cache-dir' in call_args
assert '--verbose' in call_args