Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
A golden-output test compares this run against a blessed one. actual == golden answers whether they differ and nothing else, which turns every failure into a manual scroll through two files.
Write diff_against_golden(actual, golden, key_fields). It reports what is missing, what is unexpected, what changed and in which fields.
Function to write
diff_against_golden(actual: list[dict], golden: list[dict], key_fields: list[str]) -> dictA dict describing the `missing`, `unexpected` and `changed` rows, and the `matched` count.
How to approach it
Ask what a failing golden test should print for somebody at 3am.
Sample cases
+ 2 held back until you submit
one changed, one missing, one unexpected
All three kinds of difference at once, and a matched count that excludes the changed row.
Input
Argument 1
| id | amount |
|---|---|
| 1 | 11 |
| 3 | 30 |
Argument 2
| id | amount |
|---|---|
| 1 | 10 |
| 2 | 20 |
Argument 3
[
'id'
]Returns
{
'missing': [
[
'2'
]
],
'unexpected': [
[
'3'
]
],
'changed': [
{
'key': [
'1'
],
'fields': [
{
'field': 'amount',
'golden': '10',
'actual': '11'
}
]
}
],
'matched': 0
}identical output
A clean run reports two matches and nothing else — the answer, not an absence.
Input
Argument 1
| id | amount |
|---|---|
| 1 | 10 |
| 2 | 20 |
Argument 2
| id | amount |
|---|---|
| 1 | 10 |
| 2 | 20 |
Argument 3
[
'id'
]Returns
{
'missing': [],
'unexpected': [],
'changed': [],
'matched': 2
}both sides empty
Nothing against nothing is a match of zero rather than an error or a false alarm.
Input
Argument 1
[] (empty list)
Argument 2
[] (empty list)
Argument 3
[
'id'
]Returns
{
'missing': [],
'unexpected': [],
'changed': [],
'matched': 0
}Constraints
{'missing': [...], 'unexpected': [...], 'changed': [...], 'matched': <count>}.key_fields. missing is in golden and not in actual; unexpected is the reverse. Both are lists of key values, sorted.{'field': ..., 'golden': ..., 'actual': ...}, sorted by field name.matched counts shared keys with no differences at all.Worked example
Golden has rows 1 and 2; the run produced 1 and 3, and its row 1 has an amount of 11 where golden says 10.
The diff is one missing key (2), one unexpected key (3), one changed row naming amount with both values, and a matched count of zero — row 1 is shared but changed, so it is not a match. That last distinction is what makes the number worth printing: matched should equal the row count when a run is clean, and anything less is a question.
What this tests
That a failing test is only as useful as the message it produces, and that a diff over keyed records needs three separate categories — a missing row and a changed row have different causes and different fixes.
diff_against_golden(actual: list[dict], golden: list[dict], key_fields: list[str]) -> dictSubmit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.