Skip to content

Commit 8ce9509

Browse files
authored
Auto-format .sql files using github.com/sql-formatter-org/sql-formatter - isbn-verifier (#156)
[no important files changed]
1 parent 0aca746 commit 8ce9509

File tree

5 files changed

+194
-83
lines changed

5 files changed

+194
-83
lines changed
Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
UPDATE "isbn-verifier"
2-
SET result = FALSE
3-
WHERE GLOB(
4-
'[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9X]',
5-
REPLACE(isbn, '-', '')
6-
) = 0
7-
;
2+
SET
3+
result = FALSE
4+
WHERE
5+
GLOB(
6+
'[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9X]',
7+
REPLACE(isbn, '-', '')
8+
) = 0;
89

910
UPDATE "isbn-verifier"
10-
SET result = (
11-
SUBSTRING(REPLACE(isbn,'-', ''), -10, 1) * 10 +
12-
SUBSTRING(REPLACE(isbn,'-', ''), -9, 1) * 9 +
13-
SUBSTRING(REPLACE(isbn,'-', ''), -8, 1) * 8 +
14-
SUBSTRING(REPLACE(isbn,'-', ''), -7, 1) * 7 +
15-
SUBSTRING(REPLACE(isbn,'-', ''), -6, 1) * 6 +
16-
SUBSTRING(REPLACE(isbn,'-', ''), -5, 1) * 5 +
17-
SUBSTRING(REPLACE(isbn,'-', ''), -4, 1) * 4 +
18-
SUBSTRING(REPLACE(isbn,'-', ''), -3, 1) * 3 +
19-
SUBSTRING(REPLACE(isbn,'-', ''), -2, 1) * 2 +
20-
IIF(
21-
SUBSTRING(REPLACE(isbn,'-', ''), -1, 1) = 'X',
22-
10,
23-
SUBSTRING(REPLACE(isbn,'-', ''), -1, 1)
24-
)
25-
) % 11 = 0
26-
WHERE result ISNULL
27-
;
11+
SET
12+
result = (
13+
SUBSTRING(REPLACE(isbn, '-', ''), -10, 1) * 10 + SUBSTRING(REPLACE(isbn, '-', ''), -9, 1) * 9 + SUBSTRING(REPLACE(isbn, '-', ''), -8, 1) * 8 + SUBSTRING(REPLACE(isbn, '-', ''), -7, 1) * 7 + SUBSTRING(REPLACE(isbn, '-', ''), -6, 1) * 6 + SUBSTRING(REPLACE(isbn, '-', ''), -5, 1) * 5 + SUBSTRING(REPLACE(isbn, '-', ''), -4, 1) * 4 + SUBSTRING(REPLACE(isbn, '-', ''), -3, 1) * 3 + SUBSTRING(REPLACE(isbn, '-', ''), -2, 1) * 2 + IIF(
14+
SUBSTRING(REPLACE(isbn, '-', ''), -1, 1) = 'X',
15+
10,
16+
SUBSTRING(REPLACE(isbn, '-', ''), -1, 1)
17+
)
18+
) % 11 = 0
19+
WHERE
20+
result ISNULL;
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
DROP TABLE IF EXISTS "isbn-verifier";
2-
CREATE TABLE "isbn-verifier" (
3-
isbn TEXT NOT NULL,
4-
result BOOL
5-
);
2+
3+
CREATE TABLE "isbn-verifier" (isbn TEXT NOT NULL, result BOOL);
64

75
.mode csv
86
.import ./data.csv "isbn-verifier"
9-
10-
UPDATE "isbn-verifier" SET result = NULL;
7+
UPDATE "isbn-verifier"
8+
SET
9+
result = NULL;
Lines changed: 130 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,134 @@
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-
isbn TEXT NOT NULL,
14-
expected BOOL 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+
isbn TEXT NOT NULL,
15+
expected BOOL NOT NULL
1516
);
1617

