A two-row test that catches a broken CSV diff

2026-11-25来源 dev.toEnglish
A two-row test that catches a broken CSV diff

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.

原文:https://www.a8q9.cn/en/article/a-two-row-test-that-catches-a-broken-csv-diff-7d6bc8.html

相关文章