Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 63 additions & 37 deletions exercises/practice/bottle-song/.meta/example.sql
Original file line number Diff line number Diff line change
@@ -1,48 +1,74 @@
DROP TABLE IF EXISTS pairs;

CREATE TEMPORARY TABLE pairs (
verse_number INTEGER NOT NULL,
start_number TEXT NOT NULL,
next_number TEXT NOT NULL
verse_number INTEGER NOT NULL,
start_number TEXT NOT NULL,
next_number TEXT NOT NULL
);
INSERT INTO pairs (verse_number, start_number, next_number)
VALUES (10, 'Ten' , 'Nine' ),
( 9, 'Nine' , 'Eight'),
( 8, 'Eight', 'Seven'),
( 7, 'Seven', 'Six' ),
( 6, 'Six' , 'Five' ),
( 5, 'Five' , 'Four' ),
( 4, 'Four' , 'Three'),
( 3, 'Three', 'Two' ),
( 2, 'Two' , 'One' ),
( 1, 'One' , 'no' );

INSERT INTO
pairs (verse_number, start_number, next_number)
VALUES
(10, 'Ten', 'Nine'),
(9, 'Nine', 'Eight'),
(8, 'Eight', 'Seven'),
(7, 'Seven', 'Six'),
(6, 'Six', 'Five'),
(5, 'Five', 'Four'),
(4, 'Four', 'Three'),
(3, 'Three', 'Two'),
(2, 'Two', 'One'),
(1, 'One', 'no');

DROP TABLE IF EXISTS verses;

CREATE TABLE verses AS
SELECT verse_number,
PRINTF('%s green bottles hanging on the wall,' || CHAR(10), start_number) ||
PRINTF('%s green bottles hanging on the wall,' || CHAR(10), start_number) ||
'And if one green bottle should accidentally fall,' || CHAR(10) ||
PRINTF('There''ll be %s green bottles hanging on the wall.',LOWER(next_number)) AS verse
FROM pairs
;
SELECT
verse_number,
PRINTF(
'%s green bottles hanging on the wall,' || CHAR(10),
start_number
) || PRINTF(
'%s green bottles hanging on the wall,' || CHAR(10),
start_number
) || 'And if one green bottle should accidentally fall,' || CHAR(10) || PRINTF(
'There''ll be %s green bottles hanging on the wall.',
LOWER(next_number)
) AS verse
FROM
pairs;

UPDATE verses
SET verse = REPLACE(verse, 'be one green bottles', 'be one green bottle')
WHERE verse_number = 2;
UPDATE verses
SET verse = REPLACE(verse, 'One green bottles', 'One green bottle')
WHERE verse_number = 1;
SET
verse = REPLACE(
verse,
'be one green bottles',
'be one green bottle'
)
WHERE
verse_number = 2;

UPDATE verses
SET
verse = REPLACE(verse, 'One green bottles', 'One green bottle')
WHERE
verse_number = 1;

UPDATE "bottle-song"
SET result = (
SELECT GROUP_CONCAT(verse, CHAR(10)||CHAR(10))
FROM (
SELECT verse
FROM verses
WHERE verse_number <= start_bottles
AND verse_number > start_bottles - take_down
ORDER BY verse_number DESC
)
)
;
SET
result = (
SELECT
GROUP_CONCAT(verse, CHAR(10) || CHAR(10))
FROM
(
SELECT
verse
FROM
verses
WHERE
verse_number <= start_bottles
AND verse_number > start_bottles - take_down
ORDER BY
verse_number DESC
)
);
75 changes: 56 additions & 19 deletions exercises/practice/bottle-song/bottle-song_test.sql
Original file line number Diff line number Diff line change
@@ -1,40 +1,77 @@
-- Create database:
.read ./create_fixture.sql

-- Read user student solution and save any output as markdown in user_output.md:
.mode markdown
.output user_output.md
.read ./bottle-song.sql
.output

-- Create a clean testing environment:
.read ./create_test_table.sql

-- Comparison of user input and the tests updates the status for each test:
UPDATE tests
SET status = 'pass'
FROM (SELECT start_bottles, take_down, result FROM "bottle-song") AS actual
WHERE (actual.start_bottles, actual.take_down, actual.result) = (tests.start_bottles, tests.take_down, tests.expected);
SET
status = 'pass'
FROM
(
SELECT
start_bottles,
take_down,
result
FROM
"bottle-song"
) AS actual
WHERE
(
actual.start_bottles,
actual.take_down,
actual.result
) = (
tests.start_bottles,
tests.take_down,
tests.expected
);

-- Update message for failed tests to give helpful information:
UPDATE tests
SET message = (
'Result for "'
|| PRINTF('start_bottles=%d, take_down=%d', tests.start_bottles,tests.take_down)
|| '"'
|| ' is <' || COALESCE(actual.result, 'NULL')
|| '> but should be <' || tests.expected || '>'
)
FROM (SELECT start_bottles, take_down, result FROM "bottle-song") AS actual
WHERE (actual.start_bottles, actual.take_down) = (tests.start_bottles, tests.take_down) AND tests.status = 'fail';
SET
message = (
'Result for "' || PRINTF(
'start_bottles=%d, take_down=%d',
tests.start_bottles,
tests.take_down
) || '"' || ' is <' || COALESCE(actual.result, 'NULL') || '> but should be <' || tests.expected || '>'
)
FROM
(
SELECT
start_bottles,
take_down,
result
FROM
"bottle-song"
) AS actual
WHERE
(actual.start_bottles, actual.take_down) = (tests.start_bottles, tests.take_down)
AND tests.status = 'fail';

-- Save results to ./output.json (needed by the online test-runner)
.mode json
.once './output.json'
SELECT description, status, message, output, test_code, task_id
FROM tests;
SELECT
description,
status,
message,
output,
test_code,
task_id
FROM
tests;

