QA AND TEST AUTOMATION · PART 9 · NON-FUNCTIONAL TESTING · DATA · 86 OF 112

Data and ETL Testing: What to Check

Testing data, not screens: what ETL pipelines do, source-to-target mapping, and the checks that matter (row counts, sum reconciliation, completeness, uniqueness, nulls, referential integrity, business rules, freshness), written as SQL against ShopRung's database, where a stock reconciliation caught the stock-not-reduced bug and an orphan check caught a row the database accepted because it declares no foreign keys.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • ETL and mapping
  • reconcile counts and sums
  • invariants
  • stock check caught a bug
  • orphans
  • quality dimensions
  • dbt tests

Notes

ETL

ETL means extract, transform, load. For ShopRung, a nightly job could extract orders, order items and products, transform them by joining, converting cents to amounts, totalling sales per day and category, and dropping test accounts, then load the result into a warehouse table that a sales dashboard reads. Many modern teams do ELT instead: load raw data first, then transform inside the warehouse with tools like dbt.

A ShopRung pipeline
  • extract: orders, order_items, products from ShopRung
  • transform: join, convert cents to amounts, daily totals by category, drop test users
  • load: into a warehouse table daily_sales
  • used by: the sales dashboard
  • ELT: load first, transform inside the warehouse (dbt)

Mapping

The specification for data work is a source to target mapping. For each target column, it says where the value comes from and which rule transforms it: sale date is the date of the order's created at, units is the sum of item quantities, revenue is the sum of totals divided by a hundred to two decimals, and cancelled orders and staff accounts are filtered out. Testers derive test cases from every row, exactly like requirements.

Source-to-target mapping
target column   source / rule
sale_date       date(orders.created_at)
category        products.category
units           SUM(order_items.qty)
revenue         SUM(orders.total)
                / 100, 2 decimals
filter          status != CANCELLED
                and role = CUSTOMER

Reconciliation

Reconciliation compares source and target. Row counts: the number of qualifying orders must match what the target was built from. Sums: total revenue in the source, in cents, must equal the target's revenue times a hundred. When they differ, do not stop at the counts; list the keys that are missing or extra, because a count can match while different rows are wrong.

Reconciliation checks
-- row counts
SELECT COUNT(*) FROM orders
  WHERE status != 'CANCELLED';
-- vs rows feeding daily_sales

-- money
SELECT SUM(total_cents) FROM orders
  WHERE ...;
-- = SUM(revenue) * 100 in target
differences → list the missing
  or extra keys, not just a count

Invariants

Invariants are rules that must always be true. Each order's total must equal subtotal minus discount plus shipping. Each order's subtotal must equal the sum of its items' price times quantity. A good data check returns the rows that break the rule, so an empty result means pass. We placed three orders in ShopRung through its API and ran both: zero rows, zero rows. Pass.

Rules that must always hold
-- total = subtotal - discount
--         + shipping
SELECT id FROM orders WHERE
  total_cents != subtotal_cents
  - discount_cents + shipping_cents;
-- subtotal = sum of its items
...GROUP BY o.id HAVING
  o.subtotal_cents != SUM(
  i.price_cents * i.quantity);
→ after 3 orders: 0 rows, 0 rows

Stock reconciliation

Then a reconciliation across tables. The Clean Code Notebook started with a stock of forty. We ordered three, then four, so seven were sold and stock should be thirty three. The database said thirty seven. The second order had run with ShopRung's stock not reduced bug turned on. Every API call returned two hundred, and nothing looked wrong on screen; only the data showed the bug.

Product 1, Clean Code Notebook
seed stock                 40
ordered: 3 + 4      =  7 sold
expected stock  40 - 7 =  33
actual stock               37

the second order ran with the
  stock-not-reduced bug on
every API call returned 200;
  only the data shows the bug

Integrity

ShopRung's tables declare no foreign keys, so the database does not protect relationships itself. We inserted an order item pointing at product nine nine nine, which does not exist, and it was accepted. The orphan check, a left join where the product is missing, found it straight away, and the subtotal invariant now failed for order three too. Where constraints are missing, tests must cover them.

