-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht.js
237 lines (207 loc) · 7.56 KB
/
t.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
const dotenv = require('dotenv');
const mysql = require("mysql");
const config = require('./Server/SecertConfig.js');
const sleep = require('./Util/Util.js').sleep;
const RecordService = require('../Server/RecordService.js');
const ApiKeyService = require('../Server/ApiKeyService.js');
const db = mysql.createConnection(config.db_config());
let api_service = new ApiKeyService({
startAutomaticCheck:false,
});
let records_service = new RecordService();
class Tests {
constructor(){
this.subroutines = new Map();
this.subroutines.set("default",[]);
this.mysqlReady = false;
db.connect((err) => {
if (err) {
throw err;
}
this.mysqlReady = true;
console.log('mysql connected...');
});
}
/**
Adds a testcase object to a mapped subroutine
*/
addTestCases(testCase,subroutine = 'default'){
if(process.env.DEV_STATE !== "1"){
console.log("Unable to add t case: DEV_STATE is not set to the testing state");
return;
}
//if the testcase is missing return
if(!testCase || testCase === {})
return;
//subroutine was found
if(this.subroutines.has(subroutine)){
let testCases = this.subroutines.get(subroutine);
if(typeof testCases === 'object'){
for(let i = 0;i<testCase.length; i++){
testCases.push(testCase[i]);
}
}else{
testCases.push(testCase);
}
this.subroutines.set(subroutine,testCases);
//subroutine was not found
}else{
let testCases;
//if there are more than one t case
if(testCase === 'object')
testCases = testCase;
else
testCases = [...testCase];
this.subroutines.set(subroutine,testCases);
}
}
async runTestCase(subroutine = 'default'){
while(!this.mysqlReady){
console.log("waiting for mysql to mount before running tests");
await sleep(500);
}
if(!this.subroutines.has(subroutine) || this.subroutines.get(subroutine).length === 0){
console.log("Skipping...failed to start tests for missing or empty subroutine "+subroutine);
return;
}
console.log(".".repeat(2)+"starting executing tests from subroutine "+subroutine+" ")
//Gather each testcase and run all tests in the subroutine
let testCases = this.subroutines.get(subroutine);
for(let i = 0; i<testCases.length; i++){
testCases[i].runTest();
}
}
//Gathers all subroutine keys in the map and runs all the testcases in each
runTestsForAllSubroutines(){
let keys = this.subroutines[Symbol.iterator]();
for(let subroutine of keys){
this.runTestCase(subroutine[0]);
}
}
}
class TestCase {
constructor(classPointer,functionPointer,inputs,expectedResult,numParmsForInput) {
this.classPointer = classPointer;
this.functionPointer = functionPointer;
this.inputs = inputs;
this.expectedResult = expectedResult;
this.numParmsForInput = numParmsForInput;
}
runTest(){
//Loop through all the input cases.
for(let i = 0; i<this.inputs.length; i++){
let output;
let result;
//Call the function that is going to be tested with its passed input.
try{
switch(this.numParmsForInput){
case 1:
output = this.classPointer[this.functionPointer](this.inputs[i]);
break;
case 2:
output = this.classPointer[this.functionPointer](this.inputs[i][0],this.inputs[i][1]);
break;
case 3:
output = this.classPointer[this.functionPointer](this.inputs[i][0],this.inputs[i][1],this.inputs[i][2]);
break;
default:
output = this.classPointer[this.functionPointer](this.inputs[i]);
}
}catch(error){
//set error and print then exit this input t
result = {status:false,error:error}
this.printTestResult(result,this.inputs[i]);
continue;
}
/*determines what type is passed and determines how
to calcuate if the output result is correct*/
if(typeof this.expectedResult === 'function'){
result = {status:this.expectedResult(this.inputs[i],output),output:output}
/*if expectedResult is of type object
each mapped value will be compared to the same mapped output
if all keys have the same value then the t is correct.*/
}else if(typeof this.expectedResult[i] === 'object'){
let keys = Object.keys(this.expectedResult[i]);
result = {status:true,output:output};
for(let key in keys){
if(this.expectedResult[i][keys[key]] !== output[keys[key]]){
result.status = false;
}
}
//If expectedResult is of type array
}else{
result = {status:output === this.expectedResult[i],output:output};
}
//print the result of the t case [correct,wrong,error]
this.printTestResult(result,this.inputs[i]);
}
}
/**
*
* @param {[type]} data input or ouput data
* @return {[type]} inline string of the objects keys paired with the value
*/
getFormatInputOutputString(data){
if(!data)
return "empty";
let output = [];
if(typeof data === "object"){
let keys = Object.keys(data);
for(let i = 0; i<keys.length; i++){
let line = [keys[i],data[keys[i]]]
output = [...output,...line];
}
return output;
}else{
return data;
}
}
printTestResult(result,input){
if(result.error){
console.log(".".repeat(10)+"⛔️ " + this.functionPointer + " Caused an error with input ["+input+"]")
console.log(result.error);
return;
}
if(result.status){
console.log(".".repeat(10)+"✅ " + this.functionPointer + " passed with input ["+this.getFormatInputOutputString(input)+ "] and output ["+this.getFormatInputOutputString(result.output)+"]");
}else{
console.log(".".repeat(10)+"❌" + this.functionPointer + " Failed with input ["+this.getFormatInputOutputString(input)+ "] and output ["+this.getFormatInputOutputString(result.output)+"]");
}
}
}
module.exports = TestCase;
// * ------------------ Example of how to perfom a unit t --------------------
/*
class Example {
testFunction1(arr){
if(arr.length > 5){
return true;
}
return false;
}
testFunction2(str){
for(let i = 0;i<str.length; i++){
if(str[i] === '%'){
}
}
return str;
}
testFunction3(input){
return input;
}
}
let example = new Example();
let test1 = new TestCase(example,"testFunction1",[[1,2,3,4,5,6],[1,3,5,6]],[true,false]);
let test2 = new TestCase(example,"testFunction2",["hello%joe","what%%t"],(input,output) => {return true});
let test3 = new TestCase(example,"testFunction3",[{name:"hi",number:5}],[{name:"hi",number:5}]);
*/
let t = new Tests();
let apiKeyServiceTestCases = [];
apiKeyServiceTestCases.push(new TestCase(api_service,'createKeyForUser',[["jfj5666",false,18000],["mmm5666",true,0]],
(input,output)=>{return api_service.apiKeyInsertionTest(input,output)},3));
//apiKeyServiceTestCases.push(new TestCase(api_service,'validHashedKeyForUser',
//[[api_service.openKeys[0].owner,api_service.openKeys[0].hash,api_service.openKeys[0].admin]],[true],3));
apiKeyServiceTestCases.push(new TestCase(api_service,'expireOpenKeyForUser',['jfj5666','mmm5666','notfoundowner'],
(input,output)=>{return api_service.apiKeyExpireTest(input,output)},1));
t.addTestCases(apiKeyServiceTestCases,'api');
t.runTestsForAllSubroutines();