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

Add validation for unique or External Id Text field #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions force-app/main/default/classes/TestDataFactory.cls
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,26 @@ public class TestDataFactory {
&& fieldDesc.isCreateable(); // value can be set on creation
}

/**
* @description check if a field is unique
* @param fieldDesc (Schema.DescribeFieldResult): field describe information
* @return true if a value should be unique
*/
@TestVisible
private Boolean isUniqueValueRequired(Schema.DescribeFieldResult fieldDesc){
return
fieldDesc?.isUnique() == true
|| fieldDesc?.isExternalID() == true;
}

/**
* @description return the unique 6 digits Integer
* @return unique Integer
*/
private Integer getUniqueNumber() {
return Integer.valueOf(Math.random()*1000000);
}

/**
* @description generate a default value for a given field of an sObject
* depending on the field type, the corresponding get{FieldType}DefaultValue method will be invoked to retrieve the default value
Expand Down Expand Up @@ -1121,7 +1141,7 @@ public class TestDataFactory {
* @return email default value
*/
public virtual String getEmailDefaultValue(Schema.DescribeSObjectResult sObjectDesc, Schema.DescribeFieldResult fieldDesc, Integer recordIndex){
return 'test'+recordIndex.format()+'@email.com';
return 'test'+recordIndex.format()+getUniqueNumber()+'@email.com';
}
/**
* @description get the default value for Geolocation field type
Expand Down Expand Up @@ -1191,7 +1211,10 @@ public class TestDataFactory {
* @return text default value
*/
public virtual String getTextDefaultValue(Schema.DescribeSObjectResult sObjectDesc, Schema.DescribeFieldResult fieldDesc, Integer recordIndex){
return 'test'+recordIndex.format();
String textValue = 'test' + recordIndex.format();
if (isUniqueValueRequired(fieldDesc)) {
textValue = getUniqueNumber() + textValue;
}
}
/**
* @description get the default value for TextArea field type
Expand Down
15 changes: 11 additions & 4 deletions force-app/main/default/classes/TestDataFactory_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,8 @@ public class TestDataFactory_Test {
System.assertEquals('test',conList.get(0).Account.Name);
}catch(DmlException e) {
System.assert (true); // if missing required fields
}catch(TestDataFactory.TestDataFactoryException e) {
System.assert (true); // if missing required fields
}
}
/**
Expand Down Expand Up @@ -566,6 +568,8 @@ public class TestDataFactory_Test {
System.assertNotEquals(null,accList.get(0).Id);
}catch(DmlException e) {
System.assert (true); // if missing required fields
}catch(TestDataFactory.TestDataFactoryException e) {
System.assert (true); // if missing required fields
}
}

Expand Down Expand Up @@ -1011,7 +1015,8 @@ public class TestDataFactory_Test {
System.assertEquals(0,dvProvider.getDefaultValue(Lead.SObjectType.getDescribe(),Lead.fields.AnnualRevenue.getDescribe(),0)); // currency
System.assertEquals(Date.today(),dvProvider.getDefaultValue(Account.SObjectType.getDescribe(),Account.fields.LastActivityDate.getDescribe(),0)); // date
System.assertEquals(Datetime.now(),dvProvider.getDefaultValue(Account.SObjectType.getDescribe(),Account.fields.CreatedDate.getDescribe(),0)); // datetime
System.assertEquals('[email protected]',dvProvider.getDefaultValue(Contact.SObjectType.getDescribe(),Contact.fields.Email.getDescribe(),0)); // email
System.assert(String.valueOf(dvProvider.getDefaultValue(Contact.SObjectType.getDescribe(),Contact.fields.Email.getDescribe(),0)).startsWith('test0')); // email
System.assert(String.valueOf(dvProvider.getDefaultValue(Contact.SObjectType.getDescribe(),Contact.fields.Email.getDescribe(),0)).endsWith('@email.com')); // email
// no standard geolocation field
System.assertEquals(0,dvProvider.getDefaultValue(Attachment.SObjectType.getDescribe(),Attachment.fields.BodyLength.getDescribe(),0)); // number
System.assertEquals(0,dvProvider.getDefaultValue(Asset.SObjectType.getDescribe(),Asset.fields.Quantity.getDescribe(),0)); // number
Expand All @@ -1022,7 +1027,7 @@ public class TestDataFactory_Test {
System.assertEquals('test0',dvProvider.getDefaultValue(Contact.SObjectType.getDescribe(),Contact.fields.FirstName.getDescribe(),0)); // text
System.assertEquals('test0',dvProvider.getDefaultValue(Account.SObjectType.getDescribe(),Account.fields.Description.getDescribe(),0)); // textarea
// no standard time field
System.assertEquals('http://test0.com',dvProvider.getDefaultValue(Account.SObjectType.getDescribe(),Account.fields.Website.getDescribe(),0));
System.assertEquals('http://test0.com',dvProvider.getDefaultValue(Account.SObjectType.getDescribe(),Account.fields.Website.getDescribe(),0)); // url
System.assertEquals(null,dvProvider.getDefaultValue(null,null,0));
}
@isTest
Expand All @@ -1033,7 +1038,8 @@ public class TestDataFactory_Test {
System.assertEquals(0,dvProvider.getCurrencyDefaultValue(null,null,0));
System.assertEquals(Date.today(),dvProvider.getDateDefaultValue(null,null,0));
System.assertEquals(Datetime.now(),dvProvider.getDateTimeDefaultValue(null,null,0));
System.assertEquals('[email protected]',dvProvider.getEmailDefaultValue(null,null,0));
System.assert(String.valueOf(dvProvider.getEmailDefaultValue(null,null,0)).startsWith('test0'));
System.assert(String.valueOf(dvProvider.getEmailDefaultValue(null,null,0)).endsWith('@email.com'));
System.assertNotEquals(null,dvProvider.getGeolocationDefaultValue(null,null,0));
System.assertEquals(0,dvProvider.getNumberDefaultValue(null,null,0));
System.assertEquals(0,dvProvider.getPercentDefaultValue(null,null,0));
Expand Down Expand Up @@ -1076,7 +1082,8 @@ public class TestDataFactory_Test {
TestDataFactory.IFieldDefaultValue fieldDV8 = new TestDataFactory.FieldDefaultValue(new TestDataFactory.DefaultValueProvider(),Asset.SObjectType.getDescribe(),Asset.fields.Quantity.getDescribe());
System.assertEquals(0,fieldDV8.getValue(0));
TestDataFactory.IFieldDefaultValue fieldDV9 = new TestDataFactory.FieldDefaultValue(new TestDataFactory.DefaultValueProvider(),Contact.SObjectType.getDescribe(),Contact.fields.Email.getDescribe());
System.assertEquals('[email protected]',fieldDV9.getValue(0));
System.assert(String.valueOf(fieldDV9.getValue(0)).startsWith('test0'));
System.assert(String.valueOf(fieldDV9.getValue(0)).endsWith('@email.com'));
TestDataFactory.IFieldDefaultValue fieldDV10 = new TestDataFactory.FieldDefaultValue(new TestDataFactory.DefaultValueProvider(),Account.SObjectType.getDescribe(),Account.fields.Id.getDescribe());
System.assertEquals(null,fieldDV10.getValue(0));
TestDataFactory.IFieldDefaultValue fieldDV11 = new TestDataFactory.FieldDefaultValue(new TestDataFactory.DefaultValueProvider(),Attachment.SObjectType.getDescribe(),Attachment.fields.BodyLength.getDescribe());
Expand Down