Skip to content

Commit 1ff4ad5

Browse files
authored
Auto-format .sql files using github.com/sql-formatter-org/sql-formatter - eliuds-eggs (#149)
[no important files changed]
1 parent 07b7219 commit 1ff4ad5

File tree

5 files changed

+120
-57
lines changed

5 files changed

+120
-57
lines changed
Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
UPDATE "eliuds-eggs"
2-
SET result = 0;
2+
SET
3+
result = 0;
34

45
UPDATE "eliuds-eggs"
5-
SET result = counter FROM
6-
(WITH RECURSIVE powers_of_two(x) AS (
7-
SELECT 1
8-
UNION ALL
9-
SELECT x*2 FROM powers_of_two WHERE x<=(SELECT MAX(number) FROM "eliuds-eggs")
10-
)
11-
SELECT number, sum(number / (x / 2) % 2) as counter FROM powers_of_two, "eliuds-eggs"
12-
WHERE x <= 2*number
13-
GROUP BY number) AS calculation
14-
WHERE calculation.number = "eliuds-eggs".number;
6+
SET
7+
result = counter
8+
FROM
9+
(
10+
WITH RECURSIVE
11+
powers_of_two (x) AS (
12+
SELECT
13+
1
14+
UNION ALL
15+
SELECT
16+
x * 2
17+
FROM
18+
powers_of_two
19+
WHERE
20+
x <= (
21+
SELECT
22+
MAX(number)
23+
FROM
24+
"eliuds-eggs"
25+
)
26+
)
27+
SELECT
28+
number,
29+
sum(number / (x / 2) % 2) as counter
30+
FROM
31+
powers_of_two,
32+
"eliuds-eggs"
33+
WHERE
34+
x <= 2 * number
35+
GROUP BY
36+
number
37+
) AS calculation
38+
WHERE
39+
calculation.number = "eliuds-eggs".number;
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
DROP TABLE IF EXISTS "eliuds-eggs";
2-
CREATE TABLE "eliuds-eggs" (
3-
"number" INT,
4-
"result" INT
5-
);
62

7-
-- Note: the CSV file may contain literal tab, newline, carriage returns.
3+
CREATE TABLE "eliuds-eggs" ("number" INT, "result" INT);
84

5+
-- Note: the CSV file may contain literal tab, newline, carriage returns.
96
.mode csv
107
.import ./data.csv eliuds-eggs
Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,49 @@
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-
7-
INSERT INTO tests (name, uuid,
8-
number, expected)
9-
VALUES
10-
('0 eggs', '559e789d-07d1-4422-9004-3b699f83bca3',
11-
0, 0),
12-
('1 egg', '97223282-f71e-490c-92f0-b3ec9e275aba',
13-
16, 1),
14-
('4 eggs', '1f8fd18f-26e9-4144-9a0e-57cdfc4f4ff5',
15-
89, 4),
16-
('13 eggs', '0c18be92-a498-4ef2-bcbb-28ac4b06cb81',
17-
2000000000, 13);
5+
INSERT INTO
6+
tests (name, uuid, number, expected)
7+
VALUES
8+
(
9+
'0 eggs',
10+
'559e789d-07d1-4422-9004-3b699f83bca3',
11+
0,
12+
0
13+
),
14+
(
15+
'1 egg',
16+
'97223282-f71e-490c-92f0-b3ec9e275aba',
17+
16,
18+
1
19+
),
20+
(
21+
'4 eggs',
22+
'1f8fd18f-26e9-4144-9a0e-57cdfc4f4ff5',
23+
89,
24+
4
25+
),
26+
(
27+
'13 eggs',
28+
'0c18be92-a498-4ef2-bcbb-28ac4b06cb81',
29+
2000000000,
30+
13
31+
);
1832

1933
-- Comparison of user input and the tests updates the status for each test:
2034
UPDATE tests
21-
SET status = 'pass'
22-
FROM (SELECT number, result FROM "eliuds-eggs") AS actual
23-
WHERE (actual.number, actual.result) = (tests.number, tests.expected);
35+
SET
36+
status = 'pass'
37+
FROM
38+
(
39+
SELECT
40+
number,
41+
result
42+
FROM
43+
"eliuds-eggs"
44+
) AS actual
45+
WHERE
46+
(actual.number, actual.result) = (tests.number, tests.expected);
2447

2548
-- Write results and debug info:
2649
.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.number
5-
|| ' is <' || COALESCE(actual.result, 'NULL')
6-
|| '> but should be <' || tests.expected || '>'
7-
)
8-
FROM (SELECT number, result FROM "eliuds-eggs") AS actual
9-
WHERE actual.number = tests.number AND tests.status = 'fail';
3+
SET
4+
message = (
5+
'Result for ' || tests.number || ' is <' || COALESCE(actual.result, 'NULL') || '> but should be <' || tests.expected || '>'
6+
)
7+
FROM
8+
(
9+
SELECT
10+
number,
11+
result
12+
FROM
13+
"eliuds-eggs"
14+
) AS actual
15+
WHERE
16+
actual.number = tests.number
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;
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
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 ./eliuds-eggs.sql
87
.output
9-
108
-- Create a clean testing environment:
119
DROP TABLE IF EXISTS tests;
10+
1211
CREATE TABLE IF NOT EXISTS tests (
13-
-- uuid and name (description) are taken from the test.toml file
14-
uuid TEXT PRIMARY KEY,
15-
name TEXT NOT NULL,
16-
-- The following section is needed by the online test-runner
17-
status TEXT DEFAULT 'fail',
18-
message TEXT,
19-
output TEXT,
20-
test_code TEXT,
21-
task_id INTEGER DEFAULT NULL,
22-
-- Here are columns for the actual tests
23-
number INT NOT NULL,
24-
expected TEXT NOT NULL
12+
-- uuid and name (description) are taken from the test.toml file
13+
uuid TEXT PRIMARY KEY,
14+
name TEXT NOT NULL,
15+
-- The following section is needed by the online test-runner
16+
status TEXT DEFAULT 'fail',
17+
message TEXT,
18+
output TEXT,
19+
test_code TEXT,
20+
task_id INTEGER DEFAULT NULL,
21+
-- Here are columns for the actual tests
22+
number INT NOT NULL,
23+
expected TEXT NOT NULL
2524
);

0 commit comments

Comments
 (0)