-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
base: main
Are you sure you want to change the base?
Changes from all commits
91e48a5
1c3d67f
93bc000
ac316d1
581bda6
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 REAL 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.1,72 | ||
| "2025-10-22","Seattle",56.2,66 | ||
| "2025-10-22","Boise",60.4,55 | ||
| "2025-10-23","Portland",54.6,70 | ||
| "2025-10-23","Seattle",57.8,68 | ||
| "2025-10-23","Boise",62.0,58 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| from pathlib import Path | ||
| import json | ||
| import sys | ||
|
|
||
|
|
||
| def gather_results(filename): | ||
| text = Path(filename).read_text() | ||
| list_start_indices = find_all_occurrences('[', text) | ||
| list_end_indices = find_all_occurrences(']', text) | ||
| return [ | ||
| json.loads(text[begin:end+1]) | ||
| for begin, end in zip(list_start_indices, list_end_indices) | ||
| ] | ||
|
|
||
|
|
||
| def find_all_occurrences(char, string): | ||
| return [i for i, c, in enumerate(string) if c == char] | ||
|
|
||
|
|
||
| def format_diff(expected, actual): | ||
| return ( | ||
| f"\tExpected:\n" | ||
| f"\t\t{expected}\n" | ||
| f"\tActual:\n" | ||
| f"\t\t{actual}" | ||
| ) | ||
|
|
||
| def report_results(all_test_data, actual_results): | ||
| results = [] | ||
| for test_data, actual in zip(all_test_data, actual_results): | ||
| result = {'description': test_data['description']} | ||
| if test_data['expected'] == actual: | ||
| result['status'] = 'pass' | ||
| else: | ||
| result['status'] = 'fail' | ||
| result['message'] = "Expected: " + json.dumps(test_data['expected']) | ||
| result['output'] = "Actual: " + json.dumps(actual) | ||
| results.append(result) | ||
| return make_result_report(results) | ||
|
|
||
|
|
||
| def make_result_report(results): | ||
| status = 'pass' if all(r['status'] == 'pass' for r in results) else 'fail' | ||
| return { | ||
| "version": 3, | ||
| "status": status, | ||
| "tests": results | ||
| } | ||
|
|
||
|
|
||
| def main(): | ||
| test_data_filename, user_output_filename = sys.argv[1:3] | ||
| test_data = json.loads(Path(test_data_filename).read_text()) | ||
| actual_results = gather_results(user_output_filename) | ||
| results = report_results(test_data, actual_results) | ||
| Path('results.json').write_text(json.dumps(results, indent=2)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| SELECT * FROM weather_readings; | ||
|
|
||
| SELECT location, temperature FROM weather_readings; | ||
|
|
||
| -- This one will fail on purpose | ||
| SELECT 'Hello, world.' AS say_hi; | ||
|
|
||
| SELECT * FROM weather_readings WHERE location = 'Seattle'; | ||
|
|
||
| SELECT * FROM weather_readings WHERE humidity BETWEEN 60 AND 70; | ||
|
|
||
| SELECT location FROM weather_readings; | ||
|
|
||
| SELECT DISTINCT location FROM weather_readings; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| -- Create database: | ||
| .read ./create_fixture.sql | ||
|
|
||
| .mode json | ||
|
|
||
| -- Run user solution | ||
| .output user_output.txt | ||
| .read ./intro-select.sql | ||
|
|
||
| -- Compare expected vs actual | ||
| .shell python evaluate.py test_data.json user_output.txt |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| [ | ||
| { | ||
| "description": "ALL records => SELECT * FROM weather_readings", | ||
| "expected": [ | ||
| {"date":"2025-10-22","location":"Portland","temperature":53.1,"humidity":72}, | ||
| {"date":"2025-10-22","location":"Seattle","temperature":56.2,"humidity":66}, | ||
| {"date":"2025-10-22","location":"Boise","temperature":60.4,"humidity":55}, | ||
| {"date":"2025-10-23","location":"Portland","temperature":54.6,"humidity":70}, | ||
| {"date":"2025-10-23","location":"Seattle","temperature":57.79999999999999,"humidity":68}, | ||
|
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. For some reason, this comes out as 57.79999999999 instead of the original 57.8 in the input data (see
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. Floating point numbers are hard for computers.
Numbers like 57.8 (or 0.8) cannot actually be handled very well by computers using the common representations. The original only looks like 57.8 when input or rounded. Float comparisons generally go better when you compare that the two values are close enough.
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. You can bypass this problem by picking nicer values, eg fractions which can be represented as |
||
| {"date":"2025-10-23","location":"Boise","temperature":62.0,"humidity":58} | ||
| ] | ||
| }, | ||
| { | ||
| "description": "Just location and temperature columns => SELECT location, temperature FROM weather_readings", | ||
| "expected": [ | ||
| {"location":"Portland","temperature":53.1}, | ||
| {"location":"Seattle","temperature":56.2}, | ||
| {"location":"Boise","temperature":60.4}, | ||
| {"location":"Portland","temperature":54.6}, | ||
| {"location":"Seattle","temperature":57.79999999999999}, | ||
| {"location":"Boise","temperature":62.0} | ||
| ] | ||
| }, | ||
| { | ||
| "description": "Without \"FROM\" => SELECT 'Hello, world.'", | ||
| "expected": [ | ||
| {"'Hello, world.'":"Hello, world."} | ||
| ] | ||
| }, | ||
| { | ||
| "description": "All records from Seatle location => SELECT * FROM weather_readings WHERE location = 'Seattle'", | ||
| "expected": [ | ||
| {"date":"2025-10-22","location":"Seattle","temperature":56.2,"humidity":66}, | ||
| {"date":"2025-10-23","location":"Seattle","temperature":57.79999999999999,"humidity":68} | ||
| ] | ||
| }, | ||
| { | ||
| "description": "All records where humidity in range => SELECT * FROM weather_readings WHERE humidity BETWEEN 60 AND 70", | ||
| "expected": [ | ||
| {"date":"2025-10-22","location":"Seattle","temperature":56.2,"humidity":66}, | ||
| {"date":"2025-10-23","location":"Portland","temperature":54.6,"humidity":70}, | ||
| {"date":"2025-10-23","location":"Seattle","temperature":57.79999999999999,"humidity":68} | ||
| ] | ||
| }, | ||
| { | ||
| "description": "Just location column => SELECT location FROM weather_readings", | ||
| "expected": [ | ||
| {"location":"Portland"}, | ||
| {"location":"Seattle"}, | ||
| {"location":"Boise"}, | ||
| {"location":"Portland"}, | ||
| {"location":"Seattle"}, | ||
| {"location":"Boise"} | ||
| ] | ||
| }, | ||
| { | ||
| "description": "Only unique locations => SELECT DISTINCT location FROM weather_readings", | ||
| "expected": [ | ||
| {"location":"Portland"}, | ||
| {"location":"Seattle"}, | ||
| {"location":"Boise"} | ||
| ] | ||
| } | ||
| ] | ||
Uh oh!
There was an error while loading. Please reload this page.