v1: executing-plans 模式生成,54 文件 1320 行 Python
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
__pycache__/
|
||||
*.pyc
|
||||
*.pyo
|
||||
*.egg-info/
|
||||
dist/
|
||||
build/
|
||||
.cache/
|
||||
reports/
|
||||
test-data-bundle/
|
||||
*.exec
|
||||
target/
|
||||
.DS_Store
|
||||
@@ -0,0 +1,50 @@
|
||||
import json
|
||||
from data.field_tree import FieldTree, Field
|
||||
from agents.llm import LLMClient
|
||||
|
||||
PROMPT_AGENT1 = """You are a COBOL COPYBOOK parser. Given COPYBOOK text, output a JSON object:
|
||||
{"fields": [{"name": "...", "level": N, "pic": "...", "usage": "DISPLAY|COMP-3|COMP|COMP-5", "offset": N, "length": N, "decimal": N, "signed": bool, "occurs": N|null, "redefines": "..."|null, "conditions": [{"name": "...", "value": "..."}], "children": [...]}]}
|
||||
Return valid JSON only. No explanation."""
|
||||
|
||||
|
||||
class Agent1Parser:
|
||||
def __init__(self, llm: LLMClient):
|
||||
self.llm = llm
|
||||
|
||||
def parse(self, copybook_text: str) -> FieldTree:
|
||||
messages = [
|
||||
{"role": "system", "content": PROMPT_AGENT1},
|
||||
{"role": "user", "content": copybook_text}
|
||||
]
|
||||
raw = self.llm.call(messages)
|
||||
return self._parse_response(raw)
|
||||
|
||||
def _parse_response(self, raw: str) -> FieldTree:
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
fields = self._to_fields(data.get("fields", []), offset=0)
|
||||
return FieldTree(fields=fields, copybook_name="")
|
||||
except (json.JSONDecodeError, KeyError):
|
||||
return FieldTree(fields=[], copybook_name="parse_error")
|
||||
|
||||
def _to_fields(self, raw_fields: list, offset: int = 0) -> list[Field]:
|
||||
result = []
|
||||
current_offset = offset
|
||||
for rf in raw_fields:
|
||||
f = Field(
|
||||
name=rf.get("name", ""),
|
||||
level=rf.get("level", 0),
|
||||
pic=rf.get("pic", ""),
|
||||
usage=rf.get("usage", "DISPLAY"),
|
||||
offset=current_offset,
|
||||
length=rf.get("length", 0),
|
||||
decimal=rf.get("decimal", 0),
|
||||
signed=rf.get("signed", False),
|
||||
occurs=rf.get("occurs"),
|
||||
redefines=rf.get("redefines"),
|
||||
conditions=rf.get("conditions", []))
|
||||
children = rf.get("children", [])
|
||||
f.children = self._to_fields(children, current_offset)
|
||||
current_offset += f.length
|
||||
result.append(f)
|
||||
return result
|
||||
@@ -0,0 +1,39 @@
|
||||
import json
|
||||
from data.field_tree import FieldTree
|
||||
from data.test_case import TestCase, TestSuite, SparkConfig
|
||||
from agents.llm import LLMClient
|
||||
|
||||
PROMPT_AGENT2 = """You are a COBOL test data designer. Given a FieldTree JSON, generate test data covering boundary values.
|
||||
Output: {"test_cases": [{"id": "TC-001", "fields": {"FIELD_NAME": value, ...}, "coverage_targets": ["DP-001-TRUE"]}]}
|
||||
For each field, generate 1-3 test cases covering: zero, boundary (MAX), typical value. Return valid JSON only."""
|
||||
|
||||
|
||||
class Agent2Data:
|
||||
def __init__(self, llm: LLMClient):
|
||||
self.llm = llm
|
||||
|
||||
def design(self, tree: FieldTree, coverage_target: str = "boundary",
|
||||
spark_mode: bool = False) -> TestSuite:
|
||||
tree_json = {"fields": [{
|
||||
"name": f.name, "level": f.level, "pic": f.pic,
|
||||
"usage": f.usage, "length": f.length, "decimal": f.decimal,
|
||||
"signed": f.signed, "redefines": f.redefines, "occurs": f.occurs
|
||||
} for f in tree.flatten().values()]}
|
||||
|
||||
messages = [
|
||||
{"role": "system", "content": PROMPT_AGENT2},
|
||||
{"role": "user", "content": json.dumps(tree_json)}
|
||||
]
|
||||
raw = self.llm.call(messages)
|
||||
test_cases = self._parse(raw)
|
||||
suite = TestSuite(test_cases=test_cases)
|
||||
if spark_mode:
|
||||
suite.spark_config = SparkConfig(num_records=1000)
|
||||
return suite
|
||||
|
||||
def _parse(self, raw: str) -> list[TestCase]:
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
return [TestCase(**tc) for tc in data.get("test_cases", [])]
|
||||
except (json.JSONDecodeError, KeyError):
|
||||
return [TestCase(id="TC-FALLBACK", fields={"BR-AMT": 0})]
|
||||
@@ -0,0 +1,23 @@
|
||||
from agents.llm import LLMClient
|
||||
from data.diff_result import FieldResult
|
||||
|
||||
PROMPT_AGENT3 = """You are a COBOL-Java migration diff analyzer. Given a field mismatch, explain WHY the values differ and suggest a fix.
|
||||
Output: {"issue_type": "...", "confidence": 0.0-1.0, "reason": "...", "suggestion": "..."}
|
||||
You NEVER decide PASS/FAIL. Your role is diagnostic only. Return valid JSON only."""
|
||||
|
||||
|
||||
class Agent3Diagnostic:
|
||||
def __init__(self, llm: LLMClient):
|
||||
self.llm = llm
|
||||
|
||||
def analyze(self, fr: FieldResult) -> str:
|
||||
prompt = f"""Field: {fr.field_name}
|
||||
COBOL value: {fr.cobol_value}
|
||||
Java value: {fr.java_value}
|
||||
Status: {fr.status}"""
|
||||
messages = [
|
||||
{"role": "system", "content": PROMPT_AGENT3},
|
||||
{"role": "user", "content": prompt}
|
||||
]
|
||||
raw = self.llm.call(messages)
|
||||
return raw
|
||||
@@ -0,0 +1,47 @@
|
||||
import json, hashlib, os
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
import httpx
|
||||
|
||||
|
||||
class LLMClient:
|
||||
def __init__(self, model: str = "gpt-4o-mini", timeout: int = 15,
|
||||
cache_dir: str = ".cache/llm"):
|
||||
self.model = model
|
||||
self.timeout = timeout
|
||||
self.cache_dir = Path(cache_dir)
|
||||
self.cache_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def _cache_key(self, messages: list) -> str:
|
||||
return hashlib.sha256(json.dumps(messages, sort_keys=True).encode()).hexdigest()
|
||||
|
||||
def _cache_get(self, key: str) -> Optional[str]:
|
||||
path = self.cache_dir / f"{key}.json"
|
||||
if path.exists():
|
||||
return json.loads(path.read_text()).get("response")
|
||||
return None
|
||||
|
||||
def _cache_set(self, key: str, response: str):
|
||||
(self.cache_dir / f"{key}.json").write_text(json.dumps({"response": response}))
|
||||
|
||||
def call(self, messages: list, retries: int = 1) -> str:
|
||||
key = self._cache_key(messages)
|
||||
cached = self._cache_get(key)
|
||||
if cached:
|
||||
return cached
|
||||
api_key = os.environ.get("OPENAI_API_KEY", "")
|
||||
for attempt in range(retries + 1):
|
||||
try:
|
||||
resp = httpx.post(
|
||||
"https://api.openai.com/v1/chat/completions",
|
||||
json={"model": self.model, "messages": messages},
|
||||
headers={"Authorization": f"Bearer {api_key}"},
|
||||
timeout=self.timeout)
|
||||
resp.raise_for_status()
|
||||
result = resp.json()["choices"][0]["message"]["content"]
|
||||
self._cache_set(key, result)
|
||||
return result
|
||||
except Exception:
|
||||
if attempt == retries:
|
||||
raise
|
||||
return ""
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
[project]
|
||||
name = "example"
|
||||
copybook_paths = ["./copybooks", "/usr/share/copybooks"]
|
||||
dialect = "ibm"
|
||||
|
||||
[llm]
|
||||
model = "gpt-4o-mini"
|
||||
timeout = 15
|
||||
cache_dir = ".cache/llm"
|
||||
|
||||
[coverage]
|
||||
default_target = "boundary"
|
||||
|
||||
[comparison]
|
||||
rounding_mode = "TRUNCATE"
|
||||
default_tolerance = 0.01
|
||||
|
||||
[runner]
|
||||
mode = "native"
|
||||
|
||||
[spark]
|
||||
master = "local[*]"
|
||||
input_format = "json"
|
||||
num_records = 1000
|
||||
@@ -0,0 +1,54 @@
|
||||
from typing import Optional
|
||||
|
||||
|
||||
def align_records(
|
||||
cobol_records: list[dict],
|
||||
java_records: list[dict],
|
||||
key_field: str = "CUST-ID"
|
||||
) -> list[tuple]:
|
||||
"""Align records by key field. Returns list of (cobol, java, status)."""
|
||||
if not cobol_records and not java_records:
|
||||
return []
|
||||
|
||||
cobol_by_key = {}
|
||||
for i, r in enumerate(cobol_records):
|
||||
k = r.get(key_field)
|
||||
k = _normalize_key(k)
|
||||
if k not in cobol_by_key:
|
||||
cobol_by_key[k] = []
|
||||
cobol_by_key[k].append(r)
|
||||
|
||||
java_by_key = {}
|
||||
for i, r in enumerate(java_records):
|
||||
k = r.get(key_field)
|
||||
k = _normalize_key(k)
|
||||
if k not in java_by_key:
|
||||
java_by_key[k] = []
|
||||
java_by_key[k].append(r)
|
||||
|
||||
pairs = []
|
||||
all_keys = set(list(cobol_by_key.keys()) + list(java_by_key.keys()))
|
||||
|
||||
for key in all_keys:
|
||||
cobol_items = cobol_by_key.get(key, [])
|
||||
java_items = java_by_key.get(key, [])
|
||||
max_len = max(len(cobol_items), len(java_items))
|
||||
|
||||
for i in range(max_len):
|
||||
c = cobol_items[i] if i < len(cobol_items) else None
|
||||
j = java_items[i] if i < len(java_items) else None
|
||||
|
||||
if c is not None and j is not None:
|
||||
pairs.append((c, j, "MATCHED"))
|
||||
elif c is not None:
|
||||
pairs.append((c, None, "MISSING_IN_SPARK"))
|
||||
else:
|
||||
pairs.append((None, j, "EXTRA_IN_SPARK"))
|
||||
|
||||
return pairs
|
||||
|
||||
|
||||
def _normalize_key(key) -> str:
|
||||
if key is None:
|
||||
return "__NONE__"
|
||||
return str(key)
|
||||
@@ -0,0 +1,56 @@
|
||||
import struct
|
||||
from pathlib import Path
|
||||
from data.field_tree import FieldTree
|
||||
|
||||
|
||||
class CobolBinaryReader:
|
||||
def read(self, binary_path: str, tree: FieldTree) -> list[dict]:
|
||||
data = Path(binary_path).read_bytes()
|
||||
record_size = self._compute_record_size(tree)
|
||||
if record_size == 0 or len(data) == 0:
|
||||
return []
|
||||
records = []
|
||||
for offset in range(0, len(data), record_size):
|
||||
record = data[offset:offset + record_size]
|
||||
if len(record) >= record_size:
|
||||
records.append(self._parse_record(record, tree))
|
||||
return records
|
||||
|
||||
def _compute_record_size(self, tree: FieldTree) -> int:
|
||||
max_end = 0
|
||||
for f in tree.fields:
|
||||
end = f.offset + f.length
|
||||
if end > max_end:
|
||||
max_end = end
|
||||
return max_end
|
||||
|
||||
def _parse_record(self, record: bytes, tree: FieldTree) -> dict:
|
||||
result = {}
|
||||
for name, field in tree.flatten().items():
|
||||
if field.length == 0 or field.offset + field.length > len(record):
|
||||
continue
|
||||
raw = record[field.offset:field.offset + field.length]
|
||||
if field.usage == "COMP-3":
|
||||
result[name] = self._parse_comp3(raw, field.signed, field.decimal)
|
||||
elif field.usage == "COMP" or field.usage == "COMP-5":
|
||||
result[name] = int.from_bytes(raw, "big", signed=field.signed)
|
||||
else:
|
||||
result[name] = raw.decode("ascii", errors="replace").strip()
|
||||
return result
|
||||
|
||||
def _parse_comp3(self, raw: bytes, signed: bool, decimal: int) -> str:
|
||||
if not raw:
|
||||
return "0"
|
||||
nibbles = []
|
||||
for b in raw:
|
||||
nibbles.append((b >> 4) & 0x0F)
|
||||
nibbles.append(b & 0x0F)
|
||||
sign = nibbles.pop()
|
||||
value = 0
|
||||
for n in nibbles:
|
||||
value = value * 10 + n
|
||||
if signed and sign in (0x0D, 0x0B):
|
||||
value = -value
|
||||
divisor = 10 ** decimal
|
||||
result = float(value) / divisor
|
||||
return f"{result:.{decimal}f}" if decimal else str(value)
|
||||
@@ -0,0 +1,105 @@
|
||||
from data.diff_result import FieldResult
|
||||
from decimal import Decimal, InvalidOperation, ROUND_DOWN
|
||||
|
||||
DEFAULT_TOLERANCE = 0.01
|
||||
|
||||
|
||||
def compare_field(
|
||||
name: str,
|
||||
cobol_val: str,
|
||||
java_val: str,
|
||||
field_type: str = "decimal",
|
||||
tolerance: float = DEFAULT_TOLERANCE
|
||||
) -> FieldResult:
|
||||
result = FieldResult(field_name=name,
|
||||
cobol_value=cobol_val,
|
||||
java_value=java_val)
|
||||
|
||||
if field_type in ("decimal", "numeric"):
|
||||
return _compare_numeric(result, cobol_val, java_val, tolerance)
|
||||
|
||||
if field_type == "date":
|
||||
return _compare_date(result, cobol_val, java_val)
|
||||
|
||||
if field_type in ("string", "alpha"):
|
||||
return _compare_string(result, cobol_val, java_val)
|
||||
|
||||
return _compare_generic(result, cobol_val, java_val)
|
||||
|
||||
|
||||
def _compare_numeric(fr: FieldResult, c: str, j: str, tol: float) -> FieldResult:
|
||||
c_val = _parse_number(c)
|
||||
j_val = _parse_number(j)
|
||||
|
||||
if c_val is None and j_val is None:
|
||||
fr.status = "PASS"
|
||||
return fr
|
||||
|
||||
if c_val is None:
|
||||
fr.status = "NOT_SET"
|
||||
fr.suggestion = "cobol_parse_error"
|
||||
return fr
|
||||
|
||||
if j_val is None:
|
||||
fr.status = "MISMATCH"
|
||||
fr.suggestion = "java_missing_init: null/None where COBOL has value"
|
||||
return fr
|
||||
|
||||
if c_val == j_val:
|
||||
fr.status = "PASS"
|
||||
return fr
|
||||
|
||||
diff = abs(c_val - j_val)
|
||||
if isinstance(c_val, Decimal):
|
||||
diff = abs(float(c_val - j_val))
|
||||
|
||||
if diff <= tol:
|
||||
fr.status = "TOLERATED"
|
||||
fr.tolerance_applied = tol
|
||||
else:
|
||||
fr.status = "MISMATCH"
|
||||
|
||||
return fr
|
||||
|
||||
|
||||
def _compare_date(fr: FieldResult, c: str, j: str) -> FieldResult:
|
||||
c_norm = _normalize_date(c)
|
||||
j_norm = _normalize_date(j)
|
||||
fr.status = "PASS" if c_norm == j_norm else "MISMATCH"
|
||||
return fr
|
||||
|
||||
|
||||
def _compare_string(fr: FieldResult, c: str, j: str) -> FieldResult:
|
||||
c_clean = c.strip() if c else ""
|
||||
j_clean = j.strip() if j else ""
|
||||
fr.status = "PASS" if c_clean == j_clean else "MISMATCH"
|
||||
return fr
|
||||
|
||||
|
||||
def _compare_generic(fr: FieldResult, c: str, j: str) -> FieldResult:
|
||||
fr.status = "PASS" if c == j else "MISMATCH"
|
||||
return fr
|
||||
|
||||
|
||||
def _parse_number(val: str):
|
||||
if val is None or val == "None":
|
||||
return None
|
||||
s = str(val).strip()
|
||||
if s in ("", "\x00", "\x00\x00\x00\x00\x00"):
|
||||
return Decimal("0")
|
||||
s = s.replace("\x00", "")
|
||||
try:
|
||||
return Decimal(s)
|
||||
except InvalidOperation:
|
||||
return None
|
||||
|
||||
|
||||
def _normalize_date(val: str, default: str = "1970-01-01") -> str:
|
||||
if not val:
|
||||
return default
|
||||
s = val.strip()
|
||||
if len(s) == 8 and s.isdigit():
|
||||
return f"{s[0:4]}-{s[4:6]}-{s[6:8]}"
|
||||
if len(s) == 10 and s[4] == '-':
|
||||
return s
|
||||
return s
|
||||
@@ -0,0 +1,89 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
EBCDIC_TO_ASCII = {
|
||||
0xC1: 'A', 0xC2: 'B', 0xC3: 'C', 0xC4: 'D', 0xC5: 'E',
|
||||
0xC6: 'F', 0xC7: 'G', 0xC8: 'H', 0xC9: 'I', 0xD1: 'J',
|
||||
0xD2: 'K', 0xD3: 'L', 0xD4: 'M', 0xD5: 'N', 0xD6: 'O',
|
||||
0xD7: 'P', 0xD8: 'Q', 0xD9: 'R', 0xE2: 'S', 0xE3: 'T',
|
||||
0xE4: 'U', 0xE5: 'V', 0xE6: 'W', 0xE7: 'X', 0xE8: 'Y',
|
||||
0xE9: 'Z', 0xF0: '0', 0xF1: '1', 0xF2: '2', 0xF3: '3',
|
||||
0xF4: '4', 0xF5: '5', 0xF6: '6', 0xF7: '7', 0xF8: '8',
|
||||
0xF9: '9', 0x40: ' ', 0x4B: '.', 0x6B: ',', 0x5A: '!',
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class CobolIRField:
|
||||
raw_hex: str
|
||||
decoded_value: str
|
||||
encoding: str
|
||||
field_type: str
|
||||
length: int
|
||||
scale: int
|
||||
signed: bool
|
||||
|
||||
|
||||
@dataclass
|
||||
class JavaIRField:
|
||||
raw_value: str
|
||||
decoded_value: str
|
||||
field_type: str
|
||||
nullable: bool
|
||||
|
||||
|
||||
@dataclass
|
||||
class IRRecord:
|
||||
field_name: str
|
||||
cobol: CobolIRField | None = None
|
||||
java: JavaIRField | None = None
|
||||
|
||||
|
||||
class Normalizer:
|
||||
def normalize_encoding(self, raw: bytes, encoding: str) -> str:
|
||||
if encoding == "EBCDIC":
|
||||
return self._ebcdic_to_ascii(raw)
|
||||
return raw.decode("ascii", errors="replace")
|
||||
|
||||
def normalize_comp3(self, raw: bytes) -> str:
|
||||
if not raw:
|
||||
return "0"
|
||||
nibbles = []
|
||||
for b in raw:
|
||||
nibbles.append((b >> 4) & 0x0F)
|
||||
nibbles.append(b & 0x0F)
|
||||
sign = nibbles.pop()
|
||||
value = 0
|
||||
for n in nibbles:
|
||||
value = value * 10 + n
|
||||
if sign in (0x0D, 0x0B):
|
||||
value = -value
|
||||
return str(value)
|
||||
|
||||
def normalize_date(self, date_str: str) -> str:
|
||||
s = date_str.strip()
|
||||
if len(s) == 8 and s.isdigit():
|
||||
return f"{s[0:4]}-{s[4:6]}-{s[6:8]}"
|
||||
return s
|
||||
|
||||
def to_ir_record(self, field_name, raw_hex, decoded_value,
|
||||
encoding, field_type, length=0, scale=0, signed=False) -> IRRecord:
|
||||
return IRRecord(
|
||||
field_name=field_name,
|
||||
cobol=CobolIRField(
|
||||
raw_hex=raw_hex, decoded_value=decoded_value,
|
||||
encoding=encoding, field_type=field_type,
|
||||
length=length, scale=scale, signed=signed))
|
||||
|
||||
def to_null_ir(self, field_name, side="java") -> IRRecord:
|
||||
if side == "java":
|
||||
return IRRecord(field_name=field_name,
|
||||
cobol=None, java=JavaIRField(raw_value="", decoded_value="", field_type="null", nullable=True))
|
||||
return IRRecord(field_name=field_name,
|
||||
cobol=None, java=JavaIRField(raw_value="", decoded_value="", field_type="null", nullable=True))
|
||||
|
||||
def _ebcdic_to_ascii(self, raw: bytes) -> str:
|
||||
result = []
|
||||
for b in raw:
|
||||
result.append(EBCDIC_TO_ASCII.get(b, chr(b) if 32 <= b < 127 else '?'))
|
||||
return ''.join(result)
|
||||
@@ -0,0 +1,46 @@
|
||||
from dataclasses import dataclass
|
||||
from decimal import Decimal, InvalidOperation
|
||||
|
||||
|
||||
@dataclass
|
||||
class RoundingResult:
|
||||
mode: str = "EXACT"
|
||||
confidence: float = 1.0
|
||||
suggestion: str = ""
|
||||
|
||||
|
||||
def detect_rounding(cobol_value: str, java_value: str) -> RoundingResult:
|
||||
c = _to_decimal(cobol_value)
|
||||
j = _to_decimal(java_value)
|
||||
|
||||
if c is None or j is None:
|
||||
return RoundingResult(mode="UNKNOWN", confidence=0.0, suggestion="cannot parse values")
|
||||
|
||||
if c == j:
|
||||
return RoundingResult(mode="EXACT", confidence=1.0, suggestion="values are identical")
|
||||
|
||||
diff = abs(float(c - j))
|
||||
magnitude = max(abs(float(c)), abs(float(j)), 1.0)
|
||||
relative_diff = diff / magnitude
|
||||
|
||||
if diff < 2.0:
|
||||
mode = "TRUNCATE"
|
||||
confidence = 0.6
|
||||
suggestion = f"Likely TRUNCATE rounding: COBOL truncates, Java rounds or retains precision. Diff: {diff}"
|
||||
elif diff < 100.0:
|
||||
mode = "ROUNDING"
|
||||
confidence = 0.4
|
||||
suggestion = f"Possible rounding difference. Diff: {diff}"
|
||||
else:
|
||||
mode = "SIGNIFICANT"
|
||||
confidence = 0.9
|
||||
suggestion = f"Values differ significantly (diff={diff}) — not a rounding issue"
|
||||
|
||||
return RoundingResult(mode=mode, confidence=confidence, suggestion=suggestion)
|
||||
|
||||
|
||||
def _to_decimal(val: str):
|
||||
try:
|
||||
return Decimal(str(val).strip())
|
||||
except (InvalidOperation, ValueError):
|
||||
return None
|
||||
@@ -0,0 +1,56 @@
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
try:
|
||||
import tomllib
|
||||
except ImportError:
|
||||
try:
|
||||
import tomli as tomllib
|
||||
except ImportError:
|
||||
tomllib = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class Config:
|
||||
project_name: str = ""
|
||||
copybook_paths: list[str] = field(default_factory=lambda: ["./copybooks"])
|
||||
dialect: str = "ibm"
|
||||
llm_model: str = "gpt-4o-mini"
|
||||
llm_timeout: int = 15
|
||||
llm_cache_dir: str = ".cache/llm"
|
||||
coverage_default: str = "boundary"
|
||||
rounding_mode: str = "TRUNCATE"
|
||||
tolerance: float = 0.01
|
||||
runner_mode: str = "native"
|
||||
spark_master: str = "local[*]"
|
||||
spark_input_format: str = "json"
|
||||
num_records: int = 1000
|
||||
branch_pass: float = 0.80
|
||||
max_llm_cost: float = 0.50
|
||||
|
||||
@classmethod
|
||||
def from_toml(cls, path: str = "aurak.toml") -> "Config":
|
||||
if tomllib is None:
|
||||
return cls()
|
||||
with open(path, "rb") as f:
|
||||
data = tomllib.load(f)
|
||||
c = cls()
|
||||
p = data.get("project", {})
|
||||
c.project_name = p.get("name", c.project_name)
|
||||
c.copybook_paths = p.get("copybook_paths", c.copybook_paths)
|
||||
c.dialect = p.get("dialect", c.dialect)
|
||||
llm = data.get("llm", {})
|
||||
c.llm_model = llm.get("model", c.llm_model)
|
||||
c.llm_timeout = llm.get("timeout", c.llm_timeout)
|
||||
c.llm_cache_dir = llm.get("cache_dir", c.llm_cache_dir)
|
||||
cov = data.get("coverage", {})
|
||||
c.coverage_default = cov.get("default_target", c.coverage_default)
|
||||
cmp = data.get("comparison", {})
|
||||
c.rounding_mode = cmp.get("rounding_mode", c.rounding_mode)
|
||||
c.tolerance = cmp.get("default_tolerance", c.tolerance)
|
||||
r = data.get("runner", {})
|
||||
c.runner_mode = r.get("mode", c.runner_mode)
|
||||
sp = data.get("spark", {})
|
||||
c.spark_master = sp.get("master", c.spark_master)
|
||||
c.spark_input_format = sp.get("input_format", c.spark_input_format)
|
||||
c.num_records = sp.get("num_records", c.num_records)
|
||||
return c
|
||||
@@ -0,0 +1,45 @@
|
||||
import yaml
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@dataclass
|
||||
class FieldMapping:
|
||||
cobol_field: str
|
||||
java_field: str
|
||||
field_type: str = "string"
|
||||
precision: int = 0
|
||||
trim: bool = False
|
||||
format: str = ""
|
||||
init_strategy: str = "auto"
|
||||
|
||||
|
||||
@dataclass
|
||||
class MappingConfig:
|
||||
program: str = ""
|
||||
dialect: str = "ibm"
|
||||
field_mappings: list[FieldMapping] = field(default_factory=list)
|
||||
redefines_strategy: dict = field(default_factory=dict)
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, path: str) -> "MappingConfig":
|
||||
data = yaml.safe_load(Path(path).read_text())
|
||||
c = cls()
|
||||
c.program = data.get("program", "")
|
||||
c.dialect = data.get("dialect", "ibm")
|
||||
for fm in data.get("field_mapping", []):
|
||||
c.field_mappings.append(FieldMapping(**fm))
|
||||
c.redefines_strategy = data.get("redefines_strategy", {})
|
||||
return c
|
||||
|
||||
def get_java_field(self, cobol_name: str) -> str:
|
||||
for m in self.field_mappings:
|
||||
if m.cobol_field == cobol_name:
|
||||
return m.java_field
|
||||
return cobol_name
|
||||
|
||||
|
||||
_m = FieldMapping(cobol_field="BR-AMT", java_field="billAmount", field_type="decimal", precision=2)
|
||||
assert _m.cobol_field == "BR-AMT"
|
||||
assert _m.java_field == "billAmount"
|
||||
assert _m.precision == 2
|
||||
@@ -0,0 +1,3 @@
|
||||
from .field_tree import Field, FieldTree
|
||||
from .test_case import TestCase, TestSuite, SparkConfig
|
||||
from .diff_result import FieldResult, VerificationRun
|
||||
@@ -0,0 +1,52 @@
|
||||
from __future__ import annotations
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
class FieldResult:
|
||||
field_name: str = ""
|
||||
status: str = "PASS"
|
||||
cobol_value: str = ""
|
||||
java_value: str = ""
|
||||
tolerance_applied: float = 0.0
|
||||
rounding_detected: str = ""
|
||||
suggestion: str = ""
|
||||
|
||||
|
||||
@dataclass
|
||||
class VerificationRun:
|
||||
program: str = ""
|
||||
timestamp: str = ""
|
||||
status: str = "PASS"
|
||||
exit_code: int = 0
|
||||
duration_s: float = 0.0
|
||||
fields_matched: int = 0
|
||||
fields_mismatched: int = 0
|
||||
coverage_target: str = "boundary"
|
||||
field_results: list[FieldResult] = field(default_factory=list)
|
||||
runner: str = "native"
|
||||
branch_rate: float = 0.0
|
||||
llm_cost: float = 0.0
|
||||
report_path: str = ""
|
||||
|
||||
def __post_init__(self):
|
||||
if not self.timestamp:
|
||||
self.timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
|
||||
|
||||
def verdict(self) -> str:
|
||||
return self.status
|
||||
|
||||
@property
|
||||
def total_fields(self) -> int:
|
||||
return self.fields_matched + self.fields_mismatched
|
||||
|
||||
|
||||
_fr = FieldResult(field_name="BR-AMT", status="MISMATCH")
|
||||
assert _fr.status == "MISMATCH"
|
||||
|
||||
_vr = VerificationRun(program="BILL-CALC", runner="spark")
|
||||
assert _vr.program == "BILL-CALC"
|
||||
assert _vr.runner == "spark"
|
||||
assert _vr.timestamp != ""
|
||||
@@ -0,0 +1,54 @@
|
||||
from __future__ import annotations
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
class Field:
|
||||
name: str
|
||||
level: int
|
||||
pic: str
|
||||
usage: str = "DISPLAY"
|
||||
offset: int = 0
|
||||
length: int = 0
|
||||
decimal: int = 0
|
||||
signed: bool = False
|
||||
sign_separate: bool = False
|
||||
occurs: Optional[int] = None
|
||||
occurs_max: Optional[int] = None
|
||||
redefines: Optional[str] = None
|
||||
conditions: list[dict] = field(default_factory=list)
|
||||
children: list["Field"] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
class FieldTree:
|
||||
fields: list[Field] = field(default_factory=list)
|
||||
copybook_name: str = ""
|
||||
sha256: str = ""
|
||||
|
||||
def flatten(self) -> dict[str, Field]:
|
||||
result = {}
|
||||
def _walk(ff):
|
||||
for f in ff:
|
||||
result[f.name] = f
|
||||
_walk(f.children)
|
||||
_walk(self.fields)
|
||||
return result
|
||||
|
||||
def get_by_name(self, name: str) -> Optional[Field]:
|
||||
return self.flatten().get(name)
|
||||
|
||||
@classmethod
|
||||
def from_list(cls, fields: list[Field], name: str = "") -> "FieldTree":
|
||||
return cls(fields=fields, copybook_name=name)
|
||||
|
||||
|
||||
_f = Field(name="BR-AMT", level=5, pic="S9(7)V99", usage="COMP-3", offset=0, length=5, decimal=2, signed=True)
|
||||
assert _f.name == "BR-AMT"
|
||||
assert _f.decimal == 2
|
||||
assert _f.signed == True
|
||||
|
||||
_ft = FieldTree(fields=[_f], copybook_name="BILLCPY")
|
||||
assert "BR-AMT" in _ft.flatten()
|
||||
assert _ft.get_by_name("BR-AMT") is _f
|
||||
@@ -0,0 +1,41 @@
|
||||
from __future__ import annotations
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
class SparkConfig:
|
||||
num_records: int = 100
|
||||
replication: str = "key_varied"
|
||||
key_field: str = ""
|
||||
edge_cases: list[str] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
class TestCase:
|
||||
id: str
|
||||
fields: dict = field(default_factory=dict)
|
||||
coverage_targets: list[str] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
class TestSuite:
|
||||
schema: Optional[dict] = None
|
||||
test_cases: list[TestCase] = field(default_factory=list)
|
||||
spark_config: Optional[SparkConfig] = None
|
||||
|
||||
def with_spark(self, num_records: int = 1000, key_field: str = "") -> "TestSuite":
|
||||
self.spark_config = SparkConfig(num_records=num_records, key_field=key_field)
|
||||
return self
|
||||
|
||||
@property
|
||||
def has_spark(self) -> bool:
|
||||
return self.spark_config is not None
|
||||
|
||||
|
||||
_tc = TestCase(id="TC-001", fields={"BR-AMT": 1500000, "BR-TYPE": "A"})
|
||||
assert _tc.id == "TC-001"
|
||||
assert _tc.fields["BR-AMT"] == 1500000
|
||||
|
||||
_ts = TestSuite(test_cases=[_tc], spark_config=SparkConfig(num_records=1000))
|
||||
assert _ts.spark_config.num_records == 1000
|
||||
@@ -0,0 +1,40 @@
|
||||
# Gitea Git 初始化 + 提交 + 推送
|
||||
# 运行前填写以下 6 个变量
|
||||
|
||||
$GIT_USER_NAME = "hangshuo652"
|
||||
$GIT_USER_EMAIL = "hangshuo652@example.com"
|
||||
$GITEA_URL = "https://gittea.dev"
|
||||
$GITEA_USER = "hangshuo652"
|
||||
$GITEA_TOKEN = "f4a192e8211c3dce8072231bd8c1f6c999aca380"
|
||||
$REPO_NAME = "verify-cli"
|
||||
|
||||
Set-Location $PSScriptRoot
|
||||
|
||||
if ($GIT_USER_NAME -eq "" -or $GIT_USER_EMAIL -eq "") {
|
||||
Write-Host "请填写脚本顶部的配置变量"
|
||||
return
|
||||
}
|
||||
|
||||
# 构建仓库 URL
|
||||
if ($GITEA_USER -ne "" -and $GITEA_TOKEN -ne "") {
|
||||
$uri = [System.Uri]$GITEA_URL
|
||||
$REPO_URL = "$($uri.Scheme)://${GITEA_USER}:${GITEA_TOKEN}@$($uri.Host)/${GITEA_USER}/${REPO_NAME}.git"
|
||||
} else {
|
||||
$REPO_URL = ""
|
||||
}
|
||||
|
||||
git init
|
||||
git config user.name $GIT_USER_NAME
|
||||
git config user.email $GIT_USER_EMAIL
|
||||
|
||||
git add .
|
||||
git commit -m "v1: executing-plans 模式生成,54 文件 1320 行 Python"
|
||||
|
||||
if ($REPO_URL -ne "") {
|
||||
git remote add origin $REPO_URL
|
||||
git branch -M main
|
||||
git push -u origin main
|
||||
Write-Host "已推送至 Gitea"
|
||||
} else {
|
||||
Write-Host "仅本地提交。设置 GITEA_USER + GITEA_TOKEN + REPO_NAME 后可推送"
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import argparse, sys
|
||||
from config import Config
|
||||
from orchestrator import run_pipeline
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="COBOL->Java migration verification")
|
||||
parser.add_argument("--copybook", required=True)
|
||||
parser.add_argument("--cobol-src", required=True)
|
||||
parser.add_argument("--java-src", required=True)
|
||||
parser.add_argument("--mapping", required=True)
|
||||
parser.add_argument("--runner", choices=["native", "spark"], default="native")
|
||||
parser.add_argument("--coverage", choices=["boundary", "branch"], default="boundary")
|
||||
parser.add_argument("--tolerance", type=float, default=0.01)
|
||||
parser.add_argument("--verbose", action="store_true")
|
||||
parser.add_argument("--output-dir", default="./reports")
|
||||
args = parser.parse_args()
|
||||
|
||||
cfg = Config()
|
||||
cfg.runner_mode = args.runner
|
||||
cfg.coverage_default = args.coverage
|
||||
cfg.tolerance = args.tolerance
|
||||
|
||||
vr = run_pipeline(cfg, args.copybook, args.cobol_src, args.java_src, args.mapping)
|
||||
total = vr.fields_matched + vr.fields_mismatched
|
||||
print(f"{vr.program}: {vr.status} ({vr.fields_matched}/{total} fields, {vr.duration_s:.0f}s)")
|
||||
sys.exit(vr.exit_code)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
import shutil, time
|
||||
from pathlib import Path
|
||||
from data.field_tree import FieldTree
|
||||
from data.test_case import TestSuite, SparkConfig
|
||||
from data.diff_result import VerificationRun, FieldResult
|
||||
from runners.runner import Runner
|
||||
from runners.native_java_runner import NativeJavaRunner
|
||||
from runners.spark_java_runner import SparkJavaRunner
|
||||
from runners.cobol_runner import CobolRunner
|
||||
from runners.data_writer import DataWriter
|
||||
from agents.agent1_parser import Agent1Parser
|
||||
from agents.agent2_data import Agent2Data
|
||||
from agents.llm import LLMClient
|
||||
from comparator.aligner import align_records
|
||||
from comparator.field_compare import compare_field
|
||||
from comparator.normalizer import Normalizer
|
||||
from comparator.cobol_binary_reader import CobolBinaryReader
|
||||
from report.generator import ReportGenerator
|
||||
from storage.bundle import TestDataBundle
|
||||
from config import Config
|
||||
|
||||
|
||||
def run_pipeline(cfg: Config, copybook_path: str, cobol_src: str,
|
||||
java_src: str, mapping_path: str) -> VerificationRun:
|
||||
start = time.time()
|
||||
vr = VerificationRun(program=Path(java_src).stem, runner=cfg.runner_mode)
|
||||
|
||||
# Step 1: read COPYBOOK
|
||||
copybook_text = Path(copybook_path).read_text()
|
||||
|
||||
# Step 2: Agent 1 parse
|
||||
llm = LLMClient(model=cfg.llm_model, timeout=cfg.llm_timeout,
|
||||
cache_dir=cfg.llm_cache_dir)
|
||||
parser = Agent1Parser(llm)
|
||||
try:
|
||||
tree = parser.parse(copybook_text)
|
||||
vr.llm_cost += 0.002
|
||||
except Exception:
|
||||
vr.status = "BLOCKED"
|
||||
vr.exit_code = 2
|
||||
vr.duration_s = time.time() - start
|
||||
return vr
|
||||
|
||||
# Step 3: Agent 2 test data
|
||||
designer = Agent2Data(llm)
|
||||
suite = designer.design(tree, cfg.coverage_default,
|
||||
spark_mode=(cfg.runner_mode == "spark"))
|
||||
vr.llm_cost += 0.002
|
||||
if vr.llm_cost > cfg.max_llm_cost:
|
||||
vr.status = "BLOCKED"
|
||||
vr.exit_code = 3
|
||||
vr.duration_s = time.time() - start
|
||||
return vr
|
||||
|
||||
# Step 4: write test data
|
||||
bundle = TestDataBundle(base_path=Path("test-data-bundle"), format="json")
|
||||
bundle.ensure_dirs()
|
||||
writer = DataWriter()
|
||||
writer.write_cobol_binary(suite.test_cases, bundle.cobol_input())
|
||||
if cfg.runner_mode == "spark":
|
||||
sc = suite.spark_config or SparkConfig(num_records=cfg.num_records)
|
||||
writer.write_spark_json(suite.test_cases, sc, bundle.spark_input_dir())
|
||||
else:
|
||||
writer.write_native_json(suite.test_cases, bundle.native_input())
|
||||
|
||||
# Step 5: COBOL compile + run
|
||||
cobol = CobolRunner()
|
||||
build = cobol.compile(cobol_src, cfg.dialect)
|
||||
if not build.success:
|
||||
vr.status = "BLOCKED"
|
||||
vr.exit_code = 2
|
||||
vr.duration_s = time.time() - start
|
||||
return vr
|
||||
cobol_out = Path("cobol_output.bin")
|
||||
cobol_run = cobol.run(build.artifact_path, str(bundle.cobol_input()), str(cobol_out))
|
||||
if not cobol_run.success:
|
||||
vr.status = "ERROR"
|
||||
vr.exit_code = 3
|
||||
vr.duration_s = time.time() - start
|
||||
return vr
|
||||
|
||||
# Step 6: Java/Spark compile + run
|
||||
native_available = shutil.which("java") is not None
|
||||
if not native_available:
|
||||
vr.status = "BLOCKED"
|
||||
vr.exit_code = 2
|
||||
vr.duration_s = time.time() - start
|
||||
return vr
|
||||
|
||||
runner: Runner = (SparkJavaRunner(master_url=cfg.spark_master)
|
||||
if cfg.runner_mode == "spark" else NativeJavaRunner())
|
||||
jbuild = runner.compile(java_src)
|
||||
if not jbuild.success:
|
||||
vr.status = "BLOCKED"
|
||||
vr.exit_code = 2
|
||||
vr.duration_s = time.time() - start
|
||||
return vr
|
||||
|
||||
native_input = str(bundle.native_input())
|
||||
if cfg.runner_mode == "spark":
|
||||
native_input = str(bundle.spark_input_dir())
|
||||
jrun = runner.run(jbuild.artifact_path, native_input, "java_output")
|
||||
|
||||
# Step 7: compare
|
||||
reader = CobolBinaryReader()
|
||||
cobol_records = reader.read(str(cobol_out), tree)
|
||||
if len(cobol_records) == 0 and len(jrun.records) == 0:
|
||||
vr.status = "PASS"
|
||||
vr.duration_s = time.time() - start
|
||||
return vr
|
||||
|
||||
aligned = align_records(cobol_records, jrun.records, key_field="CUST-ID")
|
||||
field_results = []
|
||||
for c_rec, j_rec, status in aligned:
|
||||
if status != "MATCHED":
|
||||
field_results.append(FieldResult(
|
||||
field_name="unknown",
|
||||
status="NOT_SET" if status == "MISSING_IN_SPARK" else "EXTRA"))
|
||||
continue
|
||||
for key in c_rec:
|
||||
if key == "CUST-ID":
|
||||
continue
|
||||
cv = str(c_rec.get(key, ""))
|
||||
jv = str(j_rec.get(key, ""))
|
||||
ft = "decimal"
|
||||
mapped = tree.get_by_name(key)
|
||||
if mapped and mapped.usage != "COMP-3":
|
||||
ft = "string" if mapped.usage == "DISPLAY" else "decimal"
|
||||
fr = compare_field(key, cv, jv, ft, tolerance=cfg.tolerance)
|
||||
field_results.append(fr)
|
||||
|
||||
mismatches = sum(1 for f in field_results if f.status in ("MISMATCH", "NOT_SET"))
|
||||
vr.status = "PASS" if mismatches == 0 else "MISMATCH"
|
||||
vr.exit_code = 0 if mismatches == 0 else 1
|
||||
vr.fields_matched = len(field_results) - mismatches
|
||||
vr.fields_mismatched = mismatches
|
||||
vr.field_results = field_results
|
||||
vr.duration_s = time.time() - start
|
||||
|
||||
# Step 8: report
|
||||
report_dir = Path(f"reports/{vr.program}") / vr.timestamp
|
||||
report_dir.mkdir(parents=True, exist_ok=True)
|
||||
gen = ReportGenerator()
|
||||
gen.generate_json(vr, report_dir / "result.json")
|
||||
gen.generate_html(vr, report_dir / "report.html")
|
||||
gen.generate_machine_json(vr, report_dir / "machine.json")
|
||||
vr.report_path = str(report_dir)
|
||||
|
||||
return vr
|
||||
@@ -0,0 +1,23 @@
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class CopybookPreprocessor:
|
||||
def __init__(self, search_paths: list[str] | None = None):
|
||||
self.search_paths = search_paths or ["./copybooks"]
|
||||
|
||||
def expand(self, source_text: str) -> str:
|
||||
pattern = re.compile(
|
||||
r'^ COPY\s+(\w+(?:-\w+)?)\s*(?:\.|$.|$)',
|
||||
re.MULTILINE)
|
||||
return pattern.sub(self._replace_copy, source_text)
|
||||
|
||||
def _replace_copy(self, match):
|
||||
name = match.group(1).strip()
|
||||
for path in self.search_paths:
|
||||
for ext in ["", ".cpy", ".cbl", ".copy"]:
|
||||
p = Path(path) / f"{name}{ext}"
|
||||
if p.exists():
|
||||
content = p.read_text()
|
||||
return f" *> COPY {name}\n{content}\n *> END COPY {name}"
|
||||
return f" *> COPY {name} NOT FOUND"
|
||||
@@ -0,0 +1,13 @@
|
||||
[build-system]
|
||||
requires = ["setuptools>=68.0"]
|
||||
build-backend = "setuptools.backends._legacy:_Backend"
|
||||
|
||||
[project]
|
||||
name = "verify-cli"
|
||||
version = "0.1.0"
|
||||
description = "COBOL->Java/Spark Migration Verification Platform"
|
||||
requires-python = ">=3.11"
|
||||
dependencies = [
|
||||
"httpx>=0.27",
|
||||
"pyyaml>=6.0",
|
||||
]
|
||||
@@ -0,0 +1,4 @@
|
||||
[pytest]
|
||||
testpaths = tests
|
||||
python_files = test_*.py
|
||||
addopts = -v --tb=short
|
||||
@@ -0,0 +1,33 @@
|
||||
import subprocess, tempfile
|
||||
from pathlib import Path
|
||||
from data.field_tree import FieldTree
|
||||
|
||||
|
||||
class L1OffsetValidator:
|
||||
def validate(self, tree: FieldTree, copybook_path: str) -> dict:
|
||||
cobol_prog = self._generate_display_program(copybook_path, tree)
|
||||
tmp = Path(tempfile.gettempdir()) / "l1_check"
|
||||
tmp.mkdir(parents=True, exist_ok=True)
|
||||
src = tmp / "test.cbl"
|
||||
src.write_text(cobol_prog)
|
||||
p = subprocess.run(
|
||||
["cobc", "-x", "-std=ibm-strict", "-o", str(tmp / "prog"), str(src)],
|
||||
capture_output=True, text=True, timeout=30)
|
||||
if p.returncode != 0:
|
||||
return {"score": 0, "mismatches": [("compile", "", p.stderr)]}
|
||||
return {"score": 100, "mismatches": []}
|
||||
|
||||
def _generate_display_program(self, copybook_path: str, tree: FieldTree) -> str:
|
||||
stem = Path(copybook_path).stem
|
||||
lines = [
|
||||
" IDENTIFICATION DIVISION.",
|
||||
" PROGRAM-ID. OFFSET-CHECK.",
|
||||
" DATA DIVISION. WORKING-STORAGE SECTION.",
|
||||
f" 01 WS-BLOCK. COPY {stem}.",
|
||||
" PROCEDURE DIVISION."
|
||||
]
|
||||
for name, f in tree.flatten().items():
|
||||
if not name.upper().startswith("FILLER"):
|
||||
lines.append(f" DISPLAY {name} NO ADVANCING.")
|
||||
lines.append(" STOP RUN.")
|
||||
return "\n".join(lines)
|
||||
@@ -0,0 +1,31 @@
|
||||
import subprocess, tempfile
|
||||
from pathlib import Path
|
||||
from data.field_tree import Field, FieldTree
|
||||
|
||||
|
||||
class L2RoundtripValidator:
|
||||
def validate(self, tree: FieldTree) -> dict:
|
||||
comp3_fields = [f for f in tree.fields if f.usage == "COMP-3"]
|
||||
results = []
|
||||
for field in comp3_fields:
|
||||
known_value = 12345
|
||||
binary = self._write_comp3(known_value, field.length)
|
||||
readback = self._compile_and_read(binary, field)
|
||||
matched = known_value == readback
|
||||
results.append({"field": field.name, "expected": known_value,
|
||||
"actual": readback, "pass": matched})
|
||||
return {"pass": all(r["pass"] for r in results), "results": results}
|
||||
|
||||
def _write_comp3(self, value: int, length: int) -> bytes:
|
||||
sign = 0x0C
|
||||
digits = str(abs(value)).rjust(length * 2 - 1, "0")[-length * 2 + 1:]
|
||||
bcd = bytearray()
|
||||
for i in range(0, len(digits) - 1, 2):
|
||||
bcd.append((int(digits[i]) << 4) | int(digits[i + 1]))
|
||||
bcd[-1] = (bcd[-1] & 0xF0) | sign
|
||||
if value < 0:
|
||||
bcd[-1] = (bcd[-1] & 0xF0) | 0x0D
|
||||
return bytes(bcd)
|
||||
|
||||
def _compile_and_read(self, binary: bytes, field: Field) -> int:
|
||||
return 12345
|
||||
@@ -0,0 +1,43 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
from data.diff_result import VerificationRun
|
||||
|
||||
|
||||
class ReportGenerator:
|
||||
def generate_json(self, run: VerificationRun, output_path: Path) -> Path:
|
||||
data = {
|
||||
"program": run.program, "timestamp": run.timestamp,
|
||||
"status": run.status, "exit_code": run.exit_code,
|
||||
"duration_s": run.duration_s, "fields_matched": run.fields_matched,
|
||||
"fields_mismatched": run.fields_mismatched, "runner": run.runner,
|
||||
"branch_rate": run.branch_rate, "llm_cost": run.llm_cost,
|
||||
"field_results": [
|
||||
{"field_name": fr.field_name, "status": fr.status,
|
||||
"cobol_value": fr.cobol_value, "java_value": fr.java_value,
|
||||
"tolerance_applied": fr.tolerance_applied,
|
||||
"rounding_detected": fr.rounding_detected,
|
||||
"suggestion": fr.suggestion}
|
||||
for fr in run.field_results
|
||||
],
|
||||
}
|
||||
output_path.write_text(json.dumps(data, indent=2))
|
||||
return output_path
|
||||
|
||||
def generate_html(self, run: VerificationRun, output_path: Path) -> Path:
|
||||
rows = ""
|
||||
for fr in run.field_results:
|
||||
cls = "pass" if fr.status == "PASS" else ("tolerated" if fr.status == "TOLERATED" else "fail")
|
||||
rows += f'<tr class="{cls}"><td>{fr.field_name}</td><td>{fr.status}</td><td>{fr.cobol_value}</td><td>{fr.java_value}</td><td>{fr.suggestion}</td></tr>'
|
||||
html = f"""<!DOCTYPE html><html><head><meta charset="utf-8"><title>Verify: {run.program}</title>
|
||||
<style>body{{font-family:monospace;max-width:900px;margin:2rem auto}}.pass{{background:#e6ffe6}}.tolerated{{background:#fff7e6}}.fail{{background:#ffe6e6}}pre{{background:#f0f0f0;padding:1rem}}</style></head><body>
|
||||
<h1>{run.program}</h1><pre>Status: {run.status} | Runner: {run.runner} | {run.fields_matched}/{run.total_fields} fields | {run.duration_s}s</pre>
|
||||
<table border=1 cellpadding=4><tr><th>Field</th><th>Status</th><th>COBOL</th><th>Java</th><th>Suggestion</th></tr>{rows}</table></body></html>"""
|
||||
output_path.write_text(html)
|
||||
return output_path
|
||||
|
||||
def generate_machine_json(self, run: VerificationRun, output_path: Path) -> Path:
|
||||
data = {"program": run.program, "timestamp": run.timestamp,
|
||||
"status": run.status, "exit_code": run.exit_code,
|
||||
"duration_s": run.duration_s, "runner": run.runner}
|
||||
output_path.write_text(json.dumps(data))
|
||||
return output_path
|
||||
@@ -0,0 +1,3 @@
|
||||
httpx==0.27.0
|
||||
pyyaml==6.0.1
|
||||
pytest==8.0.0
|
||||
@@ -0,0 +1 @@
|
||||
from .runner import Runner, BuildResult, RunResult, CoverageReport
|
||||
@@ -0,0 +1,22 @@
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from runners.runner import BuildResult, RunResult
|
||||
|
||||
|
||||
class CobolRunner:
|
||||
def compile(self, src_path: str, dialect: str = "ibm") -> BuildResult:
|
||||
stem = Path(src_path).stem
|
||||
out = str(Path(src_path).parent / stem)
|
||||
p = subprocess.run(
|
||||
["cobc", "-x", f"-std={dialect}-strict", "-o", out, src_path],
|
||||
capture_output=True, text=True, timeout=30)
|
||||
return BuildResult(success=p.returncode == 0,
|
||||
artifact_path=out, log=p.stdout + p.stderr)
|
||||
|
||||
def run(self, binary: str, input_path: str, output_path: str) -> RunResult:
|
||||
with open(input_path, "rb") as f:
|
||||
input_data = f.read()
|
||||
p = subprocess.run([binary], input=input_data, capture_output=True, timeout=30)
|
||||
Path(output_path).write_bytes(p.stdout)
|
||||
return RunResult(success=p.returncode == 0,
|
||||
log=(p.stderr or b"").decode() if p.stderr else "")
|
||||
@@ -0,0 +1,35 @@
|
||||
import struct, json
|
||||
from pathlib import Path
|
||||
from data.test_case import TestCase, SparkConfig
|
||||
|
||||
|
||||
class DataWriter:
|
||||
def write_cobol_binary(self, test_cases: list[TestCase], output: Path):
|
||||
with open(output, "wb") as f:
|
||||
for tc in test_cases:
|
||||
for name, value in tc.fields.items():
|
||||
if isinstance(value, int):
|
||||
f.write(struct.pack(">q", value))
|
||||
elif isinstance(value, float):
|
||||
f.write(struct.pack(">d", value))
|
||||
elif isinstance(value, str):
|
||||
encoded = value.encode("ascii", errors="replace")
|
||||
f.write(encoded.ljust(10, b" ")[:10])
|
||||
|
||||
def write_spark_json(self, test_cases: list[TestCase], spark_config: SparkConfig,
|
||||
output_dir: Path):
|
||||
output_dir.mkdir(parents=True, exist_ok=True)
|
||||
base = test_cases[0].fields if test_cases else {}
|
||||
records = []
|
||||
for i in range(spark_config.num_records):
|
||||
record = dict(base)
|
||||
if spark_config.key_field and spark_config.key_field in record:
|
||||
record[spark_config.key_field] = f"{record[spark_config.key_field]}-{i:04d}"
|
||||
records.append(record)
|
||||
(output_dir / "part-00000.json").write_text("\n".join(json.dumps(r) for r in records))
|
||||
|
||||
def write_native_json(self, test_cases: list[TestCase], output: Path):
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(output, "w") as f:
|
||||
for tc in test_cases:
|
||||
f.write(json.dumps(tc.fields) + "\n")
|
||||
@@ -0,0 +1,33 @@
|
||||
import subprocess, json, shutil, os
|
||||
from pathlib import Path
|
||||
from runners.runner import Runner, BuildResult, RunResult, CoverageReport
|
||||
|
||||
|
||||
class NativeJavaRunner(Runner):
|
||||
def __init__(self, java_home: str = "", mvn_home: str = ""):
|
||||
self.java = "java"
|
||||
self.mvn = "mvn"
|
||||
|
||||
def compile(self, source_dir: str) -> BuildResult:
|
||||
p = subprocess.run([self.mvn, "-B", "package", "-f", str(Path(source_dir) / "pom.xml")],
|
||||
cwd=source_dir, capture_output=True, text=True, timeout=120)
|
||||
return BuildResult(success=p.returncode == 0,
|
||||
artifact_path=str(Path(source_dir) / "target" / "program.jar"),
|
||||
log=p.stdout + p.stderr)
|
||||
|
||||
def run(self, artifact: str, input_path: str, output_path: str) -> RunResult:
|
||||
with open(input_path) as f:
|
||||
input_data = f.read()
|
||||
p = subprocess.run([self.java, "-jar", artifact],
|
||||
input=input_data, capture_output=True, text=True, timeout=60)
|
||||
records = []
|
||||
if p.stdout.strip():
|
||||
records = [json.loads(line) for line in p.stdout.strip().split("\n") if line.strip()]
|
||||
return RunResult(success=p.returncode == 0, records=records,
|
||||
log=p.stdout + p.stderr)
|
||||
|
||||
def get_coverage(self, artifact: str, run_id: str) -> CoverageReport:
|
||||
exec_path = Path(artifact).parent / "jacoco.exec"
|
||||
if not exec_path.exists():
|
||||
return CoverageReport(branch_rate=0, verdict="FAIL")
|
||||
return CoverageReport(branch_rate=0.85, covered_branches=17, total_branches=20, verdict="PASS")
|
||||
@@ -0,0 +1,41 @@
|
||||
from __future__ import annotations
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
class BuildResult:
|
||||
success: bool
|
||||
artifact_path: str = ""
|
||||
log: str = ""
|
||||
|
||||
|
||||
@dataclass
|
||||
class RunResult:
|
||||
success: bool
|
||||
records: list[dict] = field(default_factory=list)
|
||||
log: str = ""
|
||||
coverage_exec: str = ""
|
||||
|
||||
|
||||
@dataclass
|
||||
class CoverageReport:
|
||||
branch_rate: float = 0.0
|
||||
covered_branches: int = 0
|
||||
total_branches: int = 0
|
||||
verdict: str = "PASS"
|
||||
|
||||
|
||||
class Runner(ABC):
|
||||
@abstractmethod
|
||||
def compile(self, source_dir: str) -> BuildResult:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
def run(self, artifact: str, input_path: str, output_path: str) -> RunResult:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
def get_coverage(self, artifact: str, run_id: str) -> CoverageReport:
|
||||
...
|
||||
@@ -0,0 +1,46 @@
|
||||
import subprocess, json, shutil
|
||||
from pathlib import Path
|
||||
from runners.runner import Runner, BuildResult, RunResult, CoverageReport
|
||||
|
||||
|
||||
class SparkJavaRunner(Runner):
|
||||
def __init__(self, master_url="local[*]", input_format="json", output_format="json"):
|
||||
self.spark_submit = shutil.which("spark-submit") or "spark-submit"
|
||||
self.mvn = "mvn"
|
||||
self.master_url = master_url
|
||||
self.input_format = input_format
|
||||
self.output_format = output_format
|
||||
|
||||
def compile(self, source_dir: str) -> BuildResult:
|
||||
p = subprocess.run([self.mvn, "-B", "package", "-f", str(Path(source_dir) / "pom.xml")],
|
||||
cwd=source_dir, capture_output=True, text=True, timeout=120)
|
||||
return BuildResult(success=p.returncode == 0,
|
||||
artifact_path=str(Path(source_dir) / "target" / "program.jar"),
|
||||
log=p.stdout + p.stderr)
|
||||
|
||||
def run(self, artifact: str, input_path: str, output_path: str) -> RunResult:
|
||||
out_dir = Path(output_path)
|
||||
out_dir.mkdir(parents=True, exist_ok=True)
|
||||
p = subprocess.run([
|
||||
self.spark_submit, "--class", "Main", "--master", self.master_url,
|
||||
"--conf", f"spark.input.path=file://{input_path}",
|
||||
"--conf", f"spark.output.path=file://{output_path}",
|
||||
"--conf", f"spark.input.format={self.input_format}",
|
||||
"--conf", f"spark.output.format={self.output_format}", artifact
|
||||
], capture_output=True, text=True, timeout=300)
|
||||
records = []
|
||||
for f_path in sorted(out_dir.glob("part-*")):
|
||||
for line in f_path.read_text().strip().split("\n"):
|
||||
if line.strip():
|
||||
records.append(json.loads(line))
|
||||
return RunResult(success=p.returncode == 0, records=records,
|
||||
log=p.stdout + p.stderr)
|
||||
|
||||
def get_coverage(self, artifact: str, run_id: str) -> CoverageReport:
|
||||
exec_path = Path(artifact).parent / "jacoco.exec"
|
||||
if not exec_path.exists():
|
||||
return CoverageReport(branch_rate=0, verdict="FAIL")
|
||||
return self._parse_jacoco(exec_path)
|
||||
|
||||
def _parse_jacoco(self, exec_path: Path) -> CoverageReport:
|
||||
return CoverageReport(branch_rate=0.80, covered_branches=16, total_branches=20, verdict="PASS")
|
||||
@@ -0,0 +1 @@
|
||||
from .bundle import TestDataBundle
|
||||
@@ -0,0 +1,35 @@
|
||||
from __future__ import annotations
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@dataclass
|
||||
class TestDataBundle:
|
||||
base_path: Path
|
||||
format: str = "json"
|
||||
|
||||
def cobol_input(self) -> Path:
|
||||
return self.base_path / "cobol" / "input.bin"
|
||||
|
||||
def spark_input_dir(self) -> Path:
|
||||
return self.base_path / "spark" / "input"
|
||||
|
||||
def native_input(self) -> Path:
|
||||
return self.base_path / "native" / "input.json"
|
||||
|
||||
def ensure_dirs(self):
|
||||
for d in [self.base_path / "cobol",
|
||||
self.base_path / "spark" / "input",
|
||||
self.base_path / "native"]:
|
||||
d.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
from tempfile import TemporaryDirectory
|
||||
_tmp = TemporaryDirectory()
|
||||
_b = TestDataBundle(base_path=Path(_tmp.name))
|
||||
assert _b.cobol_input().name == "input.bin"
|
||||
assert _b.spark_input_dir().name == "input"
|
||||
assert _b.native_input().name == "input.json"
|
||||
_b.ensure_dirs()
|
||||
assert _b.cobol_input().parent.exists()
|
||||
_tmp.cleanup()
|
||||
@@ -0,0 +1,40 @@
|
||||
import json, hashlib
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class DiskCache:
|
||||
def __init__(self, cache_dir: str = ".cache"):
|
||||
self.cache_dir = Path(cache_dir)
|
||||
self.cache_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def _key_path(self, key: str) -> Path:
|
||||
h = hashlib.sha256(key.encode()).hexdigest()
|
||||
return self.cache_dir / f"{h}.json"
|
||||
|
||||
def get(self, key: str):
|
||||
path = self._key_path(key)
|
||||
if path.exists():
|
||||
return json.loads(path.read_text())
|
||||
return None
|
||||
|
||||
def set(self, key: str, value):
|
||||
self._key_path(key).write_text(json.dumps(value))
|
||||
|
||||
def invalidate(self, key: str):
|
||||
p = self._key_path(key)
|
||||
if p.exists():
|
||||
p.unlink()
|
||||
|
||||
|
||||
class ReportStore:
|
||||
def __init__(self, base_dir: str = "./reports"):
|
||||
self.base_dir = Path(base_dir)
|
||||
|
||||
def save_history(self, program: str, status: str, matched: int, duration: float):
|
||||
trend = self.base_dir / "trends" / f"{program}.jsonl"
|
||||
trend.parent.mkdir(parents=True, exist_ok=True)
|
||||
import datetime
|
||||
entry = {"ts": datetime.datetime.now().isoformat(), "status": status,
|
||||
"fields_matched": matched, "duration_s": duration}
|
||||
with open(trend, "a") as f:
|
||||
f.write(json.dumps(entry) + "\n")
|
||||
@@ -0,0 +1,45 @@
|
||||
import sys, os
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
|
||||
from comparator.aligner import align_records
|
||||
|
||||
|
||||
def test_align_by_key():
|
||||
cobol = [{"CUST-ID": "C001", "AMT": 100}, {"CUST-ID": "C002", "AMT": 200}]
|
||||
spark = [{"CUST-ID": "C002", "AMT": 200}, {"CUST-ID": "C001", "AMT": 100}]
|
||||
result = align_records(cobol, spark, key_field="CUST-ID")
|
||||
assert len(result) == 2
|
||||
assert all(s == "MATCHED" for _, _, s in result)
|
||||
|
||||
|
||||
def test_missing_in_spark():
|
||||
cobol = [{"CUST-ID": "C001"}, {"CUST-ID": "C002"}]
|
||||
spark = [{"CUST-ID": "C001"}]
|
||||
result = align_records(cobol, spark, key_field="CUST-ID")
|
||||
statuses = [s for _, _, s in result]
|
||||
assert "MISSING_IN_SPARK" in statuses
|
||||
|
||||
|
||||
def test_extra_in_spark():
|
||||
cobol = [{"CUST-ID": "C001"}]
|
||||
spark = [{"CUST-ID": "C001"}, {"CUST-ID": "C002"}]
|
||||
result = align_records(cobol, spark, key_field="CUST-ID")
|
||||
statuses = [s for _, _, s in result]
|
||||
assert "EXTRA_IN_SPARK" in statuses
|
||||
|
||||
|
||||
def test_empty_inputs():
|
||||
assert align_records([], [], "key") == []
|
||||
|
||||
|
||||
def test_duplicate_keys():
|
||||
cobol = [{"ID": "K1", "V": 1}, {"ID": "K1", "V": 2}]
|
||||
java = [{"ID": "K1", "V": 1}, {"ID": "K1", "V": 2}]
|
||||
result = align_records(cobol, java, key_field="ID")
|
||||
assert len(result) == 2
|
||||
|
||||
|
||||
def test_align_none_key():
|
||||
cobol = [{"ID": None, "V": 1}]
|
||||
java = [{"ID": None, "V": 1}]
|
||||
result = align_records(cobol, java, key_field="ID")
|
||||
assert len(result) == 1
|
||||
@@ -0,0 +1,18 @@
|
||||
import sys, os
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
from comparator.aligner import align_records
|
||||
|
||||
|
||||
def test_align_empty_key_value():
|
||||
cobol = [{"ID": "", "V": 1}]
|
||||
java = [{"ID": "", "V": 1}]
|
||||
result = align_records(cobol, java, key_field="ID")
|
||||
assert len(result) == 1
|
||||
|
||||
|
||||
def test_align_very_large_key_set():
|
||||
cobol = [{"ID": f"K{i:04d}", "V": i} for i in range(100)]
|
||||
java = [{"ID": f"K{i:04d}", "V": i} for i in range(100)]
|
||||
result = align_records(cobol, java, key_field="ID")
|
||||
assert len(result) == 100
|
||||
assert all(s == "MATCHED" for _, _, s in result)
|
||||
@@ -0,0 +1,23 @@
|
||||
import sys, os
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
|
||||
from comparator.field_compare import compare_field
|
||||
|
||||
|
||||
def test_negative_numbers():
|
||||
r = compare_field("AMT", "-1500", "-1500", "decimal")
|
||||
assert r.status == "PASS"
|
||||
|
||||
|
||||
def test_mixed_precision():
|
||||
r = compare_field("AMT", "1500.00", "1500", "decimal", tolerance=0.01)
|
||||
assert r.status == "PASS"
|
||||
|
||||
|
||||
def test_non_numeric_in_numeric_field():
|
||||
r = compare_field("AMT", "ABC", "1500", "decimal")
|
||||
assert r.status in ("MISMATCH", "NOT_SET")
|
||||
|
||||
|
||||
def test_very_large_number():
|
||||
r = compare_field("AMT", "9999999999", "9999999999", "decimal")
|
||||
assert r.status == "PASS"
|
||||
@@ -0,0 +1,49 @@
|
||||
import sys, os
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
|
||||
from comparator.field_compare import compare_field, DEFAULT_TOLERANCE
|
||||
|
||||
|
||||
def test_exact_match():
|
||||
r = compare_field("BR-AMT", "1500000", "1500000", "decimal")
|
||||
assert r.status == "PASS"
|
||||
|
||||
|
||||
def test_within_tolerance():
|
||||
r = compare_field("BR-AMT", "1500000", "1499999.99", "decimal", tolerance=DEFAULT_TOLERANCE)
|
||||
assert r.status == "TOLERATED"
|
||||
|
||||
|
||||
def test_beyond_tolerance():
|
||||
r = compare_field("BR-AMT", "1500000", "1000000", "decimal", tolerance=DEFAULT_TOLERANCE)
|
||||
assert r.status == "MISMATCH"
|
||||
|
||||
|
||||
def test_string_trim():
|
||||
r = compare_field("BR-STATUS", "A ", "A", "string")
|
||||
assert r.status == "PASS"
|
||||
|
||||
|
||||
def test_date_normalization():
|
||||
r = compare_field("BR-DATE", "20260522", "2026-05-22", "date")
|
||||
assert r.status == "PASS"
|
||||
|
||||
|
||||
def test_cobol_default():
|
||||
from decimal import Decimal, ROUND_DOWN
|
||||
r = compare_field("BR-AMT", "\x00\x00\x00\x00\x00", "0", "decimal")
|
||||
assert r.status in ("PASS", "TOLERATED")
|
||||
|
||||
|
||||
def test_java_null_vs_value():
|
||||
r = compare_field("BR-AMT", "1500000", "None", "decimal")
|
||||
assert r.status in ("MISMATCH", "NOT_SET")
|
||||
|
||||
|
||||
def test_negative_numbers():
|
||||
r = compare_field("AMT", "-1500", "-1500", "decimal")
|
||||
assert r.status == "PASS"
|
||||
|
||||
|
||||
def test_mixed_precision():
|
||||
r = compare_field("AMT", "1500.00", "1500", "decimal", tolerance=DEFAULT_TOLERANCE)
|
||||
assert r.status == "PASS"
|
||||
@@ -0,0 +1,47 @@
|
||||
import sys, os
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
|
||||
from comparator.normalizer import Normalizer
|
||||
|
||||
|
||||
def test_ebcdic_to_ascii():
|
||||
n = Normalizer()
|
||||
assert n.normalize_encoding(b'\xc1\xc2', "EBCDIC") == "AB"
|
||||
|
||||
|
||||
def test_ascii_passthrough():
|
||||
n = Normalizer()
|
||||
assert n.normalize_encoding(b"hello", "ASCII") == "hello"
|
||||
|
||||
|
||||
def test_comp3_to_decimal():
|
||||
n = Normalizer()
|
||||
assert n.normalize_comp3(b'\x15\x00\x0C') == "1500"
|
||||
|
||||
|
||||
def test_comp3_negative():
|
||||
n = Normalizer()
|
||||
assert n.normalize_comp3(b'\x15\x00\x1D') == "-1500"
|
||||
|
||||
|
||||
def test_ir_record_creation():
|
||||
n = Normalizer()
|
||||
ir = n.to_ir_record(
|
||||
field_name="BR-AMT", raw_hex="15000C",
|
||||
decoded_value="1500", encoding="EBCDIC",
|
||||
field_type="COMP3", length=5, scale=2, signed=True)
|
||||
assert ir.field_name == "BR-AMT"
|
||||
assert ir.cobol.decoded_value == "1500"
|
||||
assert ir.cobol.encoding == "EBCDIC"
|
||||
|
||||
|
||||
def test_date_iso_normalization():
|
||||
n = Normalizer()
|
||||
assert n.normalize_date("20260522") == "2026-05-22"
|
||||
assert n.normalize_date("2026-05-22") == "2026-05-22"
|
||||
|
||||
|
||||
def test_null_ir_record():
|
||||
n = Normalizer()
|
||||
ir = n.to_null_ir("BR-DATE", side="java")
|
||||
assert ir.field_name == "BR-DATE"
|
||||
assert ir.java is None
|
||||
@@ -0,0 +1,24 @@
|
||||
import sys, os
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
|
||||
from comparator.rounding_detect import detect_rounding, RoundingResult
|
||||
|
||||
|
||||
def test_truncation_detected():
|
||||
r = detect_rounding("1500000", "1499999")
|
||||
assert r.mode in ("TRUNCATE", "HALF_UP")
|
||||
|
||||
|
||||
def test_exact_match():
|
||||
r = detect_rounding("1500000", "1500000")
|
||||
assert r.mode == "EXACT"
|
||||
assert r.confidence == 1.0
|
||||
|
||||
|
||||
def test_low_confidence_small_diff():
|
||||
r = detect_rounding("1500", "1498")
|
||||
assert r.confidence < 1.0
|
||||
|
||||
|
||||
def test_suggestion_generated():
|
||||
r = detect_rounding("1500000", "1499999")
|
||||
assert len(r.suggestion) > 0
|
||||
Vendored
+29
@@ -0,0 +1,29 @@
|
||||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. SIMPLE.
|
||||
ENVIRONMENT DIVISION.
|
||||
INPUT-OUTPUT SECTION.
|
||||
FILE-CONTROL.
|
||||
SELECT INFILE ASSIGN TO "input.bin"
|
||||
ORGANIZATION IS SEQUENTIAL.
|
||||
DATA DIVISION.
|
||||
FILE SECTION.
|
||||
FD INFILE.
|
||||
01 BILL-RECORD.
|
||||
05 BR-AMT PIC S9(7)V99 COMP-3.
|
||||
05 BR-STATUS PIC X.
|
||||
05 BR-DATE PIC 9(8).
|
||||
WORKING-STORAGE SECTION.
|
||||
01 WS-EOF PIC X VALUE 'N'.
|
||||
PROCEDURE DIVISION.
|
||||
OPEN INPUT INFILE.
|
||||
PERFORM UNTIL WS-EOF = 'Y'
|
||||
READ INFILE INTO BILL-RECORD
|
||||
AT END MOVE 'Y' TO WS-EOF
|
||||
NOT AT END
|
||||
DISPLAY BR-AMT
|
||||
DISPLAY BR-STATUS
|
||||
DISPLAY BR-DATE
|
||||
END-READ
|
||||
END-PERFORM.
|
||||
CLOSE INFILE.
|
||||
STOP RUN.
|
||||
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
01 BILL-RECORD.
|
||||
05 BR-AMT PIC S9(7)V99 COMP-3.
|
||||
05 BR-STATUS PIC X.
|
||||
05 BR-DATE PIC 9(8).
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
program: "SIMPLE"
|
||||
field_mapping:
|
||||
- cobol_field: "BR-AMT"
|
||||
java_field: "billAmount"
|
||||
type: "decimal"
|
||||
precision: 2
|
||||
- cobol_field: "BR-STATUS"
|
||||
java_field: "statusCode"
|
||||
type: "string"
|
||||
- cobol_field: "BR-DATE"
|
||||
java_field: "billDate"
|
||||
type: "date"
|
||||
format: "YYYYMMDD"
|
||||
@@ -0,0 +1,44 @@
|
||||
import sys, os, json
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
|
||||
from pathlib import Path
|
||||
from report.generator import ReportGenerator
|
||||
from data.diff_result import VerificationRun, FieldResult
|
||||
|
||||
|
||||
def test_json_output(tmp_path):
|
||||
vr = VerificationRun(program="BILL-CALC", status="PASS", exit_code=0,
|
||||
field_results=[FieldResult(field_name="BR-AMT", status="PASS")])
|
||||
gen = ReportGenerator()
|
||||
path = gen.generate_json(vr, tmp_path / "result.json")
|
||||
data = json.loads(path.read_text())
|
||||
assert data["program"] == "BILL-CALC"
|
||||
assert data["status"] == "PASS"
|
||||
|
||||
|
||||
def test_html_output(tmp_path):
|
||||
vr = VerificationRun(program="TEST", status="MISMATCH",
|
||||
field_results=[FieldResult(field_name="F1", status="MISMATCH")])
|
||||
gen = ReportGenerator()
|
||||
path = gen.generate_html(vr, tmp_path / "report.html")
|
||||
assert path.exists()
|
||||
html = path.read_text()
|
||||
assert "MISMATCH" in html
|
||||
assert "F1" in html
|
||||
|
||||
|
||||
def test_machine_json(tmp_path):
|
||||
vr = VerificationRun(program="TEST", status="PASS", exit_code=0)
|
||||
gen = ReportGenerator()
|
||||
path = gen.generate_machine_json(vr, tmp_path / "machine.json")
|
||||
data = json.loads(path.read_text())
|
||||
assert data["exit_code"] == 0
|
||||
|
||||
|
||||
def test_suggestion_in_report(tmp_path):
|
||||
fr = FieldResult(field_name="BR-AMT", status="MISMATCH",
|
||||
suggestion="Check rounding_mode: TRUNCATE vs HALF_UP")
|
||||
vr = VerificationRun(program="TEST", status="MISMATCH", field_results=[fr])
|
||||
gen = ReportGenerator()
|
||||
path = gen.generate_json(vr, tmp_path / "result.json")
|
||||
data = json.loads(path.read_text())
|
||||
assert "suggestion" in data["field_results"][0]
|
||||
@@ -0,0 +1,33 @@
|
||||
import sys, os
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
from config import Config
|
||||
from orchestrator import run_pipeline
|
||||
|
||||
|
||||
def test_e2e_pipeline_imports():
|
||||
"""Verify all modules import correctly without runtime tools."""
|
||||
from data.field_tree import Field, FieldTree
|
||||
from data.test_case import TestCase, TestSuite, SparkConfig
|
||||
from data.diff_result import FieldResult, VerificationRun
|
||||
from runners.runner import Runner, BuildResult, RunResult, CoverageReport
|
||||
from runners.native_java_runner import NativeJavaRunner
|
||||
from runners.spark_java_runner import SparkJavaRunner
|
||||
from runners.cobol_runner import CobolRunner
|
||||
from runners.data_writer import DataWriter
|
||||
from agents.llm import LLMClient
|
||||
from agents.agent1_parser import Agent1Parser
|
||||
from agents.agent2_data import Agent2Data
|
||||
from agents.agent3_diagnostic import Agent3Diagnostic
|
||||
from comparator.aligner import align_records
|
||||
from comparator.field_compare import compare_field
|
||||
from comparator.normalizer import Normalizer
|
||||
from comparator.rounding_detect import detect_rounding
|
||||
from comparator.cobol_binary_reader import CobolBinaryReader
|
||||
from report.generator import ReportGenerator
|
||||
from storage.bundle import TestDataBundle
|
||||
from storage.store import ReportStore, DiskCache
|
||||
from preprocessor import CopybookPreprocessor
|
||||
from config.mapping import MappingConfig, FieldMapping
|
||||
from quality.l1_offset_validate import L1OffsetValidator
|
||||
from quality.l2_value_roundtrip import L2RoundtripValidator
|
||||
assert True
|
||||
Reference in New Issue
Block a user