This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
/
render.test.sql.generate.js
60 lines (46 loc) · 1.72 KB
/
render.test.sql.generate.js
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
// usage: node render.test.sql.generate.js > render.test.sql
// re-generate render.test.sql
const fs = require('fs');
const path = require('path');
const FIXTURES_PATH = path.resolve(__dirname, '_files');
const files = fs.readdirSync(FIXTURES_PATH).filter(file => !file.startsWith('_'));
function group_by(f){
return (memo, file) => {
const key = f(file);
if(!memo.hasOwnProperty(key)){
memo[key] = [];
}
memo[key].push(file);
return memo;
};
}
const get_filename = (file) => path.basename(file, path.extname(file));
const tests = files.reduce(group_by(get_filename), {});
const is_supported_test_files = ([test_name, files]) => files.every(file => file.endsWith('.txt') || file.endsWith('.mustache') || file.endsWith('.js') );
const is_supported_test = ({test_name, files, js}) => !js.includes('function');
const read_fixture = (test_name, ext) => fs.readFileSync(path.join(FIXTURES_PATH, `${test_name}.${ext}`), 'utf8');
const escape = (str) => str.length > 0 ? `$_$${str}$_$` : `''`;
const tests_sql = Object.entries(tests)
.filter(is_supported_test_files)
.map(([test_name, files]) => {
return {
test_name,
files,
txt: read_fixture(test_name, 'txt'),
js: JSON.stringify(eval(read_fixture(test_name, 'js'))),
mustache: read_fixture(test_name, 'mustache'),
};
})
.filter(is_supported_test)
.map(({test_name, txt, js, mustache}) => {
return `create or replace function lib_test.test_case_lib_mustache${test_name}() returns void as
$$
declare
begin
perform lib_test.assert_equal(lib_mustache.render(${escape(mustache)}, ${escape(js)}::jsonb), ${escape(txt)});
end;
$$ language plpgsql;
`
})
.join('\n');
console.log(tests_sql);