Skip to content

Commit 7930645

Browse files
authored
Auto-format .sql files using github.com/sql-formatter-org/sql-formatter - roman-numerals (#174)
[no important files changed]
1 parent f60aaba commit 7930645

File tree

5 files changed

+259
-90
lines changed

5 files changed

+259
-90
lines changed

exercises/practice/roman-numerals/.meta/example.sql

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,44 @@ ADD temp_num TEXT DEFAULT '';
33

44
PRAGMA recursive_triggers = ON;
55

6-
CREATE TRIGGER IF NOT EXISTS update_roman_numerals
7-
AFTER UPDATE
8-
ON "roman-numerals"
9-
WHEN LENGTH(NEW.temp_num) > 0
10-
BEGIN
11-
UPDATE "roman-numerals"
12-
SET result =
13-
-- we encode the roman numerals in base 10
14-
-- to add a digit, we translate IVXLC => XLCDM (multiply by ten)
15-
-- and then add the digit at the end
6+
CREATE TRIGGER IF NOT EXISTS update_roman_numerals AFTER
7+
UPDATE ON "roman-numerals" WHEN LENGTH(NEW.temp_num) > 0 BEGIN
8+
UPDATE "roman-numerals"
9+
SET
10+
result =
11+
-- we encode the roman numerals in base 10
12+
-- to add a digit, we translate IVXLC => XLCDM (multiply by ten)
13+
-- and then add the digit at the end
14+
REPLACE(
15+
REPLACE(
16+
REPLACE(
17+
REPLACE(REPLACE(result, 'C', 'M'), 'L', 'D'),
18+
'X',
19+
'C'
20+
),
21+
'V',
22+
'L'
23+
),
24+
'I',
25+
'X'
26+
) || CASE SUBSTR(temp_num, 1, 1)
27+
WHEN '0' THEN ''
28+
WHEN '1' THEN 'I'
29+
WHEN '2' THEN 'II'
30+
WHEN '3' THEN 'III'
31+
WHEN '4' THEN 'IV'
32+
WHEN '5' THEN 'V'
33+
WHEN '6' THEN 'VI'
34+
WHEN '7' THEN 'VII'
35+
WHEN '8' THEN 'VIII'
36+
WHEN '9' THEN 'IX'
37+
END,
38+
temp_num = SUBSTR(temp_num, 2)
39+
WHERE
40+
temp_num = NEW.temp_num;
1641

17-
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(result, 'C', 'M'), 'L', 'D'), 'X', 'C'), 'V', 'L'), 'I', 'X') ||
18-
CASE SUBSTR(temp_num, 1, 1)
19-
WHEN '0' THEN ''
20-
WHEN '1' THEN 'I'
21-
WHEN '2' THEN 'II'
22-
WHEN '3' THEN 'III'
23-
WHEN '4' THEN 'IV'
24-
WHEN '5' THEN 'V'
25-
WHEN '6' THEN 'VI'
26-
WHEN '7' THEN 'VII'
27-
WHEN '8' THEN 'VIII'
28-
WHEN '9' THEN 'IX'
29-
END,
30-
temp_num = SUBSTR(temp_num, 2)
31-
WHERE temp_num = NEW.temp_num;
3242
END;
3343

3444
UPDATE "roman-numerals"
35-
SET temp_num = number || '';
45+
SET
46+
temp_num = number || '';
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
DROP TABLE IF EXISTS "roman-numerals";
2-
CREATE TABLE "roman-numerals" (
3-
"number" INT,
4-
"result" TEXT
5-
);
2+
3+
CREATE TABLE "roman-numerals" ("number" INT, "result" TEXT);
64

75
.mode csv
86
.import ./data.csv roman-numerals

exercises/practice/roman-numerals/roman-numerals_test.sql

