Skip to content

Commit 27cb79f

Browse files
authored
Auto-format .sql files using github.com/sql-formatter-org/sql-formatter - bottle-song (#145)
[no important files changed]
1 parent 58108ba commit 27cb79f

File tree

4 files changed

+201
-87
lines changed

4 files changed

+201
-87
lines changed
Lines changed: 63 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,74 @@
11
DROP TABLE IF EXISTS pairs;
2+
23
CREATE TEMPORARY TABLE pairs (
3-
verse_number INTEGER NOT NULL,
4-
start_number TEXT NOT NULL,
5-
next_number TEXT NOT NULL
4+
verse_number INTEGER NOT NULL,
5+
start_number TEXT NOT NULL,
6+
next_number TEXT NOT NULL
67
);
7-
INSERT INTO pairs (verse_number, start_number, next_number)
8-
VALUES (10, 'Ten' , 'Nine' ),
9-
( 9, 'Nine' , 'Eight'),
10-
( 8, 'Eight', 'Seven'),
11-
( 7, 'Seven', 'Six' ),
12-
( 6, 'Six' , 'Five' ),
13-
( 5, 'Five' , 'Four' ),
14-
( 4, 'Four' , 'Three'),
15-
( 3, 'Three', 'Two' ),
16-
( 2, 'Two' , 'One' ),
17-
( 1, 'One' , 'no' );
8+
9+
INSERT INTO
10+
pairs (verse_number, start_number, next_number)
11+
VALUES
12+
(10, 'Ten', 'Nine'),
13+
(9, 'Nine', 'Eight'),
14+
(8, 'Eight', 'Seven'),
15+
(7, 'Seven', 'Six'),
16+
(6, 'Six', 'Five'),
17+
(5, 'Five', 'Four'),
18+
(4, 'Four', 'Three'),
19+
(3, 'Three', 'Two'),
20+
(2, 'Two', 'One'),
21+
(1, 'One', 'no');
1822

1923
DROP TABLE IF EXISTS verses;
24+
2025
CREATE TABLE verses AS
21-
SELECT verse_number,
22-
PRINTF('%s green bottles hanging on the wall,' || CHAR(10), start_number) ||
23-
PRINTF('%s green bottles hanging on the wall,' || CHAR(10), start_number) ||
24-
'And if one green bottle should accidentally fall,' || CHAR(10) ||
25-
PRINTF('There''ll be %s green bottles hanging on the wall.',LOWER(next_number)) AS verse
26-
FROM pairs
27-
;
26+
SELECT
27+
verse_number,
28+
PRINTF(
29+
'%s green bottles hanging on the wall,' || CHAR(10),
30+
start_number
31+
) || PRINTF(
32+
'%s green bottles hanging on the wall,' || CHAR(10),
33+
start_number
34+
) || 'And if one green bottle should accidentally fall,' || CHAR(10) || PRINTF(
35+
'There''ll be %s green bottles hanging on the wall.',
36+
LOWER(next_number)
37+
) AS verse
38+
FROM
39+
pairs;
2840

2941
UPDATE verses
30-
SET verse = REPLACE(verse, 'be one green bottles', 'be one green bottle')
31-
WHERE verse_number = 2;
32-
UPDATE verses
33-
SET verse = REPLACE(verse, 'One green bottles', 'One green bottle')
34-
WHERE verse_number = 1;
42+
SET
43+
verse = REPLACE(
44+
verse,
45+
'be one green bottles',
46+
'be one green bottle'
47+
)
48+
WHERE
49+
verse_number = 2;
3550

51+
UPDATE verses
52+
SET
53+
verse = REPLACE(verse, 'One green bottles', 'One green bottle')
54+
WHERE
55+
verse_number = 1;
3656

3757
UPDATE "bottle-song"
38-
SET result = (
39-
SELECT GROUP_CONCAT(verse, CHAR(10)||CHAR(10))
40-
FROM (
41-
SELECT verse
42-
FROM verses
43-
WHERE verse_number <= start_bottles
44-
AND verse_number > start_bottles - take_down
45-
ORDER BY verse_number DESC
46-
)
47-
)
48-
;
58+
SET
59+
result = (
60+
SELECT
61+
GROUP_CONCAT(verse, CHAR(10) || CHAR(10))
62+
FROM
63+
(
64+
SELECT
65+
verse
66+
FROM
67+
verses
68+
WHERE
69+
verse_number <= start_bottles
70+
AND verse_number > start_bottles - take_down
71+
ORDER BY
72+
verse_number DESC
73+
)
74+
);
Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,77 @@
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 ./bottle-song.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 start_bottles, take_down, result FROM "bottle-song") AS actual
17-
WHERE (actual.start_bottles, actual.take_down, actual.result) = (tests.start_bottles, tests.take_down, tests.expected);
12+
SET
13+
status = 'pass'
14+
FROM
15+
(
16+
SELECT
17+
start_bottles,
18+
take_down,
19+
result
20+
FROM
21+
"bottle-song"
22+
) AS actual
23+
WHERE
24+
(
25+
actual.start_bottles,
26+
actual.take_down,
27+
actual.result
28+
) = (
29+
tests.start_bottles,
30+
tests.take_down,
31+
tests.expected
32+
);
1833

