Skip to content

Commit

Permalink
perf: efficient describe calls (#141)
Browse files Browse the repository at this point in the history
feat: enable case-insensitive uses
  • Loading branch information
dschach authored Mar 8, 2024
1 parent 7fad074 commit 3eadfcf
Show file tree
Hide file tree
Showing 11 changed files with 589 additions and 153 deletions.
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
"files": "*.{cmp,page,component}",
"options": { "parser": "html" }
},
{
"files": "*.{cls,trigger}",
"options": { "parser": "apex", "tabWidth": 2, "useTabs": true }
},
{
"files": "*.{apex,soql}",
"options": { "parser": "anonymous-apex" }
},
{
"files": "*.{yaml,yml}",
"options": { "useTabs": false, "tabWidth": 2, "singleQuote": true }
Expand Down
311 changes: 175 additions & 136 deletions force-app/main/default/classes/RecordTypes.cls

Large diffs are not rendered by default.

61 changes: 59 additions & 2 deletions force-app/main/default/classes/RecordTypesTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @author {@link [David Schach](https://github.com/dschach)}
* @since 2021 Various enhancements and updates to use new Apex methods
* @since 2023 Addition of methods to handle no profile-based RT permissions with permissions via permission set
* @since 2024 Check for case insensitivity
* @group RecordTypes
* @see RecordTypes
*/
Expand Down Expand Up @@ -107,6 +108,50 @@ private class RecordTypesTest {
Assert.isFalse(RecordTypes.getAllRecordTypesForSelectList('Account').isEmpty(), 'Should return at least single item select list');
}

/**
* @description Test against account, an object with record types in package testing
* @author Evan Callahan
*/
@IsTest
private static void testAccountRecTypesCaseInsensitive() {
//Test with account

Assert.isNotNull(RecordTypes.getRecordTypesForObject('account'), 'A real sobject should have at least Master record type');
Assert.isNull(RecordTypes.getRecordTypeIdFromName('account', 'BogusRT'), 'BogusRT record type should not exist');
Assert.isNull(RecordTypes.getRecordTypeDevNameFromName('account', 'BogusRT'), 'BogusRT record type should not exist');

Assert.isNull(RecordTypes.getRecordTypeNameFromDevName('account', 'BogusRT'), 'BogusRT record type should not exist');
Assert.isNull(RecordTypes.getRecordTypeIdFromDevName('account', 'BogusRT'), 'BogusRT record type should not exist');

Assert.isNull(RecordTypes.getRecordTypeNameFromId('account', null), 'Null record type should not exist');
Assert.isNull(RecordTypes.getRecordTypeDevNameFromId('account', null), 'Null record type should not exist');

Assert.isNull(RecordTypes.getRecordTypeIdFromDevName('account', 'BogusRT'), 'BogusRT record type should not exist');
Assert.isNull(RecordTypes.getRecordTypeIdFromName('account', 'BogusRT'), 'BogusRT record type should not exist');

Assert.isNotNull(RecordTypes.getDefaultRecordTypeName('account'), 'A real sobject should have at least Master record type');
Assert.isNotNull(RecordTypes.getDefaultRecordTypeDevName('account'), 'A real sobject should have at least Master record type');

Assert.isNotNull(RecordTypes.getRecordTypeDevNameIdMap('account'), 'A real sobject should return at least an empty map');

Assert.isNotNull(
RecordTypes.getAvailableRecordTypeDevNameIdMap('account'),
'Should return at least an empty map, and populated one if we have record types for account.'
);
Assert.isNotNull(
RecordTypes.getAvailableRecordTypeNameIdMap('account'),
'Should return at least an empty map, and a populated one if we have record types for account.'
);

Assert.isNotNull(
RecordTypes.getAvailableRecordTypesIdSet('account'),
'Should return at least an empty map, and populated one if we have record types for account.'
);

resetTest();
Assert.isFalse(RecordTypes.getAllRecordTypesForSelectList('account').isEmpty(), 'Should return at least single item select list');
}

/**
* @description Test against Solution, an object likely to have no record types
* @author Evan Callahan
Expand Down Expand Up @@ -158,6 +203,16 @@ private class RecordTypesTest {
Assert.isTrue(testSObjectRecordTypeReturns('Account'), 'Test failure checking Account');
}

/**
* @description Separate method for checking if we have Account record type, and then testing that
* @author {@link [David Schach](https://github.com/dschach)}
* @see testSObjectRecordTypeReturns
*/
@IsTest
private static void testAccountRecordTypesLowercase() {
Assert.isTrue(testSObjectRecordTypeReturns('account'), 'Test failure checking account (object name lowercase)');
}

/**
* @description Separate method for checking if we have Contact record type, and then testing that
* @author {@link [David Schach](https://github.com/dschach)}
Expand Down Expand Up @@ -186,6 +241,7 @@ private class RecordTypesTest {
*/
private static Boolean testSObjectRecordTypeReturns(String sobjectname) {
List<RecordType> queriedRecordTypes = [SELECT Id, Name, DeveloperName FROM RecordType WHERE SObjectType = :sobjectname];
//System.debug(queriedRecordTypes);

RecordTypes.getRecordTypeNameIdMap(sobjectname);
resetTest();
Expand All @@ -195,6 +251,7 @@ private class RecordTypesTest {

if (!queriedRecordTypes.isEmpty()) {
String defaultRTDevName = RecordTypes.getDefaultRecordType(sobjectName).getDeveloperName();
//System.debug(sobjectname + ' default RT DevName = ' + defaultRTDevName);
Assert.isTrue(
RecordTypes.isRecordTypeDefault(sobjectName, defaultRTDevName),
'We should have found ' + sobjectname.capitalize() + ' default RT DevName as ' + defaultRTDevName
Expand All @@ -214,8 +271,8 @@ private class RecordTypesTest {

Assert.areEqual(rti.getDeveloperName(), rti2.getDeveloperName(), 'Overloaded ' + sobjectname.capitalize() + ' method failed to retrieve same RTId');

Assert.areNotEqual(null, RecordTypes.getRecordTypeDevNameFromId(activeRT.Id), 'We should have had an' + sobjectname.capitalize() + ' RecordType');
Assert.areNotEqual(null, RecordTypes.getRecordTypeNameFromId(activeRT.Id), 'We should have had an' + sobjectname.capitalize() + ' RecordType');
Assert.areNotEqual(null, RecordTypes.getRecordTypeDevNameFromId(activeRT.Id), 'We should have had ' + sobjectname.capitalize() + ' Record Type');
Assert.areNotEqual(null, RecordTypes.getRecordTypeNameFromId(activeRT.Id), 'We should have had ' + sobjectname.capitalize() + ' Record Type');

Assert.areEqual(
activeRT.Id,
Expand Down
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions scripts/orginit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ sf org create scratch --definition-file config/project-scratch-def.json --durati
echo "Pushing metadata"
sf project deploy start --source-dir force-app
sf project deploy start --source-dir unpackaged
sf project deploy start --manifest unpackaged/manifest.xml --post-destructive-changes unpackaged/destructiveChangesPost.xml --wait 20

echo "Assigning permission set"
sf org assign permset --name RecordTypes_DefaultRT_for_Testing
Expand Down
116 changes: 116 additions & 0 deletions unpackaged/main/default/classes/UnpackagedRecordTypesTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* This class contains unit tests for validating the behavior of Apex classes
* and triggers.
*
* Unit tests are class methods that verify whether a particular piece
* of code is working properly. Unit test methods take no arguments,
* commit no data to the database, and are flagged with the testMethod
* keyword in the method definition.
*
* All test methods in an org are executed whenever Apex code is deployed
* to a production org to confirm correctness, ensure code
* coverage, and prevent regressions. All Apex classes are
* required to have at least 75% code coverage in order to be deployed
* to a production org. In addition, all triggers must have some code coverage.
*
* The @isTest class annotation indicates this class only contains test
* methods. Classes defined with the @isTest annotation do not count against
* the org size limit for all Apex scripts.
*
* See the Apex Language Reference for more information about Testing and Code Coverage.
*/
@isTest
private class UnpackagedRecordTypesTest {
/**
* @description Test against Account, an object with record types in package testing, but with lowercase RT name
* <br>Assumes we have a record type called `Default` and tests `Default`
* @author Evan Callahan
* @author David Schach
*/
@IsTest
private static void testAccountRecTypes() {
//Test with Account

Assert.isNotNull(RecordTypes.getRecordTypesForObject('Account'), 'A real sobject should have at least Master record type');
Assert.isNotNull(RecordTypes.getRecordTypeIdFromName('Account', 'Default'), 'Default record type should exist');
Assert.isNotNull(RecordTypes.getRecordTypeDevNameFromName('Account', 'Default'), 'Default record type should exist');

Assert.isNotNull(RecordTypes.getRecordTypeNameFromDevName('Account', 'Default'), 'Default record type should exist');
Assert.isNotNull(RecordTypes.getRecordTypeIdFromDevName('Account', 'Default'), 'Default record type should exist');

Assert.isNull(RecordTypes.getRecordTypeNameFromId('Account', null), 'Null record type should not exist');
Assert.isNull(RecordTypes.getRecordTypeDevNameFromId('Account', null), 'Null record type should not exist');

Assert.isNotNull(RecordTypes.getRecordTypeIdFromDevName('Account', 'Default'), 'Default record type should exist');
Assert.isNotNull(RecordTypes.getRecordTypeIdFromName('Account', 'Default'), 'Default record type should exist');

//Assert.areEqual('Default', RecordTypes.getDefaultRecordTypeName('Account'), 'Our default Account RT should be "Default"');
//Assert.areEqual('Default', RecordTypes.getDefaultRecordTypeDevName('Account'), 'Our default Account RT should be "Default"');

Assert.areEqual(
'Master',
RecordTypes.getDefaultRecordTypeName('Account'),
'Our default Account RT should be "Master" because we assigned via permission set'
);
Assert.areEqual(
'Master',
RecordTypes.getDefaultRecordTypeDevName('Account'),
'Our default Account RT should be "Master" because we assigned via permission set'
);

Assert.isNotNull(RecordTypes.getRecordTypeDevNameIdMap('Account'), 'A real sobject should return at least an empty map');

Assert.isNotNull(
RecordTypes.getAvailableRecordTypeDevNameIdMap('Account'),
'Should return at least an empty map, and populated one if we have record types for Account.'
);
Assert.isNotNull(
RecordTypes.getAvailableRecordTypeNameIdMap('Account'),
'Should return at least an empty map, and a populated one if we have record types for Account.'
);

Assert.isNotNull(
RecordTypes.getAvailableRecordTypesIdSet('Account'),
'Should return at least an empty map, and populated one if we have record types for Account.'
);
}

/**
* @description Test against Account, an object with record types in package testing, but with lowercase RT name
* <br>Assumes we have a record type called `Default` and tests `default`
* @author Evan Callahan
* @author David Schach
*/
@IsTest
private static void testAccountRecTypesLowercaseRT() {
//Test with Account

Assert.isNotNull(RecordTypes.getRecordTypesForObject('Account'), 'A real sobject should have at least Master record type');
Assert.isNotNull(RecordTypes.getRecordTypeIdFromName('Account', 'default'), 'default record type should exist');
Assert.isNotNull(RecordTypes.getRecordTypeDevNameFromName('Account', 'default'), 'default record type should exist');

Assert.isNotNull(RecordTypes.getRecordTypeNameFromDevName('Account', 'default'), 'default record type should exist');
Assert.isNotNull(RecordTypes.getRecordTypeIdFromDevName('Account', 'default'), 'default record type should exist');

Assert.isNotNull(RecordTypes.getRecordTypeIdFromDevName('Account', 'default'), 'default record type should exist');
Assert.isNotNull(RecordTypes.getRecordTypeIdFromName('Account', 'default'), 'default record type should exist');
}

/**
* @description Test for running certain methods without filling maps
*/
@IsTest
static void testMethodsAfterReset() {
resetTest();
List<RecordType> queriedRecordTypes = [SELECT Id, Name, DeveloperName FROM RecordType WHERE SObjectType = 'Account'];
Assert.isNotNull(RecordTypes.getRecordTypeNameFromId(queriedRecordTypes[0].Id), 'Error');
}

/**
* @description Clear all record type maps to enable using fewer test methods - reset within a method
* @author {@link [David Schach](https://github.com/dschach)}
*/
private static void resetTest() {
RecordTypes.clearMapsInTest();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>59.0</apiVersion>
<status>Active</status>
</ApexClass>
10 changes: 10 additions & 0 deletions unpackaged/main/default/destructiveChangesPost.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Account-Account %28Marketing%29 Layout</members>
<members>Account-Account %28Sales%29 Layout</members>
<members>Account-Account %28Support%29 Layout</members>
<name>Layout</name>
</types>
<version>60.0</version>
</Package>
Loading

0 comments on commit 3eadfcf

Please sign in to comment.