Skip to content

Commit 1bbff43

Browse files
authored
Auto-format .sql files using github.com/sql-formatter-org/sql-formatter - luhn (#160)
[no important files changed]
1 parent ca9f449 commit 1bbff43

File tree

5 files changed

+236
-129
lines changed

5 files changed

+236
-129
lines changed
Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,59 @@
11
ALTER TABLE luhn
22
ADD luhn_sum INT DEFAULT 0;
3+
34
ALTER TABLE luhn
45
ADD result_string TEXT;
56

6-
CREATE VIEW cleaned_up (value, invalid_chars, no_spaces)
7-
AS
8-
SELECT
9-
value,
10-
value GLOB '*[^0-9 ]*',
11-
REPLACE(value, ' ', '')
12-
FROM luhn;
7+
CREATE VIEW cleaned_up (value, invalid_chars, no_spaces) AS
8+
SELECT
9+
value,
10+
value GLOB '*[^0-9 ]*',
11+
REPLACE(value, ' ', '')
12+
FROM
13+
luhn;
1314

1415
PRAGMA recursive_triggers = ON;
1516

16-
CREATE TRIGGER IF NOT EXISTS update_luhn
17-
AFTER UPDATE
18-
ON luhn
19-
WHEN LENGTH(NEW.result_string) > 0
20-
BEGIN
21-
UPDATE luhn
22-
SET luhn_sum = luhn_sum + SUBSTR(NEW.result_string, -1, 1) + IIF(LENGTH(NEW.result_string) > 1,
23-
IIF(SUBSTR(NEW.result_string, -2, 1) > '4',
24-
2 * SUBSTR(NEW.result_string, -2, 1) - 9,
25-
2 * SUBSTR(NEW.result_string, -2, 1)),
26-
0),
27-
result_string = SUBSTR(NEW.result_string, 1, LENGTH(NEW.result_string) - 2)
28-
WHERE result_string = NEW.result_string;
29-
END;
17+
CREATE TRIGGER IF NOT EXISTS update_luhn AFTER
18+
UPDATE ON luhn WHEN LENGTH(NEW.result_string) > 0 BEGIN
19+
UPDATE luhn
20+
SET
21+
luhn_sum = luhn_sum + SUBSTR(NEW.result_string, -1, 1) + IIF(
22+
LENGTH(NEW.result_string) > 1,
23+
IIF(
24+
SUBSTR(NEW.result_string, -2, 1) > '4',
25+
2 * SUBSTR(NEW.result_string, -2, 1) - 9,
26+
2 * SUBSTR(NEW.result_string, -2, 1)
27+
),
28+
0
29+
),
30+
result_string = SUBSTR(
31+
NEW.result_string,
32+
1,
33+
LENGTH(NEW.result_string) - 2
34+
)
35+
WHERE
36+
result_string = NEW.result_string;
3037

38+
END;
3139

3240
UPDATE luhn
33-
SET result = False
34-
WHERE value GLOB '*[^0-9 ]*' OR LENGTH(REPLACE(value, ' ', '')) <= 1;
41+
SET
42+
result = False
43+
WHERE
44+
value GLOB '*[^0-9 ]*'
45+
OR LENGTH(REPLACE(value, ' ', '')) <= 1;
3546

3647
UPDATE luhn
37-
SET result_string = REPLACE(value, ' ', '')
38-
WHERE NOT value GLOB '*[^0-9 ]*' AND LENGTH(REPLACE(value, ' ', '')) > 1;
48+
SET
49+
result_string = REPLACE(value, ' ', '')
50+
WHERE
51+
NOT value GLOB '*[^0-9 ]*'
52+
AND LENGTH(REPLACE(value, ' ', '')) > 1;
3953

4054
UPDATE luhn
41-
SET result = (luhn_sum % 10 = 0)
42-
WHERE NOT value GLOB '*[^0-9 ]*' AND LENGTH(REPLACE(value, ' ', '')) > 1;
55+
SET
56+
result = (luhn_sum % 10 = 0)
57+
WHERE
58+
NOT value GLOB '*[^0-9 ]*'
59+
AND LENGTH(REPLACE(value, ' ', '')) > 1;
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
DROP TABLE IF EXISTS luhn;
2-
CREATE TABLE "luhn" (
3-
value TEXT NOT NULL,
4-
result Boolean NOT NULL
5-
);
62

7-
-- Note: the CSV file may contain literal tab, newline, carriage returns.
3+
CREATE TABLE "luhn" (value TEXT NOT NULL, result Boolean NOT NULL);
84

5+
-- Note: the CSV file may contain literal tab, newline, carriage returns.
96
.mode csv
107
.import ./data.csv luhn

exercises/practice/luhn/luhn_test.sql

Lines changed: 147 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,157 @@
11
-- Setup test table and read in student solution:
22
.read ./test_setup.sql
3-
43
-- Test cases:
54
-- Note: the strings below _may_ contain literal tab, newline, or carriage returns.
6-
INSERT INTO tests (name, uuid,
7-
value, expected)
8-
VALUES
9-
('single digit strings can not be valid', '792a7082-feb7-48c7-b88b-bbfec160865e',
10-
'1', false),
11-
12-
('a single zero is invalid', '698a7924-64d4-4d89-8daa-32e1aadc271e',
13-
'0', false),
14-
15-
('a simple valid SIN that remains valid if reversed', '73c2f62b-9b10-4c9f-9a04-83cee7367965',
16-
'059', true),
17-
18-
('a simple valid SIN that becomes invalid if reversed', '9369092e-b095-439f-948d-498bd076be11',
19-
'59', true),
20-
21-
('a valid Canadian SIN', '8f9f2350-1faf-4008-ba84-85cbb93ffeca',
22-
'055 444 285', true),
23-
24-
('invalid Canadian SIN', '1cdcf269-6560-44fc-91f6-5819a7548737',
25-
'055 444 286', false),
26-
27-
('invalid credit card', '656c48c1-34e8-4e60-9a5a-aad8a367810a',
28-
'8273 1232 7352 0569', false),
29-
30-
('invalid long number with an even remainder', '20e67fad-2121-43ed-99a8-14b5b856adb9',
31-
'1 2345 6789 1234 5678 9012', false),
32-
33-
('invalid long number with a remainder divisible by 5', '7e7c9fc1-d994-457c-811e-d390d52fba5e',
34-
'1 2345 6789 1234 5678 9013', false),
35-
36-
('valid number with an even number of digits', 'ad2a0c5f-84ed-4e5b-95da-6011d6f4f0aa',
37-
'095 245 88', true),
38-
39-
('valid number with an odd number of spaces', 'ef081c06-a41f-4761-8492-385e13c8202d',
40-
'234 567 891 234', true),
41-
42-
('valid strings with a non-digit added at the end become invalid', 'bef66f64-6100-4cbb-8f94-4c9713c5e5b2',
43-
'059a', false),
44-
45-
('valid strings with punctuation included become invalid', '2177e225-9ce7-40f6-b55d-fa420e62938e',
46-
'055-444-285', false),
47-
48-
('valid strings with symbols included become invalid', 'ebf04f27-9698-45e1-9afe-7e0851d0fe8d',
49-
'055# 444$ 285', false),
50-
51-
('single zero with space is invalid', '08195c5e-ce7f-422c-a5eb-3e45fece68ba',
52-
' 0', false),
53-
54-
('more than a single zero is valid', '12e63a3c-f866-4a79-8c14-b359fc386091',
55-
'0000 0', true),
56-
57-
('input digit 9 is correctly converted to output digit 9', 'ab56fa80-5de8-4735-8a4a-14dae588663e',
58-
'091', true),
59-
60-
('very long input is valid', 'b9887ee8-8337-46c5-bc45-3bcab51bc36f',
61-
'9999999999 9999999999 9999999999 9999999999', true),
62-
63-
('valid luhn with an odd number of digits and non zero first digit', '8a7c0e24-85ea-4154-9cf1-c2db90eabc08',
64-
'109', true),
65-
66-
('using ascii value for non-doubled non-digit isn''t allowed', '39a06a5a-5bad-4e0f-b215-b042d46209b1',
67-
'055b 444 285', false),
68-
69-
('using ascii value for doubled non-digit isn''t allowed', 'f94cf191-a62f-4868-bc72-7253114aa157',
70-
':9', false),
71-
72-
('non-numeric, non-space char in the middle with a sum that''s divisible by 10 isn''t allowed', '8b72ad26-c8be-49a2-b99c-bcc3bf631b33',
73-
'59%59', false);
5+
INSERT INTO
6+
tests (name, uuid, value, expected)
7+
VALUES
8+
(
9+
'single digit strings can not be valid',
10+
'792a7082-feb7-48c7-b88b-bbfec160865e',
11+
'1',
12+
false
13+
),
14+
(
15+
'a single zero is invalid',
16+
'698a7924-64d4-4d89-8daa-32e1aadc271e',
17+
'0',
18+
false
19+
),
20+
(
21+
'a simple valid SIN that remains valid if reversed',
22+
'73c2f62b-9b10-4c9f-9a04-83cee7367965',
23+
'059',
24+
true
25+
),
26+
(
27+
'a simple valid SIN that becomes invalid if reversed',
28+
'9369092e-b095-439f-948d-498bd076be11',
29+
'59',
30+
true
31+
),
32+
(
33+
'a valid Canadian SIN',
34+
'8f9f2350-1faf-4008-ba84-85cbb93ffeca',
35+
'055 444 285',
36+
true
37+
),
38+
(
39+
'invalid Canadian SIN',
40+
'1cdcf269-6560-44fc-91f6-5819a7548737',
41+
'055 444 286',
42+
false
43+
),
44+
(
45+
'invalid credit card',
46+
'656c48c1-34e8-4e60-9a5a-aad8a367810a',
47+
'8273 1232 7352 0569',
48+
false
49+
),
50+
(
51+
'invalid long number with an even remainder',
52+
'20e67fad-2121-43ed-99a8-14b5b856adb9',
53+
'1 2345 6789 1234 5678 9012',
54+
false
55+
),
56+
(
57+
'invalid long number with a remainder divisible by 5',
58+
'7e7c9fc1-d994-457c-811e-d390d52fba5e',
59+
'1 2345 6789 1234 5678 9013',
60+
false
61+
),
62+
(
63+
'valid number with an even number of digits',
64+
'ad2a0c5f-84ed-4e5b-95da-6011d6f4f0aa',
65+
'095 245 88',
66+
true
67+
),
68+
(
69+
'valid number with an odd number of spaces',
70+
'ef081c06-a41f-4761-8492-385e13c8202d',
71+
'234 567 891 234',
72+
true
73+
),
74+
(
75+
'valid strings with a non-digit added at the end become invalid',
76+
'bef66f64-6100-4cbb-8f94-4c9713c5e5b2',
77+
'059a',
78+
false
79+
),
80+
(
81+
'valid strings with punctuation included become invalid',
82+
'2177e225-9ce7-40f6-b55d-fa420e62938e',
83+
'055-444-285',
84+
false
85+
),
86+
(
87+
'valid strings with symbols included become invalid',
88+
'ebf04f27-9698-45e1-9afe-7e0851d0fe8d',
89+
'055# 444$ 285',
90+
false
91+
),
92+
(
93+
'single zero with space is invalid',
94+
'08195c5e-ce7f-422c-a5eb-3e45fece68ba',
95+
' 0',
96+
false
97+
),
98+
(
99+
'more than a single zero is valid',
100+
'12e63a3c-f866-4a79-8c14-b359fc386091',
101+
'0000 0',
102+
true
103+
),
104+
(
105+
'input digit 9 is correctly converted to output digit 9',
106+
'ab56fa80-5de8-4735-8a4a-14dae588663e',
107+
'091',
108+
true
109+
),
110+
(
111+
'very long input is valid',
112+
'b9887ee8-8337-46c5-bc45-3bcab51bc36f',
113+
'9999999999 9999999999 9999999999 9999999999',
114+
true
115+
),
116+
(
117+
'valid luhn with an odd number of digits and non zero first digit',
118+
'8a7c0e24-85ea-4154-9cf1-c2db90eabc08',
119+
'109',
120+
true
121+
),
122+
(
123+
'using ascii value for non-doubled non-digit isn''t allowed',
124+
'39a06a5a-5bad-4e0f-b215-b042d46209b1',
125+
'055b 444 285',
126+
false
127+
),
128+
(
129+
'using ascii value for doubled non-digit isn''t allowed',
130+
'f94cf191-a62f-4868-bc72-7253114aa157',
131+
':9',
132+
false
133+
),
134+
(
135+
'non-numeric, non-space char in the middle with a sum that''s divisible by 10 isn''t allowed',
136+
'8b72ad26-c8be-49a2-b99c-bcc3bf631b33',
137+
'59%59',
138+
false
139+
);
74140

75141
-- Comparison of user input and the tests updates the status for each test:
76142
UPDATE tests
77-
SET status = 'pass'
78-
FROM (SELECT value, result FROM luhn) AS actual
79-
WHERE (actual.value, actual.result) = (tests.value, tests.expected);
143+
SET
144+
status = 'pass'
145+
FROM
146+
(
147+
SELECT
148+
value,
149+
result
150+
FROM
151+
luhn
152+
) AS actual
153+
WHERE
154+
(actual.value, actual.result) = (tests.value, tests.expected);
80155

81156
-- Write results and debug info:
82157
.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.value
5-
|| ' is <' || COALESCE(actual.result, 'NULL')
6-
|| '> but should be <' || tests.expected || '>'
7-
)
8-
FROM (SELECT value, result FROM luhn) AS actual
9-
WHERE (actual.value) = (tests.value) AND tests.status = 'fail';
3+
SET
4+
message = (
5+
'Result for ' || tests.value || ' is <' || COALESCE(actual.result, 'NULL') || '> but should be <' || tests.expected || '>'
6+
)
7+
FROM
8+
(
9+
SELECT
10+
value,
11+
result
12+
FROM
13+
luhn
14+
) AS actual
15+
WHERE
16+
(actual.value) = (tests.value)
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;

0 commit comments

Comments
 (0)