Skip to main content

Generate Machine-Checked Examples for a 2scraper Export Data Dictionary

Make contract examples fail CI when fields or nullability change.

Updated 9 min readMarkdown
Generate Machine-Checked Examples for a 2scraper Export Data Dictionary

An authorized export associated with 2scraper can be read correctly by the job that produced it and still confuse every later consumer. A data dictionary is useful when its examples can be loaded under the actual contract. I would keep examples and the schema in one small test so documentation changes are visible before a report uses the wrong interpretation.

Define the contract for field identity, type and presence before choosing a sample. A nullable field can be present with no observation, while an optional field may be missing entirely. Make those states distinct. If the actual exporter supports more than one schema version, publish a sample under the version users will receive.

The example isolates a single decision about whether the example agrees with the contract using a local input. Keep the schema version, sample and validation result in sight while comparing results. A report can publish one computed field for consumers while the audit trail retains the individual source decisions. A small fixture lets you check both surfaces without credentials, production input or an external request.

Follow two records through the boundary

A reader following documentation for version two sees a product ID and a nullable integer stock quantity. An example from version one gives only the ID. Copying it into a new discussion might look harmless, but a consumer could assume missing and null are interchangeable. The fixture demonstrates how the documented presence rule distinguishes the two types of evidence.

For the cases in the table, record the input version and expected decision next to each example. I would expect someone familiar with a consumer-facing data dictionary to be able to make the same decision by hand. Ask them first about an example that silently describes an old field. If they cannot tell which evidence is missing, improve the case description before increasing the number of input rows. A larger dataset will multiply the ambiguity without settling the rule.

State the boundary in the contract

Store sample records in a readable data file and validate them using independent executable rules. The local fixture accepts one genuine example shape and checks an older example that omitted the stock field. Use code on both the proposed and current contract. A prose example copied into Markdown is still part of the verified output and should be rendered from the validated source.

Choose one human-readable JSON example and a small invalid copy for each important boundary. Keep a representative case with a zero value so any falsely “helpful” truthiness fallback is caught. When the input contract changes, run the example generator and compare the render with source control so the change has visible release evidence.

Read the fixture as a decision table

Case Evidence Expected decision
Required field present known value valid
Explicit unknown stock equal to null valid
Missing field stock absent invalid
Zero stock stock equals zero valid

Required field. The reader can use the documented field.

Explicit unknown. The field still belongs to the shape.

Missing field. The boundary distinguishes absent from null.

Zero stock. Zero is an actual observation.

Run the example on a synthetic input

This example accepts only fields needed for the decision about whether the example agrees with the contract. It prints a compact result and asserts each consequential branch before printing. The table explains any operational cases beyond that small code sample. Copy the code into its own Python file and run it without credentials.

def validate(record):
    if set(record) != {'sku','stock'}:
        return False
    sku, stock = record['sku'], record['stock']
    return bool(sku) and isinstance(sku,str) and (stock is None or (type(stock) is int and stock >= 0))

examples = [{'sku':'A','stock':0},{'sku':'B','stock':None},{'sku':'C'}]
assert [validate(r) for r in examples] == [True,True,False]
assert not validate({'sku':'A','stock':False})
print('Explicit zero and null are valid. Missing stock fails the schema.')

The local execution printed this output.

Explicit zero and null are valid. Missing stock fails the schema.

When you adapt the code, keep the schema version, sample and validation result available to the test runner. Add a separate integration fixture at the input boundary, because a downstream assertion can pass while the input producer silently changes the meaning of a field. I would check the observed output and the persisted decision record independently.

Give an unexpected case one clear outcome

A failed example tells maintainers that the documentation should be reviewed alongside the contract. It cannot decide which member of the pair is wrong. Preserve the failed input under its version, along with the rule that failed. A reviewer can decide whether the sample is stale or the contract inadvertently drifted before changing a public instruction.

