152 lines
5.9 KiB
Python
152 lines
5.9 KiB
Python
"""黑盒 JSON → cobol_testgen 内部 record 映射测试"""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
|
|
|
|
from cobol_testgen.blackbox import norm_name, load_black_box_groups
|
|
|
|
|
|
def _write_group(tmp, name, payload):
|
|
d = Path(tmp) / "black_box" / "g1"
|
|
d.mkdir(parents=True, exist_ok=True)
|
|
p = d / f"{name}.json"
|
|
p.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")
|
|
return p
|
|
|
|
|
|
def test_norm_name():
|
|
assert norm_name("R01-APPL-ID") == "R01APPLID"
|
|
assert norm_name("R01APPLID") == "R01APPLID"
|
|
assert norm_name("r01_appl_id") == "R01APPLID"
|
|
|
|
|
|
def test_load_groups_fd_assign_and_field_hyphen():
|
|
fd_fields = {"R01INNFIL": ["R01APPL-ID", "R01EMP-ID"]}
|
|
select_info = {"R01INNFIL": {"assign": "ZAN04R01"}}
|
|
data_fields = [
|
|
{"name": "R01APPL-ID", "pic": "X(8)"},
|
|
{"name": "R01EMP-ID", "pic": "9(8)"},
|
|
]
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
_write_group(tmp, "ZAN04MAT_g1", {
|
|
"program": "ZAN04MAT",
|
|
"records": [{
|
|
"input": {"ZAN04R01": {
|
|
"R01-APPL-ID": "A0000001",
|
|
"R01-EMP-ID": "00000001",
|
|
}}
|
|
}],
|
|
})
|
|
groups = load_black_box_groups(tmp, fd_fields, select_info, data_fields)
|
|
assert len(groups) == 1
|
|
g = groups[0]
|
|
assert g.label == "g1"
|
|
assert g.records[0]["R01APPL-ID"] == "A0000001"
|
|
assert g.records[0]["R01EMP-ID"] == "00000001"
|
|
assert g.term_types == ["normal"]
|
|
|
|
|
|
def test_load_groups_fd_name_variant():
|
|
fd_fields = {"R01INNFIL": ["R01APPL-ID"]}
|
|
select_info = {"R01INNFIL": {"assign": "ZAN04R01"}}
|
|
data_fields = [{"name": "R01APPL-ID", "pic": "X(8)"}]
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
_write_group(tmp, "ZAN04MAT_g1", {
|
|
"program": "ZAN04MAT",
|
|
"records": [{"input": {"R01INNFIL": {"R01-APPL-ID": "B0000002"}}}],
|
|
})
|
|
groups = load_black_box_groups(tmp, fd_fields, select_info, data_fields)
|
|
assert groups[0].records[0]["R01APPL-ID"] == "B0000002"
|
|
|
|
|
|
def test_load_groups_skips_unmatched_and_empty():
|
|
fd_fields = {"R01INNFIL": ["R01APPL-ID"]}
|
|
select_info = {"R01INNFIL": {"assign": "ZAN04R01"}}
|
|
data_fields = [{"name": "R01APPL-ID", "pic": "X(8)"}]
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
_write_group(tmp, "ZAN04MAT_g1", {
|
|
"program": "ZAN04MAT",
|
|
"records": [{"input": {"UNKNOWN-FD": {"X": "1"}}}],
|
|
})
|
|
groups = load_black_box_groups(tmp, fd_fields, select_info, data_fields)
|
|
assert groups == []
|
|
|
|
|
|
def test_load_groups_fd_prefix_variant():
|
|
fd_fields = {"R01INNFIL": ["R01APPL-ID"]}
|
|
select_info = {"R01INNFIL": {"assign": "ZAN04R01"}}
|
|
data_fields = [{"name": "R01APPL-ID", "pic": "X(8)"}]
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
_write_group(tmp, "ZAN04MAT_g1", {
|
|
"program": "ZAN04MAT",
|
|
"records": [{"input": {"R01": {"R01-APPL-ID": "C0000003"}}}],
|
|
})
|
|
groups = load_black_box_groups(tmp, fd_fields, select_info, data_fields)
|
|
assert groups[0].records[0]["R01APPL-ID"] == "C0000003"
|
|
|
|
|
|
def test_load_groups_ambiguous_prefix_skipped():
|
|
fd_fields = {"R01INNFIL": ["R01APPL-ID"], "R01OUTFIL": ["R01APPL-ID"]}
|
|
select_info = {"R01INNFIL": {"assign": "ZAN04R01"},
|
|
"R01OUTFIL": {"assign": "ZAN04W01"}}
|
|
data_fields = [{"name": "R01APPL-ID", "pic": "X(8)"}]
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
_write_group(tmp, "ZAN04MAT_g1", {
|
|
"program": "ZAN04MAT",
|
|
"records": [{"input": {"R01": {"R01-APPL-ID": "D0000004"}}}],
|
|
})
|
|
groups = load_black_box_groups(tmp, fd_fields, select_info, data_fields)
|
|
assert groups == []
|
|
|
|
|
|
def test_load_groups_accepts_black_box_dir_directly():
|
|
fd_fields = {"R01INNFIL": ["R01APPL-ID"]}
|
|
select_info = {"R01INNFIL": {"assign": "ZAN04R01"}}
|
|
data_fields = [{"name": "R01APPL-ID", "pic": "X(8)"}]
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
_write_group(tmp, "ZAN04MAT_g1", {
|
|
"program": "ZAN04MAT",
|
|
"records": [{"input": {"ZAN04R01": {"R01APPL-ID": "E0000005"}}}],
|
|
})
|
|
groups = load_black_box_groups(Path(tmp) / "black_box", fd_fields,
|
|
select_info, data_fields)
|
|
assert len(groups) == 1
|
|
|
|
|
|
def test_load_groups_skips_non_dict_records():
|
|
fd_fields = {"R01INNFIL": ["R01APPL-ID"]}
|
|
select_info = {"R01INNFIL": {"assign": "ZAN04R01"}}
|
|
data_fields = [{"name": "R01APPL-ID", "pic": "X(8)"}]
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
_write_group(tmp, "ZAN04MAT_g1", {
|
|
"program": "ZAN04MAT",
|
|
"records": ["not-a-dict",
|
|
{"input": {"ZAN04R01": {"R01APPL-ID": "F0000006"}}}],
|
|
})
|
|
groups = load_black_box_groups(tmp, fd_fields, select_info, data_fields)
|
|
assert groups[0].records[0]["R01APPL-ID"] == "F0000006"
|
|
|
|
|
|
def test_load_groups_ignores_nested_cobol_inputs():
|
|
fd_fields = {"R01INNFIL": ["R01APPL-ID"]}
|
|
select_info = {"R01INNFIL": {"assign": "ZAN04R01"}}
|
|
data_fields = [{"name": "R01APPL-ID", "pic": "X(8)"}]
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
d = Path(tmp) / "black_box" / "g1"
|
|
(d / "cobol" / "input").mkdir(parents=True)
|
|
(d / "ZAN04MAT_g1.json").write_text(json.dumps({
|
|
"program": "ZAN04MAT",
|
|
"records": [{"input": {"ZAN04R01": {"R01-APPL-ID": "A0000001"}}}],
|
|
}, ensure_ascii=False), encoding="utf-8")
|
|
(d / "cobol" / "input" / "ZAN04MAT_g1_R01INNFIL.json").write_text(
|
|
json.dumps([{"R01-APPL-ID": "A0000001"}], ensure_ascii=False),
|
|
encoding="utf-8")
|
|
groups = load_black_box_groups(tmp, fd_fields, select_info, data_fields)
|
|
assert len(groups) == 1
|
|
assert groups[0].records[0]["R01APPL-ID"] == "A0000001"
|