test: add configuration loading tests
- Add test_config.py with tests for load_settings function - Add test_config.yaml fixture with test database configuration - Tests cover: successful loading, file not found error, database URL generation Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
10
tests/fixtures/test_config.yaml
vendored
Normal file
10
tests/fixtures/test_config.yaml
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
# 测试配置
|
||||
database:
|
||||
sql_server:
|
||||
host: localhost
|
||||
port: 1433
|
||||
database: TestDB
|
||||
username: test_user
|
||||
password: test_pass
|
||||
driver: "{ODBC Driver 18 for SQL Server}"
|
||||
trust_server_certificate: yes
|
||||
25
tests/test_config.py
Normal file
25
tests/test_config.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import pytest
|
||||
from config.settings import load_settings
|
||||
|
||||
|
||||
def test_load_settings_success():
|
||||
"""测试成功加载配置文件"""
|
||||
settings = load_settings("tests/fixtures/test_config.yaml")
|
||||
assert settings.database.sql_server.port == 1433
|
||||
assert settings.database.sql_server.host == "localhost"
|
||||
assert settings.database.sql_server.database == "TestDB"
|
||||
|
||||
|
||||
def test_load_settings_file_not_found():
|
||||
"""测试配置文件不存在时抛出异常"""
|
||||
with pytest.raises(FileNotFoundError):
|
||||
load_settings("nonexistent.yaml")
|
||||
|
||||
|
||||
def test_database_url_property():
|
||||
"""测试 database_url 属性生成"""
|
||||
settings = load_settings("tests/fixtures/test_config.yaml")
|
||||
url = settings.database_url
|
||||
assert "mssql+pyodbc" in str(url)
|
||||
assert "test_user" in str(url)
|
||||
assert "TestDB" in str(url)
|
||||
Reference in New Issue
Block a user