Skip to content

Commit 46c8648

Browse files
committed
feat: add methods for building text and numeric recipe values with constraints
- Added buildTextRecipeValueWithLength(length: number) and buildNumericRecipeValueWithPrecision(precision: number) to IRecipeFakerService interface - Implemented methods in FakerJSRecipeFakerService and SnowfakeryRecipeFakerService to generate faker data respecting length/precision limits - Updated RecipeService to utilize new methods for improved fake data generation in text and numeric fields
1 parent bddf457 commit 46c8648

File tree

7 files changed

+100
-24
lines changed

7 files changed

+100
-24
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,18 @@ ${this.generateTabs(5)}${randomChoicesBreakdown}`;
504504
getMultipicklistTODOPlaceholderWithExample():string {
505505

506506
const emptyMultiSelectXMLDetailPlaceholder = `### TODO: POSSIBLE GLOBAL OR STANDARD VALUE SET USED FOR THIS MULTIPICKLIST AS DETAILS ARE NOT IN FIELD XML MARKUP -- FIND ASSOCIATED VALUE SET AND REPLACE COMMA SEPARATED FRUITS WITH VALUE SET OPTIONS: \${{ (faker.helpers.arrayElements(['apple', 'orange', 'banana']) ).join(';') }}`;
507-
return emptyMultiSelectXMLDetailPlaceholder;
507+
return emptyMultiSelectXMLDetailPlaceholder;
508508

509509
}
510510

511+
buildTextRecipeValueWithLength(length: number): string {
512+
return `${this.openingRecipeSyntax} faker.lorem.text(${length}).substring(0, ${length}) ${this.closingRecipeSyntax}`;
513+
}
514+
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}`;
519+
}
520+
511521
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ export interface IRecipeFakerService {
2828
controllingValue: string): string
2929
getMultipicklistTODOPlaceholderWithExample(): string
3030
getStandardAndGlobalValueSetTODOPlaceholderWithExample(): string
31-
}
31+
buildTextRecipeValueWithLength(length: number): string
32+
buildNumericRecipeValueWithPrecision(precision: number): string
33+
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,17 @@ ${this.generateTabs(5)}${randomChoicesBreakdown}`;
397397

398398
const emptyMultiSelectXMLDetailPlaceholder = `### TODO: POSSIBLE GLOBAL OR STANDARD VALUE SET USED FOR THIS MULTIPICKLIST AS DETAILS ARE NOT IN FIELD XML MARKUP -- FIND ASSOCIATED VALUE SET AND REPLACE COMMA SEPARATED FRUITS WITH VALUE SET OPTIONS: \${{ (';').join((fake.random_sample(elements=('apple', 'orange', 'banana')))) }}`;
399399
return emptyMultiSelectXMLDetailPlaceholder;
400-
400+
401+
}
402+
403+
buildTextRecipeValueWithLength(length: number): string {
404+
return `${this.openingRecipeSyntax}fake.text(max_nb_chars=${length})${this.closingRecipeSyntax}`;
405+
}
406+
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}`;
401411
}
402412

403-
}
413+
}

src/treecipe/src/RecipeService/RecipeService.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,32 +95,43 @@ export class RecipeService {
9595
return fakeRecipeValue;
9696

9797
case 'multiselectpicklist':
98-
98+
9999
if ( !(xmlFieldDetail.picklistValues) ) {
100100
// THIS SCENARIO INDICATEDS THAT THE PICKLIST FIELD UTILIZED A GLOBAL VALUE SET
101101
const emptyMultiSelectXMLDetailPlaceholder = this.fakerService.getMultipicklistTODOPlaceholderWithExample();
102102
return emptyMultiSelectXMLDetailPlaceholder;
103103
}
104104
const availablePicklistChoices = xmlFieldDetail.picklistValues.map(picklistOption => picklistOption.picklistOptionApiName);
105-
fakeRecipeValue = this.fakerService.buildMultiSelectPicklistRecipeValueByXMLFieldDetail(availablePicklistChoices,
105+
fakeRecipeValue = this.fakerService.buildMultiSelectPicklistRecipeValueByXMLFieldDetail(availablePicklistChoices,
106106
recordTypeApiToRecordTypeWrapperMap,
107107
xmlFieldDetail.apiName
108108
);
109-
109+
110110
return fakeRecipeValue;
111-
112-
// case 'masterdetail':
113-
114-
// return {
115-
// type: 'lookup'
116-
// };
117-
118-
// case 'lookup':
119-
120-
// return 'test';
121-
122-
default:
123-
111+
112+
case 'text':
113+
case 'textarea':
114+
case 'longtextarea':
115+
case 'html':
116+
117+
if (xmlFieldDetail.length) {
118+
fakeRecipeValue = this.fakerService.buildTextRecipeValueWithLength(xmlFieldDetail.length);
119+
return fakeRecipeValue;
120+
}
121+
// Fall through to default if no length
122+
123+
case 'number':
124+
case 'currency':
125+
case 'percent':
126+
127+
if (xmlFieldDetail.precision) {
128+
fakeRecipeValue = this.fakerService.buildNumericRecipeValueWithPrecision(xmlFieldDetail.precision);
129+
return fakeRecipeValue;
130+
}
131+
// Fall through to default if no precision
132+
133+
default:
134+
124135
fakeRecipeValue = this.getFakeValueIfExpectedSalesforceFieldType(fieldType);
125136
return fakeRecipeValue;
126137

@@ -296,4 +307,4 @@ ${this.generateTabs(1)}${fieldPropertAndRecipeValue}`;
296307