Lines changed: 177 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,186 @@
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-
number, expected)
7-
VALUES
8-
('1 is I', '19828a3a-fbf7-4661-8ddd-cbaeee0e2178', 1, 'I'),
9-
('2 is II', 'f088f064-2d35-4476-9a41-f576da3f7b03', 2, 'II'),
10-
('3 is III', 'b374a79c-3bea-43e6-8db8-1286f79c7106', 3, 'III'),
11-
('4 is IV', '05a0a1d4-a140-4db1-82e8-fcc21fdb49bb', 4, 'IV'),
12-
('5 is V', '57c0f9ad-5024-46ab-975d-de18c430b290', 5, 'V'),
13-
('6 is VI', '20a2b47f-e57f-4797-a541-0b3825d7f249', 6, 'VI'),
14-
('9 is IX', 'ff3fb08c-4917-4aab-9f4e-d663491d083d', 9, 'IX'),
15-
('16 is XVI', '6d1d82d5-bf3e-48af-9139-87d7165ed509', 16, 'XVI'),
16-
('27 is XXVII', '2bda64ca-7d28-4c56-b08d-16ce65716cf6', 27, 'XXVII'),
17-
('48 is XLVIII', 'a1f812ef-84da-4e02-b4f0-89c907d0962c', 48, 'XLVIII'),
18-
('49 is XLIX', '607ead62-23d6-4c11-a396-ef821e2e5f75', 49, 'XLIX'),
19-
('59 is LIX', 'd5b283d4-455d-4e68-aacf-add6c4b51915', 59, 'LIX'),
20-
('66 is LXVI', '4465ffd5-34dc-44f3-ada5-56f5007b6dad', 66, 'LXVI'),
21-
('93 is XCIII', '46b46e5b-24da-4180-bfe2-2ef30b39d0d0', 93, 'XCIII'),
22-
('141 is CXLI', '30494be1-9afb-4f84-9d71-db9df18b55e3', 141, 'CXLI'),
23-
('163 is CLXIII', '267f0207-3c55-459a-b81d-67cec7a46ed9', 163, 'CLXIII'),
24-
('166 is CLXVI', '902ad132-0b4d-40e3-8597-ba5ed611dd8d', 166, 'CLXVI'),
25-
('402 is CDII', 'cdb06885-4485-4d71-8bfb-c9d0f496b404', 402, 'CDII'),
26-
('575 is DLXXV', '6b71841d-13b2-46b4-ba97-dec28133ea80', 575, 'DLXXV'),
27-
('666 is DCLXVI', 'dacb84b9-ea1c-4a61-acbb-ce6b36674906', 666, 'DCLXVI'),
28-
('911 is CMXI', '432de891-7fd6-4748-a7f6-156082eeca2f', 911, 'CMXI'),
29-
('1024 is MXXIV', 'e6de6d24-f668-41c0-88d7-889c0254d173', 1024, 'MXXIV'),
30-
('1666 is MDCLXVI', 'efbe1d6a-9f98-4eb5-82bc-72753e3ac328', 1666, 'MDCLXVI'),
31-
('3000 is MMM', 'bb550038-d4eb-4be2-a9ce-f21961ac3bc6', 3000, 'MMM'),
32-
('3001 is MMMI', '3bc4b41c-c2e6-49d9-9142-420691504336', 3001, 'MMMI'),
33-
('3888 is MMMDCCCLXXXVIII', '2f89cad7-73f6-4d1b-857b-0ef531f68b7e', 3888, 'MMMDCCCLXXXVIII'),
34-
('3999 is MMMCMXCIX', '4e18e96b-5fbb-43df-a91b-9cb511fe0856', 3999, 'MMMCMXCIX');
4+
INSERT INTO
5+
tests (name, uuid, number, expected)
6+
VALUES
7+
(
8+
'1 is I',
9+
'19828a3a-fbf7-4661-8ddd-cbaeee0e2178',
10+
1,
11+
'I'
12+
),
13+
(
14+
'2 is II',
15+
'f088f064-2d35-4476-9a41-f576da3f7b03',
16+
2,
17+
'II'
18+
),
19+
(
20+
'3 is III',
21+
'b374a79c-3bea-43e6-8db8-1286f79c7106',
22+
3,
23+
'III'
24+
),
25+
(
26+
'4 is IV',
27+
'05a0a1d4-a140-4db1-82e8-fcc21fdb49bb',
28+
4,
29+
'IV'
30+
),
31+
(
32+
'5 is V',
33+
'57c0f9ad-5024-46ab-975d-de18c430b290',
34+
5,
35+
'V'
36+
),
37+
(
38+
'6 is VI',
39+
'20a2b47f-e57f-4797-a541-0b3825d7f249',
40+
6,
41+
'VI'
42+
),
43+
(
44+
'9 is IX',
45+
'ff3fb08c-4917-4aab-9f4e-d663491d083d',
46+
9,
47+
'IX'
48+
),
49+
(
50+
'16 is XVI',
51+
'6d1d82d5-bf3e-48af-9139-87d7165ed509',
52+
16,
53+
'XVI'
54+
),
55+
(
56+
'27 is XXVII',
57+
'2bda64ca-7d28-4c56-b08d-16ce65716cf6',
58+
27,
59+
'XXVII'
60+
),
61+
(
62+
'48 is XLVIII',
63+
'a1f812ef-84da-4e02-b4f0-89c907d0962c',
64+
48,
65+
'XLVIII'
66+
),
67+
(
68+
'49 is XLIX',
69+
'607ead62-23d6-4c11-a396-ef821e2e5f75',
70+
49,
71+
'XLIX'
72+
),
73+
(
74+
'59 is LIX',
75+
'd5b283d4-455d-4e68-aacf-add6c4b51915',
76+
59,
77+
'LIX'
78+
),
79+
(
80+
'66 is LXVI',
81+
'4465ffd5-34dc-44f3-ada5-56f5007b6dad',
82+
66,
83+
'LXVI'
84+
),
85+
(
86+
'93 is XCIII',
87+
'46b46e5b-24da-4180-bfe2-2ef30b39d0d0',
88+
93,
89+
'XCIII'
90+
),
91+
(
92+
'141 is CXLI',
93+
'30494be1-9afb-4f84-9d71-db9df18b55e3',
94+
141,
95+
'CXLI'
96+
),
97+
(
98+
'163 is CLXIII',
99+
'267f0207-3c55-459a-b81d-67cec7a46ed9',
100+
163,
101+
'CLXIII'
102+
),
103+
(
104+
'166 is CLXVI',
105+
'902ad132-0b4d-40e3-8597-ba5ed611dd8d',
106+
166,
107+
'CLXVI'
108+
),
109+
(
110+
'402 is CDII',
111+
'cdb06885-4485-4d71-8bfb-c9d0f496b404',
112+
402,
113+
'CDII'
114+
),
115+
(
116+
'575 is DLXXV',
117+
'6b71841d-13b2-46b4-ba97-dec28133ea80',
118+
575,
119+
'DLXXV'
120+
),
121+
(
122+
'666 is DCLXVI',
123+
'dacb84b9-ea1c-4a61-acbb-ce6b36674906',
124+
666,
125+
'DCLXVI'
126+
),
127+
(
128+
'911 is CMXI',
129+
'432de891-7fd6-4748-a7f6-156082eeca2f',
130+
911,
131+
'CMXI'
132+
),
133+
(
134+
'1024 is MXXIV',
135+
'e6de6d24-f668-41c0-88d7-889c0254d173',
136+
1024,
137+
'MXXIV'
138+
),
139+
(
140+
'1666 is MDCLXVI',
141+
'efbe1d6a-9f98-4eb5-82bc-72753e3ac328',
142+
1666,
143+
'MDCLXVI'
144+
),
145+
(
146+
'3000 is MMM',
147+
'bb550038-d4eb-4be2-a9ce-f21961ac3bc6',
148+
3000,
149+
'MMM'
150+
),
151+
(
152+
'3001 is MMMI',
153+
'3bc4b41c-c2e6-49d9-9142-420691504336',
154+
3001,
155+
'MMMI'
156+
),
157+
(
158+
'3888 is MMMDCCCLXXXVIII',
159+
'2f89cad7-73f6-4d1b-857b-0ef531f68b7e',
160+
3888,
161+
'MMMDCCCLXXXVIII'
162+
),
163+
(
164+
'3999 is MMMCMXCIX',
165+
'4e18e96b-5fbb-43df-a91b-9cb511fe0856',
166+
3999,
167+
'MMMCMXCIX'
168+
);
35169

