Skip to content
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions exercises/concept/intro-select/create_fixture.sql
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
6 changes: 6 additions & 0 deletions exercises/concept/intro-select/data.csv
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
33 changes: 33 additions & 0 deletions exercises/concept/intro-select/intro-select.sql
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
;
17 changes: 17 additions & 0 deletions exercises/concept/intro-select/intro-select_test.sql
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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shoudl the rm happen before the read?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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 results.db, I have confirmed that the .save command will overwrite such.

.save ./results.db

.output

-- Report results
.shell bash ./report-results.sh results.db
16 changes: 16 additions & 0 deletions exercises/concept/intro-select/report-results.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
DB_FILE=$1
mapfile -t SLUGS < <(jq -r 'keys[]' test_data.json)

# Generate result for each test
rm -f results.txt
for SLUG in "${SLUGS[@]}"; do
ACTUAL=$(sqlite3 -json $DB_FILE "SELECT * FROM ${SLUG};" | tr -d '[:space:]')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would there be spaces in the slags?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There wouldn't be spaces in the slugs, but there would be in the SELECT results. Maybe table would be a better variable name than SLUG in this context.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... or maybe not, since it is used as the slug on line 11.

if [ -z "$ACTUAL" ]; then
ACTUAL="[]"
fi
jq -n --slurpfile test_data test_data.json --argjson got "${ACTUAL}" --arg slug "${SLUG}" -f test-result.jq >> results.txt
done

# Aggregate results
jq -n --slurpfile results results.txt '$results' > output.json
30 changes: 30 additions & 0 deletions exercises/concept/intro-select/test-result.jq
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
else
(got | rows | tostring) as $got_rows
| (expected | rows | tostring) as $expected_rows
| "With columns " + $got_columns + ", \nbexpected " + $expected_rows + ";\nbut got " + $got_rows
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| $entry + {$description} as $entry
| $entry += {$description}

Copy link
Copy Markdown
Author

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!

Copy link
Copy Markdown
Contributor

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) }
  end

Copy link
Copy Markdown
Author

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!

| if $got != $expected then
$entry + {"status": "fail", "message": failure_message($got; $expected)}
else
$entry + {"status": "pass"}
end
64 changes: 64 additions & 0 deletions exercises/concept/intro-select/test_data.json
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},
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason there's no spaces after , and : in part of the data?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason. Will add spaces.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

{"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"}
]
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing newline please

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.