-
-
Notifications
You must be signed in to change notification settings - Fork 15
Applying tdd on select-only exercise #219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 13 commits
91e48a5
1c3d67f
93bc000
ac316d1
581bda6
20a91ec
eac0195
24d11d9
2c68fb2
878a964
d1b773e
6303fbe
284fe3a
54a1dde
4afcf77
b8c6850
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| DROP TABLE IF EXISTS weather_readings; | ||
| CREATE TABLE weather_readings ( | ||
| date TEXT NOT NULL, | ||
| location TEXT NOT NULL, | ||
| temperature INTEGER NOT NULL, | ||
| humidity INTEGER NOT NULL | ||
| ); | ||
|
|
||
| .mode csv | ||
| .import ./data.csv weather_readings |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| "2025-10-22","Portland",53,72 | ||
| "2025-10-22","Seattle",56,66 | ||
| "2025-10-22","Boise",60,55 | ||
| "2025-10-23","Portland",54,70 | ||
| "2025-10-23","Seattle",57,68 | ||
| "2025-10-23","Boise",62,58 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| CREATE TABLE all_data AS | ||
| -- Task 1. Select all records. | ||
| SELECT * FROM weather_readings | ||
| ; | ||
|
|
||
| CREATE TABLE location_and_temperature AS | ||
| -- Task 2. Select only location and temperature data. | ||
| -- Expect failure | ||
| SELECT temperature FROM weather_readings | ||
| ; | ||
|
|
||
| CREATE TABLE seattle AS | ||
| -- Task 3. Select all data for Seattle. | ||
| SELECT * FROM weather_readings WHERE location = 'Seattle' | ||
| ; | ||
|
|
||
| CREATE TABLE limited_humidity AS | ||
| -- Task 4. Select all data where the humidity is between 60% and 70%. | ||
| -- Expect failure | ||
| SELECT * FROM weather_readings WHERE humidity BETWEEN 50 AND 62 | ||
| ; | ||
| .print "My debugging output:" | ||
| SELECT humidity from weather_readings; | ||
|
|
||
| CREATE TABLE location AS | ||
| -- Task 5. Select only location data. | ||
| SELECT location FROM weather_readings | ||
| ; | ||
|
|
||
| CREATE TABLE unique_location AS | ||
| -- Task 5. Select only unique location data. | ||
| SELECT DISTINCT location FROM weather_readings | ||
| ; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| -- Create database: | ||
| .read ./create_fixture.sql | ||
|
|
||
| -- ASK: How can we correlate user output with specific tests? One way is to add .output statements | ||
| -- in the stub file. But that introduces more noise into the stub file. | ||
|
|
||
| -- Run user solution and store results | ||
| .mode markdown | ||
| .output user_output.md | ||
| .read ./intro-select.sql | ||
| .shell rm -f ./results.db | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shoudl the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so. If you're worried about a pre-existing |
||
| .save ./results.db | ||
|
|
||
| .output | ||
|
|
||
| -- Report results | ||
| .shell bash ./report-results.sh results.db | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #!/usr/bin/env bash | ||
| DB_FILE=$1 | ||
blackk-foxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
blackk-foxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mapfile -t SLUGS < <(jq -r 'keys[]' test_data.json) | ||
|
|
||
| # Generate result for each test | ||
| rm -f results.txt | ||
blackk-foxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for SLUG in "${SLUGS[@]}"; do | ||
| ACTUAL=$(sqlite3 -json $DB_FILE "SELECT * FROM ${SLUG};" | tr -d '[:space:]') | ||
|
||
| if [ -z "$ACTUAL" ]; then | ||
blackk-foxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ACTUAL="[]" | ||
| fi | ||
| jq -n --slurpfile test_data test_data.json --argjson got "${ACTUAL}" --arg slug "${SLUG}" -f test-result.jq >> results.txt | ||
| done | ||
blackk-foxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Aggregate results | ||
| jq -n --slurpfile results results.txt '$results' > output.json | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,30 @@ | ||||||
| def columns: | ||||||
| if (. | length) == 0 then [] | ||||||
| else .[0] | keys | ||||||
| end; | ||||||
|
|
||||||
| def rows: | ||||||
| map(to_entries | map(.value)); | ||||||
|
|
||||||
| def failure_message(got; expected): | ||||||
| (got | columns | tostring) as $got_columns | ||||||
| | (expected | columns | tostring) as $expected_columns | ||||||
| | if $got_columns != $expected_columns then | ||||||
| "Expected columns " + $expected_columns + "; but got " + $got_columns | ||||||
blackk-foxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| else | ||||||
| (got | rows | tostring) as $got_rows | ||||||
| | (expected | rows | tostring) as $expected_rows | ||||||
| | "With columns " + $got_columns + ", \nbexpected " + $expected_rows + ";\nbut got " + $got_rows | ||||||
blackk-foxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| end; | ||||||
|
|
||||||
| $test_data[0][$slug] as $single_test_data | ||||||
| | $single_test_data.description as $description | ||||||
| | $single_test_data.expected as $expected | ||||||
| | $single_test_data.task_id as $task_id | ||||||
| | if $task_id then {$task_id} else {} end as $entry | ||||||
| | $entry + {$description} as $entry | ||||||
|
||||||
| | $entry + {$description} as $entry | |
| | $entry += {$description} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, this suggestion didn't work as expected -- it did not modify $entry. So I will revert this one. But please let me know if you have other ideas on how to simplify it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, careless of me. How about this: what you're trying to do is select some details from the slug object in the test_data and add the status.
Can we just use that object directly?
$test_data[0][$slug]
| if $got == .expected then
. + { "status": "pass" }
else
. + { "status": "fail",
"message": failure_message($got; .expected) }
endThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost. We also want to remove .expected from the result. But I think I can slightly tweak your example to do that. Thanks!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| { | ||
| "all_data": { | ||
| "task_id": 1, | ||
| "description": "All data", | ||
| "expected": [ | ||
| {"date":"2025-10-22","location":"Portland","temperature":53,"humidity":72}, | ||
|
||
| {"date":"2025-10-22","location":"Seattle","temperature":56,"humidity":66}, | ||
| {"date":"2025-10-22","location":"Boise","temperature":60,"humidity":55}, | ||
| {"date":"2025-10-23","location":"Portland","temperature":54,"humidity":70}, | ||
| {"date":"2025-10-23","location":"Seattle","temperature":57,"humidity":68}, | ||
| {"date":"2025-10-23","location":"Boise","temperature":62,"humidity":58} | ||
| ] | ||
| }, | ||
| "location_and_temperature": { | ||
| "task_id": 2, | ||
| "description": "Just location and temperature", | ||
| "expected": [ | ||
| {"location":"Portland","temperature":53}, | ||
| {"location":"Seattle","temperature":56}, | ||
| {"location":"Boise","temperature":60}, | ||
| {"location":"Portland","temperature":54}, | ||
| {"location":"Seattle","temperature":57}, | ||
| {"location":"Boise","temperature":62} | ||
| ] | ||
| }, | ||
| "seattle": { | ||
| "task_id": 3, | ||
| "description": "Seattle only", | ||
| "expected": [ | ||
| {"date":"2025-10-22","location":"Seattle","temperature":56,"humidity":66}, | ||
| {"date":"2025-10-23","location":"Seattle","temperature":57,"humidity":68} | ||
| ] | ||
| }, | ||
| "limited_humidity": { | ||
| "task_id": 4, | ||
| "description": "Humidity within range", | ||
| "expected": [ | ||
| {"date":"2025-10-22","location":"Seattle","temperature":56,"humidity":66}, | ||
| {"date":"2025-10-23","location":"Portland","temperature":54,"humidity":70}, | ||
| {"date":"2025-10-23","location":"Seattle","temperature":57,"humidity":68} | ||
| ] | ||
| }, | ||
| "location": { | ||
| "task_id": 5, | ||
| "description": "Just locations", | ||
| "expected": [ | ||
| {"location":"Portland"}, | ||
| {"location":"Seattle"}, | ||
| {"location":"Boise"}, | ||
| {"location":"Portland"}, | ||
| {"location":"Seattle"}, | ||
| {"location":"Boise"} | ||
| ] | ||
| }, | ||
| "unique_location": { | ||
| "task_id": 5, | ||
| "description": "Only unique locations", | ||
| "expected": [ | ||
| {"location":"Portland"}, | ||
| {"location":"Seattle"}, | ||
| {"location":"Boise"} | ||
| ] | ||
| } | ||
| } | ||
|
||
Uh oh!
There was an error while loading. Please reload this page.