import pytest import tempfile import shutil from pathlib import Path from unittest.mock import Mock, patch, MagicMock import json class TestJavaE2E: """Java执行和比较端到端测试""" @pytest.fixture def temp_dir(self): """创建临时目录""" temp_dir = tempfile.mkdtemp() yield temp_dir shutil.rmtree(temp_dir) def test_java_comparison_with_cobol_output(self, temp_dir): """测试Java输出与COBOL输出的比较""" from comparator import align_records, compare_field # 模拟COBOL输出 cobol_records = [ {"ID": "001", "NAME": "Alice", "AMOUNT": "1000.00"}, {"ID": "002", "NAME": "Bob", "AMOUNT": "2000.00"} ] # 模拟Java输出 java_records = [ {"ID": "001", "NAME": "Alice", "AMOUNT": "1000.00"}, {"ID": "002", "NAME": "Bob", "AMOUNT": "2000.00"} ] # 对齐记录 aligned = align_records(cobol_records, java_records, key_field="ID") # 验证对齐 assert len(aligned) == 2 assert all(status == "MATCHED" for _, _, status in aligned) # 比较字段 field_results = [] for cobol_rec, java_rec, status in aligned: if status == "MATCHED": for field_name in cobol_rec: if field_name == "ID": continue cobol_value = str(cobol_rec.get(field_name, "")) java_value = str(java_rec.get(field_name, "")) # 确定字段类型 field_type = "decimal" if "AMOUNT" in field_name else "string" result = compare_field(field_name, cobol_value, java_value, field_type) field_results.append({ "field": result.field_name, "status": result.status, "cobol": result.cobol_value, "java": result.java_value }) # 验证比较结果 assert len(field_results) == 4 # 2个记录 * 2个字段(NAME, AMOUNT) assert all(r["status"] == "PASS" for r in field_results) def test_java_comparison_with_mismatch(self, temp_dir): """测试Java输出与COBOL输出不匹配的情况""" from comparator import align_records, compare_field # 模拟COBOL输出 cobol_records = [ {"ID": "001", "NAME": "Alice", "AMOUNT": "1000.00"} ] # 模拟Java输出(AMOUNT不同) java_records = [ {"ID": "001", "NAME": "Alice", "AMOUNT": "1500.00"} ] # 对齐记录 aligned = align_records(cobol_records, java_records, key_field="ID") # 比较字段 field_results = [] for cobol_rec, java_rec, status in aligned: if status == "MATCHED": for field_name in cobol_rec: if field_name == "ID": continue cobol_value = str(cobol_rec.get(field_name, "")) java_value = str(java_rec.get(field_name, "")) result = compare_field(field_name, cobol_value, java_value, "decimal") field_results.append({ "field": result.field_name, "status": result.status, "cobol": result.cobol_value, "java": result.java_value }) # 验证比较结果 amount_result = next(r for r in field_results if r["field"] == "AMOUNT") assert amount_result["status"] == "MISMATCH" assert amount_result["cobol"] == "1000.00" assert amount_result["java"] == "1500.00" def test_java_comparison_with_tolerance(self, temp_dir): """测试Java输出与COBOL输出在容忍度范围内""" from comparator import align_records, compare_field # 模拟COBOL输出 cobol_records = [ {"ID": "001", "NAME": "Alice", "AMOUNT": "1000.00"} ] # 模拟Java输出(AMOUNT略有差异,在容忍度内) java_records = [ {"ID": "001", "NAME": "Alice", "AMOUNT": "1000.005"} ] # 对齐记录 aligned = align_records(cobol_records, java_records, key_field="ID") # 比较字段 field_results = [] for cobol_rec, java_rec, status in aligned: if status == "MATCHED": for field_name in cobol_rec: if field_name == "ID": continue cobol_value = str(cobol_rec.get(field_name, "")) java_value = str(java_rec.get(field_name, "")) result = compare_field(field_name, cobol_value, java_value, "decimal") field_results.append({ "field": result.field_name, "status": result.status, "cobol": result.cobol_value, "java": result.java_value }) # 验证比较结果(在容忍度内) amount_result = next(r for r in field_results if r["field"] == "AMOUNT") assert amount_result["status"] in ["PASS", "TOLERATED"] def test_java_comparison_with_missing_records(self, temp_dir): """测试COBOL有记录但Java没有记录的情况""" from comparator import align_records, compare_field # 模拟COBOL输出 cobol_records = [ {"ID": "001", "NAME": "Alice", "AMOUNT": "1000.00"}, {"ID": "002", "NAME": "Bob", "AMOUNT": "2000.00"} ] # 模拟Java输出(只有一条记录) java_records = [ {"ID": "001", "NAME": "Alice", "AMOUNT": "1000.00"} ] # 对齐记录 aligned = align_records(cobol_records, java_records, key_field="ID") # 验证对齐结果 assert len(aligned) == 2 # 1个MATCHED + 1个MISSING_IN_SPARK # 检查状态 statuses = [status for _, _, status in aligned] assert "MATCHED" in statuses assert "MISSING_IN_SPARK" in statuses