Skip to content

Commit dec945d

Browse files
authored
Auto-format .sql files using github.com/sql-formatter-org/sql-formatter - resistor-color-duo (#169)
[no important files changed]
1 parent c70bc85 commit dec945d

File tree

5 files changed

+131
-68
lines changed

5 files changed

+131
-68
lines changed
Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
-- CREATE TABLE "color_code" ("color1" TEXT, "color2" TEXT, "result" INT);
2-
32
DROP TABLE IF EXISTS "colors";
3+
44
CREATE TABLE "colors" ("index" INT, "color" TEXT);
55

6-
INSERT INTO "colors" ("index", "color")
7-
VALUES
8-
(0, 'black'),
9-
(1, 'brown'),
10-
(2, 'red'),
11-
(3, 'orange'),
12-
(4, 'yellow'),
13-
(5, 'green'),
14-
(6, 'blue'),
15-
(7, 'violet'),
16-
(8, 'grey'),
17-
(9, 'white')
18-
;
6+
INSERT INTO
7+
"colors" ("index", "color")
8+
VALUES
9+
(0, 'black'),
10+
(1, 'brown'),
11+
(2, 'red'),
12+
(3, 'orange'),
13+
(4, 'yellow'),
14+
(5, 'green'),
15+
(6, 'blue'),
16+
(7, 'violet'),
17+
(8, 'grey'),
18+
(9, 'white');
1919

2020
UPDATE color_code
21-
SET result = (
22-
SELECT (c1."index" * 10) + c2."index"
23-
FROM colors AS c1, colors AS c2
24-
WHERE c1.color = color_code.color1 AND c2.color = color_code.color2
25-
)
26-
;
21+
SET
22+
result = (
23+
SELECT
24+
(c1."index" * 10) + c2."index"
25+
FROM
26+
colors AS c1,
27+
colors AS c2
28+
WHERE
29+
c1.color = color_code.color1
30+
AND c2.color = color_code.color2
31+
);
2732

2833
DROP TABLE IF EXISTS "colors";
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- The color_code has specific colors and needs the result filled in.
22
DROP TABLE IF EXISTS "color_code";
3-
CREATE TABLE "color_code" (
4-
"color1" TEXT, "color2" TEXT, "result" INT
5-
);
3+
4+
CREATE TABLE "color_code" ("color1" TEXT, "color2" TEXT, "result" INT);
5+
66
.mode csv
77
.import ./data.csv color_code
Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,67 @@
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-
color1, color2, expected)
7-
VALUES
8-
('Brown and black', 'ce11995a-5b93-4950-a5e9-93423693b2fc',
9-
'brown', 'black', 10),
10-
('Blue and grey', '7bf82f7a-af23-48ba-a97d-38d59406a920',
11-
'blue', 'grey', 68),
12-
('Yellow and violet', 'f1886361-fdfd-4693-acf8-46726fe24e0c',
13-
'yellow', 'violet', 47),
14-
('White and red', 'b7a6cbd2-ae3c-470a-93eb-56670b305640',
15-
'white', 'red', 92),
16-
('Orange and orange', '77a8293d-2a83-4016-b1af-991acc12b9fe',
17-
'orange', 'orange', 33),
18-
('Black and brown, one-digit', '4a8ceec5-0ab4-4904-88a4-daf953a5e818',
19-
'black', 'brown', 1);
4+
INSERT INTO
5+
tests (name, uuid, color1, color2, expected)
6+
VALUES
7+
(
8+
'Brown and black',
9+
'ce11995a-5b93-4950-a5e9-93423693b2fc',
10+
'brown',
11+
'black',
12+
10
13+
),
14+
(
15+
'Blue and grey',
16+
'7bf82f7a-af23-48ba-a97d-38d59406a920',
17+
'blue',
18+
'grey',
19+
68
20+
),
21+
(
22+
'Yellow and violet',
23+
'f1886361-fdfd-4693-acf8-46726fe24e0c',
24+
'yellow',
25+
'violet',
26+
47
27+
),
28+
(
29+
'White and red',
30+
'b7a6cbd2-ae3c-470a-93eb-56670b305640',
31+
'white',
32+
'red',
33+
92
34+
),
35+
(
36+
'Orange and orange',
37+
'77a8293d-2a83-4016-b1af-991acc12b9fe',
38+
'orange',
39+
'orange',
40+
33
41+
),
42+
(
43+
'Black and brown, one-digit',
44+
'4a8ceec5-0ab4-4904-88a4-daf953a5e818',
45+
'black',
46+
'brown',
47+
1
48+
);
2049

2150
-- Comparison of user input and the tests updates the status for each test:
2251
UPDATE tests
23-
SET status = 'pass'
24-
FROM (SELECT color1, color2, result FROM "color_code") AS actual
25-
WHERE (actual.color1, actual.color2, actual.result) = (tests.color1, tests.color2, tests.expected);
52+
SET
53+
status = 'pass'
54+
FROM
55+
(
56+
SELECT
57+
color1,
58+
color2,
59+
result
60+
FROM
61+
"color_code"
62+
) AS actual
63+
WHERE
64+
(actual.color1, actual.color2, actual.result) = (tests.color1, tests.color2, tests.expected);
2665

2766
-- Write results and debug info:
2867
.read ./test_reporter.sql
Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
11
-- Update message for failed tests to give helpful information:
22
UPDATE tests
3-
SET message = (
4-
'Result for "' || tests.color1 || '" and "' || tests.color2 || '"'
5-
|| ' is <' || COALESCE(actual.result, 'NULL')
6-
|| '> but should be <' || tests.expected || '>'
7-
)
8-
FROM (SELECT color1, color2, result FROM "color_code") AS actual
9-
WHERE (actual.color1, actual.color2) = (tests.color1, tests.color2) AND tests.status = 'fail';
3+
SET
4+
message = (
5+
'Result for "' || tests.color1 || '" and "' || tests.color2 || '"' || ' is <' || COALESCE(actual.result, 'NULL') || '> but should be <' || tests.expected || '>'
6+
)
7+
FROM
8+
(
9+
SELECT
10+
color1,
11+
color2,
12+
result
13+
FROM
14+
"color_code"
15+
) AS actual
16+
WHERE
17+
(actual.color1, actual.color2) = (tests.color1, tests.color2)
18+
AND tests.status = 'fail';
1019

1120
-- Save results to ./output.json (needed by the online test-runner)
1221
.mode json
1322
.once './output.json'
14-
SELECT name, status, message, output, test_code, task_id
15-
FROM tests;
23+
SELECT
24+
name,
25+
status,
26+
message,
27+
output,
28+
test_code,
29+
task_id
30+
FROM
31+
tests;
1632

1733
-- Display test results in readable form for the student:
1834
.mode table
19-
SELECT name, status, message
20-
FROM tests;
35+
SELECT
36+
name,
37+
status,
38+
message
39+
FROM
40+
tests;
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
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 ./resistor-color-duo.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-
color1 TEXT NOT NULL,
24-
color2 TEXT NOT NULL,
25-
expected INTEGER 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+
color1 TEXT NOT NULL,
23+
color2 TEXT NOT NULL,
24+
expected INTEGER NOT NULL
2625
);

0 commit comments

Comments
 (0)