Skip to content

Commit 8396880

Browse files
committed
feat(recipe-faker): add scale support to numeric recipe value generation
- Renamed `buildNumericRecipeValueWithPrecision` to `buildNumericRecipeValueWithPrecisionAndScale` in IRecipeFakerService and implementations - Added optional `scale` parameter to handle decimal fields (currency, percent) with appropriate precision and decimal places - Updated FakerJS and Snowfakery services to generate integers for scale=0 and decimals for scale>0 - Modified RecipeService to pass scale for percent fields, ensuring accurate fake data generation for numeric types
1 parent 46c8648 commit 8396880

File tree

5 files changed

+29
-10
lines changed

5 files changed

+29
-10
lines changed

src/treecipe/src/RecipeFakerService.ts/FakerJSRecipeFakerService/FakerJSRecipeFakerService.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,19 @@ ${this.generateTabs(5)}${randomChoicesBreakdown}`;
512512
return `${this.openingRecipeSyntax} faker.lorem.text(${length}).substring(0, ${length}) ${this.closingRecipeSyntax}`;
513513
}
514514

515-
buildNumericRecipeValueWithPrecision(precision: number): string {
516-
// For numeric fields, precision typically represents total digits, but we can use it to limit the range
517-
const maxValue = Math.pow(10, precision) - 1;
518-
return `${this.openingRecipeSyntax} faker.number.int({min: 0, max: ${maxValue}}) ${this.closingRecipeSyntax}`;
515+
buildNumericRecipeValueWithPrecisionAndScale(precision: number, scale?: number): string {
516+
// Handle all numeric fields (number, currency, percent) with precision and optional scale
517+
const effectiveScale = scale ?? 0;
518+
519+
if (effectiveScale === 0) {
520+
// Integer fields (number with scale=0, or scale not specified)
521+
const maxValue = Math.pow(10, precision) - 1;
522+
return `${this.openingRecipeSyntax} faker.number.int({min: 0, max: ${maxValue}}) ${this.closingRecipeSyntax}`;
523+
} else {
524+
// Decimal fields (currency, percent with scale > 0)
525+
const maxValue = Math.pow(10, precision - effectiveScale) - Math.pow(10, -effectiveScale);
526+
return `${this.openingRecipeSyntax} faker.finance.amount({min: 0, max: ${maxValue}, dec: ${effectiveScale}}) ${this.closingRecipeSyntax}`;
527+
}
519528
}
520529

521530
}

src/treecipe/src/RecipeFakerService.ts/IRecipeFakerService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ export interface IRecipeFakerService {
2929
getMultipicklistTODOPlaceholderWithExample(): string
3030
getStandardAndGlobalValueSetTODOPlaceholderWithExample(): string
3131
buildTextRecipeValueWithLength(length: number): string
32-
buildNumericRecipeValueWithPrecision(precision: number): string
32+
buildNumericRecipeValueWithPrecisionAndScale(precision: number, scale?: number): string
3333
}

src/treecipe/src/RecipeFakerService.ts/SnowfakeryRecipeFakerService/SnowfakeryRecipeFakerService.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,19 @@ ${this.generateTabs(5)}${randomChoicesBreakdown}`;
404404
return `${this.openingRecipeSyntax}fake.text(max_nb_chars=${length})${this.closingRecipeSyntax}`;
405405
}
406406

407-
buildNumericRecipeValueWithPrecision(precision: number): string {
408-
// For numeric fields, precision typically represents total digits, but we can use it to limit the range
409-
const maxValue = Math.pow(10, precision) - 1;
410-
return `${this.openingRecipeSyntax}fake.random_int(min=0, max=${maxValue})${this.closingRecipeSyntax}`;
407+
buildNumericRecipeValueWithPrecisionAndScale(precision: number, scale?: number): string {
408+
// Handle all numeric fields (number, currency, percent) with precision and optional scale
409+
const effectiveScale = scale ?? 0;
410+
411+
if (effectiveScale === 0) {
412+
// Integer fields (number with scale=0, or scale not specified)
413+
const maxValue = Math.pow(10, precision) - 1;
414+
return `${this.openingRecipeSyntax}fake.random_int(min=0, max=${maxValue})${this.closingRecipeSyntax}`;
415+
} else {
416+
// Decimal fields (currency, percent with scale > 0)
417+
const maxValue = Math.pow(10, precision - effectiveScale) - Math.pow(10, -effectiveScale);
418+
return `${this.openingRecipeSyntax}fake.pydecimal(left_digits=${precision - effectiveScale}, right_digits=${effectiveScale}, positive=True)${this.closingRecipeSyntax}`;
419+
}
411420
}
412421

413422
}

src/treecipe/src/RecipeService/RecipeService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class RecipeService {
125125
case 'percent':
126126

127127
if (xmlFieldDetail.precision) {
128-
fakeRecipeValue = this.fakerService.buildNumericRecipeValueWithPrecision(xmlFieldDetail.precision);
128+
fakeRecipeValue = this.fakerService.buildNumericRecipeValueWithPrecisionAndScale(xmlFieldDetail.precision, xmlFieldDetail.scale);
129129
return fakeRecipeValue;
130130
}
131131
// Fall through to default if no precision

src/treecipe/src/XMLProcessingService/XMLFieldDetail.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ export class XMLFieldDetail {
1111
public xmlMarkup: string;
1212
public isStandardValueSet?: boolean;
1313
public precision?: number;
14+
public scale?: number;
1415
public length?: number;
1516
}

0 commit comments

Comments
 (0)