Orphans and constraints
INSERT INTO order_items
  VALUES (3, 999, 'Ghost', 100, 1);
→ accepted: no FOREIGN KEY
  declared on order_items

SELECT i.order_id, i.product_id
  FROM order_items i LEFT JOIN
  products p ON p.id = i.product_id
  WHERE p.id IS NULL;
→ (3, 999)  and order 3's
  subtotal no longer matches

The full list

The full list, often called data quality dimensions. Completeness: required values present and all rows arrived. Uniqueness: no duplicate keys. Validity: correct types, ranges and allowed values. Consistency: invariants and totals agree. Integrity: no orphans. Accuracy: values match the source. Freshness: the data loaded on time. And idempotency: running the job twice must not double the numbers.

What to check
  • completeness: required values present, all rows arrived
  • uniqueness: no duplicate keys
  • validity: types, ranges, allowed values
  • consistency: invariants, totals
  • integrity: no orphans
  • accuracy: matches the source
  • freshness: loaded on time
  • idempotent: rerun = same result

Tools

Data checks belong in the pipeline as code. In dbt, a schema file declares tests per column: unique, not null, accepted values, and relationships, which is the orphan check. Great Expectations and Soda do the same with their own formats, and plain pytest with SQL queries works well too. Run them after every load, and stop bad data before it reaches dashboards.

Common tools
# dbt schema.yml
columns:
  - name: order_id
    tests: [unique, not_null]
  - name: product_id
    tests:
      - relationships:
          to: ref('products')
          field: id
# also: Great Expectations, Soda,
#   pytest + SQL

Common mistakes

Mistakes. Testing with ten hand made rows, when production has millions with odd values. Stopping when counts match. Not checking for duplicates after a job is rerun. Ignoring time zones, so sales slip into the wrong day. Rounding differences, because summing then rounding differs from rounding then summing. And copying real personal data into test systems instead of masking it.

Avoid these
  • testing with 10 hand-made rows
  • counts match, so "it's fine"
  • no check for duplicates after a rerun
  • time zones and dates ignored
  • rounding: sum then round vs round then sum
  • real personal data copied into test systems

Say it like this

In the interview: I start from the source to target mapping and test each column's rule. Then I reconcile row counts and sums with the source and list the missing or extra keys, and check uniqueness, nulls, ranges, orphans and business invariants, plus freshness and that a rerun gives the same result. These run as SQL or dbt tests after every load, with production like volumes and masked data.

In the interview
  • "From the source-to-target
  • mapping: per column rules, then reconcile counts and sums with the source, list missing keys, check uniqueness, nulls, ranges, orphans and business invariants, freshness and rerun idempotency.
  • As SQL or dbt tests after every load, with production-like
  • volumes and masked data."

Interview questions

Q1What is ETL?

Extract, transform, load: moving data from sources, applying rules such as joins, filters and aggregations, and loading it into a target such as a warehouse.

Q2What is a source-to-target mapping?

A document listing each target column with its source fields and transformation rule; testers derive test cases from it.

Q3What is data reconciliation?

Comparing source and target row counts, sums and keys to prove nothing was lost, duplicated or changed incorrectly.

Q4Why is a matching row count not enough?

Different rows can be missing and extra at the same time; compare sums and list missing or extra keys too.

Q5How do you find orphan records?

A LEFT JOIN from child to parent where the parent key IS NULL, or a relationships test in dbt.

Q6What are data quality dimensions?

Completeness, uniqueness, validity, consistency, integrity, accuracy and freshness (plus idempotency for jobs).

Q7How did data testing catch a bug the API tests missed on ShopRung?

Every order call returned 200, but stock reconciliation (40 - 7 sold = 33 expected, 37 actual) exposed the stock-not-reduced bug.

Q8Which tools are used for data testing?

SQL with pytest, dbt tests (unique, not_null, relationships, accepted_values), Great Expectations and Soda.