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
70 changes: 40 additions & 30 deletions exercises/practice/secret-handshake/.meta/example.sql
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
UPDATE "secret-handshake"
SET result = (
SELECT COALESCE(GROUP_CONCAT(j.value, ', '), '')
FROM (
SELECT
JSON_ARRAY(
IIF((number >> 3) & 1, 'jump' , NULL),
IIF((number >> 2) & 1, 'close your eyes', NULL),
IIF((number >> 1) & 1, 'double blink' , NULL),
IIF((number >> 0) & 1, 'wink' , NULL)
) AS commands
), JSON_EACH(commands) j
WHERE j.value NOTNULL
)
WHERE (number >> 4) & 1 = 1
;
SET
result = (
SELECT
COALESCE(GROUP_CONCAT(j.value, ', '), '')
FROM
(
SELECT
JSON_ARRAY(
IIF((number >> 3) & 1, 'jump', NULL),
IIF((number >> 2) & 1, 'close your eyes', NULL),
IIF((number >> 1) & 1, 'double blink', NULL),
IIF((number >> 0) & 1, 'wink', NULL)
) AS commands
),
JSON_EACH(commands) j
WHERE
j.value NOTNULL
)
WHERE
(number >> 4) & 1 = 1;

UPDATE "secret-handshake"
SET result = (
SELECT COALESCE(GROUP_CONCAT(j.value, ', '), '')
FROM (
SELECT
JSON_ARRAY(
IIF((number >> 0) & 1, 'wink' , NULL),
IIF((number >> 1) & 1, 'double blink' , NULL),
IIF((number >> 2) & 1, 'close your eyes', NULL),
IIF((number >> 3) & 1, 'jump' , NULL)
) AS commands
), JSON_EACH(commands) j
WHERE j.value NOTNULL
)
WHERE (number >> 4) & 1 = 0
;
SET
result = (
SELECT
COALESCE(GROUP_CONCAT(j.value, ', '), '')
FROM
(
SELECT
JSON_ARRAY(
IIF((number >> 0) & 1, 'wink', NULL),
IIF((number >> 1) & 1, 'double blink', NULL),
IIF((number >> 2) & 1, 'close your eyes', NULL),
IIF((number >> 3) & 1, 'jump', NULL)
) AS commands
),
JSON_EACH(commands) j
WHERE
j.value NOTNULL
)
WHERE
(number >> 4) & 1 = 0;
11 changes: 5 additions & 6 deletions exercises/practice/secret-handshake/create_fixture.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
DROP TABLE IF EXISTS "secret-handshake";
CREATE TABLE "secret-handshake" (
number INTEGER NOT NULL,
result TEXT
);

CREATE TABLE "secret-handshake" (number INTEGER NOT NULL, result TEXT);

.mode csv
.import ./data.csv "secret-handshake"

UPDATE "secret-handshake" SET result = NULL;
UPDATE "secret-handshake"
SET
result = NULL;
107 changes: 82 additions & 25 deletions exercises/practice/secret-handshake/create_test_table.sql
Original file line number Diff line number Diff line change
@@ -1,29 +1,86 @@
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
number 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
number INTEGER NOT NULL,
expected TEXT NOT NULL
);

INSERT INTO tests (uuid, description, number, expected)
VALUES
('b8496fbd-6778-468c-8054-648d03c4bb23', 'wink for 1', 1, 'wink'),
('83ec6c58-81a9-4fd1-bfaf-0160514fc0e3', 'double blink for 10', 2, 'double blink'),
('0e20e466-3519-4134-8082-5639d85fef71', 'close your eyes for 100', 4, 'close your eyes'),
('b339ddbb-88b7-4b7d-9b19-4134030d9ac0', 'jump for 1000', 8, 'jump'),
('40499fb4-e60c-43d7-8b98-0de3ca44e0eb', 'combine two actions', 3, 'wink, double blink'),
('9730cdd5-ef27-494b-afd3-5c91ad6c3d9d', 'reverse two actions', 19, 'double blink, wink'),
('0b828205-51ca-45cd-90d5-f2506013f25f', 'reversing one action gives the same action', 24, 'jump'),
('9949e2ac-6c9c-4330-b685-2089ab28b05f', 'reversing no actions still gives no actions', 16, ''),
('23fdca98-676b-4848-970d-cfed7be39f81', 'all possible actions', 15, 'wink, double blink, close your eyes, jump'),
('ae8fe006-d910-4d6f-be00-54b7c3799e79', 'reverse all possible actions', 31, 'jump, close your eyes, double blink, wink'),
('3d36da37-b31f-4cdb-a396-d93a2ee1c4a5', 'do nothing for zero', 0, '');
INSERT INTO
tests (uuid, description, number, expected)
VALUES
(
'b8496fbd-6778-468c-8054-648d03c4bb23',
'wink for 1',
1,
'wink'
),
(
'83ec6c58-81a9-4fd1-bfaf-0160514fc0e3',
'double blink for 10',
2,
'double blink'
),
(
'0e20e466-3519-4134-8082-5639d85fef71',
'close your eyes for 100',
4,
'close your eyes'
),
(
'b339ddbb-88b7-4b7d-9b19-4134030d9ac0',
'jump for 1000',
8,
'jump'
),
(
'40499fb4-e60c-43d7-8b98-0de3ca44e0eb',
'combine two actions',
3,
'wink, double blink'
),
(
'9730cdd5-ef27-494b-afd3-5c91ad6c3d9d',
'reverse two actions',
19,
'double blink, wink'
),
(
'0b828205-51ca-45cd-90d5-f2506013f25f',
'reversing one action gives the same action',
24,
'jump'
),
(
'9949e2ac-6c9c-4330-b685-2089ab28b05f',
'reversing no actions still gives no actions',
16,
''
),
(
'23fdca98-676b-4848-970d-cfed7be39f81',
'all possible actions',
15,
'wink, double blink, close your eyes, jump'
),
(
'ae8fe006-d910-4d6f-be00-54b7c3799e79',
'reverse all possible actions',
31,
'jump, close your eyes, double blink, wink'
),
(
'3d36da37-b31f-4cdb-a396-d93a2ee1c4a5',
'do nothing for zero',
0,
''
);
61 changes: 42 additions & 19 deletions exercises/practice/secret-handshake/secret-handshake_test.sql
Original file line number Diff line number Diff line change
@@ -1,40 +1,63 @@
-- 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 ./secret-handshake.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 number, result FROM "secret-handshake") AS actual
WHERE (actual.number, actual.result) = (tests.number, tests.expected);
SET
status = 'pass'
FROM
(
SELECT
number,
result
FROM
"secret-handshake"
) AS actual
WHERE
(actual.number, actual.result) = (tests.number, tests.expected);

-- Update message for failed tests to give helpful information:
UPDATE tests
SET message = (
'Result for "'
|| tests.number
|| '"'
|| ' is <' || COALESCE(actual.result, 'NULL')
|| '> but should be <' || tests.expected || '>'
)
FROM (SELECT number, result FROM "secret-handshake") AS actual
WHERE actual.number = tests.number AND tests.status = 'fail';
SET
message = (
'Result for "' || tests.number || '"' || ' is <' || COALESCE(actual.result, 'NULL') || '> but should be <' || tests.expected || '>'
)
FROM
(
SELECT
number,
result
FROM
"secret-handshake"
) AS actual
WHERE
actual.number = tests.number
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;