Skip to content

Commit

Permalink
internal: fake data generation with pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Oct 1, 2024
1 parent ca55e83 commit 3d39e16
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
14 changes: 2 additions & 12 deletions generators/base-application/support/prepare-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function generateFakeDataForField(this: CoreGenerator, field: Field, faker: Fake
if (field.fakerTemplate) {
data = faker.helpers.fake(field.fakerTemplate);
} else if (field.fieldValidate && field.fieldValidateRules?.includes('pattern')) {
const generated = field.generateFromPattern!();
const generated = field.generateFakeDataFromPattern!();
if (!generated) {
return undefined;
}
Expand Down Expand Up @@ -325,19 +325,14 @@ function prepareCommonFieldForTemplates(entityWithConfig: Entity, field: Field,
}

const faker = entityWithConfig.faker;
field.generateFromPattern = () => {
field.generateFakeDataFromPattern = () => {
// check if regex is valid. If not, issue warning and we skip fake data generation.
try {
new RegExp(field.fieldValidateRulesPattern!);
} catch {
generator.log.warn(`${field.fieldName} pattern is not valid: ${field.fieldValidateRulesPattern}. Skipping generating fake data. `);
return undefined;
}
try {
return faker.helpers.fromRegExp(field.fieldValidateRulesPattern!);
} catch {
// if faker fails to generate data, we will try using RandExp.
}
const re = faker.createRandexp(field.fieldValidateRulesPattern!);
if (!re) {
generator.log.warn(`Error creating generator for pattern ${field.fieldValidateRulesPattern}`);
Expand All @@ -353,11 +348,6 @@ function prepareCommonFieldForTemplates(entityWithConfig: Entity, field: Field,
generator.log.warn(`${field.fieldName} pattern is not valid: ${field.fieldValidateRulesPattern}. Skipping generating fake data. `);
return undefined;
}
try {
return faker.helpers.fromRegExp(field.fieldValidateRulesPattern!);
} catch {
// if faker fails to generate data, we will try using RandExp.
}
const re = faker.createRandexp(field.fieldValidateRulesPattern!);
if (!re) {
generator.log.warn(`Error creating generator for pattern ${field.fieldValidateRulesPattern}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,23 +331,23 @@ if (field.fieldTypeString || field.blobContentTypeText) {
const patternRegExp = new RegExp(field.fieldValidateRulesPattern);
// set infinite repetitions max range
if (!patternRegExp.test(sampleTextString.replace(/\\"/g, '"').replace(/\\\\/g, '\\'))) {
const value = field.generateFromPattern();
const value = field.generateFakeDataFromPattern();
sampleTextString = value.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
}
if (!patternRegExp.test(updatedTextString.replace(/\\"/g, '"').replace(/\\\\/g, '\\'))) {
const value = field.generateFromPattern();
const value = field.generateFakeDataFromPattern();
updatedTextString = value.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
}
} catch (error) {
this.log(this.chalkRed('Error generating test value for entity "' + entityClass +
this.log.warn('Error generating test value for entity "' + entityClass +
'" field "' + field.fieldName + '" with pattern "' + field.fieldValidateRulesPattern +
'", generating default values for this field. Detailed error message: "' + error.message + '".'));
'", generating default values for this field. Detailed error message: "' + error.message + '".');
}
if (sampleTextString === updatedTextString) {
updatedTextString = updatedTextString + "B";
this.log(this.chalkRed('Randomly generated first and second test values for entity "' + entityClass +
this.log.warn('Randomly generated first and second test values for entity "' + entityClass +
'" field "' + field.fieldName + '" with pattern "' + field.fieldValidateRulesPattern +
'" in file "' + entityClass + 'ResourceIT" where equal, added symbol "B" to second value.'));
'" in file "' + entityClass + 'ResourceIT" where equal, added symbol "B" to second value.');
}
} _%>

Expand Down
2 changes: 1 addition & 1 deletion lib/types/application/field.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface Field extends BaseField {

// Temporary fields for Faker
uniqueValue?: any[];
generateFromPattern?: () => string | undefined;
generateFakeDataFromPattern?: () => string | undefined;
/** @deprecated */
createRandexp: () => any;

Expand Down

0 comments on commit 3d39e16

Please sign in to comment.