Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add MongoDB ObjectId generation #616

Merged
merged 10 commits into from
Apr 6, 2022
11 changes: 11 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,15 @@ export class Database {
this.faker.definitions.database.engine
);
}

/**
* Returns an [ObjectId](https://docs.mongodb.com/manual/reference/method/ObjectId/) string
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
* faker.database.mongodbObjectId() // e175cac316a79afdd0ad3afb
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
*/
mongodbObjectId(): string {
// strip the "0x" from the hexadecimal output
return this.faker.datatype.hexaDecimal(24).replace('0x', '').toLowerCase();
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
}
}
18 changes: 17 additions & 1 deletion test/database.spec.ts
Original file line number Diff line number Diff line change
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');
});
});
}
});
});