Last active
April 14, 2021 18:10
-
-
Save niavesper/91f5cdcaef0a38add64912a0ad05081c to your computer and use it in GitHub Desktop.
RecipeHandler.cls: First method checks Draft checkbox when one of the key fields is blank, which is the requirement of Week 5 homework. This is not new, it just gives context to the negative tests. RecipeHandler__Test.cls: same as week 5 homework, plus some negative tests. Negative tests start at line 151. RecipeTrigger.trigger: same as Week 5 h…
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
public with sharing class RecipeHandler { | |
public static void checkDraft(List<Recipe__c> draftRecipes) { | |
for (Recipe__c r : draftRecipes){ | |
if (r.Name == null || r.Active_Time__c == null || r.Description__c == null || r.Active_Time_Units__c == null || r.Servings__c == null){ | |
r.Draft__c = true; | |
} | |
} | |
} | |
public static void rateComplexity(List<Recipe__c> ratedRecipes) { | |
List <Recipe__c> updatedComplexityRecipes = new List <Recipe__c>(); | |
for (Recipe__c r : ratedRecipes){ | |
switch on HelperFunctions.rateRecipeComplexity (r){ | |
when 1 { | |
r.Complexity__c = 'Simple'; | |
} | |
when 2 { | |
r.Complexity__c = 'Moderate'; | |
} | |
when 3 { | |
r.Complexity__c = 'Difficult'; | |
} | |
} | |
updatedComplexityRecipes.add(r); | |
} | |
} | |
public static void createTasks(List<Recipe__c> taskRecipes) { | |
List <Task> newTasks = new List<Task>(); | |
for (Cookbook__c c : [SELECT Id, OwnerId FROM Cookbook__c | |
WHERE ID IN (SELECT Cookbook__c FROM Recipe_Usage__c | |
WHERE Recipe__r.Draft__c = false AND Recipe__r.Id IN : taskRecipes)]){ | |
Task t = new Task(); | |
t.WhatId = c.Id; | |
t.OwnerId = c.OwnerId; | |
t.ActivityDate = Date.Today().addDays(7); | |
t.Subject = 'Review recipe'; | |
t.Status = 'Not Started'; | |
newTasks.add(t); | |
} | |
insert newTasks; | |
} | |
} |
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 | |
private class RecipeHandler_Test { | |
@testSetup | |
static void dataCreation(){ | |
// Create data for testing Draft recipes | |
List <Recipe__c> draftRecipes = new List<Recipe__c>(); | |
for(integer i=0; i < 4; i++){ | |
Recipe__c r = new Recipe__c( | |
Name = 'Draft Recipe ' + i, | |
Active_Time__c = i, | |
Description__c = 'Test Description ' + i, | |
Active_Time_Units__c = 'Minutes', | |
Servings__c = i); | |
draftRecipes.add(r); | |
} | |
// assign blank values to key fields: one blank key field per record | |
draftRecipes[0].Active_Time__c = null; | |
draftRecipes[1].Description__c = null; | |
draftRecipes[2].Active_Time_Units__c = null; | |
draftRecipes[3].Servings__c = null; | |
insert draftRecipes; | |
// create data for testing recipe Complexity | |
List <Recipe__c> complexityRecipes = new List<Recipe__c>(); | |
for(integer i = 0; i < 3; i++){ | |
Recipe__c r = new Recipe__c( | |
Name = 'Complexity Recipe ' + i, | |
Active_Time__c = i * 30, | |
Active_Time_Units__c = 'Minutes', | |
Servings__c = i + 6); | |
HelperFunctions.rateRecipeComplexity (r); | |
complexityRecipes.add(r); | |
} | |
// insert data | |
insert complexityRecipes; | |
} | |
/*** positive testing for before insert of the "checkDraft" method***/ | |
@isTest | |
static void testCheckDraftCheckbox_InsertPositive(){ | |
// query inserted records | |
List <Recipe__c> insertedRecipes = [SELECT Id FROM Recipe__c WHERE Name LIKE 'Draft%']; | |
// assert: the number of recipes where Draft__c = true should be four | |
System.assertEquals(4, insertedRecipes.size(), 'The number of inserted recipes where Draft__c = true should be four'); | |
} | |
/*** positive testing for before update of the "checkDraft" method***/ | |
@isTest | |
static void testCheckDraftCheckbox_UpdatePositive(){ | |
// query inserted records | |
List <Recipe__c> insertedRecipes = [SELECT Id FROM Recipe__c WHERE Name LIKE 'Draft%']; | |
// update data | |
Test.startTest(); | |
update insertedRecipes; | |
Test.stopTest(); | |
// assert: the number of recipes where Draft__c = true should be four | |
System.assertEquals(4, insertedRecipes.size(), 'The number of updated recipes where Draft__c = true should be four'); | |
} | |
/*** positive testing for before insert of the "rateComplexity" method***/ | |
@isTest | |
static void addComplexityRating_InsertPositive(){ | |
// query ratings of inserted recipes and assign them to strings for assertion | |
String ratingZero = [SELECT Id, Complexity__c FROM Recipe__c WHERE Name = 'Complexity Recipe 0'].Complexity__c; | |
String ratingOne = [SELECT Id, Complexity__c FROM Recipe__c WHERE Name = 'Complexity Recipe 1'].Complexity__c; | |
String ratingTwo = [SELECT Id, Complexity__c FROM Recipe__c WHERE Name = 'Complexity Recipe 2'].Complexity__c; | |
// assert: compare expected values of *inserted* records to actual | |
System.assertEquals('Simple', ratingZero, 'Rating Zero should be "Simple"'); | |
System.assertEquals('Moderate', ratingOne, 'Rating One should be "Moderate"'); | |
System.assertEquals('Difficult', ratingTwo, 'Rating Tw0 should be "Difficult"'); | |
} | |
/*** positive testing for before update of the "rateComplexity" method ***/ | |
@isTest | |
static void addComplexityRating_UpdatePositive(){ | |
// Create a list of inserted recipes | |
List <Recipe__c> recipesUpdatedWithComplexity = [SELECT Id FROM Recipe__c WHERE Name LIKE 'Complexity%' AND Complexity__c != null]; | |
// update data | |
Test.startTest(); | |
update recipesUpdatedWithComplexity; | |
Test.stopTest(); | |
// query ratings of updated recipes and assign them to strings for assertion | |
String ratingZero = [SELECT Id, Complexity__c FROM Recipe__c WHERE Name = 'Complexity Recipe 0'].Complexity__c; | |
String ratingOne = [SELECT Id, Complexity__c FROM Recipe__c WHERE Name = 'Complexity Recipe 1'].Complexity__c; | |
String ratingTwo = [SELECT Id, Complexity__c FROM Recipe__c WHERE Name = 'Complexity Recipe 2'].Complexity__c; | |
// assert: compare expected values of *updated* records to actual | |
System.assertEquals('Simple', ratingZero, 'Rating Zero should be "Simple"'); | |
System.assertEquals('Moderate', ratingOne, 'Rating One should be "Moderate"'); | |
System.assertEquals('Difficult', ratingTwo, 'Rating Two should be "Difficult"'); | |
} | |
/*** positive testing for after update of the "createTasks" method***/ | |
@isTest | |
static void createTasksOnCookbook_UpdatePositive(){ | |
// create and insert test data (new non-draft Recipe and Cookbook). | |
List<sObject> recordsToInsert = new List<sObject>(); | |
recordsToInsert.add (new Recipe__c (Name = 'Task Recipe', | |
Active_Time__c = 2, | |
Active_Time_Units__c = 'Hours', | |
Description__c = 'Test Description', | |
Servings__c = 8 | |
)); | |
recordsToInsert.add (new Cookbook__c (Name = 'Test Cookbook')); | |
insert recordsToInsert; | |
// query newly created records to use their IDs on the record of the junction object | |
Recipe__c newRecipe = [SELECT Id FROM Recipe__c WHERE Name = 'Task Recipe']; | |
Cookbook__c newCookbook = [SELECT Id, OwnerId FROM Cookbook__c]; | |
// create test data (new Recipe Usage with lookups to earlier created Cookbook and Recipe) | |
Recipe_Usage__c newRecipeUsage = new Recipe_Usage__c ( | |
Cookbook__c = newCookbook.Id, | |
Recipe__c = newRecipe.Id); | |
// insert test data | |
insert newRecipeUsage; | |
// update the recipe record | |
Test.startTest(); | |
update newRecipe; | |
Test.stopTest(); | |
// assign the task that should have been created to a list | |
List<Task> newTasks = [SELECT Id FROM Task | |
WHERE WhatId = : newCookbook.Id AND | |
OwnerId = : newCookbook.OwnerId AND | |
ActivityDate = NEXT_N_DAYS:7 AND | |
Subject = 'Review recipe' AND | |
Status = 'Not Started' | |
]; | |
// assert: check to see if there is one task on the list that meets the conditions | |
System.assertEquals(1, newTasks.size(), 'One task should have been inserted'); | |
} | |
/*** negative testing for before insert of the "checkDraft" method ***/ | |
@isTest | |
static void testCheckDraftCheckbox_InsertNegative(){ | |
// Create data for testing Draft recipes | |
List <Recipe__c> draftNegativeRecipes = new List<Recipe__c>(); | |
for(integer i=0; i < 4; i++){ | |
Recipe__c r = new Recipe__c( | |
Name = 'Draft Negative Recipe ' + i, | |
Active_Time__c = i, | |
Description__c = 'Test Description ' + i, | |
Active_Time_Units__c = 'Minutes', | |
Servings__c = i); | |
draftNegativeRecipes.add(r); | |
} | |
// query inserted records where Draft__c = true | |
List <Recipe__c> insertedRecipes = [SELECT Id FROM Recipe__c WHERE Name LIKE 'Draft Negative%']; | |
// assert: the number of recipes where Draft__c = true should be zero | |
System.assertEquals(0, insertedRecipes.size(), 'The number of inserted recipes where Draft__c = true should be zero'); | |
} | |
/*** negative testing for before update of the "checkDraft" method ***/ | |
@isTest | |
static void testCheckDraftCheckbox_UpdateNegative(){ | |
// Create data for testing Draft recipes | |
List <Recipe__c> draftNegativeRecipes = new List<Recipe__c>(); | |
for(integer i=0; i < 4; i++){ | |
Recipe__c r = new Recipe__c( | |
Name = 'Draft Recipe ' + i, | |
Active_Time__c = i, | |
Description__c = 'Test Description ' + i, | |
Active_Time_Units__c = 'Minutes', | |
Servings__c = i); | |
draftNegativeRecipes.add(r); | |
} | |
// query inserted records where Draft__c = true | |
List <Recipe__c> insertedRecipes = [SELECT Id FROM Recipe__c WHERE Name LIKE 'Draft Negative%']; | |
// update data | |
Test.startTest(); | |
update insertedRecipes; | |
Test.stopTest(); | |
// assert: the number of recipes where Draft__c = true should be zero | |
System.assertEquals(0, insertedRecipes.size(), 'The number of updated recipes where Draft__c = true should be zero'); | |
} | |
/*** Negative testing for after update of the "createTasks" method ***/ | |
@isTest | |
static void createTasksOnCookbook_UpdateNegative(){ | |
// create and insert test data (new draft Recipe and Cookbook). | |
List<sObject> recordsToInsert = new List<sObject>(); | |
recordsToInsert.add (new Recipe__c (Name = 'Task Recipe')); | |
recordsToInsert.add (new Cookbook__c (Name = 'Test Cookbook')); | |
insert recordsToInsert; | |
// query newly created records to use their IDs on the record of the junction object | |
Recipe__c newRecipe = [SELECT Id FROM Recipe__c WHERE Name = 'Task Recipe']; | |
Cookbook__c newCookbook = [SELECT Id, OwnerId FROM Cookbook__c]; | |
// create test data (new Recipe Usage with lookups to earlier created Cookbook and Recipe) | |
Recipe_Usage__c newRecipeUsage = new Recipe_Usage__c ( | |
Cookbook__c = newCookbook.Id, | |
Recipe__c = newRecipe.Id); | |
// insert test data | |
insert newRecipeUsage; | |
// update the recipe record | |
Test.startTest(); | |
update newRecipe; | |
Test.stopTest(); | |
// assign the task that should have been created to a list | |
List<Task> newTasks = [SELECT Id FROM Task | |
WHERE WhatId = : newCookbook.Id AND | |
OwnerId = : newCookbook.OwnerId AND | |
ActivityDate = NEXT_N_DAYS:7 AND | |
Subject = 'Review recipe' AND | |
Status = 'Not Started' | |
]; | |
// assert: check to see if there is one task on the list that meets the conditions | |
System.assertEquals(0, newTasks.size(), 'No tasks should have been inserted'); | |
} | |
} |
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
trigger RecipeTrigger on Recipe__c (before insert, after insert, before update, after update) { | |
if (trigger.isBefore && ( trigger.isInsert || trigger.isUpdate)){ | |
RecipeHandler.checkDraft(trigger.new); | |
RecipeHandler.rateComplexity(trigger.new); | |
} | |
if(trigger.isAfter && trigger.isUpdate){ | |
RecipeHandler.createTasks(trigger.new); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I really like how organized your code is! 💯 Good job @niavesper 👏