Skip to content

Commit a0ca7c9

Browse files
committed
Unrecognized
1 parent dfacd16 commit a0ca7c9

File tree

2 files changed

+182
-1
lines changed

2 files changed

+182
-1
lines changed

packages/compass-collection/src/components/mock-data-generator-modal/script-generation-utils.spec.ts

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,153 @@ describe('Script Generation', () => {
346346
}
347347
});
348348
});
349+
350+
describe('Unrecognized Field Defaults', () => {
351+
it('should use default faker method for unrecognized string fields', () => {
352+
const schema = {
353+
unknownField: {
354+
mongoType: 'string',
355+
fakerMethod: 'unrecognized',
356+
fakerArgs: [],
357+
},
358+
};
359+
360+
const result = generateScript(schema, {
361+
databaseName: 'testdb',
362+
collectionName: 'test',
363+
documentCount: 1,
364+
});
365+
366+
expect(result.success).to.equal(true);
367+
if (result.success) {
368+
expect(result.script).to.contain('faker.lorem.word()');
369+
}
370+
});
371+
372+
it('should use default faker method for unrecognized number fields', () => {
373+
const schema = {
374+
unknownNumber: {
375+
mongoType: 'number',
376+
fakerMethod: 'unrecognized',
377+
fakerArgs: [],
378+
},
379+
};
380+
381+
const result = generateScript(schema, {
382+
databaseName: 'testdb',
383+
collectionName: 'test',
384+
documentCount: 1,
385+
});
386+
387+
expect(result.success).to.equal(true);
388+
if (result.success) {
389+
expect(result.script).to.contain('faker.number.int()');
390+
}
391+
});
392+
393+
it('should use default faker method for unrecognized date fields', () => {
394+
const schema = {
395+
unknownDate: {
396+
mongoType: 'date',
397+
fakerMethod: 'unrecognized',
398+
fakerArgs: [],
399+
},
400+
};
401+
402+
const result = generateScript(schema, {
403+
databaseName: 'testdb',
404+
collectionName: 'test',
405+
documentCount: 1,
406+
});
407+
408+
expect(result.success).to.equal(true);
409+
if (result.success) {
410+
expect(result.script).to.contain('faker.date.recent()');
411+
}
412+
});
413+
414+
it('should use default faker method for unrecognized boolean fields', () => {
415+
const schema = {
416+
unknownBool: {
417+
mongoType: 'boolean',
418+
fakerMethod: 'unrecognized',
419+
fakerArgs: [],
420+
},
421+
};
422+
423+
const result = generateScript(schema, {
424+
databaseName: 'testdb',
425+
collectionName: 'test',
426+
documentCount: 1,
427+
});
428+
429+
expect(result.success).to.equal(true);
430+
if (result.success) {
431+
expect(result.script).to.contain('faker.datatype.boolean()');
432+
}
433+
});
434+
435+
it('should use default faker method for unrecognized ObjectId fields', () => {
436+
const schema = {
437+
unknownId: {
438+
mongoType: 'objectid',
439+
fakerMethod: 'unrecognized',
440+
fakerArgs: [],
441+
},
442+
};
443+
444+
const result = generateScript(schema, {
445+
databaseName: 'testdb',
446+
collectionName: 'test',
447+
documentCount: 1,
448+
});
449+
450+
expect(result.success).to.equal(true);
451+
if (result.success) {
452+
expect(result.script).to.contain('faker.database.mongodbObjectId()');
453+
}
454+
});
455+
456+
it('should use default faker method for unrecognized double fields', () => {
457+
const schema = {
458+
unknownDouble: {
459+
mongoType: 'double',
460+
fakerMethod: 'unrecognized',
461+
fakerArgs: [],
462+
},
463+
};
464+
465+
const result = generateScript(schema, {
466+
databaseName: 'testdb',
467+
collectionName: 'test',
468+
documentCount: 1,
469+
});
470+
471+
expect(result.success).to.equal(true);
472+
if (result.success) {
473+
expect(result.script).to.contain('faker.number.float()');
474+
}
475+
});
476+
477+
it('should fall back to lorem.word for unknown MongoDB types', () => {
478+
const schema = {
479+
unknownType: {
480+
mongoType: 'unknownType',
481+
fakerMethod: 'unrecognized',
482+
fakerArgs: [],
483+
},
484+
};
485+
486+
const result = generateScript(schema, {
487+
databaseName: 'testdb',
488+
collectionName: 'test',
489+
documentCount: 1,
490+
});
491+
492+
expect(result.success).to.equal(true);
493+
if (result.success) {
494+
expect(result.script).to.contain('faker.lorem.word()');
495+
}
496+
});
497+
});
349498
});

packages/compass-collection/src/components/mock-data-generator-modal/script-generation-utils.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,38 @@ function generateArrayCode(
389389
* Generate faker.js call from field mapping
390390
*/
391391
function generateFakerCall(mapping: FieldMapping): string {
392+
const method =
393+
mapping.fakerMethod === 'unrecognized'
394+
? getDefaultFakerMethod(mapping.mongoType)
395+
: mapping.fakerMethod;
396+
392397
// TODO: Handle arguments properly
393-
return `faker.${mapping.fakerMethod}()`;
398+
return `faker.${method}()`;
399+
}
400+
401+
/**
402+
* Gets default faker method for unrecognized fields based on MongoDB type
403+
*/
404+
export function getDefaultFakerMethod(mongoType: string): string {
405+
switch (mongoType.toLowerCase()) {
406+
case 'string':
407+
return 'lorem.word';
408+
case 'number':
409+
case 'int32':
410+
case 'int64':
411+
return 'number.int';
412+
case 'double':
413+
case 'decimal128':
414+
return 'number.float';
415+
case 'date':
416+
return 'date.recent';
417+
case 'objectid':
418+
return 'database.mongodbObjectId';
419+
case 'boolean':
420+
return 'datatype.boolean';
421+
case 'binary':
422+
return 'string.hexadecimal';
423+
default:
424+
return 'lorem.word';
425+
}
394426
}

0 commit comments

Comments
 (0)