Skip to content

Commit 915c9bd

Browse files
authored
Auto-format .sql files using github.com/sql-formatter-org/sql-formatter - atbash-cipher (#143)
[no important files changed]
1 parent 82f3539 commit 915c9bd

File tree

4 files changed

+253
-106
lines changed

4 files changed

+253
-106
lines changed
Lines changed: 86 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,90 @@
11
UPDATE "atbash-cipher"
2-
SET result = (
3-
WITH RECURSIVE rcte (i, c, string) AS (
4-
VALUES(1, NULL, (
5-
WITH RECURSIVE encode (phrase, c) AS (
6-
VALUES(LOWER(phrase), NULL)
7-
UNION ALL
8-
SELECT SUBSTR(phrase, 2), SUBSTR(phrase, 1, 1)
9-
FROM encode
10-
WHERE phrase <> ''
11-
)
12-
SELECT GROUP_CONCAT(c, '') AS result
13-
FROM (
14-
SELECT CASE
15-
WHEN (UNICODE(c) BETWEEN UNICODE('a') AND UNICODE('z'))
16-
THEN CHAR(UNICODE('z') - (UNICODE(c) - UNICODE('a')))
17-
WHEN (UNICODE(c) BETWEEN UNICODE('0') AND UNICODE('9'))
18-
THEN c
2+
SET
3+
result = (
4+
WITH RECURSIVE
5+
rcte (i, c, string) AS (
6+
VALUES
7+
(
8+
1,
9+
NULL,
10+
(
11+
WITH RECURSIVE
12+
encode (phrase, c) AS (
13+
VALUES
14+
(LOWER(phrase), NULL)
15+
UNION ALL
16+
SELECT
17+
SUBSTR(phrase, 2),
18+
SUBSTR(phrase, 1, 1)
19+
FROM
20+
encode
21+
WHERE
22+
phrase <> ''
23+
)
24+
SELECT
25+
GROUP_CONCAT(c, '') AS result
26+
FROM
27+
(
28+
SELECT
29+
CASE
30+
WHEN (UNICODE(c) BETWEEN UNICODE('a') AND UNICODE('z')) THEN CHAR(UNICODE('z') - (UNICODE(c) - UNICODE('a')))
31+
WHEN (UNICODE(c) BETWEEN UNICODE('0') AND UNICODE('9')) THEN c
1932
END AS c
20-
FROM encode
21-
)
22-
WHERE c NOT NULL
23-
))
24-
UNION ALL
25-
SELECT i + 1,
26-
SUBSTR(string, 1, 1) || IIF(i % 5 = 0, ' ', ''),
27-
SUBSTR(string, 2)
28-
FROM rcte
29-
WHERE string <> ''
30-
)
31-
SELECT TRIM(GROUP_CONCAT(c, '')) FROM rcte
32-
)
33-
WHERE property = 'encode'
34-
;
33+
FROM
34+
encode
35+
)
36+
WHERE
37+
c NOT NULL
38+
)
39+
)
40+
UNION ALL
41+
SELECT
42+
i + 1,
43+
SUBSTR(string, 1, 1) || IIF(i % 5 = 0, ' ', ''),
44+
SUBSTR(string, 2)
45+
FROM
46+
rcte
47+
WHERE
48+
string <> ''
49+
)
50+
SELECT
51+
TRIM(GROUP_CONCAT(c, ''))
52+
FROM
53+
rcte
54+
)
55+
WHERE
56+
property = 'encode';
3557

3658
UPDATE "atbash-cipher"
37-
SET result = (
38-
WITH RECURSIVE decode (phrase, c) AS (
39-
VALUES(phrase, NULL)
40-
UNION ALL
41-
SELECT SUBSTR(phrase, 2), SUBSTR(phrase, 1, 1)
42-
FROM decode
43-
WHERE phrase <> ''
44-
)
45-
SELECT GROUP_CONCAT(c, '') AS result
46-
FROM (
47-
SELECT CASE
48-
WHEN (UNICODE(c) BETWEEN UNICODE('a') AND UNICODE('z'))
49-
THEN CHAR(UNICODE('z') - UNICODE(c) + UNICODE('a'))
50-
WHEN (UNICODE(c) BETWEEN UNICODE('0') AND UNICODE('9')) THEN c
51-
END AS c
52-
FROM decode
53-
)
54-
WHERE c NOT NULL
55-
)
56-
WHERE property = 'decode'
57-
;
59+
SET
60+
result = (
61+
WITH RECURSIVE
62+
decode (phrase, c) AS (
63+
VALUES
64+
(phrase, NULL)
65+
UNION ALL
66+
SELECT
67+
SUBSTR(phrase, 2),
68+
SUBSTR(phrase, 1, 1)
69+
FROM
70+
decode
71+
WHERE
72+
phrase <> ''
73+
)
74+
SELECT
75+
GROUP_CONCAT(c, '') AS result
76+
FROM
77+
(
78+
SELECT
79+
CASE
80+
WHEN (UNICODE(c) BETWEEN UNICODE('a') AND UNICODE('z')) THEN CHAR(UNICODE('z') - UNICODE(c) + UNICODE('a'))
81+
WHEN (UNICODE(c) BETWEEN UNICODE('0') AND UNICODE('9')) THEN c
82+
END AS c
83+
FROM
84+
decode
85+
)
86+
WHERE
87+
c NOT NULL
88+
)
89+
WHERE
90+
property = 'decode';
Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,70 @@
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 ./atbash-cipher.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 property, phrase, result FROM "atbash-cipher") AS actual
17-
WHERE (actual.property, actual.phrase, actual.result) = (tests.property, tests.phrase, tests.expected);
12+
SET
13+
status = 'pass'
14+
FROM
15+
(
16+
SELECT
17+
property,
18+
phrase,
19+
result
20+
FROM
21+
"atbash-cipher"
22+
) AS actual
23+
WHERE
24+
(actual.property, actual.phrase, actual.result) = (tests.property, tests.phrase, tests.expected);
1825