1934
-- Update message for failed tests to give helpful information:
2035
UPDATE tests
21-
SET message = (
22-
'Result for "'
23-
|| PRINTF('start_bottles=%d, take_down=%d', tests.start_bottles,tests.take_down)
24-
|| '"'
25-
|| ' is <' || COALESCE(actual.result, 'NULL')
26-
|| '> but should be <' || tests.expected || '>'
27-
)
28-
FROM (SELECT start_bottles, take_down, result FROM "bottle-song") AS actual
29-
WHERE (actual.start_bottles, actual.take_down) = (tests.start_bottles, tests.take_down) AND tests.status = 'fail';
36+
SET
37+
message = (
38+
'Result for "' || PRINTF(
39+
'start_bottles=%d, take_down=%d',
40+
tests.start_bottles,
41+
tests.take_down
42+
) || '"' || ' is <' || COALESCE(actual.result, 'NULL') || '> but should be <' || tests.expected || '>'
43+
)
44+
FROM
45+
(
46+
SELECT
47+
start_bottles,
48+
take_down,
49+
result
50+
FROM
51+
"bottle-song"
52+
) AS actual
53+
WHERE
54+
(actual.start_bottles, actual.take_down) = (tests.start_bottles, tests.take_down)
55+
AND tests.status = 'fail';
3056

3157
-- Save results to ./output.json (needed by the online test-runner)
3258
.mode json
3359
.once './output.json'
34-
SELECT description, status, message, output, test_code, task_id
35-
FROM tests;
60+
SELECT
61+
description,
62+
status,
63+
message,
64+
output,
65+
test_code,
66+
task_id
67+
FROM
68+
tests;
3669

3770
-- Display test results in readable form for the student:
3871
.mode table
39-
SELECT description, status, message
40-
FROM tests;
72+
SELECT
73+
description,
74+
status,
75+
message
76+
FROM
77+
tests;

exercises/practice/bottle-song/create_fixture.sql

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

89
.mode csv

exercises/practice/bottle-song/create_test_table.sql

Lines changed: 78 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,91 @@
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-
start_bottles INTEGER NOT NULL,
14-
take_down INTEGER 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+
start_bottles INTEGER NOT NULL,
15+
take_down INTEGER NOT NULL,
16+
expected TEXT NOT NULL
1617
);
1718

