Skip to content

Commit d2e62a1

Browse files
authored
Auto-format .sql files using github.com/sql-formatter-org/sql-formatter - series (#177)
[no important files changed]
1 parent 6f8121d commit d2e62a1

File tree

4 files changed

+238
-96
lines changed

4 files changed

+238
-96
lines changed
Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,53 @@
11
UPDATE series
2-
SET error = 'slice length cannot be zero'
3-
WHERE slice_length = 0
4-
AND error ISNULL
5-
;
2+
SET
3+
error = 'slice length cannot be zero'
4+
WHERE
5+
slice_length = 0
6+
AND error ISNULL;
7+
68
UPDATE series
7-
SET error = 'series cannot be empty'
8-
WHERE input = ''
9-
AND error ISNULL
10-
;
9+
SET
10+
error = 'series cannot be empty'
11+
WHERE
12+
input = ''
13+
AND error ISNULL;
14+
1115
UPDATE series
12-
SET error = 'slice length cannot be greater than series length'
13-
WHERE slice_length > LENGTH(input)
14-
AND error ISNULL
15-
;
16+
SET
17+
error = 'slice length cannot be greater than series length'
18+
WHERE
19+
slice_length > LENGTH(input)
20+
AND error ISNULL;
21+
1622
UPDATE series
17-
SET error = 'slice length cannot be negative'
18-
WHERE slice_length < 0
19-
AND error ISNULL
20-
;
23+
SET
24+
error = 'slice length cannot be negative'
25+
WHERE
26+
slice_length < 0
27+
AND error ISNULL;
2128

2229
UPDATE series
23-
SET result = (
24-
WITH RECURSIVE to_series (digits, serie) AS (
25-
VALUES (input, NULL)
26-
UNION ALL
27-
SELECT SUBSTR(digits, 2), SUBSTR(digits, 1, slice_length)
28-
FROM to_series
29-
WHERE digits <> ''
30-
)
31-
SELECT GROUP_CONCAT(serie, CHAR(10)) AS series
32-
FROM to_series WHERE LENGTH(serie) = slice_length
33-
)
34-
WHERE error ISNULL
35-
;
30+
SET
31+
result = (
32+
WITH RECURSIVE
33+
to_series (digits, serie) AS (
34+
VALUES
35+
(input, NULL)
36+
UNION ALL
37+
SELECT
38+
SUBSTR(digits, 2),
39+
SUBSTR(digits, 1, slice_length)
40+
FROM
41+
to_series
42+
WHERE
43+
digits <> ''
44+
)
45+
SELECT
46+
GROUP_CONCAT(serie, CHAR(10)) AS series
47+
FROM
48+
to_series
49+
WHERE
50+
LENGTH(serie) = slice_length
51+
)
52+
WHERE
53+
error ISNULL;
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
DROP TABLE IF EXISTS series;
2+
23
CREATE TABLE series (
3-
input TEXT NOT NULL,
4-
slice_length INTEGER NOT NULL,
5-
result TEXT ,
6-
error TEXT
4+
input TEXT NOT NULL,
5+
slice_length INTEGER NOT NULL,
6+
result TEXT,
7+
error TEXT
78
);
89

910
.mode csv
1011
.import ./data.csv series
11-
12-
UPDATE series SET result = NULL, error = NULL;
12+
UPDATE series
13+
SET
14+
result = NULL,
15+
error = NULL;
Lines changed: 116 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,121 @@
11
DROP TABLE IF EXISTS tests;
2+
23
CREATE TABLE IF NOT EXISTS tests (
3-
-- uuid and description are taken from the test.toml file
4-
uuid TEXT PRIMARY KEY,
5-
description TEXT NOT NULL,
6-
-- The following section is needed by the online test-runner
7-
status TEXT DEFAULT 'fail',
8-
message TEXT,
9-
output TEXT,
10-
test_code TEXT,
11-
task_id INTEGER DEFAULT NULL,
12-
-- Here are columns for the actual tests
13-
input TEXT NOT NULL,
14-
slice_length INTEGER NOT NULL,
15-
expected_result TEXT,
16-
expected_error TEXT
4+
-- uuid and description are taken from the test.toml file
5+
uuid TEXT PRIMARY KEY,
6+
description TEXT NOT NULL,
7+
-- The following section is needed by the online test-runner
8+
status TEXT DEFAULT 'fail',
9+
message TEXT,
10+
output TEXT,
11+
test_code TEXT,
12+
task_id INTEGER DEFAULT NULL,
13+
-- Here are columns for the actual tests
14+
input TEXT NOT NULL,
15+
slice_length INTEGER NOT NULL,
16+
expected_result TEXT,
17+
expected_error TEXT
1718
);
1819

19-
INSERT INTO tests (uuid, description, input, slice_length, expected_result, expected_error)
20-
VALUES
21-
('7ae7a46a-d992-4c2a-9c15-a112d125ebad', 'slices of one from one', '1', 1, '1', NULL),
22-
('3143b71d-f6a5-4221-aeae-619f906244d2', 'slices of one from two', '12', 1, '1\n2', NULL),
23-
('dbb68ff5-76c5-4ccd-895a-93dbec6d5805', 'slices of two', '35', 2, '35', NULL),
24-
('19bbea47-c987-4e11-a7d1-e103442adf86', 'slices of two overlap', '9142', 2, '91\n14\n42', NULL),
25-
('8e17148d-ba0a-4007-a07f-d7f87015d84c', 'slices can include duplicates', '777777', 3, '777\n777\n777\n777', NULL),
26-
('bd5b085e-f612-4f81-97a8-6314258278b0', 'slices of a long series', '918493904243', 5, '91849\n18493\n84939\n49390\n93904\n39042\n90424\n04243', NULL),
27-
('6d235d85-46cf-4fae-9955-14b6efef27cd', 'slice length is too large', '12345', 6, NULL, 'slice length cannot be greater than series length'),
28-
('d7957455-346d-4e47-8e4b-87ed1564c6d7', 'slice length is way too large', '12345', 42, NULL, 'slice length cannot be greater than series length'),
29-
('d34004ad-8765-4c09-8ba1-ada8ce776806', 'slice length cannot be zero', '12345', 0, NULL, 'slice length cannot be zero'),
30-
('10ab822d-8410-470a-a85d-23fbeb549e54', 'slice length cannot be negative', '123', -1, NULL, 'slice length cannot be negative'),
31-
('c7ed0812-0e4b-4bf3-99c4-28cbbfc246a2', 'empty series is invalid', '', 1, NULL, 'series cannot be empty');
20+
INSERT INTO
21+
tests (
22+
uuid,
23+
description,
24+
input,
25+
slice_length,
26+
expected_result,
27+
expected_error
28+
)
29+
VALUES
30+
(
31+
'7ae7a46a-d992-4c2a-9c15-a112d125ebad',
32+
'slices of one from one',
33+
'1',
34+
1,
35+
'1',
36+
NULL
37+
),
38+
(
39+
'3143b71d-f6a5-4221-aeae-619f906244d2',
40+
'slices of one from two',
41+
'12',
42+
1,
43+
'1\n2',
44+
NULL
45+
),
46+
(
47+
'dbb68ff5-76c5-4ccd-895a-93dbec6d5805',
48+
'slices of two',
49+
'35',
50+
2,
51+
'35',
52+
NULL
53+
),
54+
(
55+
'19bbea47-c987-4e11-a7d1-e103442adf86',
56+
'slices of two overlap',
57+
'9142',
58+
2,
59+
'91\n14\n42',
60+
NULL
61+
),
62+
(
63+
'8e17148d-ba0a-4007-a07f-d7f87015d84c',
64+
'slices can include duplicates',
65+
'777777',
66+
3,
67+
'777\n777\n777\n777',
68+
NULL
69+
),
70+
(
71+
'bd5b085e-f612-4f81-97a8-6314258278b0',
72+
'slices of a long series',
73+
'918493904243',
74+
5,
75+
'91849\n18493\n84939\n49390\n93904\n39042\n90424\n04243',
76+
NULL
77+
),
78+
(
79+
'6d235d85-46cf-4fae-9955-14b6efef27cd',
80+
'slice length is too large',
81+
'12345',
82+
6,
83+
NULL,
84+
'slice length cannot be greater than series length'
85+
),
86+
(
87+
'd7957455-346d-4e47-8e4b-87ed1564c6d7',
88+
'slice length is way too large',
89+
'12345',
90+
42,
91+
NULL,
92+
'slice length cannot be greater than series length'
93+
),
94+
(
95+
'd34004ad-8765-4c09-8ba1-ada8ce776806',
96+
'slice length cannot be zero',
97+
'12345',
98+
0,
99+
NULL,
100+
'slice length cannot be zero'
101+
),
102+
(
103+
'10ab822d-8410-470a-a85d-23fbeb549e54',
104+
'slice length cannot be negative',
105+
'123',
106+
-1,
107+
NULL,
108+
'slice length cannot be negative'
109+
),
110+
(
111+
'c7ed0812-0e4b-4bf3-99c4-28cbbfc246a2',
112+
'empty series is invalid',
113+
'',
114+
1,
115+
NULL,
116+
'series cannot be empty'
117+
);
32118

33-
UPDATE tests SET expected_result = REPLACE(expected_result, '\n', CHAR(10));
119+
UPDATE tests
120+
SET
121+
expected_result = REPLACE(expected_result, '\n', CHAR(10));
Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,87 @@
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 ./series.sql
87
.output
9-
108
-- Create a clean testing environment:
119
.read ./create_test_table.sql
12-
1310
-- Comparison of user input and the tests updates the status for each test:
1411
UPDATE tests
15-
SET status = 'pass'
16-
FROM (SELECT input, slice_length, result, error FROM series) AS actual
17-
WHERE (actual.input, actual.slice_length) = (tests.input, tests.slice_length)
18-
AND (actual.result = tests.expected_result
19-
OR COALESCE(actual.result, tests.expected_result) ISNULL)
20-
AND (actual.error = tests.expected_error
21-
OR COALESCE(actual.error, tests.expected_error) ISNULL);
12+
SET
13+
status = 'pass'
14+
FROM
15+
(
16+
SELECT
17+
input,
18+
slice_length,
19+
result,
20+
error
21+
FROM
22+
series
23+
) AS actual
24+
WHERE
25+
(actual.input, actual.slice_length) = (tests.input, tests.slice_length)
26+
AND (
27+
actual.result = tests.expected_result
28+
OR COALESCE(actual.result, tests.expected_result) ISNULL
29+
)
30+
AND (
31+
actual.error = tests.expected_error
32+
OR COALESCE(actual.error, tests.expected_error) ISNULL
33+
);
2234

2335
-- Update message for failed tests to give helpful information:
2436
UPDATE tests
25-
SET message = (
26-
'Result for "'
27-
|| PRINTF('input=''%s'' and slice_length=''%s''',
28-
actual.input,
29-
actual.slice_length)
30-
|| '"'
31-
|| ' is <'
32-
|| PRINTF('result=%s and error="%s"',
33-
COALESCE(actual.result, 'NULL'),
34-
COALESCE(actual.error, 'NULL'))
35-
|| '> but should be <'
36-
|| PRINTF('result=%s and error="%s"',
37-
COALESCE(tests.expected_result, '"NULL"'),
38-
COALESCE(tests.expected_error, 'NULL'))
39-
|| '>'
40-
)
41-
FROM (SELECT input, slice_length, result, error FROM series) AS actual
42-
WHERE (actual.input, actual.slice_length) = (tests.input, tests.slice_length)
43-
AND tests.status = 'fail';
37+
SET
38+
message = (
39+
'Result for "' || PRINTF(
40+
'input=''%s'' and slice_length=''%s''',
41+
actual.input,
42+
actual.slice_length
43+
) || '"' || ' is <' || PRINTF(
44+
'result=%s and error="%s"',
45+
COALESCE(actual.result, 'NULL'),
46+
COALESCE(actual.error, 'NULL')
47+
) || '> but should be <' || PRINTF(
48+
'result=%s and error="%s"',
49+
COALESCE(tests.expected_result, '"NULL"'),
50+
COALESCE(tests.expected_error, 'NULL')
51+
) || '>'
52+
)
53+
FROM
54+
(
55+
SELECT
56+
input,
57+
slice_length,
58+
result,
59+
error
60+
FROM
61+
series
62+
) AS actual
63+
WHERE
64+
(actual.input, actual.slice_length) = (tests.input, tests.slice_length)
65+
AND tests.status = 'fail';
4466

4567
-- Save results to ./output.json (needed by the online test-runner)
4668
.mode json
4769
.once './output.json'
48-
SELECT description, status, message, output, test_code, task_id
49-
FROM tests;
70+
SELECT
71+
description,
72+
status,
73+
message,
74+
output,
75+
test_code,
76+
task_id
77+
FROM
78+
tests;
5079

5180
-- Display test results in readable form for the student:
5281
.mode table
53-
SELECT description, status, message
54-
FROM tests;
82+
SELECT
83+
description,
84+
status,
85+
message
86+
FROM
87+
tests;

0 commit comments

Comments
 (0)