在前后端开发联调前 的提交20260223
This commit is contained in:
@@ -10,6 +10,8 @@ import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
# scripts/ops 不是 Python 包,通过 sys.path 导入
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "scripts" / "ops"))
|
||||
|
||||
@@ -718,11 +720,11 @@ class TestResolveOutputDir:
|
||||
assert result == target
|
||||
assert target.is_dir()
|
||||
|
||||
def test_fallback_to_docs_reports(self, monkeypatch):
|
||||
"""SYSTEM_ANALYZE_ROOT 未设置时回退到 docs/reports/。"""
|
||||
def test_fallback_raises_when_env_missing(self, monkeypatch):
|
||||
"""SYSTEM_ANALYZE_ROOT 未设置时抛出 KeyError。"""
|
||||
monkeypatch.delenv("SYSTEM_ANALYZE_ROOT", raising=False)
|
||||
result = resolve_output_dir()
|
||||
assert result == Path("docs/reports")
|
||||
with pytest.raises(KeyError):
|
||||
resolve_output_dir()
|
||||
|
||||
def test_creates_directory(self, tmp_path, monkeypatch):
|
||||
"""目录不存在时自动创建。"""
|
||||
@@ -1783,91 +1785,26 @@ class TestFieldDiffSubTables:
|
||||
assert "SCD2/派生列 2 个" in report
|
||||
|
||||
|
||||
class TestGuessFieldPurpose:
|
||||
"""测试 _guess_field_purpose 字段用途推测。"""
|
||||
|
||||
def test_scd2_field(self):
|
||||
from gen_dataflow_report import _guess_field_purpose
|
||||
purpose, conf = _guess_field_purpose("scd2_start_time", "test", "DWD")
|
||||
assert "SCD2" in purpose
|
||||
assert conf == "高"
|
||||
|
||||
def test_id_field(self):
|
||||
from gen_dataflow_report import _guess_field_purpose
|
||||
purpose, conf = _guess_field_purpose("id", "test", "ODS")
|
||||
assert "主键" in purpose
|
||||
assert conf == "高"
|
||||
|
||||
def test_foreign_key(self):
|
||||
from gen_dataflow_report import _guess_field_purpose
|
||||
purpose, conf = _guess_field_purpose("tenant_id", "test", "ODS")
|
||||
assert "租户" in purpose
|
||||
assert conf == "高"
|
||||
|
||||
def test_nested_site_profile(self):
|
||||
from gen_dataflow_report import _guess_field_purpose
|
||||
purpose, conf = _guess_field_purpose("siteProfile.shop_name", "test", "API")
|
||||
assert "门店" in purpose
|
||||
assert conf == "高"
|
||||
|
||||
def test_unknown_field(self):
|
||||
from gen_dataflow_report import _guess_field_purpose
|
||||
purpose, conf = _guess_field_purpose("xyzzy_foo_bar", "test", "ODS")
|
||||
assert "待分析" in purpose
|
||||
assert conf == "低"
|
||||
|
||||
def test_price_field(self):
|
||||
from gen_dataflow_report import _guess_field_purpose
|
||||
purpose, conf = _guess_field_purpose("cx_unit_price", "test", "ODS")
|
||||
assert "金额" in purpose or "价格" in purpose
|
||||
|
||||
def test_derived_field(self):
|
||||
from gen_dataflow_report import _guess_field_purpose
|
||||
purpose, conf = _guess_field_purpose("derived_flag", "test", "DWD")
|
||||
assert "派生" in purpose
|
||||
assert conf == "高"
|
||||
|
||||
def test_is_delete_field(self):
|
||||
from gen_dataflow_report import _guess_field_purpose
|
||||
purpose, conf = _guess_field_purpose("is_delete", "test", "ODS")
|
||||
assert "删除" in purpose
|
||||
assert conf == "高"
|
||||
|
||||
|
||||
class TestDiffSubTablePurposeColumn:
|
||||
"""测试差异分表中推测用途列的输出。"""
|
||||
class TestDiffSubTableColumns:
|
||||
"""测试差异分表中列的输出格式(推测用途/置信度已移除,改为人工处理)。"""
|
||||
|
||||
def test_purpose_column_in_flat_unmapped(self, tmp_path):
|
||||
"""平层未映射分表应包含推测用途、置信度、示例值、说明列。"""
|
||||
# 复用 TestFieldDiffSubTables 的数据构造
|
||||
def test_flat_unmapped_header(self, tmp_path):
|
||||
"""平层未映射分表应包含示例值、说明列(无推测用途/置信度)。"""
|
||||
from test_dataflow_analyzer import TestFieldDiffSubTables
|
||||
inst = TestFieldDiffSubTables()
|
||||
data_dir = inst._setup_diff_data_dir(tmp_path)
|
||||
report = generate_report(data_dir)
|
||||
# 表头应有推测用途 + 示例值 + 说明列
|
||||
assert "| # | JSON 字段 | 推测用途 | 置信度 | 示例值 | 说明 | 状态 |" in report
|
||||
assert "| # | JSON 字段 | 示例值 | 说明 | 状态 |" in report
|
||||
|
||||
def test_purpose_column_in_dwd_no_ods(self, tmp_path):
|
||||
"""DWD 无 ODS 源分表应包含推测用途列。"""
|
||||
def test_dwd_no_ods_header(self, tmp_path):
|
||||
"""DWD 无 ODS 源子表应包含说明列(无推测用途/置信度)。"""
|
||||
from test_dataflow_analyzer import TestFieldDiffSubTables
|
||||
inst = TestFieldDiffSubTables()
|
||||
data_dir = inst._setup_diff_data_dir(tmp_path)
|
||||
report = generate_report(data_dir)
|
||||
# scd2_ver 应被推测为 SCD2 元数据
|
||||
assert "SCD2" in report
|
||||
# derived_flag 应被推测为派生列
|
||||
assert "派生" in report
|
||||
|
||||
def test_purpose_column_in_nested(self, tmp_path):
|
||||
"""嵌套对象分表也应包含推测用途列。"""
|
||||
from test_dataflow_analyzer import TestFieldDiffSubTables
|
||||
inst = TestFieldDiffSubTables()
|
||||
data_dir = inst._setup_diff_data_dir(tmp_path)
|
||||
report = generate_report(data_dir)
|
||||
# 嵌套对象表头
|
||||
lines = report.split("\n")
|
||||
nested_headers = [l for l in lines if "推测用途" in l and "置信度" in l]
|
||||
assert len(nested_headers) >= 1
|
||||
assert "| # | DWD 表 | DWD 列 | 说明 | 状态 |" in report
|
||||
|
||||
def test_section_numbering_incremental(self, tmp_path):
|
||||
"""多个差异分表应有递增编号 1.1.1, 1.1.2, ...。"""
|
||||
@@ -1875,7 +1812,6 @@ class TestDiffSubTablePurposeColumn:
|
||||
from test_dataflow_analyzer import TestFieldDiffSubTables
|
||||
inst = TestFieldDiffSubTables()
|
||||
data_dir = inst._setup_diff_data_dir(tmp_path)
|
||||
# 添加第二个有差异的表
|
||||
manifest = _json.loads((data_dir / "collection_manifest.json").read_text(encoding="utf-8"))
|
||||
manifest["tables"].append({
|
||||
"table": "beta_table", "task_code": "ODS_BETA", "description": "第二表",
|
||||
@@ -1936,7 +1872,6 @@ class TestDiffSubTablePurposeColumn:
|
||||
inst = TestFieldDiffSubTables()
|
||||
data_dir = inst._setup_diff_data_dir(tmp_path)
|
||||
report = generate_report(data_dir)
|
||||
# extra_flat 在 json_trees 中有 samples=["x"],应出现在差异子表的 extra_flat 行
|
||||
lines = report.split("\n")
|
||||
flat_rows = [l for l in lines if "extra_flat" in l and "未映射" in l]
|
||||
assert len(flat_rows) >= 1
|
||||
@@ -1948,7 +1883,6 @@ class TestDiffSubTablePurposeColumn:
|
||||
from test_dataflow_analyzer import TestFieldDiffSubTables
|
||||
inst = TestFieldDiffSubTables()
|
||||
data_dir = inst._setup_diff_data_dir(tmp_path)
|
||||
# 注入 bd_descriptions 中 ods_only_col 的说明
|
||||
bd = {"ods_table": "alpha_table",
|
||||
"ods_fields": {"ods_only_col": "仅ODS存在的测试列"},
|
||||
"dwd_fields": {}}
|
||||
@@ -1956,16 +1890,8 @@ class TestDiffSubTablePurposeColumn:
|
||||
_json.dumps(bd, ensure_ascii=False), encoding="utf-8"
|
||||
)
|
||||
report = generate_report(data_dir)
|
||||
# 说明应出现在 ods_only_col 所在行
|
||||
lines = report.split("\n")
|
||||
ods_only_rows = [l for l in lines if "ods_only_col" in l and "无 JSON 源" in l]
|
||||
assert len(ods_only_rows) >= 1
|
||||
assert "仅ODS存在的测试列" in ods_only_rows[0]
|
||||
|
||||
def test_dwd_no_ods_has_desc_column(self, tmp_path):
|
||||
"""DWD 无 ODS 源子表应包含说明列。"""
|
||||
from test_dataflow_analyzer import TestFieldDiffSubTables
|
||||
inst = TestFieldDiffSubTables()
|
||||
data_dir = inst._setup_diff_data_dir(tmp_path)
|
||||
report = generate_report(data_dir)
|
||||
assert "| # | DWD 表 | DWD 列 | 推测用途 | 置信度 | 说明 | 状态 |" in report
|
||||
|
||||
Reference in New Issue
Block a user