18-
INSERT INTO tests (uuid, description, start_bottles, take_down, expected)
19+
INSERT INTO
20+
tests (
21+
uuid,
22+
description,
23+
start_bottles,
24+
take_down,
25+
expected
26+
)
1927
VALUES
20-
('d4ccf8fc-01dc-48c0-a201-4fbeb30f2d03','first generic verse',10,1,'Ten green bottles hanging on the wall,
28+
(
29+
'd4ccf8fc-01dc-48c0-a201-4fbeb30f2d03',
30+
'first generic verse',
31+
10,
32+
1,
33+
'Ten green bottles hanging on the wall,
2134
Ten green bottles hanging on the wall,
2235
And if one green bottle should accidentally fall,
23-
There''ll be nine green bottles hanging on the wall.'),
24-
('0f0aded3-472a-4c64-b842-18d4f1f5f030','last generic verse',3,1,'Three green bottles hanging on the wall,
36+
There''ll be nine green bottles hanging on the wall.'
37+
),
38+
(
39+
'0f0aded3-472a-4c64-b842-18d4f1f5f030',
40+
'last generic verse',
41+
3,
42+
1,
43+
'Three green bottles hanging on the wall,
2544
Three green bottles hanging on the wall,
2645
And if one green bottle should accidentally fall,
27-
There''ll be two green bottles hanging on the wall.'),
28-
('f61f3c97-131f-459e-b40a-7428f3ed99d9','verse with 2 bottles',2,1,'Two green bottles hanging on the wall,
46+
There''ll be two green bottles hanging on the wall.'
47+
),
48+
(
49+
'f61f3c97-131f-459e-b40a-7428f3ed99d9',
50+
'verse with 2 bottles',
51+
2,
52+
1,
53+
'Two green bottles hanging on the wall,
2954
Two green bottles hanging on the wall,
3055
And if one green bottle should accidentally fall,
31-
There''ll be one green bottle hanging on the wall.'),
32-
('05eadba9-5dbd-401e-a7e8-d17cc9baa8e0','verse with 1 bottle',1,1,'One green bottle hanging on the wall,
56+
There''ll be one green bottle hanging on the wall.'
57+
),
58+
(
59+
'05eadba9-5dbd-401e-a7e8-d17cc9baa8e0',
60+
'verse with 1 bottle',
61+
1,
62+
1,
63+
'One green bottle hanging on the wall,
3364
One green bottle hanging on the wall,
3465
And if one green bottle should accidentally fall,
35-
There''ll be no green bottles hanging on the wall.'),
36-
('a4a28170-83d6-4dc1-bd8b-319b6abb6a80','first two verses',10,2,'Ten green bottles hanging on the wall,
66+
There''ll be no green bottles hanging on the wall.'
67+
),
68+
(
69+
'a4a28170-83d6-4dc1-bd8b-319b6abb6a80',
70+
'first two verses',
71+
10,
72+
2,
73+
'Ten green bottles hanging on the wall,
3774
Ten green bottles hanging on the wall,
3875
And if one green bottle should accidentally fall,
3976
There''ll be nine green bottles hanging on the wall.
4077
4178
Nine green bottles hanging on the wall,
4279
Nine green bottles hanging on the wall,
4380
And if one green bottle should accidentally fall,
44-
There''ll be eight green bottles hanging on the wall.'),
45-
('3185d438-c5ac-4ce6-bcd3-02c9ff1ed8db','last three verses',3,3,'Three green bottles hanging on the wall,
81+
There''ll be eight green bottles hanging on the wall.'
82+
),
83+
(
84+
'3185d438-c5ac-4ce6-bcd3-02c9ff1ed8db',
85+
'last three verses',
86+
3,
87+
3,
88+
'Three green bottles hanging on the wall,
4689
Three green bottles hanging on the wall,
4790
And if one green bottle should accidentally fall,
4891
There''ll be two green bottles hanging on the wall.
@@ -55,8 +98,14 @@ There''ll be one green bottle hanging on the wall.
5598
One green bottle hanging on the wall,
5699
One green bottle hanging on the wall,
57100
And if one green bottle should accidentally fall,
58-
There''ll be no green bottles hanging on the wall.'),
59-
('28c1584a-0e51-4b65-9ae2-fbc0bf4bbb28','all verses',10,10,'Ten green bottles hanging on the wall,
101+
There''ll be no green bottles hanging on the wall.'
102+
),
103+
(
104+
'28c1584a-0e51-4b65-9ae2-fbc0bf4bbb28',
105+
'all verses',
106+
10,
107+
10,
108+
'Ten green bottles hanging on the wall,
60109
Ten green bottles hanging on the wall,
61110
And if one green bottle should accidentally fall,
62111
There''ll be nine green bottles hanging on the wall.
@@ -104,4 +153,5 @@ There''ll be one green bottle hanging on the wall.
104153
One green bottle hanging on the wall,
105154
One green bottle hanging on the wall,
106155
And if one green bottle should accidentally fall,
107-
There''ll be no green bottles hanging on the wall.');
156+
There''ll be no green bottles hanging on the wall.'
157+
);

0 commit comments

Comments
 (0)