Skip to content

Commit 145d0d9

Browse files
authored
Auto-format .sql files using github.com/sql-formatter-org/sql-formatter - two-fer (#186)
1 parent 0696ebb commit 145d0d9

File tree

5 files changed

+83
-49
lines changed

5 files changed

+83
-49
lines changed
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
UPDATE twofer
2-
SET response = 'One for ' ||
3-
CASE
4-
WHEN input = '' THEN 'you'
5-
ELSE input
6-
END ||
7-
', one for me.';
2+
SET
3+
response = 'One for ' || CASE
4+
WHEN input = '' THEN 'you'
5+
ELSE input
6+
END || ', one for me.';
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
DROP TABLE IF EXISTS twofer;
2-
CREATE TABLE "twofer" (
3-
"input" TEXT,
4-
"response" TEXT
5-
);
62

7-
-- Note: the CSV file contain literal tab, newline, carriage returns.
3+
CREATE TABLE "twofer" ("input" TEXT, "response" TEXT);
84

5+
-- Note: the CSV file contain literal tab, newline, carriage returns.
96
.mode csv
107
.import ./data.csv twofer
Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
11
UPDATE tests
2-
SET message = (
3-
'Result for ' || tests.input
4-
|| ' is <' || COALESCE(actual.response, 'NULL')
5-
|| '> but should be <' || tests.expected || '>'
6-
)
7-
FROM (SELECT input, response FROM twofer) AS actual
8-
WHERE actual.input = tests.input AND tests.status = 'fail';
2+
SET
3+
message = (
4+
'Result for ' || tests.input || ' is <' || COALESCE(actual.response, 'NULL') || '> but should be <' || tests.expected || '>'
5+
)
6+
FROM
7+
(
8+
SELECT
9+
input,
10+
response
11+
FROM
12+
twofer
13+
) AS actual
14+
WHERE
15+
actual.input = tests.input
16+
AND tests.status = 'fail';
917

1018
-- Save results to ./output.json (needed by the online test-runner)
1119
.mode json
1220
.once './output.json'
13-
SELECT name, status, message, output, test_code, task_id
14-
FROM tests;
21+
SELECT
22+
name,
23+
status,
24+
message,
25+
output,
26+
test_code,
27+
task_id
28+
FROM
29+
tests;
1530

1631
-- Display test results in readable form for the student:
1732
.mode table
18-
SELECT name, status, message
19-
FROM tests;
33+
SELECT
34+
name,
35+
status,
36+
message
37+
FROM
38+
tests;
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
-- Create database:
22
.read ./create_fixture.sql
3-
43
-- Read user student solution and save any output as markdown in user_output.md:
54
.mode markdown
65
.output user_output.md
76
.read ./two-fer.sql
87
.output
9-
108
-- Create a clean testing environment:
119
DROP TABLE IF EXISTS tests;
10+
1211
CREATE TABLE IF NOT EXISTS tests (
13-
-- uuid and name (description) are taken from the test.toml file
14-
uuid TEXT PRIMARY KEY,
15-
name TEXT NOT NULL,
16-
-- The following section is needed by the online test-runner
17-
status TEXT DEFAULT 'fail',
18-
message TEXT,
19-
output TEXT,
20-
test_code TEXT,
21-
task_id INTEGER DEFAULT NULL,
22-
-- Here are columns for the actual tests
23-
input TEXT NOT NULL,
24-
expected TEXT NOT NULL
12+
-- uuid and name (description) are taken from the test.toml file
13+
uuid TEXT PRIMARY KEY,
14+
name TEXT NOT NULL,
15+
-- The following section is needed by the online test-runner
16+
status TEXT DEFAULT 'fail',
17+
message TEXT,
18+
output TEXT,
19+
test_code TEXT,
20+
task_id INTEGER DEFAULT NULL,
21+
-- Here are columns for the actual tests
22+
input TEXT NOT NULL,
23+
expected TEXT NOT NULL
2524
);
Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,42 @@
11
-- Setup test table and read in student solution:
22
.read ./test_setup.sql
3-
43
-- Test cases:
5-
INSERT INTO tests (name, uuid,
6-
input, expected)
7-
VALUES
8-
('no name given', '1cf3e15a-a3d7-4a87-aeb3-ba1b43bc8dce',
9-
'', 'One for you, one for me.'),
10-
('a name given', 'b4c6dbb8-b4fb-42c2-bafd-10785abe7709',
11-
'Alice', 'One for Alice, one for me.'),
12-
('another name given', '3549048d-1a6e-4653-9a79-b0bda163e8d5',
13-
'Bob', 'One for Bob, one for me.');
4+
INSERT INTO
5+
tests (name, uuid, input, expected)
6+
VALUES
7+
(
8+
'no name given',
9+
'1cf3e15a-a3d7-4a87-aeb3-ba1b43bc8dce',
10+
'',
11+
'One for you, one for me.'
12+
),
13+
(
14+
'a name given',
15+
'b4c6dbb8-b4fb-42c2-bafd-10785abe7709',
16+
'Alice',
17+
'One for Alice, one for me.'
18+
),
19+
(
20+
'another name given',
21+
'3549048d-1a6e-4653-9a79-b0bda163e8d5',
22+
'Bob',
23+
'One for Bob, one for me.'
24+
);
1425

1526
-- Comparison of user input and the tests updates the status for each test:
1627
UPDATE tests
17-
SET status = 'pass'
18-
FROM (SELECT input, response FROM twofer) AS actual
19-
WHERE (actual.input, actual.response) = (tests.input, tests.expected);
28+
SET
29+
status = 'pass'
30+
FROM
31+
(
32+
SELECT
33+
input,
34+
response
35+
FROM
36+
twofer
37+
) AS actual
38+
WHERE
39+
(actual.input, actual.response) = (tests.input, tests.expected);
2040

2141
-- Write results and debug info:
2242
.read ./test_reporter.sql

0 commit comments

Comments
 (0)