Skip to content

Commit 015ff2c

Browse files
authored
Auto-format .sql files using github.com/sql-formatter-org/sql-formatter - space-age (#178)
[no important files changed]
1 parent d2e62a1 commit 015ff2c

File tree

4 files changed

+152
-69
lines changed

4 files changed

+152
-69
lines changed
Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
DROP TABLE IF EXISTS planets;
2-
CREATE TEMPORARY TABLE planets (
3-
name TEXT UNIQUE NOT NULL,
4-
period REAL NOT NULL
5-
);
6-
INSERT INTO planets (name, period)
7-
VALUES ('Mercury', 0.2408467 ),
8-
('Venus' , 0.61519726),
9-
('Earth' , 1.0 ),
10-
('Mars' , 1.8808158 ),
11-
('Jupiter', 11.862615 ),
12-
('Saturn' , 29.447498 ),
13-
('Uranus' , 84.016846 ),
14-
('Neptune', 164.79132 );
2+
3+
CREATE TEMPORARY TABLE planets (name TEXT UNIQUE NOT NULL, period REAL NOT NULL);
4+
5+
INSERT INTO
6+
planets (name, period)
7+
VALUES
8+
('Mercury', 0.2408467),
9+
('Venus', 0.61519726),
10+
('Earth', 1.0),
11+
('Mars', 1.8808158),
12+
('Jupiter', 11.862615),
13+
('Saturn', 29.447498),
14+
('Uranus', 84.016846),
15+
('Neptune', 164.79132);
1516

1617
UPDATE "space-age"
17-
SET result = ROUND("space-age".seconds / 31557600.0 / planets.period, 2)
18-
FROM planets
19-
WHERE "space-age".planet = planets.name
20-
AND "space-age".result ISNULL
21-
;
18+
SET
19+
result = ROUND(
20+
"space-age".seconds / 31557600.0 / planets.period,
21+
2
22+
)
23+
FROM
24+
planets
25+
WHERE
26+
"space-age".planet = planets.name
27+
AND "space-age".result ISNULL;
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
DROP TABLE IF EXISTS "space-age";
2+
23
CREATE TABLE "space-age" (
3-
planet TEXT NOT NULL,
4-
seconds INTEGER NOT NULL,
5-
result REAL
4+
planet TEXT NOT NULL,
5+
seconds INTEGER NOT NULL,
6+
result REAL
67
);
78

89
.mode csv
910
.import ./data.csv "space-age"
10-
11-
UPDATE "space-age" SET result = NULL;
11+
UPDATE "space-age"
12+
SET
13+
result = NULL;
Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,77 @@
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-
planet TEXT NOT NULL,
14-
seconds INTEGER NOT NULL,
15-
expected REAL
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+
planet TEXT NOT NULL,
15+
seconds INTEGER NOT NULL,
16+
expected REAL
1617
);
1718