1926
-- Update message for failed tests to give helpful information:
2027
UPDATE tests
21-
SET message = (
22-
'Result for "'
23-
|| JSON_OBJECT(
24-
'property', tests.property,
25-
'phrase', tests.phrase
26-
)
27-
|| '"'
28-
|| ' is <' || COALESCE(actual.result, 'NULL')
29-
|| '> but should be <' || tests.expected || '>'
30-
)
31-
FROM (SELECT property, phrase, result FROM "atbash-cipher") AS actual
32-
WHERE (actual.property, actual.phrase) = (tests.property, tests.phrase) AND tests.status = 'fail';
28+
SET
29+
message = (
30+
'Result for "' || JSON_OBJECT(
31+
'property',
32+
tests.property,
33+
'phrase',
34+
tests.phrase
35+
) || '"' || ' is <' || COALESCE(actual.result, 'NULL') || '> but should be <' || tests.expected || '>'
36+
)
37+
FROM
38+
(
39+
SELECT
40+
property,
41+
phrase,
42+
result
43+
FROM
44+
"atbash-cipher"
45+
) AS actual
46+
WHERE
47+
(actual.property, actual.phrase) = (tests.property, tests.phrase)
48+
AND tests.status = 'fail';
3349

3450
-- Save results to ./output.json (needed by the online test-runner)
3551
.mode json
3652
.once './output.json'
37-
SELECT description, status, message, output, test_code, task_id
38-
FROM tests;
53+
SELECT
54+
description,
55+
status,
56+
message,
57+
output,
58+
test_code,
59+
task_id
60+
FROM
61+
tests;
3962

4063
-- Display test results in readable form for the student:
4164
.mode table
42-
SELECT description, status, message
43-
FROM tests;
65+
SELECT
66+
description,
67+
status,
68+
message
69+
FROM
70+
tests;

exercises/practice/atbash-cipher/create_fixture.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
DROP TABLE IF EXISTS "atbash-cipher";
2+
23
CREATE TABLE "atbash-cipher" (
34
property TEXT NOT NULL,
4-
phrase TEXT NOT NULL,
5-
result TEXT
5+
phrase TEXT NOT NULL,
6+
result TEXT
67
);
78

89
.mode csv
Lines changed: 115 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,119 @@
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-
property TEXT NOT NULL,
14-
phrase TEXT NOT NULL,
15-
expected TEXT NOT NULL
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+
property TEXT NOT NULL,
15+
phrase TEXT NOT NULL,
16+
expected TEXT NOT NULL
1617
);
1718