17-
INSERT INTO tests (uuid, description, isbn, expected)
18-
VALUES
19-
('0caa3eac-d2e3-4c29-8df8-b188bc8c9292','valid isbn','3-598-21508-8',TRUE),
20-
('19f76b53-7c24-45f8-87b8-4604d0ccd248','invalid isbn check digit','3-598-21508-9',FALSE),
21-
('4164bfee-fb0a-4a1c-9f70-64c6a1903dcd','valid isbn with a check digit of 10','3-598-21507-X',TRUE),
22-
('3ed50db1-8982-4423-a993-93174a20825c','check digit is a character other than X','3-598-21507-A',FALSE),
23-
('9416f4a5-fe01-4b61-a07b-eb75892ef562','invalid check digit in isbn is not treated as zero','4-598-21507-B',FALSE),
24-
('c19ba0c4-014f-4dc3-a63f-ff9aefc9b5ec','invalid character in isbn is not treated as zero','3-598-P1581-X',FALSE),
25-
('28025280-2c39-4092-9719-f3234b89c627','X is only valid as a check digit','3-598-2X507-9',FALSE),
26-
('f6294e61-7e79-46b3-977b-f48789a4945b','valid isbn without separating dashes','3598215088',TRUE),
27-
('185ab99b-3a1b-45f3-aeec-b80d80b07f0b','isbn without separating dashes and X as check digit','359821507X',TRUE),
28-
('7725a837-ec8e-4528-a92a-d981dd8cf3e2','isbn without check digit and dashes','359821507',FALSE),
29-
('47e4dfba-9c20-46ed-9958-4d3190630bdf','too long isbn and no dashes','3598215078X',FALSE),
30-
('737f4e91-cbba-4175-95bf-ae630b41fb60','too short isbn','00',FALSE),
31-
('5458a128-a9b6-4ff8-8afb-674e74567cef','isbn without check digit','3-598-21507',FALSE),
32-
('70b6ad83-d0a2-4ca7-a4d5-a9ab731800f7','check digit of X should not be used for 0','3-598-21515-X',FALSE),
33-
('94610459-55ab-4c35-9b93-ff6ea1a8e562','empty isbn','',FALSE),
34-
('7bff28d4-d770-48cc-80d6-b20b3a0fb46c','input is 9 characters','134456729',FALSE),
35-
('ed6e8d1b-382c-4081-8326-8b772c581fec','invalid characters are not ignored after checking length','3132P34035',FALSE),
36-
('daad3e58-ce00-4395-8a8e-e3eded1cdc86','invalid characters are not ignored before checking length','3598P215088',FALSE),
37-
('fb5e48d8-7c03-4bfb-a088-b101df16fdc3','input is too long but contains a valid isbn','98245726788',FALSE);
18+
INSERT INTO
19+
tests (uuid, description, isbn, expected)
20+
VALUES
21+
(
22+
'0caa3eac-d2e3-4c29-8df8-b188bc8c9292',
23+
'valid isbn',
24+
'3-598-21508-8',
25+
TRUE
26+
),
27+
(
28+
'19f76b53-7c24-45f8-87b8-4604d0ccd248',
29+
'invalid isbn check digit',
30+
'3-598-21508-9',
31+
FALSE
32+
),
33+
(
34+
'4164bfee-fb0a-4a1c-9f70-64c6a1903dcd',
35+
'valid isbn with a check digit of 10',
36+
'3-598-21507-X',
37+
TRUE
38+
),
39+
(
40+
'3ed50db1-8982-4423-a993-93174a20825c',
41+
'check digit is a character other than X',
42+
'3-598-21507-A',
43+
FALSE
44+
),
45+
(
46+
'9416f4a5-fe01-4b61-a07b-eb75892ef562',
47+
'invalid check digit in isbn is not treated as zero',
48+
'4-598-21507-B',
49+
FALSE
50+
),
51+
(
52+
'c19ba0c4-014f-4dc3-a63f-ff9aefc9b5ec',
53+
'invalid character in isbn is not treated as zero',
54+
'3-598-P1581-X',
55+
FALSE
56+
),
57+
(
58+
'28025280-2c39-4092-9719-f3234b89c627',
59+
'X is only valid as a check digit',
60+
'3-598-2X507-9',
61+
FALSE
62+
),
63+
(
64+
'f6294e61-7e79-46b3-977b-f48789a4945b',
65+
'valid isbn without separating dashes',
66+
'3598215088',
67+
TRUE
68+
),
69+
(
70+
'185ab99b-3a1b-45f3-aeec-b80d80b07f0b',
71+
'isbn without separating dashes and X as check digit',
72+
'359821507X',
73+
TRUE
74+
),
75+
(
76+
'7725a837-ec8e-4528-a92a-d981dd8cf3e2',
77+
'isbn without check digit and dashes',
78+
'359821507',
79+
FALSE
80+
),
81+
(
82+
'47e4dfba-9c20-46ed-9958-4d3190630bdf',
83+
'too long isbn and no dashes',
84+
'3598215078X',
85+
FALSE
86+
),
87+
(
88+
'737f4e91-cbba-4175-95bf-ae630b41fb60',
89+
'too short isbn',
90+
'00',
91+
FALSE
92+
),
93+
(
94+
'5458a128-a9b6-4ff8-8afb-674e74567cef',
95+
'isbn without check digit',
96+
'3-598-21507',
97+
FALSE
98+
),
99+
(
100+
'70b6ad83-d0a2-4ca7-a4d5-a9ab731800f7',
101+
'check digit of X should not be used for 0',
102+
'3-598-21515-X',
103+
FALSE
104+
),
105+
(
106+
'94610459-55ab-4c35-9b93-ff6ea1a8e562',
107+
'empty isbn',
108+
'',
109+
FALSE
110+
),
111+
(
112+
'7bff28d4-d770-48cc-80d6-b20b3a0fb46c',
113+
'input is 9 characters',
114+
'134456729',
115+
FALSE
116+
),
117+
(
118+
'ed6e8d1b-382c-4081-8326-8b772c581fec',
119+
'invalid characters are not ignored after checking length',
120+
'3132P34035',
121+
FALSE
122+
),
123+
(
124+
'daad3e58-ce00-4395-8a8e-e3eded1cdc86',
125+
'invalid characters are not ignored before checking length',
126+
'3598P215088',
127+
FALSE
128+
),
129+
(
130+
'fb5e48d8-7c03-4bfb-a088-b101df16fdc3',
131+
'input is too long but contains a valid isbn',
132+
'98245726788',
133+
FALSE
134+
);
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
-- Schema: CREATE TABLE "isbn-verifier" (isbn TEXT NOT NULL, result BOOL);
22
-- Task: update the isbn-verifier table and set the result based on the isbn.
3-
Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,63 @@
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 ./isbn-verifier.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 isbn, result FROM "isbn-verifier") AS actual
17-
WHERE (actual.isbn, actual.result) = (tests.isbn, tests.expected);
12+
SET
13+
status = 'pass'
14+
FROM
15+
(
16+
SELECT
17+
isbn,
18+
result
19+
FROM
20+
"isbn-verifier"
21+
) AS actual
22+
WHERE
23+
(actual.isbn, actual.result) = (tests.isbn, tests.expected);
1824

