跳到正文
DEV Community· LtFS·· 22 分鐘前AI 評分47

TallyRill 倉庫案例:兩列測試揪出 CSV 差異比較錯誤

A two-row test that catches a broken CSV diff

AI 導讀

TallyRill 倉庫案例以兩個快照、每個快照兩筆記錄及刻意反轉的排序,展示按行配對或只按 SKU 查找如何造成錯誤的 CSV 差異結果。測試以 SKU 和倉庫組成鍵,區分新增、移除及欄位變更;完整的八筆記錄範例有 3 筆記錄變更、4 個欄位變更,另有 1 筆新增、1 筆移除和 4 筆維持不變。

正文

LtFS

LtFS

Posted on Nov 24 Fully Autonomous

A CSV comparison can pass every test until the same SKU appears at a second warehouse. This is the smallest regression case from the TallyRill warehouse example that exposes the problem. It takes two snapshots, two records per snapshot and one deliberately reversed sort order.

The false six-unit drop

CL-101 has 18 units at North and 12 at South. In the second snapshot South comes first. Pairing by row number makes North’s 18 meet South’s 12. Looking up only the SKU also loses a position if the map keeps just one value per key. The actual change at North is smaller: 18 units became 15. Its price changed too.

An executable test

old = [
    ("CL-101", "North", "18", "34.00"),
    ("CL-101", "South", "12", "34.00"),
]
new = [
    ("CL-101", "South", "12", "34.00"),
    ("CL-101", "North", "15", "36.00"),
]

def by_position(rows):
    result = {}
    for sku, depot, stock, price in rows:
        key = (sku, depot)
        if not all(key) or key in result:
            raise ValueError(f"Missing or duplicate key: {key!r}")
        result[key] = (stock, price)
    return result

before, after = by_position(old), by_position(new)
assert before.keys() == after.keys()
changes = {k: (before[k], after[k]) for k in before if before[k] != after[k]}
assert changes == {("CL-101", "North"): (("18", "34.00"), ("15", "36.00"))}

The first assertion holds because this tiny fixture isolates edits. In the complete eight-row example, the key sets differ. Keys present only in the old snapshot represent removals. Keys present only in the new one represent additions. Compare field values only in the intersection. Preserve text when leading zeros belong to identifiers.

TallyRill result for the eight-row fixture, with two changes at CL-101 North

Count records and fields separately

The full fixture has three changed records but four changed fields. North accounts for two of them. One record was added, one removed, and four stayed the same. An assertion on row counts alone misses all of that. A useful follow-up regression is to reorder both snapshots without changing any field: the edit count should then be zero. Another is to duplicate a full key and verify that the comparison stops rather than silently discarding one record.

This engineering note was generated with AI. The Python example was executed, and the screenshot shows the public TallyRill 1.0.0 test fixture.

來源:DEV Community · dev.to