Skip to content

Commit 6230580

Browse files
authored
Auto-format .sql files using github.com/sql-formatter-org/sql-formatter - grains (#152)
[no important files changed]
1 parent fcf1f04 commit 6230580

File tree

5 files changed

+129
-59
lines changed

5 files changed

+129
-59
lines changed
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
UPDATE grains
2-
SET result = POWER(2, square - 1)
3-
WHERE task = 'single-square';
2+
SET
3+
result = POWER(2, square - 1)
4+
WHERE
5+
task = 'single-square';
46

57
UPDATE grains
6-
SET result = POWER(2, 64) - 1
7-
WHERE task = 'total';
8+
SET
9+
result = POWER(2, 64) - 1
10+
WHERE
11+
task = 'total';
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
DROP TABLE IF EXISTS grains;
2+
23
CREATE TABLE "grains" (
3-
"task" TEXT NOT NULL,
4-
"square" INT NOT NULL,
5-
"result" INT NOT NULL
4+
"task" TEXT NOT NULL,
5+
"square" INT NOT NULL,
6+
"result" INT NOT NULL
67
);
78

89
-- Note: the CSV file may contain literal tab, newline, carriage returns.
9-
1010
.mode csv
1111
.import ./data.csv grains
Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,82 @@
11
-- Setup test table and read in student solution:
22
.read ./test_setup.sql
3-
43
-- Test cases:
54
-- Note: the strings below may contain literal tab, newline, carriage returns.
6-
7-
INSERT INTO tests (name, uuid,
8-
task, square, expected)
9-
VALUES
10-
('grains on square 1', '9fbde8de-36b2-49de-baf2-cd42d6f28405',
11-
'single-square', 1, 1),
12-
('grains on square 2', 'ee1f30c2-01d8-4298-b25d-c677331b5e6d',
13-
'single-square', 2, 2),
14-
('grains on square 3', '10f45584-2fc3-4875-8ec6-666065d1163b',
15-
'single-square', 3, 4),
16-
('grains on square 4', 'a7cbe01b-36f4-4601-b053-c5f6ae055170',
17-
'single-square', 4, 8),
18-
('grains on square 16', 'c50acc89-8535-44e4-918f-b848ad2817d4',
19-
'single-square', 16, 32768),
20-
('grains on square 32', 'acd81b46-c2ad-4951-b848-80d15ed5a04f',
21-
'single-square', 32, 2147483648),
22-
('grains on square 64', 'c73b470a-5efb-4d53-9ac6-c5f6487f227b',
23-
'single-square', 64, 9223372036854775808),
24-
('returns the total number of grains on the board', '6eb07385-3659-4b45-a6be-9dc474222750',
25-
'total', 0, 18446744073709551615);
26-
5+
INSERT INTO
6+
tests (name, uuid, task, square, expected)
7+
VALUES
8+
(
9+
'grains on square 1',
10+
'9fbde8de-36b2-49de-baf2-cd42d6f28405',
11+
'single-square',
12+
1,
13+
1
14+
),
15+
(
16+
'grains on square 2',
17+
'ee1f30c2-01d8-4298-b25d-c677331b5e6d',
18+
'single-square',
19+
2,
20+
2
21+
),
22+
(
23+
'grains on square 3',
24+
'10f45584-2fc3-4875-8ec6-666065d1163b',
25+
'single-square',
26+
3,
27+
4
28+
),
29+
(
30+
'grains on square 4',
31+
'a7cbe01b-36f4-4601-b053-c5f6ae055170',
32+
'single-square',
33+
4,
34+
8
35+
),
36+
(
37+
'grains on square 16',
38+
'c50acc89-8535-44e4-918f-b848ad2817d4',
39+
'single-square',
40+
16,
41+
32768
42+
),
43+
(
44+
'grains on square 32',
45+
'acd81b46-c2ad-4951-b848-80d15ed5a04f',
46+
'single-square',
47+
32,
48+
2147483648
49+
),
50+
(
51+
'grains on square 64',
52+
'c73b470a-5efb-4d53-9ac6-c5f6487f227b',
53+
'single-square',
54+
64,
55+
9223372036854775808
56+
),
57+
(
58+
'returns the total number of grains on the board',
59+
'6eb07385-3659-4b45-a6be-9dc474222750',
60+
'total',
61+
0,
62+
18446744073709551615
63+
);
2764

2865
-- Comparison of user input and the tests updates the status for each test:
2966
UPDATE tests
30-
SET status = 'pass'
31-
FROM (SELECT task, square, result FROM grains) AS actual
32-
WHERE (actual.task, actual.square, actual.result) = (tests.task, tests.square, tests.expected);
67+
SET
68+
status = 'pass'
69+
FROM
70+
(
71+
SELECT
72+
task,
73+
square,
74+
result
75+
FROM
76+
grains
77+
) AS actual
78+
WHERE
79+
(actual.task, actual.square, actual.result) = (tests.task, tests.square, tests.expected);
3380

3481
-- Write results and debug info:
3582
.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.task || ' as ' || tests.square
5-
|| ' is <' || COALESCE(actual.result, 'NULL')
6-
|| '> but should be <' || tests.expected || '>'
7-
)
8-
FROM (SELECT task, square, result FROM grains) AS actual
9-
WHERE (actual.task, actual.square) = (tests.task, tests.square) AND tests.status = 'fail';
3+
SET
4+
message = (
5+
'Result for ' || tests.task || ' as ' || tests.square || ' is <' || COALESCE(actual.result, 'NULL') || '> but should be <' || tests.expected || '>'
6+
)
7+
FROM
8+
(
9+
SELECT
10+
task,
11+
square,
12+
result
13+
FROM
14+
grains
15+
) AS actual
16+
WHERE
17+
(actual.task, actual.square) = (tests.task, tests.square)
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 ./grains.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-
task TEXT NOT NULL,
24-
square INTEGER 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+
task TEXT NOT NULL,
23+
square INTEGER NOT NULL,
24+
expected INTEGER NOT NULL
2625
);

0 commit comments

Comments
 (0)