18-
INSERT INTO tests (uuid, description, planet, seconds, expected)
19-
VALUES
20-
('84f609af-5a91-4d68-90a3-9e32d8a5cd34','age on Earth','Earth',1000000000,31.69),
21-
('ca20c4e9-6054-458c-9312-79679ffab40b','age on Mercury','Mercury',2134835688,280.88),
22-
('502c6529-fd1b-41d3-8fab-65e03082b024','age on Venus','Venus',189839836,9.78),
23-
('9ceadf5e-a0d5-4388-9d40-2c459227ceb8','age on Mars','Mars',2129871239,35.88),
24-
('42927dc3-fe5e-4f76-a5b5-f737fc19bcde','age on Jupiter','Jupiter',901876382,2.41),
25-
('8469b332-7837-4ada-b27c-00ee043ebcad','age on Saturn','Saturn',2000000000,2.15),
26-
('999354c1-76f8-4bb5-a672-f317b6436743','age on Uranus','Uranus',1210123456,0.46),
27-
('80096d30-a0d4-4449-903e-a381178355d8','age on Neptune','Neptune',1821023456,0.35);
28-
19+
INSERT INTO
20+
tests (uuid, description, planet, seconds, expected)
21+
VALUES
22+
(
23+
'84f609af-5a91-4d68-90a3-9e32d8a5cd34',
24+
'age on Earth',
25+
'Earth',
26+
1000000000,
27+
31.69
28+
),
29+
(
30+
'ca20c4e9-6054-458c-9312-79679ffab40b',
31+
'age on Mercury',
32+
'Mercury',
33+
2134835688,
34+
280.88
35+
),
36+
(
37+
'502c6529-fd1b-41d3-8fab-65e03082b024',
38+
'age on Venus',
39+
'Venus',
40+
189839836,
41+
9.78
42+
),
43+
(
44+
'9ceadf5e-a0d5-4388-9d40-2c459227ceb8',
45+
'age on Mars',
46+
'Mars',
47+
2129871239,
48+
35.88
49+
),
50+
(
51+
'42927dc3-fe5e-4f76-a5b5-f737fc19bcde',
52+
'age on Jupiter',
53+
'Jupiter',
54+
901876382,
55+
2.41
56+
),
57+
(
58+
'8469b332-7837-4ada-b27c-00ee043ebcad',
59+
'age on Saturn',
60+
'Saturn',
61+
2000000000,
62+
2.15
63+
),
64+
(
65+
'999354c1-76f8-4bb5-a672-f317b6436743',
66+
'age on Uranus',
67+
'Uranus',
68+
1210123456,
69+
0.46
70+
),
71+
(
72+
'80096d30-a0d4-4449-903e-a381178355d8',
73+
'age on Neptune',
74+
'Neptune',
75+
1821023456,
76+
0.35
77+
);
Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,69 @@
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 ./space-age.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 planet, seconds, result FROM "space-age") AS actual
17-
WHERE (actual.planet, actual.seconds, JSON(actual.result)) = (tests.planet, tests.seconds, JSON(tests.expected));
12+
SET
13+
status = 'pass'
14+
FROM
15+
(
16+
SELECT
17+
planet,
18+
seconds,
19+
result
20+
FROM
21+
"space-age"
22+
) AS actual
23+
WHERE
24+
(
25+
actual.planet,
26+
actual.seconds,
27+
JSON(actual.result)
28+
) = (tests.planet, tests.seconds, JSON(tests.expected));
1829

1930
-- Update message for failed tests to give helpful information:
2031
UPDATE tests
21-
SET message = (
22-
'Result for "'
23-
|| JSON_OBJECT(
24-
'planet', tests.planet,
25-
'seconds', tests.seconds
26-
)
27-
|| '"'
28-
|| ' is <' || COALESCE(actual.result, 'NULL')
29-
|| '> but should be <' || tests.expected || '>'
30-
)
31-
FROM (SELECT planet, seconds, result FROM "space-age") AS actual
32-
WHERE (actual.planet, actual.seconds) = (tests.planet, tests.seconds) AND tests.status = 'fail';
32+
SET
33+
message = (
34+
'Result for "' || JSON_OBJECT('planet', tests.planet, 'seconds', tests.seconds) || '"' || ' is <' || COALESCE(actual.result, 'NULL') || '> but should be <' || tests.expected || '>'
35+
)
36+
FROM
37+
(
38+
SELECT
39+
planet,
40+
seconds,
41+
result
42+
FROM
43+
"space-age"
44+
) AS actual
45+
WHERE
46+
(actual.planet, actual.seconds) = (tests.planet, tests.seconds)
47+
AND tests.status = 'fail';
3348

3449
-- Save results to ./output.json (needed by the online test-runner)
3550
.mode json
3651
.once './output.json'
37-
SELECT description, status, message, output, test_code, task_id
38-
FROM tests;
52+
SELECT
53+
description,
54+
status,
55+
message,
56+
output,
57+
test_code,
58+
task_id
59+
FROM
60+
tests;
3961

4062
-- Display test results in readable form for the student:
4163
.mode table
42-
SELECT description, status, message
43-
FROM tests;
64+
SELECT
65+
description,
66+
status,
67+
message
68+
FROM
69+
tests;

0 commit comments

Comments
 (0)