Merge commit '47296980495f8bbfc9493e93de85dd62de6fa6b9' as 'paste-framework'
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
"""
|
||||
测试配置读取功能。
|
||||
使用 mock 配置文件,不依赖真实 config.json。
|
||||
"""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from paste.core import config
|
||||
|
||||
|
||||
class TestConfiguration:
|
||||
"""配置管理测试"""
|
||||
|
||||
def test_load_config_with_mock_file(self, temp_config_file):
|
||||
"""使用临时配置文件测试加载"""
|
||||
with patch.object(config, 'load_config', wraps=config.load_config):
|
||||
# 模拟配置文件路径
|
||||
with patch('paste.core.config.os.path.abspath') as mock_abspath:
|
||||
mock_abspath.return_value = str(temp_config_file)
|
||||
cfg = config.load_config()
|
||||
assert isinstance(cfg, dict)
|
||||
assert 'db_engine' in cfg
|
||||
|
||||
def test_get_config_by_path_existing(self, mock_config_dict):
|
||||
"""测试读取存在的配置项"""
|
||||
with patch('paste.core.config.GLOBAL_CONFIG', mock_config_dict):
|
||||
result = config.get_config_by_path('db_engine.engine')
|
||||
assert result == "sqlite+pysqlite:///:memory:"
|
||||
|
||||
def test_get_config_by_path_nested(self, mock_config_dict):
|
||||
"""测试读取深层嵌套配置"""
|
||||
with patch('paste.core.config.GLOBAL_CONFIG', mock_config_dict):
|
||||
result = config.get_config_by_path('redis.connection.url')
|
||||
assert result == "redis://localhost:6379/15"
|
||||
|
||||
def test_get_config_by_path_with_default(self, mock_config_dict):
|
||||
"""测试带默认值的配置读取"""
|
||||
with patch('paste.core.config.GLOBAL_CONFIG', mock_config_dict):
|
||||
result = config.get_config_by_path('nonexistent.key', default='fallback')
|
||||
assert result == 'fallback'
|
||||
|
||||
def test_get_config_by_path_missing_without_default(self, mock_config_dict):
|
||||
"""测试缺失配置项且无默认值时抛出异常"""
|
||||
with patch('paste.core.config.GLOBAL_CONFIG', mock_config_dict):
|
||||
with pytest.raises(AssertionError):
|
||||
config.get_config_by_path('nonexistent.key')
|
||||
|
||||
def test_get_config_shortcut(self, mock_config_dict):
|
||||
"""测试 get_config 快捷方法"""
|
||||
with patch('paste.core.config.GLOBAL_CONFIG', mock_config_dict):
|
||||
result = config.get_config('tornado.demo.port')
|
||||
assert result == 9000
|
||||
|
||||
def test_get_config_default_none(self, mock_config_dict):
|
||||
"""测试 get_config 默认值 None 的情况"""
|
||||
with patch('paste.core.config.GLOBAL_CONFIG', mock_config_dict):
|
||||
with pytest.raises(AssertionError):
|
||||
config.get_config('completely.nonexistent')
|
||||
Reference in New Issue
Block a user