Commit 180aaa4d806947da6f7e35f5c3057c10e81cf173
1 parent
dde61c7c
Exists in
master
update unit test
Showing
6 changed files
with
149 additions
and
74 deletions
Show diff stats
app.js
... | ... | @@ -6,7 +6,12 @@ const utils = require('utils/utils'); |
6 | 6 | var log = require('./utils/log'); |
7 | 7 | var constants = require('./utils/constants'); |
8 | 8 | |
9 | -utils.testUnit(1,2,3,4,5); | |
9 | +var testObj = {one:1,two:2,three:3}; | |
10 | +var testArray = [5,6]; | |
11 | +var testObj2 = {test:{ | |
12 | + lv2 : 10 | |
13 | +}}; | |
14 | +console.log(utils.testUnit(testObj,4,testArray,testObj2)); | |
10 | 15 | // console.log(conf.get('redis.host')); |
11 | 16 | |
12 | 17 | // setup generate request-id middleware |
... | ... | @@ -187,6 +192,9 @@ app.use( function( req, res, next ) { |
187 | 192 | next(); |
188 | 193 | } ); |
189 | 194 | |
195 | + | |
196 | + | |
197 | + | |
190 | 198 | // handle not found |
191 | 199 | // app.all('*', function(req, res) { |
192 | 200 | ... | ... |
unitTest/test.js
1 | 1 | var readTextFile = require('read-text-file'); |
2 | +const path = require('path'); | |
2 | 3 | |
3 | 4 | |
5 | +var dir = path.parse(__dirname); | |
6 | + | |
4 | 7 | try{ |
5 | 8 | var testCaseStr = readTextFile.readSync('./unitTest/testCase.txt').trim("\r\n").split("\r\n"); |
6 | 9 | var testCase = []; |
... | ... | @@ -12,11 +15,17 @@ try{ |
12 | 15 | describe('unitTest app',function() { |
13 | 16 | |
14 | 17 | for(var i=0;i<testCase.length;i++) |
15 | - { | |
16 | - var func = require(testCase[i].from); | |
18 | + { | |
19 | + var func = require(dir.dir+path.sep+testCase[i].from); | |
17 | 20 | var expect = testCase[i].expect; |
21 | + var comment = testCase[i].comment!=""?" => "+testCase[i].comment:testCase[i].comment; | |
22 | + | |
23 | + | |
24 | + | |
25 | + testCase[i].input = getTrueData(testCase[i].input); | |
26 | + | |
18 | 27 | var cal = func[testCase[i].function](...testCase[i].input); |
19 | - it(testCase[i].function ,function(done) { | |
28 | + it(testCase[i].function+comment ,function(done) { | |
20 | 29 | |
21 | 30 | if (expect == cal) { |
22 | 31 | done(); |
... | ... | @@ -32,4 +41,21 @@ catch(err) { |
32 | 41 | // console.log(err); |
33 | 42 | } |
34 | 43 | |
44 | +function getTrueData(input) | |
45 | +{ | |
35 | 46 | |
47 | + // console.log(input); | |
48 | + for(var j=0;j<input.length;j++) | |
49 | + { | |
50 | + try | |
51 | + { | |
52 | + input[j] = JSON.parse(input[j]); | |
53 | + }catch(err) { | |
54 | + // console.log(err); | |
55 | + } | |
56 | + | |
57 | + // console.log(input[j]); | |
58 | + } | |
59 | + | |
60 | + return input; | |
61 | +} | ... | ... |
unitTest/testCase.txt
1 | -{"function":"testUnit","input":[1,2,3,4,5],"expect":15,"from":"C:\\myWork\\git\\hospital\\utils\\utils.js"} | |
1 | +{"function":"testUnit","input":["{\"one\":1,\"two\":2,\"three\":3}",4,"[5,6]","{\"test\":{\"lv2\":10}}"],"expect":31,"from":"utils\\utils.js","comment":"simpleTest"} | |
2 | +{"function":"findCmdfromMethod","input":["GET"],"expect":"QUERY","from":"utils\\utils.js","comment":"GET"} | |
3 | +{"function":"findCmdfromMethod","input":["GET"],"expect":"QUERY","from":"utils\\utils.js","comment":"GET"} | |
4 | +{"function":"findCmdfromMethod","input":["GET"],"expect":"QUERY","from":"utils\\utils.js","comment":"GET"} | ... | ... |
... | ... | @@ -0,0 +1,85 @@ |
1 | +var fileTempUnitTest = './unitTest/testCase.txt'; | |
2 | +const fs = require('fs'); | |
3 | +const path = require('path'); | |
4 | +const env = 'gentest'; | |
5 | + | |
6 | +module.exports = function(rawInput,expect,comment) | |
7 | +{ | |
8 | + | |
9 | + if(process.env.NODE_ENV == env) | |
10 | + { | |
11 | + // console.log(rawInput); | |
12 | + var input = []; | |
13 | + var data; | |
14 | + if(typeof rawInput == "object") | |
15 | + { | |
16 | + for(var i=0;i<rawInput.length;i++) | |
17 | + { | |
18 | + if(typeof rawInput[i] == "object") | |
19 | + data = JSON.stringify(rawInput[i]); | |
20 | + else | |
21 | + data = rawInput[i]; | |
22 | + | |
23 | + input.push(data); | |
24 | + } | |
25 | + }else | |
26 | + input.push(rawInput); | |
27 | + | |
28 | + // var track = __stack[1].getFunctionName().split('\.'); | |
29 | + | |
30 | + var testCase = { | |
31 | + function : getFunName(2), | |
32 | + input : input, | |
33 | + expect : expect, | |
34 | + from : path.relative('', __stack[1].getFileName()), | |
35 | + comment : comment ? comment : "" | |
36 | + }; | |
37 | + | |
38 | + // console.log(testCase); | |
39 | + writeFile(fileTempUnitTest,testCase); | |
40 | + } | |
41 | +} | |
42 | + | |
43 | +Object.defineProperty(global, '__stack', { | |
44 | + get: function() { | |
45 | + var orig = Error.prepareStackTrace; | |
46 | + Error.prepareStackTrace = function(_, stack) { | |
47 | + return stack; | |
48 | + }; | |
49 | + var err = new Error; | |
50 | + Error.captureStackTrace(err, arguments.callee); | |
51 | + var stack = err.stack; | |
52 | + Error.prepareStackTrace = orig; | |
53 | + return stack; | |
54 | + } | |
55 | + }); | |
56 | + | |
57 | +function writeFile(fileName,obj){ | |
58 | + fs.appendFile(fileName, JSON.stringify(obj)+"\r\n", function(err) { | |
59 | + if(err) { | |
60 | + return console.log(err); | |
61 | + } | |
62 | + // console.log("The file was saved!"); | |
63 | + }); | |
64 | +} | |
65 | + | |
66 | +function getFunName(level) | |
67 | +{ | |
68 | + var track; | |
69 | + if(level) | |
70 | + track = __stack[level].getFunctionName().split('\.'); | |
71 | + else | |
72 | + track = __stack[1].getFunctionName().split('\.'); | |
73 | + return track[track.length-1]; | |
74 | +} | |
75 | + | |
76 | +if(process.env.NODE_ENV == env) | |
77 | +{ | |
78 | + try { | |
79 | + fs.unlinkSync(fileTempUnitTest); | |
80 | + } catch (error) { | |
81 | + | |
82 | + } | |
83 | +} | |
84 | + | |
85 | + | ... | ... |
utils/log.js
... | ... | @@ -223,6 +223,7 @@ logger.detailResponseFE = function(req) |
223 | 223 | logg.detail(logPrefix(req)+protocal+" "+method+" "+url+" Response to FE - body: "+body); |
224 | 224 | } |
225 | 225 | |
226 | +//mongo | |
226 | 227 | logger.detailSqlQuery = function(req,method,collection,data) |
227 | 228 | { |
228 | 229 | // logger4jDetail.info(logPrefix(req)+"BE Send Mongo Collection: \""+collection+"\" Query: "+JSON.stringify(data)); | ... | ... |
utils/utils.js
1 | 1 | |
2 | 2 | var parseFilter = require('ldapjs').parseFilter; |
3 | -const fs = require('fs'); | |
4 | 3 | var constants = require('./constants'); |
5 | -var fileTempUnitTest = './unitTest/testCase.txt'; | |
4 | + | |
5 | +// //unit test | |
6 | +// var fileTempUnitTest = './unitTest/testCase.txt'; | |
7 | +// const fs = require('fs'); | |
8 | +// const path = require('path'); | |
9 | + | |
10 | +var unitTest = require('../unitTest/unitTest'); | |
6 | 11 | var utils = {}; |
7 | 12 | |
8 | 13 | |
... | ... | @@ -100,75 +105,28 @@ utils.findCmdfromMethod = function (method) |
100 | 105 | cmd = 'DELETE'; |
101 | 106 | break; |
102 | 107 | } |
108 | + unitTest(arguments,cmd,method); | |
103 | 109 | return cmd; |
104 | 110 | } |
105 | 111 | |
106 | -utils.testUnit = function(a,b,c,d,e) | |
112 | +utils.testUnit = function(testObj,testInt,testArray,testObj2) | |
107 | 113 | { |
108 | - var returnData = a+b+c+d+e; | |
109 | - genUnitTest(a,b,c,d,e,returnData); | |
110 | - return returnData; | |
111 | -} | |
114 | + var returnData = 0; | |
115 | + | |
116 | + var keyObj = Object.keys(testObj); | |
117 | + for(var i=0;i<keyObj.length;i++) | |
118 | + returnData += testObj[keyObj[i]]; | |
112 | 119 | |
113 | -//private | |
114 | -Object.defineProperty(global, '__stack', { | |
115 | - get: function() { | |
116 | - var orig = Error.prepareStackTrace; | |
117 | - Error.prepareStackTrace = function(_, stack) { | |
118 | - return stack; | |
119 | - }; | |
120 | - var err = new Error; | |
121 | - Error.captureStackTrace(err, arguments.callee); | |
122 | - var stack = err.stack; | |
123 | - Error.prepareStackTrace = orig; | |
124 | - return stack; | |
125 | - } | |
126 | - }); | |
127 | - | |
128 | -function writeFile(fileName,obj){ | |
129 | - fs.appendFile(fileName, JSON.stringify(obj)+"\r\n", function(err) { | |
130 | - if(err) { | |
131 | - return console.log(err); | |
132 | - } | |
133 | - // console.log("The file was saved!"); | |
134 | - }); | |
135 | -} | |
120 | + returnData += testInt; | |
136 | 121 | |
137 | -function genUnitTest(...rawdata) | |
138 | -{ | |
139 | - if(process.env.NODE_ENV == 'gentest') | |
140 | - { | |
141 | - console.log(process.env.NODE_ENV); | |
142 | - var input = []; | |
143 | - var expect; | |
144 | - var data; | |
145 | - for(var i=0;i<rawdata.length;i++) | |
146 | - { | |
147 | - if(typeof rawdata[i] == "object") | |
148 | - data = JSON.stringify(rawdata[i]); | |
149 | - else | |
150 | - data = rawdata[i]; | |
151 | - | |
152 | - if(i==rawdata.length-1) | |
153 | - expect = data; | |
154 | - else | |
155 | - input.push(data); | |
156 | - } | |
122 | + for(var i=0;i<testArray.length;i++) | |
123 | + returnData += testArray[i]; | |
157 | 124 | |
125 | + returnData += testObj2.test.lv2; | |
158 | 126 | |
159 | - var track = __stack[1].getFunctionName().split('\.'); | |
160 | - | |
161 | - var testCase = { | |
162 | - function : track[track.length-1], | |
163 | - input : input, | |
164 | - expect : expect, | |
165 | - from : __filename | |
166 | - }; | |
167 | - | |
168 | - // console.log(testCase); | |
169 | - writeFile(fileTempUnitTest,testCase); | |
170 | - } | |
171 | -} | |
127 | + unitTest(arguments,returnData,"simpleTest"); | |
128 | + return returnData; | |
129 | +} | |
172 | 130 | |
173 | 131 | function findValueInBracket(data) |
174 | 132 | { |
... | ... | @@ -224,8 +182,6 @@ function findValueInBracket(data) |
224 | 182 | |
225 | 183 | } |
226 | 184 | |
227 | - | |
228 | - | |
229 | 185 | getQuery = async function(data,query){ |
230 | 186 | |
231 | 187 | if(!query) |
... | ... | @@ -289,8 +245,4 @@ getQuery = async function(data,query){ |
289 | 245 | |
290 | 246 | |
291 | 247 | |
292 | -if(process.env.NODE_ENV == 'gentest') | |
293 | - fs.unlinkSync(fileTempUnitTest); | |
294 | - | |
295 | - | |
296 | 248 | module.exports = utils; |
297 | 249 | \ No newline at end of file | ... | ... |