-
-
Notifications
You must be signed in to change notification settings - Fork 880
/
datatype.spec.ts
466 lines (391 loc) · 15.5 KB
/
datatype.spec.ts
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
import { describe, expect, it } from 'vitest';
import { faker } from '../src';
import { seededTests } from './support/seededRuns';
const NON_SEEDED_BASED_RUN = 25;
describe('datatype', () => {
seededTests(faker, 'datatype', (t) => {
t.describe('number', (t) => {
t.it('noArgs')
.itRepeated('repeated', 5, 6)
.it('with min', { min: -42 })
.it('with max', { max: 69 })
.it('with min and max', {
min: -42,
max: 69,
})
.it('with min, max and precision', {
min: -42,
max: 69,
precision: 0.01,
});
});
t.describe('float', (t) => {
t.it('noArgs')
.itRepeated('repeated', 6)
.it('with min', { min: -42 })
.it('with max', { max: 69 })
.it('with min and max', { min: -42, max: 69 })
.it('with min, max and precision', {
min: -42,
max: 69,
precision: 0.0001,
});
});
t.describe('datetime', (t) => {
t.it('noArgs')
.it('with given number', Date.parse('2001-04-03T23:21:10.773Z'))
.it('with min', {
min: Date.parse('1622-05-23T13:45:08.843Z'),
})
.it('with max', {
max: Date.parse('2002-01-29T19:47:52.605Z'),
})
.it('with min and max', {
min: Date.parse('1622-05-23T13:45:08.843Z'),
max: Date.parse('1802-01-29T19:47:52.605Z'),
});
});
t.describe('string', (t) => {
t.it('noArgs').it('with length', 42);
});
t.itRepeated('uuid', 5);
t.itRepeated('boolean', 5);
t.describe('hexadecimal', (t) => {
t.it('noArgs')
.it('with length', { length: 42 })
.it('with prefix', { prefix: '0x' })
.it('with casing', { case: 'lower' })
.it('with length, prefix, and casing', {
length: 20,
prefix: '0x',
case: 'lower',
});
});
t.it('json');
t.describe('array', (t) => {
t.it('noArgs').it('with length', 4);
});
t.describe('bigInt', (t) => {
t.it('noArgs').it('with value', 42);
});
});
describe(`random seeded tests for seed ${faker.seed()}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe('number', () => {
it('should return a random number given a maximum value as Number', () => {
const max = 10;
const actual = faker.datatype.number(max);
expect(actual).toBeGreaterThanOrEqual(0);
expect(actual).toBeLessThanOrEqual(max);
});
it('should return a random number given a maximum value as Object', () => {
const options = { max: 10 };
const actual = faker.datatype.number(options);
expect(actual).toBeGreaterThanOrEqual(0);
expect(actual).toBeLessThanOrEqual(options.max);
});
it('should return a random number given a maximum value of 0', () => {
const options = { max: 0 };
const actual = faker.datatype.number(options);
expect(actual).toBe(0);
});
it('should return a random number given a negative number minimum and maximum value of 0', () => {
const options = { min: -100, max: 0 };
const actual = faker.datatype.number(options);
expect(actual).toBeGreaterThanOrEqual(options.min);
expect(actual).toBeLessThanOrEqual(options.max);
});
it('should return a random number between a range', () => {
const options = { min: 22, max: 33 };
for (let i = 0; i < 100; i++) {
const actual = faker.datatype.number(options);
expect(actual).toBeGreaterThanOrEqual(options.min);
expect(actual).toBeLessThanOrEqual(options.max);
}
});
it('should return inclusive negative max value', () => {
let foundNegative4 = false;
let foundNegative5 = false;
for (let iter = 0; iter < 1000; iter++) {
const actual = faker.datatype.number({ min: -5, max: -4 });
if (actual === -4) {
foundNegative4 = true;
} else if (actual === -5) {
foundNegative5 = true;
}
expect(actual).toBeGreaterThanOrEqual(-5);
expect(actual).toBeLessThanOrEqual(-4);
if (foundNegative4 && foundNegative5) {
break;
}
}
expect(foundNegative4).toBeTruthy();
expect(foundNegative5).toBeTruthy();
});
it('provides numbers with a given precision', () => {
const options = { min: 0, max: 1.5, precision: 0.5 };
const results = Array.from(
new Set(
Array.from({ length: 50 }, () => faker.datatype.number(options))
)
).sort();
expect(results).toContain(0.5);
expect(results).toContain(1.0);
expect(results[0]).toBe(0);
expect(results[results.length - 1]).toBe(1.5);
});
it('provides numbers with a with exact precision', () => {
const options = { min: 0.5, max: 0.99, precision: 0.01 };
for (let i = 0; i < 100; i++) {
const actual = faker.datatype.number(options);
expect(actual).toBe(Number(actual.toFixed(2)));
}
});
it('should not mutate the input object', () => {
const initialMin = 1;
const initialPrecision = 1;
const initialOtherProperty = 'hello darkness my old friend';
const input: {
min?: number;
max?: number;
precision?: number;
otherProperty: string;
} = Object.freeze({
min: initialMin,
precision: initialPrecision,
otherProperty: initialOtherProperty,
});
expect(() => faker.datatype.number(input)).not.toThrow();
});
it('should throw when min > max', () => {
const min = 10;
const max = 9;
expect(() => {
faker.datatype.number({ min, max });
}).toThrowError(`Max ${max} should be greater than min ${min}.`);
});
});
describe('float', () => {
it('should return a random float with a default precision value (0.01)', () => {
const number = faker.datatype.float();
expect(number).toBe(Number(number.toFixed(2)));
});
it('should return a random float given a precision value', () => {
const number = faker.datatype.float(0.001);
expect(number).toBe(Number(number.toFixed(3)));
});
it('should return a random number given a maximum value as Object', () => {
const options = { max: 10 };
expect(faker.datatype.float(options)).toBeGreaterThanOrEqual(0);
expect(faker.datatype.float(options)).toBeLessThanOrEqual(
options.max
);
});
it('should return a random number given a maximum value of 0', () => {
const options = { max: 0 };
expect(faker.datatype.float(options)).toBe(0);
});
it('should return a random number given a negative number minimum and maximum value of 0', () => {
const options = { min: -100, max: 0 };
expect(faker.datatype.float(options)).toBeGreaterThanOrEqual(
options.min
);
expect(faker.datatype.float(options)).toBeLessThanOrEqual(
options.max
);
});
it('should return a random number between a range', () => {
const options = { min: 22, max: 33 };
for (let i = 0; i < 5; i++) {
const randomNumber = faker.datatype.float(options);
expect(randomNumber).toBeGreaterThanOrEqual(options.min);
expect(randomNumber).toBeLessThanOrEqual(options.max);
}
});
it('provides numbers with a given precision', () => {
const options = { min: 0, max: 1.5, precision: 0.5 };
const results = Array.from(
new Set(
Array.from({ length: 50 }, () => faker.datatype.float(options))
)
).sort();
expect(results).toContain(0.5);
expect(results).toContain(1.0);
expect(results[0]).toBe(0);
expect(results[results.length - 1]).toBe(1.5);
});
it('provides numbers with a with exact precision', () => {
const options = { min: 0.5, max: 0.99, precision: 0.01 };
for (let i = 0; i < 100; i++) {
const number = faker.datatype.float(options);
expect(number).toBe(Number(number.toFixed(2)));
}
});
it('should not modify the input object', () => {
const min = 1;
const max = 2;
const opts = { min, max };
faker.datatype.float(opts);
expect(opts.min).toBe(min);
expect(opts.max).toBe(max);
});
});
describe('datetime', () => {
it('check validity of date and if returned value is created by Date()', () => {
const date = faker.datatype.datetime();
expect(date).toBeTypeOf('object');
expect(date.getTime()).not.toBeNaN();
expect(Object.prototype.toString.call(date)).toBe('[object Date]');
});
});
describe('string', () => {
it('should generate a string value', () => {
const generatedString = faker.datatype.string();
expect(generatedString).toBeTypeOf('string');
expect(generatedString).toHaveLength(10);
});
it('should return empty string if negative length is passed', () => {
const negativeValue = faker.datatype.number({ min: -1000, max: -1 });
const generatedString = faker.datatype.string(negativeValue);
expect(generatedString).toBe('');
expect(generatedString).toHaveLength(0);
});
it('should return string with length of 2^20 if bigger length value is passed', () => {
const overMaxValue = Math.pow(2, 28);
const generatedString = faker.datatype.string(overMaxValue);
expect(generatedString).toHaveLength(Math.pow(2, 20));
});
});
describe('boolean', () => {
it('generates a boolean value', () => {
const bool = faker.datatype.boolean();
expect(bool).toBeTypeOf('boolean');
});
});
describe('UUID', () => {
it('generates a valid UUID', () => {
const UUID = faker.datatype.uuid();
const RFC4122 =
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
expect(UUID).toMatch(RFC4122);
});
});
describe('hexadecimal', () => {
it('generates single hex character when no additional argument was provided', () => {
const hex = faker.datatype.hexadecimal();
expect(hex).toMatch(/^[0-9a-f]{1}$/i);
expect(hex).toHaveLength(1);
});
it('generates a random hex string with a provided length', () => {
const hex = faker.datatype.hexadecimal({ length: 5 });
expect(hex).toMatch(/^[0-9a-f]+$/i);
expect(hex).toHaveLength(5);
});
it('generates a hex string with a provided prefix', () => {
const hex = faker.datatype.hexadecimal({ prefix: '0x' });
expect(hex).toMatch(/^(0x)[0-9A-F]+$/i);
expect(hex).toHaveLength(3);
});
it('generates a hex string with a provided casing', () => {
const hex = faker.datatype.hexadecimal({ case: 'lower' });
expect(hex).toMatch(/^[0-9a-f]+$/i);
expect(hex).toHaveLength(1);
});
it('generates a hex string with a provided prefix, length, and casing', () => {
const hex = faker.datatype.hexadecimal({
prefix: '0x',
length: 7,
case: 'upper',
});
expect(hex).toMatch(/^(0x)[0-9A-F]+$/i);
expect(hex.substring(2)).toHaveLength(7);
});
});
describe('json', () => {
it('generates a valid json object', () => {
const jsonObject = faker.datatype.json();
expect(jsonObject).toBeTypeOf('string');
expect(JSON.parse(jsonObject)).toBeTruthy();
});
});
describe('array', () => {
it('generates an array with passed size', () => {
const randomSize = faker.datatype.number();
const generatedArray = faker.datatype.array(randomSize);
expect(generatedArray).toHaveLength(randomSize);
});
it('generates an array with 1 element', () => {
const generatedArray = faker.datatype.array(1);
expect(generatedArray).toHaveLength(1);
});
});
describe('bigInt', () => {
it('should generate a bigInt value', () => {
const generateBigInt = faker.datatype.bigInt();
expect(generateBigInt).toBeTypeOf('bigint');
});
it('should generate a big bigInt value with low delta', () => {
const min = 999999999n;
const max = 1000000000n;
const generateBigInt = faker.datatype.bigInt({ min, max });
expect(generateBigInt).toBeTypeOf('bigint');
expect(generateBigInt).toBeGreaterThanOrEqual(min);
expect(generateBigInt).toBeLessThanOrEqual(max);
});
it('should return a random bigint given a maximum value as BigInt', () => {
const max = 10n;
expect(faker.datatype.bigInt(max)).toBeGreaterThanOrEqual(0n);
expect(faker.datatype.bigInt(max)).toBeLessThanOrEqual(max);
});
it('should return a random bigint given a maximum value as Object', () => {
const options = { max: 10n };
expect(faker.datatype.bigInt(options)).toBeGreaterThanOrEqual(0n);
expect(faker.datatype.bigInt(options)).toBeLessThanOrEqual(
options.max
);
});
it('should return a random bigint given a maximum value of 0', () => {
const options = { max: 0n };
expect(faker.datatype.bigInt(options)).toBe(0n);
});
it('should return a random bigint given a negative bigint minimum and maximum value of 0', () => {
const options = { min: -100n, max: 0n };
expect(faker.datatype.bigInt(options)).toBeGreaterThanOrEqual(
options.min
);
expect(faker.datatype.bigInt(options)).toBeLessThanOrEqual(
options.max
);
});
it('should return a random bigint between a range', () => {
const options = { min: 22, max: 33 };
for (let i = 0; i < 100; i++) {
const randomBigInt = faker.datatype.bigInt(options);
expect(randomBigInt).toBeGreaterThanOrEqual(options.min);
expect(randomBigInt).toBeLessThanOrEqual(options.max);
}
});
it('should succeed with success-rate', () => {
const min = 0n;
const max = 1000000000000n;
const randomBigInt = faker.datatype.bigInt({ min, max });
expect(randomBigInt).toBeGreaterThanOrEqual(min);
expect(randomBigInt).toBeLessThanOrEqual(max);
});
it('should not mutate the input object', () => {
const initialMin = 1n;
const initialOtherProperty = 'hello darkness my old friend';
const input: {
min?: bigint;
max?: bigint;
otherProperty: string;
} = Object.freeze({
min: initialMin,
otherProperty: initialOtherProperty,
});
expect(() => faker.datatype.bigInt(input)).not.toThrow();
});
});
}
});
});