fix(data): Task 1 review 修复(pycache清理+pytest.ini+错误处理)
- Fix 1: 移除误 commit 的 __pycache__ 文件 - Fix 2: 创建 pytest.ini 解决 PYTHONPATH 问题 - Fix 3: load_config 添加错误处理(FileNotFoundError + ValueError) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
[pytest]
|
||||
pythonpath = .
|
||||
testpaths = tests
|
||||
+11
-2
@@ -10,8 +10,17 @@ class DataConfig:
|
||||
performance: dict
|
||||
|
||||
def load_config(path: str) -> DataConfig:
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
raw = yaml.safe_load(f)
|
||||
try:
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
raw = yaml.safe_load(f)
|
||||
except FileNotFoundError:
|
||||
raise FileNotFoundError(f"配置文件不存在: {path}")
|
||||
except yaml.YAMLError as e:
|
||||
raise ValueError(f"YAML解析失败: {e}")
|
||||
|
||||
if not raw:
|
||||
raise ValueError(f"配置文件为空: {path}")
|
||||
|
||||
return DataConfig(
|
||||
data_paths=raw.get("data_paths", {}),
|
||||
data_sources=raw.get("data_sources", {}),
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,4 +1,5 @@
|
||||
# tests/data/test_config.py
|
||||
import pytest
|
||||
from sanguo_data.config import load_config, DataConfig
|
||||
|
||||
def test_load_config_returns_dataconfig(tmp_path):
|
||||
@@ -25,3 +26,7 @@ performance:
|
||||
assert cfg.data_paths["daily_dir"] == "/tmp/daily"
|
||||
assert cfg.data_sources["daily"][0]["name"] == "eastmoney"
|
||||
assert cfg.performance["max_retries"] == 3
|
||||
|
||||
def test_load_config_raises_on_missing_file():
|
||||
with pytest.raises(FileNotFoundError, match="配置文件不存在"):
|
||||
load_config("/nonexistent/path/config.yaml")
|
||||
|
||||
Reference in New Issue
Block a user