Last active
August 29, 2015 14:16
-
-
Save miragedeb/c758bce9f4200519dd33 to your computer and use it in GitHub Desktop.
Test Class for Extravagant Record Creation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
public class TestLimitCasesBulk { | |
static testMethod void testLimitCasesBulk() { | |
//Principle #1: Create records from scratch | |
//1. Create the custom setting | |
Max_Cases__c maxNoCases = new Max_Cases__c(); | |
maxNoCases.MaxCases__c = 250; | |
maxNoCases.Name = 'maxcasesetting'; | |
insert maxNoCases; | |
//2. Insert 20 users | |
Profile p = [SELECT Id FROM Profile WHERE Name='Support']; | |
List<User> allUsers = new List<User>(); | |
for(Integer i=0; i<20; i++) { | |
User newUser = new User(); | |
newUser .FirstName = 'Elsa' + i; | |
newUser .LastName = 'of Arendelle + i'; | |
newUser .Email = 'elsa@disney.com' + i; | |
newUser .Alias = 'asasa' + i; | |
newUser .Username = 'mankypanky' + i + '@clearstructure.com'; | |
newUser .LocaleSidKey = 'en_US'; | |
newUser .TimeZoneSidKey = 'GMT'; | |
newUser .ProfileID = p.Id; | |
newUser .LanguageLocaleKey = 'en_US'; | |
newUser .EmailEncodingKey = 'UTF-8'; | |
allUsers.add(newUser); | |
} | |
insert allUsers; | |
//Insert 250 cases for first x users | |
List<Case> allCases = new List<Case>(); | |
Integer firstCount = Integer.valueOf(Math.random() * 10) ; | |
for(Integer i=0; i<firstCount; i++) { | |
for(Integer j=0; j<250; j++) { | |
Case newCase = new Case(); | |
newCase.Subject = 'Issue' + j; | |
newCase.Description = 'Description' +j; | |
newCase.Status = 'Open'; | |
newCase.OwnerId = allUsers[i].Id; | |
allCases.add(newCase); | |
} | |
} | |
insert allCases; | |
//Insert upto 250 cases for rest of the users | |
List<Case> allCasesNew = new List<Case>(); | |
for(Integer i=firstCount; i<20; i++) { | |
Integer count = Integer.valueOf(Math.random() * 250); | |
for(Integer j=0; j<count ; j++) { | |
Case newCase = new Case(); | |
newCase.Subject = 'Issue' + j; | |
newCase.Description = 'Description' +j; | |
newCase.Status = 'Open'; | |
newCase.OwnerId = allUsers[i].Id; | |
allCasesNew.add(newCase); | |
} | |
} | |
insert allCasesNew; | |
//Setup data has been entered so begin testing | |
//This will give us a new set of Governor Limits | |
Test.startTest(); | |
//Find a count of all users with cases= maxNoCases.MaxCases__c = 250 | |
Integer userCtr250 = 0; | |
List<AggregateResult> groupedResults= [SELECT OwnerId , count(Id) FROM Case | |
GROUP BY OwnerId]; | |
for(AggregateResult countCase: groupedResults) { | |
if(countCase.get('expr0')==250) { | |
userCtr250++; | |
} | |
} | |
//Insert 1 case for each of the 20 users | |
Integer successCount = 0; | |
Integer failCount = 0; | |
for(Integer i=0; i<20; i++) { | |
Case newCase = new Case(); | |
newCase.Subject = 'Issue' + i; | |
newCase.Description = 'Description' +i; | |
newCase.Status = 'Open'; | |
newCase.OwnerId = allUsers[i].Id; | |
try { | |
insert newCase; | |
successCount++; | |
} catch (Exception e) | |
{ | |
System.debug('The case couldnt be inserted my friend'); | |
failCount++; | |
} | |
} | |
//Test things that should work | |
System.assertEquals( math.abs(userCtr250-20), successCount); | |
//Test things that shouldn't work | |
System.assertEquals(userCtr250, failCount); | |
Test.stopTest(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment