-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathintro-select_test.sql
More file actions
85 lines (80 loc) · 2.17 KB
/
intro-select_test.sql
File metadata and controls
85 lines (80 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
-- 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
.echo on
.read ./intro-select.sql
.echo off
.output
.shell sed -i 1d user_output.md
-- Re-run stub file to collect the results as json arrays
.mode json
.output outputs.txt
.read ./intro-select.sql
.output
-- Creating the results table from the outputs.txt file
DROP TABLE IF EXISTS outputs;
CREATE TEMPORARY TABLE outputs (line TEXT NOT NULL);
.mode tabs
.import ./outputs.txt outputs
DROP TABLE IF EXISTS results;
CREATE TABLE results (result TEXT NOT NULL);
WITH inputs (input) AS (
SELECT JSON(PRINTF('[%s]', GROUP_CONCAT(RTRIM(TRIM(line), ','), ',')))
FROM outputs
)
INSERT INTO results (result)
SELECT j.value
FROM inputs, JSON_EACH(input) j
;
-- 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 result FROM results) AS actual
WHERE NOT EXISTS (
SELECT key, value, type, path
FROM JSON_TREE(result)
WHERE type NOT IN ('array', 'object')
EXCEPT
SELECT key, value, type, path
FROM JSON_TREE(expected)
WHERE type NOT IN ('array', 'object')
)
AND NOT EXISTS (
SELECT key, value, type, path
FROM JSON_TREE(expected)
WHERE type NOT IN ('array', 'object')
EXCEPT
SELECT key, value, type, path
FROM JSON_TREE(result)
WHERE type NOT IN ('array', 'object')
)
;
-- Update message for failed tests to give helpful information:
UPDATE tests
-- SET message = 'Result for "' || tests.description || '"' || ' is <' || COALESCE(actual.result, 'NULL') || '> but should be <' || tests.expected || '>'
SET message = 'Result for <"' || tests.description || '">' || ' NOT FOUND' -- need improvements
WHERE 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;
-- Display test results in readable form for the student:
.mode table
SELECT
description,
status,
message
FROM
tests;