Skip to content

Commit e57620a

Browse files
authored
Auto-format .sql files using github.com/sql-formatter-org/sql-formatter - pascals-triangle (#165)
[no important files changed]
1 parent 13fc520 commit e57620a

File tree

5 files changed

+158
-69
lines changed

5 files changed

+158
-69
lines changed
Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,48 @@
11
UPDATE "pascals-triangle"
2-
SET result = (
3-
WITH RECURSIVE counter(k) AS (
4-
SELECT 1 UNION ALL
5-
SELECT k + 1 FROM counter WHERE k < input
6-
)
7-
8-
SELECT group_concat(
9-
(
10-
WITH RECURSIVE nums(idx, s) AS (
11-
SELECT 1, 1 UNION ALL
12-
SELECT idx + 1, s * k / idx - s FROM nums WHERE idx < k
2+
SET
3+
result = (
4+
WITH RECURSIVE
5+
counter (k) AS (
6+
SELECT
7+
1
8+
UNION ALL
9+
SELECT
10+
k + 1
11+
FROM
12+
counter
13+
WHERE
14+
k < input
1315
)
14-
15-
SELECT group_concat(s, ' ') FROM nums
16-
),
17-
char(10) -- newline
18-
) FROM counter
19-
);
16+
SELECT
17+
group_concat(
18+
(
19+
WITH RECURSIVE
20+
nums (idx, s) AS (
21+
SELECT
22+
1,
23+
1
24+
UNION ALL
25+
SELECT
26+
idx + 1,
27+
s * k / idx - s
28+
FROM
29+
nums
30+
WHERE
31+
idx < k
32+
)
33+
SELECT
34+
group_concat(s, ' ')
35+
FROM
36+
nums
37+
),
38+
char(10) -- newline
39+
)
40+
FROM
41+
counter
42+
);
2043

2144
UPDATE "pascals-triangle"
22-
SET result = ''
23-
WHERE input = 0;
45+
SET
46+
result = ''
47+
WHERE
48+
input = 0;
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
DROP TABLE IF EXISTS "pascals-triangle";
2-
CREATE TABLE "pascals-triangle" (
3-
"input" INT,
4-
"result" TEXT
5-
);
2+
3+
CREATE TABLE "pascals-triangle" ("input" INT, "result" TEXT);
64

75
.mode csv
86
.import ./data.csv pascals-triangle
Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,71 @@
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-
('zero rows', '9920ce55-9629-46d5-85d6-4201f4a4234d', 0, ''),
9-
('single row', '70d643ce-a46d-4e93-af58-12d88dd01f21', 1, '1'),
10-
('two rows', 'a6e5a2a2-fc9a-4b47-9f4f-ed9ad9fbe4bd', 2, '1
11-
1 1'),
12-
('three rows', '97206a99-79ba-4b04-b1c5-3c0fa1e16925', 3, '1
4+
INSERT INTO
5+
tests (name, uuid, input, expected)
6+
VALUES
7+
(
8+
'zero rows',
9+
'9920ce55-9629-46d5-85d6-4201f4a4234d',
10+
0,
11+
''
12+
),
13+
(
14+
'single row',
15+
'70d643ce-a46d-4e93-af58-12d88dd01f21',
16+
1,
17+
'1'
18+
),
19+
(
20+
'two rows',
21+
'a6e5a2a2-fc9a-4b47-9f4f-ed9ad9fbe4bd',
22+
2,
23+
'1
24+
1 1'
25+
),
26+
(
27+
'three rows',
28+
'97206a99-79ba-4b04-b1c5-3c0fa1e16925',
29+
3,
30+
'1
1331
1 1
14-
1 2 1'),
15-
('four rows', '565a0431-c797-417c-a2c8-2935e01ce306', 4, '1
32+
1 2 1'
33+
),
34+
(
35+
'four rows',
36+
'565a0431-c797-417c-a2c8-2935e01ce306',
37+
4,
38+
'1
1639
1 1
1740
1 2 1
18-
1 3 3 1'),
19-
('five rows', '06f9ea50-9f51-4eb2-b9a9-c00975686c27', 5, '1
41+
1 3 3 1'
42+
),
43+
(
44+
'five rows',
45+
'06f9ea50-9f51-4eb2-b9a9-c00975686c27',
46+
5,
47+
'1
2048
1 1
2149
1 2 1
2250
1 3 3 1
23-
1 4 6 4 1'),
24-
('six rows', 'c3912965-ddb4-46a9-848e-3363e6b00b13', 6, '1
51+
1 4 6 4 1'
52+
),
53+
(
54+
'six rows',
55+
'c3912965-ddb4-46a9-848e-3363e6b00b13',
56+
6,
57+
'1
2558
1 1
2659
1 2 1
2760
1 3 3 1
2861
1 4 6 4 1
29-
1 5 10 10 5 1'),
30-
('ten rows', '6cb26c66-7b57-4161-962c-81ec8c99f16b', 10, '1
62+
1 5 10 10 5 1'
63+
),
64+
(
65+
'ten rows',
66+
'6cb26c66-7b57-4161-962c-81ec8c99f16b',
67+
10,
68+
'1
3169
1 1
3270
1 2 1
3371
1 3 3 1
@@ -36,13 +74,23 @@ INSERT INTO tests (name, uuid,
3674
1 6 15 20 15 6 1
3775
1 7 21 35 35 21 7 1
3876
1 8 28 56 70 56 28 8 1
39-
1 9 36 84 126 126 84 36 9 1');
77+
1 9 36 84 126 126 84 36 9 1'
78+
);
4079

4180
-- Comparison of user input and the tests updates the status for each test:
4281
UPDATE tests
43-
SET status = 'pass'
44-
FROM (SELECT input, result FROM "pascals-triangle") AS actual
45-
WHERE (actual.input, actual.result) = (tests.input, tests.expected);
82+
SET
83+
status = 'pass'
84+
FROM
85+
(
86+
SELECT
87+
input,
88+
result
89+
FROM
90+
"pascals-triangle"
91+
) AS actual
92+
WHERE
93+
(actual.input, actual.result) = (tests.input, tests.expected);
4694

4795
-- Write results and debug info:
4896
.read ./test_reporter.sql
Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
11
-- Update message for failed tests to give helpful information:
22
UPDATE tests
3-
SET message = (
4-
'Result for ' || tests.input
5-
|| ' is:' || char(10) || COALESCE(actual.result, 'NULL')
6-
|| char(10) || 'but should be:' || char(10) || tests.expected
7-
)
8-
FROM (SELECT input, result FROM "pascals-triangle") AS actual
9-
WHERE actual.input = tests.input AND tests.status = 'fail';
3+
SET
4+
message = (
5+
'Result for ' || tests.input || ' is:' || char(10) || COALESCE(actual.result, 'NULL') || char(10) || 'but should be:' || char(10) || tests.expected
6+
)
7+
FROM
8+
(
9+
SELECT
10+
input,
11+
result
12+
FROM
13+
"pascals-triangle"
14+
) AS actual
15+
WHERE
16+
actual.input = tests.input
17+
AND tests.status = 'fail';
1018

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

1732
-- Display test results in readable form for the student:
1833
.mode table
19-
SELECT name, status, message
20-
FROM tests;
34+
SELECT
35+
name,
36+
status,
37+
message
38+
FROM
39+
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 ./pascals-triangle.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 INT 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 INT NOT NULL,
23+
expected TEXT NOT NULL
2524
);

0 commit comments

Comments
 (0)