297308
}
298309

299-
}
310+
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ describe('SnowfakeryRecipeService IRecipeService Implementation Shared Intstance
144144

145145
});
146146

147+
test('given expected text XMLFieldDetail with length, returns the expected snowfakery YAML recipe value with length limit', () => {
148+
149+
const expectedXMLDetailForTextWithLength:XMLFieldDetail = XMLMarkupMockService.getTextXMLFieldDetailWithLength();
150+
const expectedSnowfakeryValueForTextWithLength = '${{fake.text(max_nb_chars=50)}}';
151+
const recordTypeNameByRecordTypeNameToXMLMarkup = {};
152+
const actualSnowfakeryValueForTextWithLength = recipeServiceWithSnow.getRecipeFakeValueByXMLFieldDetail(expectedXMLDetailForTextWithLength, recordTypeNameByRecordTypeNameToXMLMarkup);
153+
154+
expect(actualSnowfakeryValueForTextWithLength).toBe(expectedSnowfakeryValueForTextWithLength);
155+
156+
});
157+
147158
});
148159

149160
describe('initiateRecipeByObjectName', () => {
@@ -407,4 +418,3 @@ describe('SnowfakeryRecipeService IRecipeService Implementation Shared Intstance
407418
});
408419

409420
});
410-

src/treecipe/src/XMLProcessingService/XMLFieldDetail.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ export class XMLFieldDetail {
1010
public controllingField?: string;
1111
public xmlMarkup: string;
1212
public isStandardValueSet?: boolean;
13-
}
13+
public precision?: number;
14+
public length?: number;
15+
}

src/treecipe/src/XMLProcessingService/tests/mocks/XMLMarkupMockService.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,7 @@ export class XMLMarkupMockService {
15091509
}
15101510

15111511
static getTextXMLFieldDetail():XMLFieldDetail {
1512-
1512+
15131513
let textXMLFieldDetail: XMLFieldDetail = {
15141514
fieldType: "Text",
15151515
apiName: "Text__c",
@@ -1520,6 +1520,37 @@ export class XMLMarkupMockService {
15201520
return textXMLFieldDetail;
15211521
}
15221522

1523+
static getTextXMLFieldDetailWithLength():XMLFieldDetail {
1524+
1525+
let textXMLFieldDetail: XMLFieldDetail = {
1526+
fieldType: "Text",
1527+
apiName: "TextWithLength__c",
1528+
fieldLabel: "Text With Length",
1529+
length: 50,
1530+
xmlMarkup: this.getTextFieldTypeWithLengthXMLMarkup()
1531+
};
1532+
1533+
return textXMLFieldDetail;
1534+
}
1535+
1536+
static getTextFieldTypeWithLengthXMLMarkup():string {
1537+
const xmlTextMarkup = `
1538+
<?xml version="1.0" encoding="UTF-8"?>
1539+
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
1540+
<fullName>TextWithLength__c</fullName>
1541+
<externalId>false</externalId>
1542+
<label>Text With Length</label>
1543+
<length>50</length>
1544+
<required>false</required>
1545+
<trackTrending>false</trackTrending>
1546+
<type>Text</type>
1547+
<unique>false</unique>
1548+
</CustomField>
1549+
`;
1550+
return xmlTextMarkup;
1551+
1552+
}
1553+
15231554
static getParseStringCLEGlobalValueSetMock() {
15241555
const parseCLEGlobalAny = {
15251556
GlobalValueSet: {

0 commit comments

Comments
 (0)