This repository has been archived by the owner on Jan 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgentest.js
167 lines (156 loc) · 4.46 KB
/
gentest.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const https = require("https");
const parse = require("csv-parse");
const spreadsheet_url =
"https://docs.google.com/spreadsheets/d/e/2PACX-1vSHDGzcEMT3FkEvMxCl8PyqqadIad4_CI0QyXZvywU_Hz499cvt-zE29oMV7FvPfSVeyK2jAeMD7OUg/pub?gid=624239970&single=true&output=csv";
const generate = require("./test/morris.test.ts.template");
const fs = require("fs");
/**
* Fetches the spreadsheet from Google, CSV format
* @returns {Promise<any>}
*/
const getSpreadsheetData = () =>
new Promise((resolve, reject) => {
https
.get(spreadsheet_url, response => {
let d = "";
response.on("data", chunk => (d += chunk));
response.on("end", () => {
resolve(d);
});
})
.on("error", reject);
});
/**
* Parses CSV to nested arrays
* @param data
* @returns {Promise<any>}
*/
const parseSpreadsheetData = data =>
new Promise((resolve, reject) => {
parse(data, {}, (err, out) => {
if (err) reject(err);
resolve(out);
});
});
/**
* Converts CSV parser output to objects
*/
const constructSpreadsheetLines = data =>
new Promise((resolve, reject) => {
const headers = data[0];
const lineConstructor = values => {
return headers.reduce((obj, header) => {
obj[header] = values[Object.keys(obj).length];
return obj;
}, {});
};
const lines = data.slice(1);
resolve(lines.map(lineConstructor));
});
/**
* Matches on test types
* @type {RegExp}
*/
const typeRegex = /(test|resultat)_/;
/**
* Extracts the available types of tests
* @param line
* @returns {Array}
*/
const extractTestTypes = line => {
return Object.keys(line)
.filter(k => typeRegex.test(k))
.map(key => key.replace(typeRegex, ""))
.reduce((b, a) => (b.indexOf(a) === -1 ? b.concat([a]) : b), []);
};
/**
* Extracts the tests from a table line
* @param line
* @param testTypes
* @returns {Array}
*/
const extractTestsFromLine = (line, testTypes) => {
// prendre de la ligne uniquement les clefs test_* et resultat_*
const cleanedLine = Object.entries(line).reduce((out, kv) => {
if (!typeRegex.test(kv[0])) return out;
out[kv[0]] = kv[1];
return out;
}, {});
// les grouper par type dans un objet { type: {test,resultat}, type2: {test, resultat}}
const byType = testTypes.reduce((out, tt) => {
out[tt] = Object.entries(cleanedLine).reduce((b, a) => {
if (a[0].indexOf(tt) === -1) return b;
b[a[0].replace(`_${tt}`, "")] = a[1];
return b;
}, {});
return out;
}, {});
// réduire cela en [{type, test, resultat}, {type, test, resultat}, {type, test, resultat}]
return Object.entries(byType).reduce((out, entry) => {
if (entry[1].test === "") return out;
return out.concat([{ type: entry[0], ...entry[1] }]);
}, []);
};
/**
* Transforms the lines from the spreadsheet to test rules
* Three assumptions are made : some lines have an "id" header
* Tests start with test_ & expected results start with resultat_
* Lines have a description_en & description_fr field
* This allows us to add context-specific tests to the table
* Without modifying this script
* @param lines
* @returns {Promise<any>}
*/
const spreadsheetLinesToRules = lines =>
new Promise((resolve, reject) => {
const types = extractTestTypes(lines[0]);
resolve(
lines.reduce((tests, line) => {
if (line.id && line.id !== "") {
tests.push({
id: line.id,
description_fr: line.description_fr,
description_en: line.description_en,
tests: extractTestsFromLine(line, types)
});
} else if (tests.length > 0) {
tests[tests.length - 1].tests.push(
...extractTestsFromLine(line, types)
);
}
return tests;
}, [])
);
});
/**
* Renders the tests to Jest format
* @param tests
* @returns {Promise<any>}
*/
const renderToTs = tests =>
new Promise((resolve, reject) => {
resolve(generate(tests));
});
/**
* Writes the test file
* @param testString
* @returns {Promise<any>}
*/
const writeTestFile = testString =>
new Promise((resolve, reject) => {
fs.writeFile("./test/morris.test.ts", testString, "utf8", err => {
if (err) reject(err);
resolve();
});
});
const log = thing =>
new Promise((resolve, reject) => {
console.log(thing);
resolve(thing);
});
getSpreadsheetData()
.then(parseSpreadsheetData)
.then(constructSpreadsheetLines)
.then(spreadsheetLinesToRules)
.then(renderToTs)
.then(writeTestFile);