diff --git a/force-app/main/default/classes/TestDataFactory.cls b/force-app/main/default/classes/TestDataFactory.cls index 2badf59..f660b85 100644 --- a/force-app/main/default/classes/TestDataFactory.cls +++ b/force-app/main/default/classes/TestDataFactory.cls @@ -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 @@ -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 @@ -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 diff --git a/force-app/main/default/classes/TestDataFactory_Test.cls b/force-app/main/default/classes/TestDataFactory_Test.cls index 78cec47..4ef5cbe 100644 --- a/force-app/main/default/classes/TestDataFactory_Test.cls +++ b/force-app/main/default/classes/TestDataFactory_Test.cls @@ -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 } } /** @@ -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 } } @@ -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('test0@email.com',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 @@ -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 @@ -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('test0@email.com',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)); @@ -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('test0@email.com',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());