forked from Open-EO/openeo-processes
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprocesses.test.js
300 lines (250 loc) · 8.75 KB
/
processes.test.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
const glob = require('glob');
const fs = require('fs');
const path = require('path');
const { normalizeString, checkDescription, checkSpelling, checkJsonSchema, getAjv, prepareSchema, isObject } = require('./testHelpers');
const anyOfRequired = [
"quantiles",
"array_element"
];
var jsv = null;
beforeAll(async () => {
jsv = await getAjv();
});
var loader = (file, proposal = false) => {
try {
var fileContent = fs.readFileSync(file);
// Check JSON structure for faults
var p = JSON.parse(fileContent);
// Prepare for tests
processes.push([file, p, fileContent.toString(), proposal]);
processIds.push(p.id);
} catch(err) {
processes.push([file, {}, "", proposal]);
console.error(err);
expect(err).toBeUndefined();
}
};
var processes = [];
var processIds = [];
const files = glob.sync("../*.json", {realpath: true});
files.forEach(file => loader(file));
const proposals = glob.sync("../proposals/*.json", {realpath: true});
proposals.forEach(file => loader(file, true));
test("Check for duplicate process ids", () => {
const duplicates = processIds.filter((id, index) => processIds.indexOf(id) !== index);
expect(duplicates).toEqual([]);
});
describe.each(processes)("%s", (file, p, fileContent, proposal) => {
test("File / JSON", () => {
const ext = path.extname(file);
// Check that the process file has a lower-case json extension
expect(ext).toEqual(".json");
// Check that the process name is also the file name
expect(path.basename(file, ext)).toEqual(p.id);
// lint: Check whether the file is correctly JSON formatted
expect(normalizeString(JSON.stringify(p, null, 4))).toEqual(normalizeString(fileContent));
});
test("ID", () => {
expect(typeof p.id).toBe('string');
expect(/^\w+$/.test(p.id)).toBeTruthy();
});
test("Summary", () => {
expect(typeof p.summary === 'undefined' || typeof p.summary === 'string').toBeTruthy();
// lint: Summary should be short
expect(p.summary.length).toBeLessThan(60);
// lint: Summary should not end with a dot
expect(/[^\.]$/.test(p.summary)).toBeTruthy();
checkSpelling(p.summary, p);
});
test("Description", () => {
// description
expect(typeof p.description).toBe('string');
// lint: Description should be longer than a summary
expect(p.description.length).toBeGreaterThan(60);
checkDescription(p.description, p);
});
test("Categories", () => {
// categories
expect(Array.isArray(p.categories)).toBeTruthy();
// lint: There should be at least one category assigned
expect(p.categories.length).toBeGreaterThan(0);
if (Array.isArray(p.categories)) {
for(let i in p.categories) {
expect(typeof p.categories[i]).toBe('string');
}
}
});
test("Flags", () => {
checkFlags(p, proposal);
});
test("Parameters", () => {
expect(Array.isArray(p.parameters)).toBeTruthy();
});
var params = o2a(p.parameters);
if (params.length > 0) {
test.each(params)("Parameters > %s", (key, param) => {
checkParam(param, p);
});
}
test("Return Value", () => {
expect(isObject(p.returns)).toBeTruthy();
expect(p.returns).not.toBeNull();
// return value description
expect(typeof p.returns.description).toBe('string');
// lint: Description should not be empty
expect(p.returns.description.length).toBeGreaterThan(0);
checkDescription(p.returns.description, p);
// return value schema
expect(p.returns.schema).not.toBeNull();
expect(typeof p.returns.schema).toBe('object');
// lint: Description should not be empty
checkJsonSchema(jsv, p.returns.schema);
});
test("Exceptions", () => {
expect(typeof p.exceptions === 'undefined' || isObject(p.exceptions)).toBeTruthy();
});
var exceptions = o2a(p.exceptions);
if (exceptions.length > 0) {
test.each(exceptions)("Exceptions > %s", (key, e) => {
expect(/^\w+$/.test(key)).toBeTruthy();
// exception message
expect(typeof e.message).toBe('string');
checkSpelling(e.message, p);
// exception description
expect(typeof e.description === 'undefined' || typeof e.description === 'boolean').toBeTruthy();
checkDescription(e.description, p);
// exception http code
if (typeof e.http !== 'undefined') {
expect(e.http).toBeGreaterThanOrEqual(100);
expect(e.http).toBeLessThan(600);
}
});
}
test("Examples", () => {
expect(typeof p.examples === 'undefined' || Array.isArray(p.examples)).toBeTruthy();
});
if (Array.isArray(p.examples) && p.examples.length > 0) {
test.each(p.examples)("Examples > %#", (example) => {
// Make an object for easier access later
var parametersObj = {};
for(var i in p.parameters) {
parametersObj[p.parameters[i].name] = p.parameters[i];
}
var paramKeys = Object.keys(parametersObj);
expect(isObject(example)).toBeTruthy();
expect(example).not.toBeNull();
// example title
expect(typeof example.title === 'undefined' || typeof example.title === 'string').toBeTruthy();
checkSpelling(example.title, p);
// example description
expect(typeof example.description === 'undefined' || typeof example.description === 'string').toBeTruthy();
checkDescription(example.description, p);
// example process graph
expect(example.process_graph).toBeUndefined();
// example arguments
expect(typeof example.arguments).toBe('object');
expect(example.arguments).not.toBeNull();
// Check argument values
for(let argName in example.arguments) {
// Does parameter with this name exist?
expect(paramKeys).toContain(argName);
checkJsonSchemaValue(parametersObj[argName].schema, example.arguments[argName]);
}
// Check whether all required parameters are set
for(let key in parametersObj) {
if (!parametersObj[key].optional) {
expect(example.arguments[key]).toBeDefined();
}
}
// example returns: Nothing to validate, everything is allowed
});
}
test("Links", () => {
expect(typeof p.links === 'undefined' || Array.isArray(p.links)).toBeTruthy();
});
if (Array.isArray(p.links)) {
test.each(p.links)("Links > %#", (link) => {
expect(isObject(link)).toBeTruthy();
// link href
expect(typeof link.href).toBe('string');
// link rel
expect(typeof link.rel === 'undefined' || typeof link.rel === 'string').toBeTruthy();
// link title
expect(typeof link.title === 'undefined' || typeof link.title === 'string').toBeTruthy();
checkSpelling(link.title, p);
// link type
expect(typeof link.type === 'undefined' || typeof link.type === 'string').toBeTruthy();
});
}
});
function checkFlags(p, proposal = false) {
// deprecated
expect(typeof p.deprecated === 'undefined' || typeof p.deprecated === 'boolean').toBeTruthy();
// lint: don't specify defaults
expect(typeof p.deprecated === 'undefined' || p.deprecated === true).toBeTruthy();
if (proposal) {
// experimental must be true for proposals
expect(p.experimental).toBe(true);
}
else {
// experimental must not be false for stable
// lint: don't specify defaults, so false should not be set explicitly
expect(p.experimental).toBeUndefined();
}
}
function checkParam(param, p, checkCbParams = true) {
// parameter name
expect(typeof param.name).toBe('string');
expect(/^\w+$/.test(param.name)).toBeTruthy();
// parameter description
expect(typeof param.description).toBe('string');
// lint: Description should not be empty
expect(param.description.length).toBeGreaterThan(0);
checkDescription(param.description, p);
// Parameter flags
expect(typeof param.optional === 'undefined' || typeof param.optional === 'boolean').toBeTruthy();
// lint: don't specify defaults
expect(typeof param.optional === 'undefined' || param.optional === true).toBeTruthy();
// lint: make sure there's no old required flag
expect(typeof param.required === 'undefined').toBeTruthy();
// Check flags (recommended / experimental)
checkFlags(param);
// Parameter schema
expect(param.schema).not.toBeNull();
expect(typeof param.schema).toBe('object');
checkJsonSchema(jsv, param.schema);
if (!checkCbParams) {
// Parameters that are not required should define a default value
if(param.optional === true && !anyOfRequired.includes(p.id)) {
expect(param.default).toBeDefined();
}
}
else {
// Checking that callbacks (process-graphs) define their parameters
if (typeof param.schema === 'object' && param.schema.subtype === 'process-graph') {
// lint: A callback without parameters is not very useful
expect(Array.isArray(param.schema.parameters) && param.schema.parameters.length > 0).toBeTruthy();
// Check all callback params
for(var i in param.schema.parameters) {
checkParam(param.schema.parameters[i], p, false);
}
}
}
}
function checkJsonSchemaValue(schema, value) {
jsv.validate(prepareSchema(schema), value);
expect(jsv.errors).toBeNull();
}
function o2a(o) {
if (!o) {
return [];
}
var a = [];
for(var k in o) {
a.push([
o[k].name ? o[k].name : k, // name
o[k] // obj
]);
}
return a;
}