18-
INSERT INTO tests (uuid, description, property, phrase, expected)
19-
VALUES
20-
('2f47ebe1-eab9-4d6b-b3c6-627562a31c77','encode yes','encode','yes','bvh'),
21-
('b4ffe781-ea81-4b74-b268-cc58ba21c739','encode no','encode','no','ml'),
22-
('10e48927-24ab-4c4d-9d3f-3067724ace00','encode OMG','encode','OMG','lnt'),
23-
('d59b8bc3-509a-4a9a-834c-6f501b98750b','encode spaces','encode','O M G','lnt'),
24-
('31d44b11-81b7-4a94-8b43-4af6a2449429','encode mindblowingly','encode','mindblowingly','nrmwy oldrm tob'),
25-
('d503361a-1433-48c0-aae0-d41b5baa33ff','encode numbers','encode','Testing,1 2 3, testing.','gvhgr mt123 gvhgr mt'),
26-
('79c8a2d5-0772-42d4-b41b-531d0b5da926','encode deep thought','encode','Truth is fiction.','gifgs rhurx grlm'),
27-
('9ca13d23-d32a-4967-a1fd-6100b8742bab','encode all the letters','encode','The quick brown fox jumps over the lazy dog.','gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt'),
28-
('bb50e087-7fdf-48e7-9223-284fe7e69851','decode exercism','decode','vcvix rhn','exercism'),
29-
('ac021097-cd5d-4717-8907-b0814b9e292c','decode a sentence','decode','zmlyh gzxov rhlug vmzhg vkkrm thglm v','anobstacleisoftenasteppingstone'),
30-
('18729de3-de74-49b8-b68c-025eaf77f851','decode numbers','decode','gvhgr mt123 gvhgr mt','testing123testing'),
31-
('0f30325f-f53b-415d-ad3e-a7a4f63de034','decode all the letters','decode','gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt','thequickbrownfoxjumpsoverthelazydog'),
32-
('39640287-30c6-4c8c-9bac-9d613d1a5674','decode with too many spaces','decode','vc vix r hn','exercism'),
33-
('b34edf13-34c0-49b5-aa21-0768928000d5','decode with no spaces','decode','zmlyhgzxovrhlugvmzhgvkkrmthglmv','anobstacleisoftenasteppingstone');
19+
INSERT INTO
20+
tests (uuid, description, property, phrase, expected)
21+
VALUES
22+
(
23+
'2f47ebe1-eab9-4d6b-b3c6-627562a31c77',
24+
'encode yes',
25+
'encode',
26+
'yes',
27+
'bvh'
28+
),
29+
(
30+
'b4ffe781-ea81-4b74-b268-cc58ba21c739',
31+
'encode no',
32+
'encode',
33+
'no',
34+
'ml'
35+
),
36+
(
37+
'10e48927-24ab-4c4d-9d3f-3067724ace00',
38+
'encode OMG',
39+
'encode',
40+
'OMG',
41+
'lnt'
42+
),
43+
(
44+
'd59b8bc3-509a-4a9a-834c-6f501b98750b',
45+
'encode spaces',
46+
'encode',
47+
'O M G',
48+
'lnt'
49+
),
50+
(
51+
'31d44b11-81b7-4a94-8b43-4af6a2449429',
52+
'encode mindblowingly',
53+
'encode',
54+
'mindblowingly',
55+
'nrmwy oldrm tob'
56+
),
57+
(
58+
'd503361a-1433-48c0-aae0-d41b5baa33ff',
59+
'encode numbers',
60+
'encode',
61+
'Testing,1 2 3, testing.',
62+
'gvhgr mt123 gvhgr mt'
63+
),
64+
(
65+
'79c8a2d5-0772-42d4-b41b-531d0b5da926',
66+
'encode deep thought',
67+
'encode',
68+
'Truth is fiction.',
69+
'gifgs rhurx grlm'
70+
),
71+
(
72+
'9ca13d23-d32a-4967-a1fd-6100b8742bab',
73+
'encode all the letters',
74+
'encode',
75+
'The quick brown fox jumps over the lazy dog.',
76+
'gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt'
77+
),
78+
(
79+
'bb50e087-7fdf-48e7-9223-284fe7e69851',
80+
'decode exercism',
81+
'decode',
82+
'vcvix rhn',
83+
'exercism'
84+
),
85+
(
86+
'ac021097-cd5d-4717-8907-b0814b9e292c',
87+
'decode a sentence',
88+
'decode',
89+
'zmlyh gzxov rhlug vmzhg vkkrm thglm v',
90+
'anobstacleisoftenasteppingstone'
91+
),
92+
(
93+
'18729de3-de74-49b8-b68c-025eaf77f851',
94+
'decode numbers',
95+
'decode',
96+
'gvhgr mt123 gvhgr mt',
97+
'testing123testing'
98+
),
99+
(
100+
'0f30325f-f53b-415d-ad3e-a7a4f63de034',
101+
'decode all the letters',
102+
'decode',
103+
'gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt',
104+
'thequickbrownfoxjumpsoverthelazydog'
105+
),
106+
(
107+
'39640287-30c6-4c8c-9bac-9d613d1a5674',
108+
'decode with too many spaces',
109+
'decode',
110+
'vc vix r hn',
111+
'exercism'
112+
),
113+
(
114+
'b34edf13-34c0-49b5-aa21-0768928000d5',
115+
'decode with no spaces',
116+
'decode',
117+
'zmlyhgzxovrhlugvmzhgvkkrmthglmv',
118+
'anobstacleisoftenasteppingstone'
119+
);

0 commit comments

Comments
 (0)