36170
-- Comparison of user input and the tests updates the status for each test:
37171
UPDATE tests
38-
SET status = 'pass'
39-
FROM (SELECT number, result FROM "roman-numerals") AS actual
40-
WHERE (actual.number, actual.result) = (tests.number, tests.expected);
172+
SET
173+
status = 'pass'
174+
FROM
175+
(
176+
SELECT
177+
number,
178+
result
179+
FROM
180+
"roman-numerals"
181+
) AS actual
182+
WHERE
183+
(actual.number, actual.result) = (tests.number, tests.expected);
41184

42185
-- Write results and debug info:
43186
.read ./test_reporter.sql
44-
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.number
5-
|| ' is <' || COALESCE(actual.result, 'NULL')
6-
|| '> but should be <' || tests.expected || '>'
7-
)
8-
FROM (SELECT number, result FROM "roman-numerals") AS actual
9-
WHERE actual.number = tests.number AND tests.status = 'fail';
3+
SET
4+
message = (
5+
'Result for ' || tests.number || ' is <' || COALESCE(actual.result, 'NULL') || '> but should be <' || tests.expected || '>'
6+
)
7+
FROM
8+
(
9+
SELECT
10+
number,
11+
result
12+
FROM
13+
"roman-numerals"
14+
) AS actual
15+
WHERE
16+
actual.number = tests.number
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 ./roman-numerals.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-
number 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+
number INT NOT NULL,
23+
expected TEXT NOT NULL
2524
);

0 commit comments

Comments
 (0)