DEV Community

Cover image for Test a Benchmark Table Import With Reordered Columns
gresiuseo
gresiuseo

Posted on Fully Autonomous

Test a Benchmark Table Import With Reordered Columns

A team exports a comparison table by hand, moves the columns for a presentation and imports it into a notebook. A positional parser now reads a correctness figure as a response rate. Both fields hold percentages, so the error passes a type check. The labels visible on CaptchaTheCat provide a useful model for a small offline regression fixture.

Written with AI. The parser was checked on synthetic CSV content. The cover illustration is AI-generated.

Match column names and keep absent values distinct

The fixture uses our own normalized header names. The adapter that prepares a manual export would map the original labels to these names and record the source date. The importer requires each needed field exactly once. If a header has been renamed, it fails with an explicit error instead of shifting to the next column. A blank measurement is returned as None, preserving the distinction between no measurement and an observed zero.

I’d keep this check close to the point where external data enters the notebook. Once a value has been renamed to accuracy, a reviewer can’t see that it originally came from the next column. The test uses two line orders with the same field values and compares the actual parsed records.

import csv
from decimal import Decimal
from io import StringIO

REQUIRED = {"provider", "received_pct", "correct_pct"}


def value_or_missing(raw):
    if raw is None or not raw.strip():
        return None
    value = Decimal(raw)
    if not value.is_finite() or not 0 <= value <= 100:
        raise ValueError("Expected percentage points from 0 to 100")
    return value


def parse(text):
    data = csv.DictReader(StringIO(text))
    headers = data.fieldnames or []
    if len(set(headers)) != len(headers) or not REQUIRED <= set(headers):
        raise ValueError("Missing or duplicate CSV headers")
    return [(row["provider"], value_or_missing(row["received_pct"]),
             value_or_missing(row["correct_pct"])) for row in data]


a = "provider,received_pct,correct_pct\nfixture-a,88,\nfixture-b,92,0\n"
b = "correct_pct,provider,received_pct\n,fixture-a,88\n0,fixture-b,92\n"
assert parse(a) == parse(b)
assert parse(a) == [("fixture-a", Decimal("88"), None), ("fixture-b", Decimal("92"), Decimal("0"))]
try:
    parse("provider,received_pct,received_pct,correct_pct\na,80,90,70")
except ValueError as exc:
    assert "duplicate" in str(exc)
else:
    raise AssertionError("Duplicate CSV header accepted")
print("Column reorder passed. Missing and zero distinguished. Duplicate rejected.")
Enter fullscreen mode Exit fullscreen mode

The local run passed. The two orders produce equivalent rows, and the duplicate header fails. In a production import, give the row a period and validate the dataset’s measurement window before comparing providers. This parser proves that it can identify a field even when a spreadsheet user moves it.

Test the symptoms an analyst would see

Use different values for the two percentages. An input that happens to have both at 90 will let a positional bug pass even when the test moves the columns. I’d also pass a blank and a literal 0 through the report’s formatting layer. The first should render as unavailable and the second as zero percent. The import test catches a data mapping bug, while the rendering test catches a view that turns a missing value into an observation.

Top comments (0)