Skip to content

Commit

Permalink
feat: Add MongoDB ObjectId generation (#616)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhammond101 committed Apr 6, 2022
1 parent 20f33e6 commit a5b3888
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/database.ts
Expand Up @@ -59,4 +59,15 @@ export class Database {
this.faker.definitions.database.engine
);
}

/**
* Returns a MongoDB [ObjectId](https://docs.mongodb.com/manual/reference/method/ObjectId/) string.
*
* @example
* faker.database.mongodbObjectId() // 'e175cac316a79afdd0ad3afb'
*/
mongodbObjectId(): string {
// strip the "0x" from the hexadecimal output
return this.faker.datatype.hexadecimal(24).replace('0x', '').toLowerCase();
}
}
18 changes: 17 additions & 1 deletion test/database.spec.ts
Expand Up @@ -9,6 +9,7 @@ const seededRuns = [
type: 'smallint',
collation: 'utf8_bin',
engine: 'MEMORY',
mongodbObjectId: '8be4abdd39321ad7d3fe01ff',
},
},
{
Expand All @@ -18,6 +19,7 @@ const seededRuns = [
type: 'time',
collation: 'utf8_general_ci',
engine: 'MyISAM',
mongodbObjectId: '5c346ba075bd57f5a62b82d7',
},
},
{
Expand All @@ -27,13 +29,20 @@ const seededRuns = [
type: 'geometry',
collation: 'cp1250_general_ci',
engine: 'ARCHIVE',
mongodbObjectId: 'eadb42f0e3f4a973fab0aeef',
},
},
];

const NON_SEEDED_BASED_RUN = 5;

const functionNames = ['column', 'type', 'collation', 'engine'];
const functionNames = [
'column',
'type',
'collation',
'engine',
'mongodbObjectId',
];

describe('database', () => {
afterEach(() => {
Expand Down Expand Up @@ -95,6 +104,13 @@ describe('database', () => {
expect(faker.definitions.database.type).toContain(type);
});
});

describe('mongodbObjectId', () => {
it('should generate a MongoDB ObjectId value', () => {
const generateObjectId = faker.database.mongodbObjectId();
expect(generateObjectId).toBeTypeOf('string');
});
});
}
});
});

0 comments on commit a5b3888

Please sign in to comment.