-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrunQueryTests.js
More file actions
572 lines (488 loc) · 17.9 KB
/
runQueryTests.js
File metadata and controls
572 lines (488 loc) · 17.9 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
Tests SlicingDice endpoints.
This script tests SlicingDice by running tests suites, each composed by:
- Creating columns
- Inserting data
- Querying
- Comparing results
All tests are stored in JSON files at ./examples named as the query being
tested:
- count_entity.json
- count_event.json
In order to execute the tests, simply replace API_KEY by the demo API key and
run the script with:
$ node run_tests.js
*/
"use strict";
var SlicingDice = require('../src/slicer.js');
var errors = require('../src/errors.js');
var fs = require('fs');
var async = require('async');
var https = require('https');
var sleep = require('sleep');
var HAS_FAILED_TESTS = false;
var perTestInsertion;
class SlicingDiceTester {
constructor(api_key, verbose=false) {
this.client = new SlicingDice({masterKey: api_key});
// Translation table for columns with timestamp
this.columnTranslation = {}
// Sleep Time in seconds
this.sleepTime = 20;
// Directory containing examples to test
this.path = 'examples/';
// Examples file format
this.extension = '.json';
this.numSuccesses = 0;
this.numFails = 0;
this.failedTests = [];
this.verbose = verbose;
this.perTestInsertion;
this.insertSqlData = false;
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
/* Run all tests for a determined query type
*
* @param (string) queryType - the type of the query to test
*/
runTests(queryType, callback) {
let testData = this.loadTestData(queryType);
let numTests = testData.length;
let result;
this.perTestInsertion = testData[0].hasOwnProperty("insert");
if (!this.perTestInsertion && this.insertSqlData) {
let insertData = this.loadTestData(queryType, "_insert");
for (let i = 0; i < insertData.length; i++) {
async.series([
(callback) => {
this.client.insert(insertData[i], false).then(() => {
// Wait a few seconds so the data can be inserted by SlicingDice
callback();
}, (err) => {
callback();
});
}
]);
}
sleep.sleep(this.sleepTime);
}
let tasks = [];
for(let i = 0; i < numTests; i++){
let _queryType = queryType;
tasks.push(((i) => (callback) => {
try {
let test = testData[i];
this._emptyColumnTranslation();
console.log("({0}/{1}) Executing test \"{2}\"".format(i + 1, numTests, test['name']));
if("description" in test){
console.log(" Description: {0}".format(test['description']));
}
console.log(" Query type: {0}".format(queryType));
if (this.perTestInsertion) {
async.series([
(callback) => {
this.createColumns(test, callback);
},
(callback) => {
this.insertData(test, callback);
},
(callback) => {
this._runAdditionalOperations(queryType, test, callback);
}
], callback);
} else {
async.series([
(callback) => {
this.executeQuery(queryType, test, callback);
}
], callback);
}
} catch (err) {
callback();
}
})(i));
}
async.series(tasks, callback);
}
// Method used to run delete and update operations, this operations
// are executed before the query and the result comparison
_runAdditionalOperations(queryType, test, callback) {
if (queryType === "delete" || queryType === "update") {
let queryData = this._translateColumnNames(test['additional_operation']);
if (queryType == "delete") {
console.log(" Deleting");
} else {
console.log(" Updating");
}
if (this.verbose){
console.log(" - {0}".format(queryData));
}
if (queryType == "delete") {
async.series([(callback) => {
this.client.delete(queryData).then((resp) => {
this.compareResultAndMakeQuery('count_entity', test, callback, resp).then((resp) => {
callback();
}, (err) => {
throw "An error occurred";
});
}, (err) => {
throw "An error occurred";
})
}], callback);
} else if (queryType == "update") {
async.series([(callback) => {
this.client.update(queryData).then((resp) => {
this.compareResultAndMakeQuery('count_entity', test, callback, resp).then((resp) => {
callback();
}, (err) => {
throw "An error occurred";
});
}, (err) => {
throw "An error occurred";
})
}], callback);
}
} else {
async.series([(callback) => {
this.executeQuery(queryType, test, callback);
}], callback);
}
}
compareResultAndMakeQuery(queryType, test, callback, result) {
async.series([(callback) => {
let expected = this._translateColumnNames(test['result_additional']);
for(var key in expected) {
let value = expected[key];
if (value === 'ignore') {
continue;
}
if (!this.compareJson(expected[key], result[key])){
this.numFails += 1;
this.failedTests.push(test['name']);
console.log(" Expected: \"{0}\": {1}".format(key, JSON.stringify(expected[key])));
console.log(" Result: \"{0}\": {1}".format(key, JSON.stringify(result[key])));
console.log(" Status: Failed\n");
this.updateResult();
return;
}
this.numSuccesses += 1;
console.log(' Status: Passed\n');
this.updateResult();
}
this.executeQuery(queryType, test, callback);
}], callback);
}
// Erase columnTranslation object
_emptyColumnTranslation(){
this.columnTranslation = {};
}
// Load test data from examples files
loadTestData(queryType, suffix = "") {
let filename = this.path + queryType + suffix + this.extension;
return JSON.parse(fs.readFileSync(filename));
}
/* Create columns on Slicing Dice API
*
* @param (array) test - the test data containing the column to create
*/
createColumns(test, callback){
let isSingular = test['columns'].length == 1;
let column_or_columns;
if (isSingular){
column_or_columns = 'column';
} else{
column_or_columns = 'columns';
}
console.log(" Creating {0} {1}".format(test['columns'].length, column_or_columns));
let tasks = [];
for(var data in test['columns']) {
let column = test['columns'][data];
this._appendTimestampToColumnName(column);
tasks.push((callback) => {
this.client.createColumn(column).then((resp) => {
callback();
}, (err) => {
this.compareResult(test, err);
callback();
});
});
if (this.verbose){
console.log(" - {0}".format(column['api-name']));
}
}
async.series(tasks, callback);
}
/* Append integer timestamp to column name
*
* This technique allows the same test suite to be executed over and over
* again, since each execution will use different column names.
*
* @param (array) column - array containing column name
*/
_appendTimestampToColumnName(column){
let oldName = '"{0}"'.format(column['api-name']);
let timestamp = this._getTimestamp();
column['name'] += timestamp
column['api-name'] += timestamp
let newName = '"{0}"'.format(column['api-name'])
this.columnTranslation[oldName] = newName
}
// Get actual timestamp in string format
_getTimestamp(){
return new Date().getTime().toString();
}
/* Insert data on Slicing Dice API
*
* @param (array) test - the test data containing the data to insert on Slicing Dice API
*/
insertData(test, callback) {
let isSingular = test['insert'].length == 1;
let column_or_columns;
if (isSingular){
column_or_columns = 'entity';
} else{
column_or_columns = 'entities';
}
console.log(" Inserting {0} {1}".format(Object.keys(test['insert']).length, column_or_columns));
let insertData = this._translateColumnNames(test['insert']);
if (this.verbose){
console.log(insertData);
}
this.client.insert(insertData, false).then(() => {
// Wait a few seconds so the data can be inserted by SlicingDice
sleep.sleep(this.sleepTime);
callback();
}, (err) => {
this.compareResult(test, err);
callback();
});
}
/* Execute query on Slicing Dice API
*
* @param (string) queryType - the type of the query to send
* @param (string) test - the test data containing the data to query on Slicing Dice API
*/
executeQuery(queryType, test, callback) {
let result;
let queryData;
if (this.perTestInsertion) {
queryData = this._translateColumnNames(test['query']);
} else {
queryData = test['query'];
}
console.log(' Querying');
if (this.verbose){
console.log(' - ' + JSON.stringify(queryData));
}
var queryTypeMethodMap = {
'count_entity': 'countEntity',
'count_event': 'countEvent',
'top_values': 'topValues',
'aggregation': 'aggregation',
'result': 'result',
'score': 'score',
'sql': 'sql'
};
this.client[queryTypeMethodMap[queryType]](queryData).then((resp) =>{
this.compareResult(test, resp);
callback();
}, (err) =>{
this.compareResult(test, err);
callback(err);
})
}
/* Translate column name to match column name with timestamp
*
* @param (array) jsonData - the json to translate the column name
*/
_translateColumnNames(jsonData){
let dataString = JSON.stringify(jsonData);
for(var oldName in this.columnTranslation){
let newName = this.columnTranslation[oldName];
dataString = dataString.replaceAll(oldName, newName);
}
return JSON.parse(dataString);
}
/* Compare the result received from Slicing Dice API and the expected
*
* @param (array) test - the data expected
* @param (array) result - the data received from Slicing Dice API
*/
compareResult(test, result) {
let expected;
if (this.perTestInsertion) {
expected = this._translateColumnNames(test['expected']);
} else {
expected = test['expected'];
}
let dataExpected = test['expected'];
for(var key in dataExpected) {
let value = dataExpected[key];
if (value === 'ignore') {
continue;
}
if (!this.compareJson(expected[key], result[key])){
this.numFails += 1;
this.failedTests.push(test['name']);
console.log(" Expected: \"{0}\": {1}".format(key, JSON.stringify(expected[key])));
console.log(" Result: \"{0}\": {1}".format(key, JSON.stringify(result[key])));
console.log(" Status: Failed\n");
this.updateResult();
return;
}
this.numSuccesses += 1;
console.log(' Status: Passed\n');
this.updateResult();
}
}
/* Compare two JSON's
*
* @param (object) expected - the data expected
* @param (object) result - the data received from Slicing Dice API
*/
compareJson(expected, result) {
if (typeof expected !== typeof result) return false;
if (expected.constructor !== result.constructor) return false;
if (expected instanceof Array) {
return this.arrayEqual(expected, result);
}
if (typeof expected === "object") {
return this.compareJsonValue(expected, result);
}
if (isNaN(expected)) {
return expected === result;
}
return this.numberIsClose(expected, result);
}
numberIsClose(a, b, rel_tol=1e-09, abs_tol=0.0) {
return Math.abs(a - b) <= Math.max(rel_tol * Math.max(Math.abs(a), Math.abs(b)), abs_tol);
}
/* Compare two JSON's values
*
* @param (object) expected - the data expected
* @param (object) result - the data received from Slicing Dice API
*/
compareJsonValue(expected, result) {
for (let key in expected) {
if (expected.hasOwnProperty(key)) {
if (!result.hasOwnProperty(key)) {
return false;
}
if (!this.compareJson(expected[key], result[key])) {
return false;
}
}
}
return true;
}
/* Compare two JSON's arrays
*
* @param (array) expected - the data expected
* @param (array) result - the data received from Slicing Dice API
*/
arrayEqual(expected, result) {
if (expected.length !== result.length) {
return false;
}
let i = expected.length;
while (i--) {
let j = result.length;
let found = false;
while (!found && j--) {
if (this.compareJson(expected[i], result[j])) {
found = true;
}
}
if (!found) {
return false;
}
}
return true;
}
// Write a file testResult.tmp with the result of the tests
updateResult() {
if (process.platform == "win32") {
return;
}
let finalMessage;
let failedTestsStr = "";
if (this.failedTests.length > 0){
for(let item in this.failedTests) {
failedTestsStr += " - {0}\n".format(this.failedTests[item]);
}
}
if (this.numFails > 0){
HAS_FAILED_TESTS = true;
let isSingular = this.numFails == 1;
let testOrTests = null;
if (isSingular){
testOrTests = "test has";
} else {
testOrTests = "tests have";
}
finalMessage = "FAIL: {0} {1} failed\n".format(this.numFails, testOrTests);
} else {
finalMessage = "SUCCESS: All tests passed\n";
}
let content = "\nResults:\n Successes: {0}\n Fails: {1} \n{2}\n{3}".format(this.numSuccesses, this.numFails, failedTestsStr, finalMessage);
fs.writeFile('testResult.tmp', content, function (err) {
if (err) return console.log(err);
});
}
}
// Show results of the test saved on testResult.tmp file
function showResults(){
console.log(fs.readFileSync('testResult.tmp', 'utf8'));
fs.unlink('testResult.tmp', (err) => {
if (err) throw err;
});
if (HAS_FAILED_TESTS)
process.exit(1);
process.exit(0);
}
process.on('SIGINT', function() {
showResults();
});
function main(){
// SlicingDice queries to be tested. Must match the JSON file name.
let queryTypes = [
'count_entity',
'count_event',
'top_values',
'aggregation',
'result',
'score',
'sql',
'delete',
'update'
];
let apiKey = process.env.SD_API_KEY;
if (apiKey === undefined){
apiKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfX3NhbHQiOiIxNTI0MjQ4NzIwNzg2IiwicGVybWlzc2lvbl9sZXZlbCI6MywicHJvamVjdF9pZCI6MzAwMjksImNsaWVudF9pZCI6MTF9.SaRJX3Li_0AcrfDFaMOPHJNS9oGxYPjkYmpEza00IMk";
}
// Testing class with demo API key
// To get a demo api key visit: http://panel.slicingdice.com/docs/#api-details-api-connection-api-keys-demo-key
let sdTester = new SlicingDiceTester(apiKey, false);
let tests = [];
for(let i = 0; i < queryTypes.length; i++) {
tests.push((callback) => sdTester.runTests(queryTypes[i], callback));
}
async.series(tests, () => {
showResults();
});
}
if (require.main === module) {
main();
}