11import { expect } from 'chai' ;
22import { faker } from '@faker-js/faker/locale/en' ;
3+ import { ObjectId } from 'bson' ;
34import { generateScript , generateDocument } from './script-generation-utils' ;
45import type { FakerFieldMapping } from './types' ;
56import { UNRECOGNIZED_FAKER_METHOD } from '../../modules/collection-tab' ;
@@ -26,17 +27,19 @@ function testDocumentCodeExecution(script: string): any {
2627 const returnExpression = returnMatch ! [ 1 ] ;
2728
2829 // Create a new function
29- // This is equivalent to: function(faker) { return <returnExpression>; }
30+ // This is equivalent to: function(faker, ObjectId ) { return <returnExpression>; }
3031 // The 'faker' parameter will receive the real faker.js library when we pass it in on call
32+ // The 'ObjectId' parameter will receive the BSON ObjectId constructor
3133 // eslint-disable-next-line @typescript-eslint/no-implied-eval
3234 const generateDocument = new Function (
3335 'faker' , // Parameter name for the faker library
36+ 'ObjectId' , // Parameter name for the ObjectId constructor
3437 `return ${ returnExpression } ;` // Function body: return the document structure
3538 ) ;
3639
37- // Execute the function with the real faker library
40+ // Execute the function with the real faker library and ObjectId constructor
3841 // This actually generates a document using faker methods and returns it
39- return generateDocument ( faker ) ;
42+ return generateDocument ( faker , ObjectId ) ;
4043}
4144
4245describe ( 'Script Generation' , ( ) => {
@@ -941,10 +944,49 @@ describe('Script Generation', () => {
941944
942945 expect ( result . success ) . to . equal ( true ) ;
943946 if ( result . success ) {
944- expect ( result . script ) . to . contain ( 'faker.database.mongodbObjectId ()' ) ;
947+ expect ( result . script ) . to . contain ( 'new ObjectId ()' ) ;
945948
946949 // Test that the generated document code is executable
947- testDocumentCodeExecution ( result . script ) ;
950+ const document = testDocumentCodeExecution ( result . script ) ;
951+ expect ( document ) . to . be . an ( 'object' ) ;
952+ expect ( document ) . to . have . property ( 'unknownId' ) ;
953+ expect ( document . unknownId ) . to . be . instanceOf ( ObjectId ) ;
954+ }
955+ } ) ;
956+
957+ it ( 'should use direct new ObjectId() generation for ObjectId fields' , ( ) => {
958+ const schema = {
959+ _id : {
960+ mongoType : 'ObjectId' as const ,
961+ fakerMethod : 'database.mongodbObjectId' ,
962+ fakerArgs : [ ] ,
963+ } ,
964+ movie_id : {
965+ mongoType : 'ObjectId' as const ,
966+ fakerMethod : 'database.mongodbObjectId' ,
967+ fakerArgs : [ ] ,
968+ } ,
969+ } ;
970+
971+ const result = generateScript ( schema , {
972+ databaseName : 'testdb' ,
973+ collectionName : 'test' ,
974+ documentCount : 1 ,
975+ } ) ;
976+
977+ expect ( result . success ) . to . equal ( true ) ;
978+ if ( result . success ) {
979+ // Check that ObjectId fields use direct new ObjectId() generation
980+ expect ( result . script ) . to . contain ( '_id: new ObjectId()' ) ;
981+ expect ( result . script ) . to . contain ( 'movie_id: new ObjectId()' ) ;
982+
983+ // Test that the generated document code is executable and produces ObjectId instances
984+ const document = testDocumentCodeExecution ( result . script ) ;
985+ expect ( document ) . to . be . an ( 'object' ) ;
986+ expect ( document ) . to . have . property ( '_id' ) ;
987+ expect ( document ) . to . have . property ( 'movie_id' ) ;
988+ expect ( document . _id ) . to . be . instanceOf ( ObjectId ) ;
989+ expect ( document . movie_id ) . to . be . instanceOf ( ObjectId ) ;
948990 }
949991 } ) ;
950992
0 commit comments