Skip to content

Commit 4d0e5c9

Browse files
committed
fix(RecipeFakerService): correctly calculate left_digits for numeric and currency fields based on precision - scale
- Updated buildNumericRecipeValueWithPrecisionAndScale and buildCurrencyRecipeValueWithPrecisionAndScale to use left_digits = precision - effectiveScale, ensuring accurate representation of digits before the decimal point. - Adjusted the corresponding test to expect left_digits=16 for a currency field with 18 precision and 2 scale, matching the correct calculation. This fixes a potential issue where total precision was mistakenly used as left_digits, leading to incorrect fake data generation.
1 parent 303b874 commit 4d0e5c9

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,19 +407,20 @@ ${this.generateTabs(5)}${randomChoicesBreakdown}`;
407407
buildNumericRecipeValueWithPrecisionAndScale(precision: number, scale?: number): string {
408408
// Handle all numeric fields (number, currency, percent) with precision and optional scale
409409
const effectiveScale = scale ?? 0;
410-
const maxValuePrecision = '9'.repeat(precision);
410+
const maxNumbersLeftOfDecimal = '9'.repeat(precision - effectiveScale);
411411

412412
if (effectiveScale === 0) {
413-
return `${this.openingRecipeSyntax}fake.random_int(min=0, max=${maxValuePrecision})${this.closingRecipeSyntax}`;
413+
return `${this.openingRecipeSyntax}fake.random_int(min=0, max=${maxNumbersLeftOfDecimal})${this.closingRecipeSyntax}`;
414414
} else {
415-
return `${this.openingRecipeSyntax}fake.pydecimal(left_digits=${maxValuePrecision}, right_digits=${effectiveScale}, positive=True)${this.closingRecipeSyntax}`;
415+
return `${this.openingRecipeSyntax}fake.pydecimal(left_digits=${maxNumbersLeftOfDecimal}, right_digits=${effectiveScale}, positive=True)${this.closingRecipeSyntax}`;
416416
}
417417
}
418418

419419
buildCurrencyRecipeValueWithPrecisionAndScale(precision: number, scale?: number): string {
420420
// Special handling for currency fields - use full precision as left_digits
421421
const effectiveScale = scale ?? 0;
422-
return `${this.openingRecipeSyntax}fake.pydecimal(left_digits=${precision}, right_digits=${effectiveScale}, positive=True)${this.closingRecipeSyntax}`;
422+
const maxNumbersLeftOfDecimal = precision - effectiveScale;
423+
return `${this.openingRecipeSyntax}fake.pydecimal(left_digits=${maxNumbersLeftOfDecimal}, right_digits=${effectiveScale}, positive=True)${this.closingRecipeSyntax}`;
423424
}
424425

425426
}

src/treecipe/src/RecipeService/tests/RecipeService.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ describe('SnowfakeryRecipeService IRecipeService Implementation Shared Intstance
136136
test('given expected currency XMLFieldDetail, returns the expected snowfakery YAML recipe value', () => {
137137

138138
const expectedXMLDetailForCurrency:XMLFieldDetail = XMLMarkupMockService.getCurrencyFieldDetail();
139-
const expectedSnowfakeryValueForCurrency = '${{fake.pydecimal(left_digits=18, right_digits=2, positive=True)}}';
139+
const expectedSnowfakeryValueForCurrency = '${{fake.pydecimal(left_digits=16, right_digits=2, positive=True)}}';
140140
const recordTypeNameByRecordTypeNameToXMLMarkup = {};
141141
const actualSnowfakeryValueForCurrency = recipeServiceWithSnow.getRecipeFakeValueByXMLFieldDetail(expectedXMLDetailForCurrency, recordTypeNameByRecordTypeNameToXMLMarkup);
142142

0 commit comments

Comments
 (0)