Lompat ke konten

Pelajaran 20 ยท Validation and Pipelines

Testing and Documenting Cleaning Work

Test cleaning rules with examples and edge cases, then record decisions in a data dictionary, a change log and a run report others can review and trust.

Cleaning rules are code, and code breaks. A new rule for one source can quietly damage another. Tests help catch that, and documentation lets other people understand what your cleaned data means.

Test with examples

For each cleaning function, list inputs and expected outputs. Include normal values, edge cases and values that must be rejected.

Python Table-driven tests for a cleaning rule

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

One test fails, on purpose. The extension digits get glued to the number, so the function returns a wrong phone number that looks fine. The test caught a real weakness. Decide what you want (reject, or split off the extension), then fix the function and run the tests again.

In a real project, use a test runner such as pytest.

Kinds of tests

Test Question
Example Does a known input give the expected output?
Property Is something true for many inputs, such as idempotence?
Regression Does a bug you fixed stay fixed?
Data check Does the actual output meet validation rules?
Comparison What changed compared with the previous version?

When you find a bad value in real data, add it to the example cases. Use made-up values that reproduce the problem, not real personal data.

Compare versions of output

Before you release a new rule, run the old and new pipelines on the same input and compare.

Python Show what a rule change affects

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Unexpected changes deserve an explanation before release.

The data dictionary

A data dictionary describes each column of the cleaned output:

Column Type Meaning Rules Missing means
customer_id text Our customer number Unique, C plus 5 digits Not allowed
signup_date date Date the first account was made Not in the future Imported from the old system
weight_kg decimal Package weight Greater than 0 Not measured

The "Missing means" column is easy to forget and very useful.

The cleaning log

Record decisions, not just code. For each one, note the date, the rule, the reason, the number of rows affected, who approved it and any open questions. Version control keeps the history of code and mapping files. A short README explains how to run the pipeline.

The run report

A report produced by each run helps readers judge the data:

Python Generate a plain-text run report

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

These numbers are invented to show the format.

State limitations

Say what the data cannot support: which fields are estimated, which sources are missing and which problems remain. A short limitations section protects your readers from reading too much into the results.

Review

Ask a colleague to review rules, especially ones that delete or change many records. Someone who knows the business can spot wrong assumptions that tests cannot.

AI tools can help draft test cases or explain a regular expression. Treat their output as a draft: read the code, run the tests, and do not send private data to a service you are not allowed to use. See prompt engineering for ways to write better requests.

Common mistake: updating expected test results just to make tests pass. First confirm that the new behavior is truly correct.

What to test first

You cannot test everything, so start where a mistake would hurt most or hide longest:

  1. Rules that delete or merge records.
  2. Parsers for money, dates and identifiers.
  3. Anything that affects consent, permissions or payments.
  4. Rules that you have already had to fix once.

For each of these, include at least one input that looks almost valid. A date with day 31 in a 30-day month and an ID with one digit too many are good examples. Tests that use only friendly input give false comfort.

Write notes for a stranger

Imagine that someone new has to run your process while you are away. Can they find the raw files, run one command, read the report and know what to do when a check fails? Ask a colleague to try, and watch where they get stuck. Every question they ask is a missing line in your notes.

Keep documentation close to the code so they change together. A data dictionary in a forgotten folder becomes wrong without anyone noticing. You can even test it: compare the column names in the output with the names in the dictionary, and fail when they differ.

Tests do not prove the data is right

A passing test shows that code behaves as you described on the inputs you tried. It cannot show that your description is correct. If everyone assumed that a source uses day-first dates and it does not, the tests will pass while the results are wrong.

That is why you combine tests with data checks, comparisons with trusted totals and review by people who know the process. Each one catches mistakes that the others miss. When a problem slips through all of them, ask which check should have caught it, add that check and record what happened without blaming anyone.

Make tests quick

Keep test inputs small and independent of live systems. Tests that run in seconds get run after every change. Slow tests that need a real database or a fresh export tend to be skipped just when they matter.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. What is a regression test for cleaning code?

    It records the failing case so a future change cannot reintroduce it silently.
  2. 2. Which belongs in a data dictionary?

    The dictionary explains how to interpret each column.
  3. 3. Why compare old and new outputs before releasing a rule change?

    A comparison reveals side effects that example tests may miss.

Summary

  • Test normal, edge and rejected cases.
  • Turn real failure patterns into made-up test values.
  • Compare outputs before you change rules.
  • Keep a data dictionary, a decision log and run reports.

Next: Cleaning Data with SQL

Progres Anda disimpan di browser ini. Tidak perlu akun.