-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
loggermongo-tests.js
292 lines (264 loc) · 15.2 KB
/
loggermongo-tests.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
import { _ } from 'meteor/underscore';
import { Meteor } from 'meteor/meteor';
import { LoggerMongo } from 'meteor/ostrio:loggermongo';
import { Logger, LoggerMessage } from 'meteor/ostrio:logger';
const log = new Logger();
const mongoLogger = (new LoggerMongo(log)).enable();
if (Meteor.isServer) {
mongoLogger.collection.remove({});
}
Tinytest.add('LoggerMessage Instance', (test) => {
test.instanceOf(log.info('This is message "info"', {data: 'Sample data "info"'}, 'userId "info"'), LoggerMessage);
test.instanceOf(log.debug('This is message "debug"', {data: 'Sample data "debug"'}, 'userId "debug"'), LoggerMessage);
test.instanceOf(log.error('This is message "error"', {data: 'Sample data "error"'}, 'userId "error"'), LoggerMessage);
test.instanceOf(log.fatal('This is message "fatal"', {data: 'Sample data "fatal"'}, 'userId "fatal"'), LoggerMessage);
test.instanceOf(log.warn('This is message "warn"', {data: 'Sample data "warn"'}, 'userId "warn"'), LoggerMessage);
test.instanceOf(log.trace('This is message "trace"', {data: 'Sample data "trace"'}, 'userId "trace"'), LoggerMessage);
test.instanceOf(log._('This is message "_"', {data: 'Sample data "_"'}, 'userId "_"'), LoggerMessage);
});
Tinytest.add('LoggerMessage#toString', (test) => {
test.equal(log.info('This is message "info"', {data: 'Sample data "info"'}, 'userId "info"').toString(), '[This is message "info"] \nLevel: INFO; \nDetails: {"data":"Sample data \\"info\\""}; \nUserId: userId "info";');
test.equal(log.debug('This is message "debug"', {data: 'Sample data "debug"'}, 'userId "debug"').toString(), '[This is message "debug"] \nLevel: DEBUG; \nDetails: {"data":"Sample data \\"debug\\""}; \nUserId: userId "debug";');
test.equal(log.error('This is message "error"', {data: 'Sample data "error"'}, 'userId "error"').toString(), '[This is message "error"] \nLevel: ERROR; \nDetails: {"data":"Sample data \\"error\\""}; \nUserId: userId "error";');
test.equal(log.fatal('This is message "fatal"', {data: 'Sample data "fatal"'}, 'userId "fatal"').toString(), '[This is message "fatal"] \nLevel: FATAL; \nDetails: {"data":"Sample data \\"fatal\\""}; \nUserId: userId "fatal";');
test.equal(log.warn('This is message "warn"', {data: 'Sample data "warn"'}, 'userId "warn"').toString(), '[This is message "warn"] \nLevel: WARN; \nDetails: {"data":"Sample data \\"warn\\""}; \nUserId: userId "warn";');
test.equal(log._('This is message "_"', {data: 'Sample data "_"'}, 'userId "_"').toString(), '[This is message "_"] \nLevel: LOG; \nDetails: {"data":"Sample data \\"_\\""}; \nUserId: userId "_";');
});
Tinytest.add('Throw', (test) => {
try {
throw log.fatal('This is message "fatal"', {data: 'Sample data "fatal"'}, 'userId "fatal"');
} catch (e) {
test.instanceOf(e, LoggerMessage);
test.equal(e.level, 'FATAL');
test.equal(e.toString(), '[This is message "fatal"] \nLevel: FATAL; \nDetails: {"data":"Sample data \\"fatal\\""}; \nUserId: userId "fatal";');
}
});
Tinytest.add('Log a Number', (test) => {
test.instanceOf(log.info(10, {data: 10}, 10), LoggerMessage);
test.instanceOf(log.debug(20, {data: 20}, 20), LoggerMessage);
test.instanceOf(log.error(30, {data: 30}, 30), LoggerMessage);
test.instanceOf(log.fatal(40, {data: 40}, 40), LoggerMessage);
test.instanceOf(log.warn(50, {data: 50}, 50), LoggerMessage);
test.instanceOf(log.trace(60, {data: 60}, 60), LoggerMessage);
test.instanceOf(log._(70, {data: 70}, 70), LoggerMessage);
});
Tinytest.add('Log a null', (test) => {
test.instanceOf(log.info(10, null), LoggerMessage);
test.instanceOf(log.debug(20, null), LoggerMessage);
test.instanceOf(log.error(30, null), LoggerMessage);
test.instanceOf(log.fatal(40, null), LoggerMessage);
test.instanceOf(log.warn(50, null), LoggerMessage);
test.instanceOf(log.trace(60, null), LoggerMessage);
test.instanceOf(log._(70, null), LoggerMessage);
});
Tinytest.add('Log a Object', (test) => {
test.instanceOf(log.info(10, {keyNull: null, keyStr: 'str'}), LoggerMessage);
test.instanceOf(log.debug(20, {keyNull: null, keyStr: 'str'}), LoggerMessage);
test.instanceOf(log.error(30, {keyNull: null, keyStr: 'str'}), LoggerMessage);
test.instanceOf(log.fatal(40, {keyNull: null, keyStr: 'str'}), LoggerMessage);
test.instanceOf(log.warn(50, {keyNull: null, keyStr: 'str'}), LoggerMessage);
test.instanceOf(log.trace(60, {keyNull: null, keyStr: 'str'}), LoggerMessage);
test.instanceOf(log._(70, {keyNull: null, keyStr: 'str'}), LoggerMessage);
});
Tinytest.add('Log a String', (test) => {
test.instanceOf(log.info(10, 'string value'), LoggerMessage);
test.instanceOf(log.debug(20, 'string value'), LoggerMessage);
test.instanceOf(log.error(30, 'string value'), LoggerMessage);
test.instanceOf(log.fatal(40, 'string value'), LoggerMessage);
test.instanceOf(log.warn(50, 'string value'), LoggerMessage);
test.instanceOf(log.trace(60, 'string value'), LoggerMessage);
test.instanceOf(log._(70, 'string value'), LoggerMessage);
});
Tinytest.add('Log with wrong arguments', (test) => {
test.instanceOf(log.info('info wrong values', false), LoggerMessage);
test.instanceOf(log.debug('debug wrong values', true), LoggerMessage);
test.instanceOf(log.error('error wrong values', true), LoggerMessage);
test.instanceOf(log.fatal('fatal wrong values', false), LoggerMessage);
test.instanceOf(log.warn('warn wrong values', undefined), LoggerMessage);
test.instanceOf(log.trace('trace wrong values', ''), LoggerMessage);
test.instanceOf(log._('_ wrong values', []), LoggerMessage);
});
Tinytest.add('Log Boolean message', (test) => {
test.instanceOf(log.info('info', true), LoggerMessage);
test.instanceOf(log.debug('debug', true), LoggerMessage);
test.instanceOf(log.error('error', false), LoggerMessage);
test.instanceOf(log.fatal('fatal', false), LoggerMessage);
test.instanceOf(log.warn('warn', true), LoggerMessage);
test.instanceOf(log.trace('trace', true), LoggerMessage);
test.instanceOf(log._('_', true), LoggerMessage);
});
Tinytest.add('Log without message', (test) => {
test.instanceOf(log.info(10), LoggerMessage);
test.instanceOf(log.debug(20), LoggerMessage);
test.instanceOf(log.error(30), LoggerMessage);
test.instanceOf(log.fatal(40), LoggerMessage);
test.instanceOf(log.warn(50), LoggerMessage);
test.instanceOf(log.trace(60), LoggerMessage);
test.instanceOf(log._(70), LoggerMessage);
});
Tinytest.add('Log without arguments', (test) => {
test.instanceOf(log.info(), LoggerMessage);
test.instanceOf(log.debug(), LoggerMessage);
test.instanceOf(log.error(), LoggerMessage);
test.instanceOf(log.fatal(), LoggerMessage);
test.instanceOf(log.warn(), LoggerMessage);
test.instanceOf(log.trace(), LoggerMessage);
test.instanceOf(log._(), LoggerMessage);
});
const dataObj = {
time: new Date,
subObj: {
keyStr: 'str'
}
};
dataObj.subObj.do = dataObj;
Tinytest.addAsync('Log a Circular', (test, done) => {
test.instanceOf(log.info('Circular 10', dataObj), LoggerMessage);
test.instanceOf(log.debug('Circular 20', dataObj), LoggerMessage);
test.instanceOf(log.error('Circular 30', dataObj), LoggerMessage);
test.instanceOf(log.fatal('Circular 40', dataObj), LoggerMessage);
test.instanceOf(log.warn('Circular 50', dataObj), LoggerMessage);
test.instanceOf(log.trace('Circular 60', dataObj), LoggerMessage);
test.instanceOf(log._('Circular 70', dataObj), LoggerMessage);
if (Meteor.isServer) {
Meteor.setTimeout(() => {
test.isTrue(mongoLogger.collection.findOne({message: 'Circular 10'}).additional.subObj.do.includes('[Circular]'));
test.isTrue(mongoLogger.collection.findOne({message: 'Circular 10'}).additional.subObj.do.includes('[Circular]'));
test.isTrue(mongoLogger.collection.findOne({message: 'Circular 20'}).additional.subObj.do.includes('[Circular]'));
test.isTrue(mongoLogger.collection.findOne({message: 'Circular 30'}).additional.subObj.do.includes('[Circular]'));
test.isTrue(mongoLogger.collection.findOne({message: 'Circular 40'}).additional.subObj.do.includes('[Circular]'));
test.isTrue(mongoLogger.collection.findOne({message: 'Circular 50'}).additional.subObj.do.includes('[Circular]'));
test.isTrue(mongoLogger.collection.findOne({message: 'Circular 60'}).additional.subObj.do.includes('[Circular]'));
test.isTrue(mongoLogger.collection.findOne({message: 'Circular 70'}).additional.subObj.do.includes('[Circular]'));
done();
}, 256);
} else {
done();
}
});
Tinytest.add('Trace', (test) => {
if (Meteor.isServer) {
test.isTrue(_.has(log.trace(602, {data: 602}, 602).details, 'stackTrace'));
test.isTrue(_.has(log.trace(602, {data: 602}, 602).data, 'stackTrace'));
} else {
test.isTrue(true);
}
});
Tinytest.addAsync('Check written data, without {data} [SERVER]', (test, done) => {
if (Meteor.isServer) {
log.info('cwdwods Test "info"');
log.debug('cwdwods Test "debug"');
log.error('cwdwods Test "error"');
log.fatal('cwdwods Test "fatal"');
log.warn('cwdwods Test "warn"');
log.trace('cwdwods Test "trace"');
log._('cwdwods Test "_"');
Meteor.setTimeout(() => {
test.isTrue(!!mongoLogger.collection.findOne({message: 'cwdwods Test "info"'}));
test.isTrue(!!mongoLogger.collection.findOne({message: 'cwdwods Test "debug"'}));
test.isTrue(!!mongoLogger.collection.findOne({message: 'cwdwods Test "error"'}));
test.isTrue(!!mongoLogger.collection.findOne({message: 'cwdwods Test "fatal"'}));
test.isTrue(!!mongoLogger.collection.findOne({message: 'cwdwods Test "warn"'}));
test.isTrue(!!mongoLogger.collection.findOne({message: 'cwdwods Test "trace"'}));
test.isTrue(!!mongoLogger.collection.findOne({message: 'cwdwods Test "trace"'}).additional.stackTrace);
test.isTrue(!!mongoLogger.collection.findOne({message: 'cwdwods Test "_"'}));
done();
}, 256);
} else {
test.isTrue(true);
done();
}
});
Tinytest.addAsync('Check written data, with {data} [SERVER]', (test, done) => {
if (Meteor.isServer) {
log.info(103, {data: 'cwdwds Test "info"'});
log.debug(203, {data: 'cwdwds Test "debug"'});
log.error(303, {data: 'cwdwds Test "error"'});
log.fatal(403, {data: 'cwdwds Test "fatal"'});
log.warn(503, {data: 'cwdwds Test "warn"'});
log.trace(603, {data: 'cwdwds Test "trace"'});
log._(703, {data: 'cwdwds Test "_"'});
Meteor.setTimeout(() => {
test.isTrue(!!mongoLogger.collection.findOne({'additional.data': 'cwdwds Test "info"'}), 'Data test: info');
test.isTrue(!!mongoLogger.collection.findOne({message: 103}), 'Number test: 10');
test.isTrue(!!mongoLogger.collection.findOne({'additional.data': 'cwdwds Test "debug"'}), 'Data test: debug');
test.isTrue(!!mongoLogger.collection.findOne({message: 203}), 'Number test: 20');
test.isTrue(!!mongoLogger.collection.findOne({'additional.data': 'cwdwds Test "error"'}), 'Data test: error');
test.isTrue(!!mongoLogger.collection.findOne({message: 303}), 'Number test: 30');
test.isTrue(!!mongoLogger.collection.findOne({'additional.data': 'cwdwds Test "fatal"'}), 'Data test: fatal');
test.isTrue(!!mongoLogger.collection.findOne({message: 403}), 'Number test: 40');
test.isTrue(!!mongoLogger.collection.findOne({'additional.data': 'cwdwds Test "warn"'}), 'Data test: warn');
test.isTrue(!!mongoLogger.collection.findOne({message: 503}), 'Number test: 50');
test.isTrue(!!mongoLogger.collection.findOne({'additional.data': 'cwdwds Test "trace"'}), 'Data test: trace');
test.isTrue(!!mongoLogger.collection.findOne({message: 603}), 'Data test: stackTrace');
test.isTrue(!!mongoLogger.collection.findOne({message: 603}).additional.stackTrace, 'Number test: 60');
test.isTrue(!!mongoLogger.collection.findOne({'additional.data': 'cwdwds Test "_"'}), 'Data test: _');
test.isTrue(!!mongoLogger.collection.findOne({message: 703}), 'Number test: 70');
done();
}, 256);
} else {
test.isTrue(true);
done();
}
});
if (Meteor.isClient) {
log.info('cwdwodfc2s Test "info"');
log.debug('cwdwodfc2s Test "debug"');
log.error('cwdwodfc2s Test "error"');
log.fatal('cwdwodfc2s Test "fatal"');
log.warn('cwdwodfc2s Test "warn"');
log.trace('cwdwodfc2s Test "trace"');
log._('cwdwodfc2s Test "_"');
}
Tinytest.addAsync('Check written data, without {data} [From CLIENT to SERVER]', (test, done) => {
if (Meteor.isServer) {
Meteor.setTimeout(() => {
test.isTrue(!!mongoLogger.collection.findOne({message: 'cwdwodfc2s Test "info"'}));
test.isTrue(!!mongoLogger.collection.findOne({message: 'cwdwodfc2s Test "debug"'}));
test.isTrue(!!mongoLogger.collection.findOne({message: 'cwdwodfc2s Test "error"'}));
test.isTrue(!!mongoLogger.collection.findOne({message: 'cwdwodfc2s Test "fatal"'}));
test.isTrue(!!mongoLogger.collection.findOne({message: 'cwdwodfc2s Test "warn"'}));
test.isTrue(!!mongoLogger.collection.findOne({message: 'cwdwodfc2s Test "trace"'}));
test.isTrue(!!mongoLogger.collection.findOne({message: 'cwdwodfc2s Test "trace"'}).additional.stackTrace);
test.isTrue(!!mongoLogger.collection.findOne({message: 'cwdwodfc2s Test "_"'}));
done();
}, 512);
} else {
test.isTrue(true);
done();
}
});
if (Meteor.isClient) {
log.info(100, {data: 'cwdwdfc2s Test "info"'});
log.debug(200, {data: 'cwdwdfc2s Test "debug"'});
log.error(300, {data: 'cwdwdfc2s Test "error"'});
log.fatal(400, {data: 'cwdwdfc2s Test "fatal"'});
log.warn(500, {data: 'cwdwdfc2s Test "warn"'});
log.trace(600, {data: 'cwdwdfc2s Test "trace"'});
log._(700, {data: 'cwdwdfc2s Test "_"'});
}
Tinytest.addAsync('Check written data, with data [From CLIENT to SERVER]', (test, done) => {
if (Meteor.isServer) {
Meteor.setTimeout(() => {
test.isTrue(!!mongoLogger.collection.findOne({'additional.data': 'cwdwdfc2s Test "info"'}), 'Data test: info');
test.isTrue(!!mongoLogger.collection.findOne({message: 100}), 'Number test: 10');
test.isTrue(!!mongoLogger.collection.findOne({'additional.data': 'cwdwdfc2s Test "debug"'}), 'Data test: debug');
test.isTrue(!!mongoLogger.collection.findOne({message: 200}), 'Number test: 20');
test.isTrue(!!mongoLogger.collection.findOne({'additional.data': 'cwdwdfc2s Test "error"'}), 'Data test: error');
test.isTrue(!!mongoLogger.collection.findOne({message: 300}), 'Number test: 30');
test.isTrue(!!mongoLogger.collection.findOne({'additional.data': 'cwdwdfc2s Test "fatal"'}), 'Data test: fatal');
test.isTrue(!!mongoLogger.collection.findOne({message: 400}), 'Number test: 40');
test.isTrue(!!mongoLogger.collection.findOne({'additional.data': 'cwdwdfc2s Test "warn"'}), 'Data test: warn');
test.isTrue(!!mongoLogger.collection.findOne({message: 500}), 'Number test: 50');
test.isTrue(!!mongoLogger.collection.findOne({'additional.data': 'cwdwdfc2s Test "trace"'}), 'Data test: trace');
test.isTrue(!!mongoLogger.collection.findOne({'additional.data': 'cwdwdfc2s Test "trace"'}).additional.stackTrace);
test.isTrue(!!mongoLogger.collection.findOne({message: 600}), 'Number test: 60');
test.isTrue(!!mongoLogger.collection.findOne({'additional.data': 'cwdwdfc2s Test "_"'}), 'Data test: _');
test.isTrue(!!mongoLogger.collection.findOne({message: 700}), 'Number test: 70');
done();
}, 512);
} else {
test.isTrue(true);
done();
}
});