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
49 changes: 49 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,53 @@ 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
*
* @param value When provided, and is a valid objectId, this method simply returns it as is.
*
* @example
* faker.database.objectId() // e175cac316a79afdd0ad3afb
* faker.database.objectId('6228fb6ec55ed495fc1e5f80') // 6228fb6ec55ed495fc1e5f80
*/
objectId(value?: string): string {
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
const length = 24;

if (typeof value === 'string' && value?.length === 24) {
return value;
}

// a-z0-9
const charCodeOptions = [
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
{
min: 'a'.charCodeAt(0),
max: 'f'.charCodeAt(0),
},
{
min: '0'.charCodeAt(0),
max: '9'.charCodeAt(0),
},
];

let returnString = '';

for (let i = 0; i < length; i++) {
// randomly chose if a number or letter should be selected
const charCodeOption =
charCodeOptions[
this.faker.datatype.number({
min: 0,
max: charCodeOptions.length - 1,
})
];

// converts the randomly selected UTF-16 number to the corresponding character
returnString += String.fromCharCode(
this.faker.datatype.number(charCodeOption)
);
}

return returnString;
}
}
19 changes: 18 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',
objectId: 'e175cac316a79afdd0ad3afb',
},
},
{
Expand All @@ -18,6 +19,7 @@ const seededRuns = [
type: 'time',
collation: 'utf8_general_ci',
engine: 'MyISAM',
objectId: 'dbdab5c23ea3e1f70cc53cf4',
},
},
{
Expand All @@ -27,13 +29,14 @@ const seededRuns = [
type: 'geometry',
collation: 'cp1250_general_ci',
engine: 'ARCHIVE',
objectId: '47a0124a70679b99c9ad40b0',
},
},
];

const NON_SEEDED_BASED_RUN = 5;

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

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

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

it('should return the same value if valid value is passed', () => {
const generateObjectId = faker.database.objectId(
'6228fb6ec55ed495fc1e5f80'
);
expect(generateObjectId).toBe('6228fb6ec55ed495fc1e5f80');
});
});
}
});
});