fix: resolve CSV data truncation in save_to_csv by correctly identifying column widths and remove redundant logic in ZWaferParser

This commit is contained in:
jack
2026-07-04 16:46:44 -07:00
parent 9e28a25695
commit ecab3d81b1
4 changed files with 57 additions and 18 deletions
+34 -14
View File
@@ -266,20 +266,25 @@ class TestSaveToCsv:
assert result is not None
assert "P12345" in result
def test_csv_has_sensor_headers(self, tmp_path):
# Header count is driven by csv_column_count(family), not data width.
# P family → 48 sensors; F family → 22 sensors; X family → 80 sensors.
cases = [("P", 48), ("F", 22), ("X", 80), ("B", 29)]
for family, expected_cols in cases:
data = [["25.0", "24.5"]]
result = save_to_csv(data, family, f"{family}00001", str(tmp_path))
assert result is not None, f"save_to_csv returned None for {family}"
headers = open(result).readline().strip().split(",")
assert (
len(headers) == expected_cols
), f"{family}: expected {expected_cols} headers, got {len(headers)}"
assert headers[0] == "Sensor1"
assert headers[-1] == f"Sensor{expected_cols}"
def test_csv_header_wins_when_data_wider_than_display(self, tmp_path):
# Header count = max(csv_column_count, data width).
# When data has 60 columns and P display is 48, header is 60 — no truncation.
data = [[f"{i}.0" for i in range(60)]]
result = save_to_csv(data, "P", "P00001", str(tmp_path))
assert result is not None
headers = open(result).readline().strip().split(",")
assert len(headers) == 60, f"data width 60 should win over display 48"
assert headers[0] == "Sensor1"
assert headers[-1] == "Sensor60"
def test_csv_uses_display_count_when_data_is_narrower(self, tmp_path):
# When data has fewer columns than the display count, pad to display.
data = [["25.0"]]
result = save_to_csv(data, "X", "X00001", str(tmp_path))
assert result is not None
headers = open(result).readline().strip().split(",")
# X display = 80, data = 1 → headers = 80
assert len(headers) == 80
def test_csv_row_count_matches_data(self, tmp_path):
data = [["25.0"], ["24.5"], ["23.0"]]
@@ -302,3 +307,18 @@ class TestSaveToCsv:
data = [["25.0"]]
result = save_to_csv(data, "P", "P00001", "/\x00invalid\x00path")
assert result is None
def test_preserves_all_columns_when_data_wider_than_display(self, tmp_path):
# Bug fix: when data has more columns than csv_column_count(family),
# all data columns must be preserved (no silent truncation).
data = [[f"{i}.0" for i in range(244)]]
result = save_to_csv(data, "P", "P00001", str(tmp_path))
assert result is not None
with open(result) as f:
lines = f.readlines()
headers = lines[0].strip().split(",")
data_row = lines[1].strip().split(",")
# All 244 columns should be preserved, not truncated to 48
assert len(headers) == 244, f"expected 244 cols, got {len(headers)}"
assert len(data_row) == 244, f"expected 244 data cols, got {len(data_row)}"
assert data_row[243] == "243.0", "last column value must be preserved"