@coderzz/mocker-data-generator 3.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +561 -0
- package/LICENSE +21 -0
- package/README.md +550 -0
- package/build/main/array-includes.d.ts +0 -0
- package/build/main/array-includes.js +45 -0
- package/build/main/index.d.ts +7 -0
- package/build/main/index.js +12 -0
- package/build/main/index.spec.d.ts +1 -0
- package/build/main/index.spec.js +16 -0
- package/build/main/lib/Generator.d.ts +61 -0
- package/build/main/lib/Generator.js +134 -0
- package/build/main/lib/Mocker.d.ts +22 -0
- package/build/main/lib/Mocker.js +76 -0
- package/build/main/lib/Schema.d.ts +9 -0
- package/build/main/lib/Schema.js +210 -0
- package/build/main/lib/types.d.ts +8 -0
- package/build/main/lib/types.js +2 -0
- package/build/main/lib/utils.d.ts +10 -0
- package/build/main/lib/utils.js +222 -0
- package/build/main/tests/Generator.db.spec.d.ts +1 -0
- package/build/main/tests/Generator.db.spec.js +33 -0
- package/build/main/tests/Generator.eval.spec.d.ts +1 -0
- package/build/main/tests/Generator.eval.spec.js +21 -0
- package/build/main/tests/Generator.function.spec.d.ts +1 -0
- package/build/main/tests/Generator.function.spec.js +48 -0
- package/build/main/tests/Generator.generator.spec.d.ts +1 -0
- package/build/main/tests/Generator.generator.spec.js +187 -0
- package/build/main/tests/Generator.hasMany.spec.d.ts +1 -0
- package/build/main/tests/Generator.hasMany.spec.js +115 -0
- package/build/main/tests/Generator.hasOne.spec.d.ts +1 -0
- package/build/main/tests/Generator.hasOne.spec.js +146 -0
- package/build/main/tests/Generator.incrementalId.spec.d.ts +1 -0
- package/build/main/tests/Generator.incrementalId.spec.js +26 -0
- package/build/main/tests/Generator.self.spec.d.ts +1 -0
- package/build/main/tests/Generator.self.spec.js +24 -0
- package/build/main/tests/Generator.static.spec.d.ts +1 -0
- package/build/main/tests/Generator.static.spec.js +15 -0
- package/build/main/tests/Generator.values.spec.d.ts +1 -0
- package/build/main/tests/Generator.values.spec.js +16 -0
- package/build/main/tests/Mocker.build.spec.d.ts +1 -0
- package/build/main/tests/Mocker.build.spec.js +196 -0
- package/build/main/tests/Mocker.reset.spec.d.ts +1 -0
- package/build/main/tests/Mocker.reset.spec.js +17 -0
- package/build/main/tests/Mocker.restart.spec.d.ts +1 -0
- package/build/main/tests/Mocker.restart.spec.js +24 -0
- package/build/main/tests/Mocker.schema.spec.d.ts +1 -0
- package/build/main/tests/Mocker.schema.spec.js +18 -0
- package/build/main/tests/Mocker.seed.spec.d.ts +1 -0
- package/build/main/tests/Mocker.seed.spec.js +42 -0
- package/build/main/tests/Schema.Array.spec.d.ts +1 -0
- package/build/main/tests/Schema.Array.spec.js +332 -0
- package/build/main/tests/Schema.BuildSingle.spec.d.ts +1 -0
- package/build/main/tests/Schema.BuildSingle.spec.js +13 -0
- package/build/main/tests/mocker.spec.d.ts +1 -0
- package/build/main/tests/mocker.spec.js +405 -0
- package/build/module/array-includes.js +45 -0
- package/build/module/index.js +7 -0
- package/build/module/lib/Generator.js +131 -0
- package/build/module/lib/Mocker.js +73 -0
- package/build/module/lib/Schema.js +207 -0
- package/build/module/lib/types.js +1 -0
- package/build/module/lib/utils.js +209 -0
- package/package.json +130 -0
@@ -0,0 +1,405 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
var tslib_1 = require("tslib");
|
4
|
+
var ava_1 = require("ava");
|
5
|
+
var __1 = require("..");
|
6
|
+
(0, ava_1.default)('Should return an new instance of mocker', function (t) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
7
|
+
return tslib_1.__generator(this, function (_a) {
|
8
|
+
t.deepEqual((0, __1.default)(), new __1.Mocker());
|
9
|
+
return [2 /*return*/];
|
10
|
+
});
|
11
|
+
}); });
|
12
|
+
(0, ava_1.default)('Should iterate root level too with fields in models', function (t) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
13
|
+
var length, expectedResult, user, db;
|
14
|
+
return tslib_1.__generator(this, function (_a) {
|
15
|
+
switch (_a.label) {
|
16
|
+
case 0:
|
17
|
+
length = 1;
|
18
|
+
expectedResult = { user: ['firstName'] };
|
19
|
+
user = { static: 'firstName' };
|
20
|
+
return [4 /*yield*/, (0, __1.default)().schema('user', user, length).build()];
|
21
|
+
case 1:
|
22
|
+
db = _a.sent();
|
23
|
+
t.deepEqual(db, expectedResult);
|
24
|
+
return [2 /*return*/];
|
25
|
+
}
|
26
|
+
});
|
27
|
+
}); });
|
28
|
+
(0, ava_1.default)('Virtuals should be eliminated in the final object and can be accesible during generation', function (t) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
29
|
+
var model, expectedResult, db;
|
30
|
+
return tslib_1.__generator(this, function (_a) {
|
31
|
+
switch (_a.label) {
|
32
|
+
case 0:
|
33
|
+
model = {
|
34
|
+
exampleVirtual: {
|
35
|
+
incrementalId: 0,
|
36
|
+
virtual: true
|
37
|
+
},
|
38
|
+
id: {
|
39
|
+
function: function () {
|
40
|
+
return this.object.exampleVirtual;
|
41
|
+
}
|
42
|
+
},
|
43
|
+
deep: {
|
44
|
+
more: {
|
45
|
+
field: {
|
46
|
+
static: 'im here',
|
47
|
+
virtual: true
|
48
|
+
}
|
49
|
+
}
|
50
|
+
},
|
51
|
+
deep2: {
|
52
|
+
more: {
|
53
|
+
field: {
|
54
|
+
static: 'im here'
|
55
|
+
}
|
56
|
+
}
|
57
|
+
}
|
58
|
+
};
|
59
|
+
expectedResult = {
|
60
|
+
id: 0,
|
61
|
+
deep2: {
|
62
|
+
more: {
|
63
|
+
field: 'im here'
|
64
|
+
}
|
65
|
+
}
|
66
|
+
};
|
67
|
+
return [4 /*yield*/, (0, __1.default)().schema('situation', model, 1).build()];
|
68
|
+
case 1:
|
69
|
+
db = _a.sent();
|
70
|
+
t.deepEqual(db.situation[0], expectedResult);
|
71
|
+
return [2 /*return*/];
|
72
|
+
}
|
73
|
+
});
|
74
|
+
}); });
|
75
|
+
(0, ava_1.default)('Should iterate over more complex levels (deeper & function used...)', function (t) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
76
|
+
var model, expectedResult, db;
|
77
|
+
return tslib_1.__generator(this, function (_a) {
|
78
|
+
switch (_a.label) {
|
79
|
+
case 0:
|
80
|
+
model = {
|
81
|
+
name: {
|
82
|
+
firstName: {
|
83
|
+
static: 'firstName'
|
84
|
+
},
|
85
|
+
lastName: {
|
86
|
+
static: 'lastName'
|
87
|
+
},
|
88
|
+
much: {
|
89
|
+
deeper: {
|
90
|
+
function: function () {
|
91
|
+
return (this.object.name.firstName +
|
92
|
+
' ' +
|
93
|
+
this.object.name.lastName);
|
94
|
+
}
|
95
|
+
},
|
96
|
+
more: {
|
97
|
+
deeper: {
|
98
|
+
function: function () {
|
99
|
+
return (this.object.name.firstName +
|
100
|
+
' ' +
|
101
|
+
this.object.name.lastName);
|
102
|
+
}
|
103
|
+
},
|
104
|
+
level: {
|
105
|
+
deeper: {
|
106
|
+
function: function () {
|
107
|
+
return (this.object.name.firstName +
|
108
|
+
' ' +
|
109
|
+
this.object.name.lastName);
|
110
|
+
}
|
111
|
+
},
|
112
|
+
awesome: {
|
113
|
+
deeper: {
|
114
|
+
function: function () {
|
115
|
+
return (this.object.name.firstName +
|
116
|
+
' ' +
|
117
|
+
this.object.name.lastName);
|
118
|
+
}
|
119
|
+
},
|
120
|
+
deeper2: {
|
121
|
+
function: function () {
|
122
|
+
return (this.object.name.firstName +
|
123
|
+
' ' +
|
124
|
+
this.object.name.lastName);
|
125
|
+
}
|
126
|
+
}
|
127
|
+
}
|
128
|
+
}
|
129
|
+
}
|
130
|
+
}
|
131
|
+
}
|
132
|
+
};
|
133
|
+
expectedResult = {
|
134
|
+
name: {
|
135
|
+
firstName: 'firstName',
|
136
|
+
lastName: 'lastName',
|
137
|
+
much: {
|
138
|
+
deeper: 'firstName lastName',
|
139
|
+
more: {
|
140
|
+
deeper: 'firstName lastName',
|
141
|
+
level: {
|
142
|
+
deeper: 'firstName lastName',
|
143
|
+
awesome: {
|
144
|
+
deeper: 'firstName lastName',
|
145
|
+
deeper2: 'firstName lastName'
|
146
|
+
}
|
147
|
+
}
|
148
|
+
}
|
149
|
+
}
|
150
|
+
}
|
151
|
+
};
|
152
|
+
return [4 /*yield*/, (0, __1.default)().schema('situation', model, 1).build()];
|
153
|
+
case 1:
|
154
|
+
db = _a.sent();
|
155
|
+
t.deepEqual(db.situation[0], expectedResult);
|
156
|
+
return [2 /*return*/];
|
157
|
+
}
|
158
|
+
});
|
159
|
+
}); });
|
160
|
+
(0, ava_1.default)('Should work with conditional keys', function (t) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
161
|
+
var conditional, expectedResult, db;
|
162
|
+
return tslib_1.__generator(this, function (_a) {
|
163
|
+
switch (_a.label) {
|
164
|
+
case 0:
|
165
|
+
conditional = {
|
166
|
+
condition: {
|
167
|
+
static: 'a'
|
168
|
+
},
|
169
|
+
'object.condition==="a",a': {
|
170
|
+
static: 'conditionLinkedToA'
|
171
|
+
},
|
172
|
+
'object.condition==="b",b': {
|
173
|
+
static: 'conditionLinkedToB'
|
174
|
+
}
|
175
|
+
};
|
176
|
+
expectedResult = {
|
177
|
+
condition: 'a',
|
178
|
+
a: 'conditionLinkedToA'
|
179
|
+
};
|
180
|
+
return [4 /*yield*/, (0, __1.default)().schema('situation', conditional, 1).build()];
|
181
|
+
case 1:
|
182
|
+
db = _a.sent();
|
183
|
+
t.deepEqual(db.situation[0], expectedResult);
|
184
|
+
return [2 /*return*/];
|
185
|
+
}
|
186
|
+
});
|
187
|
+
}); });
|
188
|
+
(0, ava_1.default)('Should work with conditional keys II', function (t) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
189
|
+
var conditional, user, email, db;
|
190
|
+
return tslib_1.__generator(this, function (_a) {
|
191
|
+
switch (_a.label) {
|
192
|
+
case 0:
|
193
|
+
conditional = {
|
194
|
+
condition: {
|
195
|
+
values: ['email', 'user']
|
196
|
+
},
|
197
|
+
'object.condition==="email",show': {
|
198
|
+
static: 'email'
|
199
|
+
},
|
200
|
+
'object.condition==="user",show': {
|
201
|
+
static: 'user'
|
202
|
+
},
|
203
|
+
'object.condition==="email",email': {
|
204
|
+
hasOne: 'emails'
|
205
|
+
},
|
206
|
+
'object.condition==="user",user': {
|
207
|
+
hasOne: 'users'
|
208
|
+
}
|
209
|
+
};
|
210
|
+
user = { static: 'name.findName' };
|
211
|
+
email = { static: 'internet.email' };
|
212
|
+
return [4 /*yield*/, (0, __1.default)()
|
213
|
+
.schema('users', user, 2)
|
214
|
+
.schema('emails', email, 2)
|
215
|
+
.schema('situation', conditional, 3)
|
216
|
+
.build()];
|
217
|
+
case 1:
|
218
|
+
db = _a.sent();
|
219
|
+
t.true(true);
|
220
|
+
return [2 /*return*/];
|
221
|
+
}
|
222
|
+
});
|
223
|
+
}); });
|
224
|
+
(0, ava_1.default)('Should not affect init values to next entity', function (t) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
225
|
+
var length, request, request2, db;
|
226
|
+
return tslib_1.__generator(this, function (_a) {
|
227
|
+
switch (_a.label) {
|
228
|
+
case 0:
|
229
|
+
length = 10;
|
230
|
+
request = {
|
231
|
+
type: {
|
232
|
+
values: ['kitty', 'pitxi', 'txuri']
|
233
|
+
}
|
234
|
+
};
|
235
|
+
request2 = {
|
236
|
+
type: {
|
237
|
+
static: 'staticValue'
|
238
|
+
}
|
239
|
+
};
|
240
|
+
return [4 /*yield*/, (0, __1.default)()
|
241
|
+
.schema('request', request, { uniqueField: 'type' })
|
242
|
+
.schema('request2', request2, 10)
|
243
|
+
.build()];
|
244
|
+
case 1:
|
245
|
+
db = _a.sent();
|
246
|
+
t.notDeepEqual(db.request, db.request2);
|
247
|
+
db.request2.forEach(function (r2) {
|
248
|
+
db.request.forEach(function (r) {
|
249
|
+
t.notDeepEqual(r2, r);
|
250
|
+
});
|
251
|
+
});
|
252
|
+
return [2 /*return*/];
|
253
|
+
}
|
254
|
+
});
|
255
|
+
}); });
|
256
|
+
(0, ava_1.default)('Should generate more entities', function (t) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
257
|
+
var length, model1, model2, data;
|
258
|
+
return tslib_1.__generator(this, function (_a) {
|
259
|
+
switch (_a.label) {
|
260
|
+
case 0:
|
261
|
+
length = 10;
|
262
|
+
model1 = {
|
263
|
+
request: {
|
264
|
+
id: {
|
265
|
+
static: 0
|
266
|
+
},
|
267
|
+
title: {
|
268
|
+
static: 'hello im a cat'
|
269
|
+
},
|
270
|
+
number: {
|
271
|
+
static: 0
|
272
|
+
}
|
273
|
+
}
|
274
|
+
};
|
275
|
+
model2 = {
|
276
|
+
request: {
|
277
|
+
id: {
|
278
|
+
static: 0
|
279
|
+
},
|
280
|
+
title: {
|
281
|
+
static: 'hello im a cat'
|
282
|
+
},
|
283
|
+
number: {
|
284
|
+
static: 0
|
285
|
+
}
|
286
|
+
}
|
287
|
+
};
|
288
|
+
return [4 /*yield*/, (0, __1.default)()
|
289
|
+
.schema('act', model1, length)
|
290
|
+
.schema('act2', model2, length)
|
291
|
+
.build()];
|
292
|
+
case 1:
|
293
|
+
data = _a.sent();
|
294
|
+
t.true(Object.keys(data).length === 2);
|
295
|
+
t.deepEqual(Object.keys(data), Array('act', 'act2'));
|
296
|
+
t.true(data.act.length === length);
|
297
|
+
t.true(data.act2.length === length);
|
298
|
+
data.act.forEach(function (d) {
|
299
|
+
t.true(Object.keys(d).length === Object.keys(model1).length);
|
300
|
+
t.deepEqual(Object.keys(d), Object.keys(model1));
|
301
|
+
t.deepEqual(Object.keys(d.request), Object.keys(model1.request));
|
302
|
+
});
|
303
|
+
data.act2.forEach(function (d) {
|
304
|
+
t.true(Object.keys(d).length === Object.keys(model2).length);
|
305
|
+
t.deepEqual(Object.keys(d), Object.keys(model2));
|
306
|
+
t.deepEqual(Object.keys(d.request), Object.keys(model2.request));
|
307
|
+
});
|
308
|
+
return [2 /*return*/];
|
309
|
+
}
|
310
|
+
});
|
311
|
+
}); });
|
312
|
+
(0, ava_1.default)('Should uniqueField works', function (t) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
313
|
+
var cat, cat2, result, data;
|
314
|
+
return tslib_1.__generator(this, function (_a) {
|
315
|
+
switch (_a.label) {
|
316
|
+
case 0:
|
317
|
+
cat = {
|
318
|
+
name: ['txuri', 'pitxi', 'kitty']
|
319
|
+
};
|
320
|
+
cat2 = {
|
321
|
+
name: ['txuri', 'pitxi', 'kitty']
|
322
|
+
};
|
323
|
+
result = [{ name: 'txuri' }, { name: 'pitxi' }, { name: 'kitty' }];
|
324
|
+
return [4 /*yield*/, (0, __1.default)()
|
325
|
+
.schema('cat', cat, { uniqueField: 'name' })
|
326
|
+
.schema('cat2', cat2, { uniqueField: 'name' })
|
327
|
+
.build()];
|
328
|
+
case 1:
|
329
|
+
data = _a.sent();
|
330
|
+
t.deepEqual(data.cat, data.cat2);
|
331
|
+
t.deepEqual(data.cat, result);
|
332
|
+
t.deepEqual(data.cat2, result);
|
333
|
+
return [2 /*return*/];
|
334
|
+
}
|
335
|
+
});
|
336
|
+
}); });
|
337
|
+
(0, ava_1.default)('Should max works', function (t) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
338
|
+
var cat, data;
|
339
|
+
return tslib_1.__generator(this, function (_a) {
|
340
|
+
switch (_a.label) {
|
341
|
+
case 0:
|
342
|
+
cat = {
|
343
|
+
name: { values: ['txuri', 'pitxi', 'kitty'] }
|
344
|
+
};
|
345
|
+
return [4 /*yield*/, (0, __1.default)()
|
346
|
+
.schema('cat', cat, { max: 10 })
|
347
|
+
.schema('cat2', cat, { max: 40 })
|
348
|
+
.build()];
|
349
|
+
case 1:
|
350
|
+
data = _a.sent();
|
351
|
+
t.true(data.cat.length <= 10);
|
352
|
+
t.true(data.cat2.length <= 40);
|
353
|
+
return [2 /*return*/];
|
354
|
+
}
|
355
|
+
});
|
356
|
+
}); });
|
357
|
+
/*
|
358
|
+
test('Should max and min works', async t => {
|
359
|
+
let cat = {
|
360
|
+
name: ['txuri', 'pitxi', 'kitty']
|
361
|
+
}
|
362
|
+
|
363
|
+
let data = await mocker()
|
364
|
+
.schema('cat', cat, { min: 5, max: 10 })
|
365
|
+
.schema('cat2', cat, { min: 10, max: 40 })
|
366
|
+
.build()
|
367
|
+
|
368
|
+
t.true(data.cat.length <= 10)
|
369
|
+
t.true(data.cat.length >= 5)
|
370
|
+
t.true(data.cat2.length <= 40)
|
371
|
+
t.true(data.cat2.length >= 10)
|
372
|
+
})
|
373
|
+
|
374
|
+
test('Should generate correctly with 2 ways of uniqueField', function(done) {
|
375
|
+
var cat = {
|
376
|
+
name: ['txuri', 'pitxi', 'kitty']
|
377
|
+
};
|
378
|
+
var cat2 = {
|
379
|
+
name: {
|
380
|
+
values: ['txuri', 'pitxi', 'kitty']
|
381
|
+
}
|
382
|
+
};
|
383
|
+
var result = [ { name: 'txuri' }, { name: 'pitxi' }, { name: 'kitty' } ]
|
384
|
+
var m = mocker()
|
385
|
+
.schema('cat', cat, {uniqueField: 'name'})
|
386
|
+
.schema('cat2', cat2, {uniqueField: 'name'})
|
387
|
+
.build(function(data){
|
388
|
+
try {
|
389
|
+
expect(data.cat)
|
390
|
+
.to.deep.equal(data.cat2)
|
391
|
+
.to.deep.equal(result)
|
392
|
+
.to.not.be.undefined
|
393
|
+
.to.not.be.null
|
394
|
+
done()
|
395
|
+
} catch (x) {
|
396
|
+
done(x)
|
397
|
+
}
|
398
|
+
})
|
399
|
+
})*/
|
400
|
+
(0, ava_1.default)('Should be awesome', function (t) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
401
|
+
return tslib_1.__generator(this, function (_a) {
|
402
|
+
t.true(true);
|
403
|
+
return [2 /*return*/];
|
404
|
+
});
|
405
|
+
}); });
|
@@ -0,0 +1,45 @@
|
|
1
|
+
if (!Array.prototype.includes) {
|
2
|
+
Object.defineProperty(Array.prototype, 'includes', {
|
3
|
+
value: function (searchElement, fromIndex) {
|
4
|
+
// 1. Let O be ? ToObject(this value).
|
5
|
+
if (this == null) {
|
6
|
+
throw new TypeError('"this" is null or not defined');
|
7
|
+
}
|
8
|
+
var o = Object(this);
|
9
|
+
// 2. Let len be ? ToLength(? Get(O, "length")).
|
10
|
+
var len = o.length >>> 0;
|
11
|
+
// 3. If len is 0, return false.
|
12
|
+
if (len === 0) {
|
13
|
+
return false;
|
14
|
+
}
|
15
|
+
// 4. Let n be ? ToInteger(fromIndex).
|
16
|
+
// (If fromIndex is undefined, this step produces the value 0.)
|
17
|
+
var n = fromIndex | 0;
|
18
|
+
// 5. If n ≥ 0, then
|
19
|
+
// a. Let k be n.
|
20
|
+
// 6. Else n < 0,
|
21
|
+
// a. Let k be len + n.
|
22
|
+
// b. If k < 0, let k be 0.
|
23
|
+
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
|
24
|
+
function sameValueZero(x, y) {
|
25
|
+
return (x === y ||
|
26
|
+
(typeof x === 'number' &&
|
27
|
+
typeof y === 'number' &&
|
28
|
+
isNaN(x) &&
|
29
|
+
isNaN(y)));
|
30
|
+
}
|
31
|
+
// 7. Repeat, while k < len
|
32
|
+
while (k < len) {
|
33
|
+
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
|
34
|
+
// b. If SameValueZero(searchElement, elementK) is true, return true.
|
35
|
+
// c. Increase k by 1.
|
36
|
+
if (sameValueZero(o[k], searchElement)) {
|
37
|
+
return true;
|
38
|
+
}
|
39
|
+
k++;
|
40
|
+
}
|
41
|
+
// 8. Return false
|
42
|
+
return false;
|
43
|
+
}
|
44
|
+
});
|
45
|
+
}
|
@@ -0,0 +1,131 @@
|
|
1
|
+
import { __assign, __spreadArray } from "tslib";
|
2
|
+
import { loopInside, stringToPathOrCall } from './utils';
|
3
|
+
var Generator = /** @class */ (function () {
|
4
|
+
function Generator() {
|
5
|
+
}
|
6
|
+
Generator.prototype.self = function (cfg) {
|
7
|
+
var object = this.object;
|
8
|
+
return cfg.eval
|
9
|
+
? eval('object.' + cfg.self)
|
10
|
+
: loopInside(this.object, cfg.self);
|
11
|
+
};
|
12
|
+
Generator.prototype.db = function (cfg) {
|
13
|
+
var db = this.DB;
|
14
|
+
if (cfg.eval) {
|
15
|
+
return eval('db.' + cfg.db);
|
16
|
+
}
|
17
|
+
else {
|
18
|
+
return loopInside(this.DB, cfg.db);
|
19
|
+
}
|
20
|
+
};
|
21
|
+
Generator.prototype.custom = function (cfg) {
|
22
|
+
var db = this.DB;
|
23
|
+
var object = this.object;
|
24
|
+
var re;
|
25
|
+
var matches;
|
26
|
+
var strFn;
|
27
|
+
var generator = cfg.generator;
|
28
|
+
if (cfg.run) {
|
29
|
+
return cfg.run(generator, cfg.input);
|
30
|
+
}
|
31
|
+
else if (cfg.eval) {
|
32
|
+
return eval('generator.' + cfg.input);
|
33
|
+
}
|
34
|
+
else {
|
35
|
+
return stringToPathOrCall.call(generator, 'generator', generator, cfg.input);
|
36
|
+
}
|
37
|
+
};
|
38
|
+
Generator.prototype.eval = function (cfg) {
|
39
|
+
var db = this.DB;
|
40
|
+
var object = this.object;
|
41
|
+
var generators = this.generators;
|
42
|
+
return eval(cfg.eval);
|
43
|
+
};
|
44
|
+
Generator.prototype.values = function (cfg) {
|
45
|
+
var i = Math.floor(cfg.values.length * Math.random());
|
46
|
+
return cfg.values[i];
|
47
|
+
};
|
48
|
+
Generator.prototype.function = function (cfg) {
|
49
|
+
var _a;
|
50
|
+
var args = [];
|
51
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
52
|
+
args[_i - 1] = arguments[_i];
|
53
|
+
}
|
54
|
+
var object = this.object;
|
55
|
+
var db = this.DB;
|
56
|
+
var generators = this.generators;
|
57
|
+
return (_a = cfg.function).call.apply(_a, __spreadArray([{ object: object, db: db, generators: generators }], args, false));
|
58
|
+
};
|
59
|
+
Generator.prototype.static = function (cfg) {
|
60
|
+
return cfg.static;
|
61
|
+
};
|
62
|
+
Generator.prototype.incrementalId = function (cfg) {
|
63
|
+
var n = 0;
|
64
|
+
var db = this.DB;
|
65
|
+
if (db[this.name] && db[this.name].length) {
|
66
|
+
n = db[this.name].length;
|
67
|
+
}
|
68
|
+
if (cfg.incrementalId === true) {
|
69
|
+
cfg.incrementalId = '0';
|
70
|
+
}
|
71
|
+
return n + parseInt(cfg.incrementalId, 10);
|
72
|
+
};
|
73
|
+
Generator.prototype.hasOne = function (cfg) {
|
74
|
+
var db = this.DB;
|
75
|
+
var entity = null;
|
76
|
+
if (cfg.uniqueDB) {
|
77
|
+
var dbString = JSON.stringify(cfg.uniqueDB);
|
78
|
+
for (var i = 0; i < db[cfg.hasOne].length; i++) {
|
79
|
+
var element = db[cfg.hasOne][i];
|
80
|
+
element = cfg.get
|
81
|
+
? cfg.eval
|
82
|
+
? eval('element.' + cfg.get)
|
83
|
+
: loopInside(element, cfg.get)
|
84
|
+
: element;
|
85
|
+
if (cfg.uniqueDB.length === 0 ||
|
86
|
+
dbString.indexOf(JSON.stringify(element)) < 0) {
|
87
|
+
entity = element;
|
88
|
+
break;
|
89
|
+
}
|
90
|
+
}
|
91
|
+
if (entity === null) {
|
92
|
+
throw "Can\u00B4t get unique data. Source \"".concat(cfg.hasOne, "\" has not enough data");
|
93
|
+
}
|
94
|
+
}
|
95
|
+
else {
|
96
|
+
var i = Math.floor(db[cfg.hasOne].length * Math.random());
|
97
|
+
entity = db[cfg.hasOne][i];
|
98
|
+
entity = cfg.get
|
99
|
+
? cfg.eval
|
100
|
+
? eval('entity.' + cfg.get)
|
101
|
+
: loopInside(entity, cfg.get)
|
102
|
+
: entity;
|
103
|
+
}
|
104
|
+
return entity;
|
105
|
+
};
|
106
|
+
Generator.prototype.hasMany = function (cfg) {
|
107
|
+
var _this = this;
|
108
|
+
var amount = 1;
|
109
|
+
var db = this.DB;
|
110
|
+
var min = cfg.min || cfg.min === 0 ? cfg.min : 1;
|
111
|
+
var max = cfg.max ? cfg.max : cfg.hasMany ? db[cfg.hasMany].length : 1;
|
112
|
+
if (cfg.amount) {
|
113
|
+
amount = cfg.amount;
|
114
|
+
}
|
115
|
+
else {
|
116
|
+
amount = Math.floor(Math.random() * (max - min + 1)) + min;
|
117
|
+
}
|
118
|
+
var newCfg = {
|
119
|
+
hasOne: cfg.hasMany,
|
120
|
+
get: cfg.get ? cfg.get : undefined,
|
121
|
+
eval: cfg.eval ? true : false
|
122
|
+
};
|
123
|
+
return cfg.unique
|
124
|
+
? Array.from(new Array(amount)).reduce(function (acc, val) { return __spreadArray(__spreadArray([], acc, true), [
|
125
|
+
_this.hasOne(__assign(__assign({}, newCfg), { uniqueDB: acc }))
|
126
|
+
], false); }, [])
|
127
|
+
: Array.from(new Array(amount)).map(function () { return _this.hasOne(newCfg); });
|
128
|
+
};
|
129
|
+
return Generator;
|
130
|
+
}());
|
131
|
+
export { Generator };
|
@@ -0,0 +1,73 @@
|
|
1
|
+
import { Schema } from './Schema';
|
2
|
+
import { cleanVirtuals } from './utils';
|
3
|
+
var Mocker = /** @class */ (function () {
|
4
|
+
function Mocker(options) {
|
5
|
+
if (options === void 0) { options = {}; }
|
6
|
+
this.schemas = [];
|
7
|
+
this.DB = {};
|
8
|
+
this.options = {};
|
9
|
+
this.generators = {};
|
10
|
+
this.options = options;
|
11
|
+
this.DB = {};
|
12
|
+
}
|
13
|
+
Mocker.prototype.seed = function (name, data) {
|
14
|
+
this.DB[name] = data;
|
15
|
+
return this;
|
16
|
+
};
|
17
|
+
Mocker.prototype.addGenerator = function (name, library, run) {
|
18
|
+
this.generators[name] = { library: library, run: run };
|
19
|
+
return this;
|
20
|
+
};
|
21
|
+
Mocker.prototype.schema = function (name, schema, options) {
|
22
|
+
this.schemas.push(new Schema(name, schema, options));
|
23
|
+
return this;
|
24
|
+
};
|
25
|
+
Mocker.prototype.reset = function () {
|
26
|
+
this.DB = {};
|
27
|
+
return this;
|
28
|
+
};
|
29
|
+
Mocker.prototype.restart = function () {
|
30
|
+
this.DB = {};
|
31
|
+
this.schemas = [];
|
32
|
+
return this;
|
33
|
+
};
|
34
|
+
Mocker.prototype._buildSync = function () {
|
35
|
+
var _this = this;
|
36
|
+
this.schemas.reduce(function (acc, schema) {
|
37
|
+
var instances;
|
38
|
+
try {
|
39
|
+
instances = schema.build(_this.generators, acc);
|
40
|
+
}
|
41
|
+
catch (e) {
|
42
|
+
throw new Error('Schema: "' + schema.name + '" ' + e);
|
43
|
+
}
|
44
|
+
// Clean virtuals
|
45
|
+
if (schema.virtualPaths.length > 0) {
|
46
|
+
instances.forEach(function (x) {
|
47
|
+
return cleanVirtuals(schema.virtualPaths, x, {
|
48
|
+
strict: true,
|
49
|
+
symbol: ','
|
50
|
+
});
|
51
|
+
});
|
52
|
+
}
|
53
|
+
// Add to db
|
54
|
+
acc[schema.name] = instances;
|
55
|
+
return acc;
|
56
|
+
}, this.DB);
|
57
|
+
};
|
58
|
+
Mocker.prototype.buildSync = function () {
|
59
|
+
this._buildSync();
|
60
|
+
return this.DB;
|
61
|
+
};
|
62
|
+
Mocker.prototype.build = function (cb) {
|
63
|
+
try {
|
64
|
+
this._buildSync();
|
65
|
+
}
|
66
|
+
catch (e) {
|
67
|
+
return cb ? cb(e) : Promise.reject(e);
|
68
|
+
}
|
69
|
+
return cb ? cb(null, this.DB) : Promise.resolve(this.DB);
|
70
|
+
};
|
71
|
+
return Mocker;
|
72
|
+
}());
|
73
|
+
export { Mocker };
|