1925
-- Update message for failed tests to give helpful information:
2026
UPDATE tests
21-
SET message = (
22-
'Result for "'
23-
|| tests.isbn
24-
|| '"'
25-
|| ' is <' || COALESCE(actual.result, 'NULL')
26-
|| '> but should be <' || tests.expected || '>'
27-
)
28-
FROM (SELECT isbn, result FROM "isbn-verifier") AS actual
29-
WHERE actual.isbn = tests.isbn AND tests.status = 'fail';
27+
SET
28+
message = (
29+
'Result for "' || tests.isbn || '"' || ' is <' || COALESCE(actual.result, 'NULL') || '> but should be <' || tests.expected || '>'
30+
)
31+
FROM
32+
(
33+
SELECT
34+
isbn,
35+
result
36+
FROM
37+
"isbn-verifier"
38+
) AS actual
39+
WHERE
40+
actual.isbn = tests.isbn
41+
AND tests.status = 'fail';
3042

3143
-- Save results to ./output.json (needed by the online test-runner)
3244
.mode json
3345
.once './output.json'
34-
SELECT description, status, message, output, test_code, task_id
35-
FROM tests;
46+
SELECT
47+
description,
48+
status,
49+
message,
50+
output,
51+
test_code,
52+
task_id
53+
FROM
54+
tests;
3655

3756
-- Display test results in readable form for the student:
3857
.mode table
39-
SELECT description, status, message
40-
FROM tests;
58+
SELECT
59+
description,
60+
status,
61+
message
62+
FROM
63+
tests;

0 commit comments

Comments
 (0)