-- Display test results in readable form for the student:
.mode table
SELECT description, status, message
FROM tests;
SELECT
description,
status,
message
FROM
tests;
7 changes: 4 additions & 3 deletions exercises/practice/bottle-song/create_fixture.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
DROP TABLE IF EXISTS "bottle-song";

CREATE TABLE "bottle-song" (
start_bottles INTEGER NOT NULL,
take_down INTEGER NOT NULL,
result TEXT
start_bottles INTEGER NOT NULL,
take_down INTEGER NOT NULL,
result TEXT
);

.mode csv
Expand Down
106 changes: 78 additions & 28 deletions exercises/practice/bottle-song/create_test_table.sql
Original file line number Diff line number Diff line change
@@ -1,48 +1,91 @@
DROP TABLE IF EXISTS tests;

CREATE TABLE IF NOT EXISTS tests (
-- uuid and description are taken from the test.toml file
uuid TEXT PRIMARY KEY,
description TEXT NOT NULL,
-- The following section is needed by the online test-runner
status TEXT DEFAULT 'fail',
message TEXT,
output TEXT,
test_code TEXT,
task_id INTEGER DEFAULT NULL,
-- Here are columns for the actual tests
start_bottles INTEGER NOT NULL,
take_down INTEGER NOT NULL,
expected TEXT NOT NULL
-- uuid and description are taken from the test.toml file
uuid TEXT PRIMARY KEY,
description TEXT NOT NULL,
-- The following section is needed by the online test-runner
status TEXT DEFAULT 'fail',
message TEXT,
output TEXT,
test_code TEXT,
task_id INTEGER DEFAULT NULL,
-- Here are columns for the actual tests
start_bottles INTEGER NOT NULL,
take_down INTEGER NOT NULL,
expected TEXT NOT NULL
);

INSERT INTO tests (uuid, description, start_bottles, take_down, expected)
INSERT INTO
tests (
uuid,
description,
start_bottles,
take_down,
expected
)
VALUES
('d4ccf8fc-01dc-48c0-a201-4fbeb30f2d03','first generic verse',10,1,'Ten green bottles hanging on the wall,
(
'd4ccf8fc-01dc-48c0-a201-4fbeb30f2d03',
'first generic verse',
10,
1,
'Ten green bottles hanging on the wall,
Ten green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There''ll be nine green bottles hanging on the wall.'),
('0f0aded3-472a-4c64-b842-18d4f1f5f030','last generic verse',3,1,'Three green bottles hanging on the wall,
There''ll be nine green bottles hanging on the wall.'
),
(
'0f0aded3-472a-4c64-b842-18d4f1f5f030',
'last generic verse',
3,
1,
'Three green bottles hanging on the wall,
Three green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There''ll be two green bottles hanging on the wall.'),
('f61f3c97-131f-459e-b40a-7428f3ed99d9','verse with 2 bottles',2,1,'Two green bottles hanging on the wall,
There''ll be two green bottles hanging on the wall.'
),
(
'f61f3c97-131f-459e-b40a-7428f3ed99d9',
'verse with 2 bottles',
2,
1,
'Two green bottles hanging on the wall,
Two green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There''ll be one green bottle hanging on the wall.'),
('05eadba9-5dbd-401e-a7e8-d17cc9baa8e0','verse with 1 bottle',1,1,'One green bottle hanging on the wall,
There''ll be one green bottle hanging on the wall.'
),
(
'05eadba9-5dbd-401e-a7e8-d17cc9baa8e0',
'verse with 1 bottle',
1,
1,
'One green bottle hanging on the wall,
One green bottle hanging on the wall,
And if one green bottle should accidentally fall,
There''ll be no green bottles hanging on the wall.'),
('a4a28170-83d6-4dc1-bd8b-319b6abb6a80','first two verses',10,2,'Ten green bottles hanging on the wall,
There''ll be no green bottles hanging on the wall.'
),
(
'a4a28170-83d6-4dc1-bd8b-319b6abb6a80',
'first two verses',
10,
2,
'Ten green bottles hanging on the wall,
Ten green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There''ll be nine green bottles hanging on the wall.

Nine green bottles hanging on the wall,
Nine green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There''ll be eight green bottles hanging on the wall.'),
('3185d438-c5ac-4ce6-bcd3-02c9ff1ed8db','last three verses',3,3,'Three green bottles hanging on the wall,
There''ll be eight green bottles hanging on the wall.'
),
(
'3185d438-c5ac-4ce6-bcd3-02c9ff1ed8db',
'last three verses',
3,
3,
'Three green bottles hanging on the wall,
Three green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There''ll be two green bottles hanging on the wall.
Expand All @@ -55,8 +98,14 @@ There''ll be one green bottle hanging on the wall.
One green bottle hanging on the wall,
One green bottle hanging on the wall,
And if one green bottle should accidentally fall,
There''ll be no green bottles hanging on the wall.'),
('28c1584a-0e51-4b65-9ae2-fbc0bf4bbb28','all verses',10,10,'Ten green bottles hanging on the wall,
There''ll be no green bottles hanging on the wall.'
),
(
'28c1584a-0e51-4b65-9ae2-fbc0bf4bbb28',
'all verses',
10,
10,
'Ten green bottles hanging on the wall,
Ten green bottles hanging on the wall,
And if one green bottle should accidentally fall,
There''ll be nine green bottles hanging on the wall.
Expand Down Expand Up @@ -104,4 +153,5 @@ There''ll be one green bottle hanging on the wall.
One green bottle hanging on the wall,
One green bottle hanging on the wall,
And if one green bottle should accidentally fall,
There''ll be no green bottles hanging on the wall.');
There''ll be no green bottles hanging on the wall.'
);