An alert should tell the report owner what happened to a document readers can reproduce and which input was involved. I use stable reason values for metrics and a run or case reference for the review. Putting complete input records in alert text may expose information outside the normal access boundary. The destination for a repaired record should accept the same run reference as the original so corrections can be traced.

What I would retain for the next review

For the first deployment, I would keep a small directory of approved examples beside the code that reads a consumer-facing data dictionary. Keep the unmodified source fixture and the derived record in separate files. If you only keep the winner or the visible dashboard row, you lose the inputs that made the decision about whether the example agrees with the contract explainable. Include an example the program explicitly rejected and an example that legitimately passed. A reviewer should be able to identify which is which from the inputs before opening the expected result.

I would give an analyst who consumes the export a short checklist with one entry for the schema version, sample and validation result. The first check is whether two readers can independently point to the same underlying record. The second is whether they agree about the decision under the documented rule. The third is whether a discrepancy about an example that silently describes an old field becomes visible to the person who can resolve it. Keep an expected output in the test directory and a human-readable account in the release notes. They serve different readers and should describe the same decision.

I would have the test report more than a pass count. Give the failing case a stable name and preserve the decision stage where it stopped. That makes it possible to compare results months later after a field has been renamed or a report has moved. Count missing evidence independently of invalid evidence. A problem with collection can look like a problem with the rule if both types land in the same generic error bucket. The difference is consequential for whoever will support a consumer-facing data dictionary.

For rollout, run the new rule next to the existing report for one real reporting interval and compare individual cases, using only data already approved for that workflow. Keep the previous version available until you can explain every material difference in a document readers can reproduce. Record a representative changed row with the source identifier and the rule version. Then look at a small sample of unchanged rows so the release review can also say what the change preserved. The useful output is a reproducible decision, plus a record of where the rule needs more evidence.

I would make the handoff include a deliberate bad run. Remove the evidence for an example that silently describes an old field from a copy of the fixture while leaving the rest of the records intact. Ask the operator to identify the first place where the uncertainty appears, then try the diagnosis using only the artifacts that someone on call can access. If the needed evidence can only be reached through an administrator’s personal workspace or an undocumented manual step, give the review a shared run reference before rollout. The exercise is complete when the operator can explain which conclusions about whether the example agrees with the contract remain available and can reproduce them with a clean copy. Keep the failing input in the test suite as a fixture, with real records and any access tokens excluded.

Make the change reversible before the first report

Give a downstream user the public data dictionary without asking them to run your code first. Ask them which values could be present and what blank means. Then reproduce their examples locally. Use the results to reconcile the schema, sample and description. A readable example that cannot pass its promised contract needs a clear annotation or revision.

Choose a fixture that shows whether an example that silently describes an old field would become visible before the report is released. Compare exact decisions before comparing totals, because opposite errors can cancel out in an aggregate. Document which decisions still need human review and who owns them. After an input contract changes, replay the previous fixture with the old policy and the new fixture with the new policy before changing a published comparison.

Teach the example how to evolve

A second consumer may require a new reporting measure without needing every upstream field. Instead of copying the entire dictionary into that team’s repository, point to the verified schema version and add an excerpt generated from the same source if there is a concrete reason. Keep field identifiers stable across formatted presentations and distinguish a label change from a semantic one.

Create a migration note that reproduces one accepted example from each supported version. Point out where a consumer must update a mapping or make a choice about compatibility. Generate the invalid example as well. A warning that never demonstrates a failure leaves the reader guessing about what happens when the old sample arrives after the change.

Run one small trial first

Select one export from 2scraper and choose two fields a real team uses. Write a local sample with each important state and render the documentation from that source. Run the example against the schema every time those fields change.

Prepared with AI assistance. The code was run locally against a synthetic fixture. The fixture describes the downstream workflow illustrated here.

Further reading

Keep a Catalog Export’s Column Order Stable Across App Releases A companion check for stable positional exports.

Explain What Changed Between Two 2scraper Catalog Snapshots Inspect differences between independently versioned snapshots.

1 view

No comments yet. Be the first to comment.