Last active
November 17, 2022 10:37
-
-
Save albarivas/401703caa55d4661ad53916740a74633 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 with sharing class RecipeTriggerTest { | |
@isTest static void fieldsAreRequired() { | |
Recipe__c recipe = new Recipe__c(); | |
String exceptionMessage; | |
try { | |
Test.startTest(); | |
insert recipe; | |
Test.stopTest(); | |
} catch (Exception e) { | |
exceptionMessage = e.getMessage(); | |
} | |
System.assert(exceptionMessage.contains('This recipe is missing required values')); | |
} | |
@isTest static void fieldsAreRequired2() { | |
// GIVEN | |
Recipe__c recipe = new Recipe__c(); | |
try { | |
// WHEN | |
Test.startTest(); | |
insert recipe; | |
Test.stopTest(); | |
System.assert(false, 'Exception expected'); | |
} catch (Exception e) { | |
// THEN | |
System.assert(e.getMessage().contains('This recipe is missing required values')); | |
} | |
} | |
// Check that task is created if the recipe is not marked as Draft | |
@isTest | |
static void taskCreatedIfNoDraft(){ | |
// GIVEN - Test data factory | |
// Create recipe that is marked as draft | |
Recipe__c recipe = new Recipe__c(); | |
recipe.Name = 'Vegetarian Lasagne'; | |
recipe.Active_Time__c = 3; | |
recipe.Description__c = 'This is Really Good'; | |
recipe.Active_Time_Units__c = 'Hours'; | |
recipe.Servings__c = 8; | |
recipe.Draft__c = false; | |
insert recipe; | |
// Create cookbook | |
Cookbook__c cookbook = new Cookbook__c(); | |
cookbook.Name = 'Greatest Cookbook Ever'; | |
insert cookbook; | |
// Create recipe usage for the recipe | |
Recipe__c recipeToUse = [SELECT Id, Name FROM Recipe__c]; | |
Recipe_Usage__c recipeUsage = new Recipe_Usage__c(); | |
recipeUsage.Cookbook__c = cookbook.Id; | |
recipeUsage.Recipe__c = recipeToUse.Id; | |
insert recipeUsage; | |
// WHEN | |
Test.startTest(); | |
update recipeToUse; | |
Test.stopTest(); | |
// THEN | |
List<Task> reviewTasks = [SELECT Id, Subject FROM Task WHERE WhatId = :cookbook.Id]; | |
System.assert(reviewTasks.size() == 1, 'There should be a task here!'); | |
} | |
// Check that no task is created if the recipe is marked as Draft | |
@isTest | |
static void noTaskCreatedIfDraft(){ | |
// Create recipe that is marked as draft | |
Recipe__c recipe = new Recipe__c(); | |
recipe.Name = 'Vegetarian Lasagne'; | |
recipe.Active_Time__c = 3; | |
recipe.Description__c = 'This is Really Good'; | |
recipe.Active_Time_Units__c = 'Hours'; | |
recipe.Servings__c = 8; | |
recipe.Draft__c = true; | |
insert recipe; | |
// Create cookbook | |
Cookbook__c cookbook = new Cookbook__c(); | |
cookbook.Name = 'Greatest Cookbook Ever'; | |
insert cookbook; | |
// Create recipe usage for the recipe | |
Recipe__c recipeToUse = [SELECT Id, Name FROM Recipe__c]; | |
Recipe_Usage__c recipeUsage = new Recipe_Usage__c(); | |
recipeUsage.Cookbook__c = cookbook.Id; | |
recipeUsage.Recipe__c = recipeToUse.Id; | |
insert recipeUsage; | |
Test.startTest(); | |
update recipeToUse; | |
Test.stopTest(); | |
List<Task> reviewTasks = [SELECT Id, Subject FROM Task WHERE WhatId = :cookbook.Id]; | |
System.assert(reviewTasks.size() == 0, 'There should not be a task here!'); | |
} | |
} |
This file contains hidden or 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 RecipeUtilsAlba { | |
List<Recipe__c> newRecipes; | |
Map<Id, Recipe__c> oldRecipesMap; | |
public RecipeUtilsAlba(List<Recipe__c> newRecipesVar, Map<Id, Recipe__c> oldRecipesMapVar) { | |
this.newRecipes = newRecipesVar; | |
this.oldRecipesMap = oldRecipesMapVar; | |
} | |
public void setComplexity(Recipe__c recipe) { | |
Integer complexity = HelperFunctions.rateRecipeComplexity(recipe); | |
if (complexity == 1) { | |
recipe.Complexity__c = 'Simple'; | |
} else if (complexity == 2) { | |
recipe.Complexity__c = 'Moderate'; | |
} else { | |
recipe.Complexity__c = 'Difficult'; | |
} | |
} | |
public void handleBeforeInsert() { | |
for (Recipe__c recipe : this.newRecipes) { | |
setDraftStatus(recipe); | |
setComplexity(recipe); | |
} | |
} | |
public void handleBeforeUpdate() { | |
for (Recipe__c recipe : this.newRecipes) { | |
setDraftStatus(recipe); | |
setComplexity(recipe); | |
} | |
} | |
public void handleAfterUpdate() { | |
createTasksForRecipes(this.newRecipes); | |
} | |
public static void setDraftStatus(Recipe__c recipe) { | |
/* On week 5: | |
Boolean setAsDraft = ( | |
recipe.Active_Time__c == null || | |
recipe.Servings__c == null || | |
String.isBlank(recipe.Name) || | |
String.isBlank(recipe.Description__c) || | |
String.isBlank(recipe.Active_Time_Units__c) | |
); | |
recipe.Draft__c = setAsDraft; | |
*/ | |
// On week 7 | |
if ( | |
recipe.Active_Time__c == null || | |
recipe.Servings__c == null || | |
String.isBlank(recipe.Name) || | |
String.isBlank(recipe.Description__c) || | |
String.isBlank(recipe.Active_Time_Units__c) | |
) { | |
recipe.addError('This recipe is missing required values'); | |
} | |
} | |
public void createTasksForRecipes(List<Recipe__c> recipes) { | |
// Filter out draft recipes | |
Set<Id> notDraftRecipes = new Set<Id>(); | |
for (Recipe__c recipe: recipes) { | |
if (!recipe.Draft__c) { | |
notDraftRecipes.add(recipe.Id); | |
} | |
} | |
Map<Id, Recipe__c> recipesWithRecipeUsage = new Map<Id, Recipe__c>( | |
[SELECT Id, (SELECT Cookbook__r.Id, Cookbook__r.OwnerId FROM Recipe_Usages__r ) | |
FROM Recipe__c | |
WHERE Id IN: notDraftRecipes] | |
); | |
List<Task> newTasks = new List<Task>(); | |
Set<Id> cookbooksWithTasks = new Set<Id>(); | |
for (Recipe__c newRecipe : this.newRecipes) { | |
// Criteria (which records need a task?) | |
// If the recipe is not a draft and | |
// If the recipe is used in at least one cookbook | |
Recipe__c recipeToCheck = recipesWithRecipeUsage.get(newRecipe.Id); | |
if (recipeToCheck != null && recipeToCheck.Recipe_Usages__r.size() > 0) { | |
// Create a Review task for each cookbook in which this recipe appears | |
for (Recipe_Usage__c usage : recipeToCheck.Recipe_Usages__r) { | |
if (!cookbooksWithTasks.contains(usage.Cookbook__r.Id)) { | |
// Assign to the user who owns the cookbook | |
// Make the task due-date 1 week from today | |
Task task = new Task(); | |
task.OwnerId = usage.Cookbook__r.OwnerId; | |
task.WhatId = usage.Cookbook__r.Id; | |
task.ActivityDate = date.today().addDays(7); | |
cookbooksWithTasks.add(usage.Cookbook__r.Id); | |
newTasks.add(task); | |
} | |
} | |
} | |
} | |
insert newTasks; | |
} | |
} |
This file contains hidden or 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 RecipeUtilsAlba2 { | |
List<Recipe__c> newRecipes; | |
Map<Id, Recipe__c> oldRecipesMap; | |
public RecipeUtilsAlba(List<Recipe__c> newRecipesVar, Map<Id, Recipe__c> oldRecipesMapVar) { | |
this.newRecipes = newRecipesVar; | |
this.oldRecipesMap = oldRecipesMapVar; | |
} | |
public void setComplexity(Recipe__c recipe) { | |
Integer complexity = HelperFunctions.rateRecipeComplexity(recipe); | |
if (complexity == 1) { | |
recipe.Complexity__c = 'Simple'; | |
} else if (complexity == 2) { | |
recipe.Complexity__c = 'Moderate'; | |
} else { | |
recipe.Complexity__c = 'Difficult'; | |
} | |
} | |
public void handleBeforeInsert() { | |
for (Recipe__c recipe : this.newRecipes) { | |
setDraftStatus(recipe); | |
setComplexity(recipe); | |
} | |
} | |
public void handleBeforeUpdate() { | |
for (Recipe__c recipe : this.newRecipes) { | |
setDraftStatus(recipe); | |
setComplexity(recipe); | |
} | |
} | |
public void handleAfterUpdate() { | |
createTasksForRecipes(this.newRecipes); | |
} | |
public static void setDraftStatus(Recipe__c recipe) { | |
Boolean setAsDraft = ( | |
recipe.Active_Time__c == null || | |
recipe.Servings__c == null || | |
String.isBlank(recipe.Name) || | |
String.isBlank(recipe.Description__c) || | |
String.isBlank(recipe.Active_Time_Units__c) | |
); | |
recipe.Draft__c = setAsDraft; | |
} | |
public void createTasksForRecipes(List<Recipe__c> recipes) { | |
// Filter out draft recipes | |
Set<Id> notDraftRecipes = new Set<Id>(); | |
for (Recipe__c recipe: recipes) { | |
if (!recipe.Draft__c) { | |
notDraftRecipes.add(recipe.Id); | |
} | |
} | |
Map<Id, Recipe__c> recipesWithRecipeUsage = new Map<Id, Recipe__c>( | |
[SELECT Id, (SELECT Cookbook__r.Id, Cookbook__r.OwnerId FROM Recipe_Usages__r ) | |
FROM Recipe__c | |
WHERE Id IN: notDraftRecipes] | |
); | |
// Find unique cookbooks that need task | |
List<Task> newTasks = new List<Task>(); | |
Set<Cookbook__c> cookbooksNeedingTask = new Set<Cookbook__c>(); | |
for (Recipe__c newRecipe : this.newRecipes) { | |
Recipe__c recipeToCheck = recipesWithRecipeUsage.get(newRecipe.Id); | |
if (recipeToCheck != null && recipeToCheck.Recipe_Usages__r.size() > 0) { | |
for (Recipe_Usage__c usage : recipeToCheck.Recipe_Usages__r) { | |
cookbooksNeedingTask.add(usage.Cookbook__r); | |
} | |
} | |
} | |
// Create tasks | |
for (Cookbook__c cookbook :cookbooksNeedingTask) { | |
Task task = new Task(); | |
task.OwnerId = cookbook.OwnerId; | |
task.WhatId = cookbook.Id; | |
task.ActivityDate = date.today().addDays(7); | |
newTasks.add(task); | |
} | |
insert newTasks; | |
} | |
} |
This file contains hidden or 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 RecipeUtilsTyra { | |
List<Recipe__c> newRecipes; | |
Map<Id, Recipe__c> oldRecipesMap; | |
public RecipeUtilsTyra(List<Recipe__c> newRecipes, Map<Id, Recipe__c> oldRecipesMap) { | |
this.newRecipes = newRecipes; | |
this.oldRecipesMap = oldRecipesMap; | |
} | |
public void setComplexity(Recipe__c recipe) { | |
Integer complexity = HelperFunctions.rateRecipeComplexity(recipe); | |
if (complexity == 1) { | |
recipe.Complexity__c = 'Simple'; | |
} else if (complexity == 2) { | |
recipe.Complexity__c = 'Moderate'; | |
} else { | |
recipe.Complexity__c = 'Difficult'; | |
} | |
} | |
public void handleBeforeInsert() { | |
for (Recipe__c recipe : this.newRecipes) { | |
setDraftStatus(recipe); | |
setComplexity(recipe); | |
} | |
} | |
public void handleBeforeUpdate() { | |
for (Recipe__c recipe : this.newRecipes) { | |
setDraftStatus(recipe); | |
setComplexity(recipe); | |
} | |
} | |
public void handleAfterUpdate() { | |
createTasksForRecipes(this.newRecipes); | |
} | |
public static void setDraftStatus(Recipe__c recipe) { | |
Boolean setAsDraft = ( | |
recipe.Active_Time__c == null || | |
recipe.Servings__c == null || | |
String.isBlank(recipe.Name) || | |
String.isBlank(recipe.Description__c) || | |
String.isBlank(recipe.Active_Time_Units__c) | |
); | |
recipe.Draft__c = setAsDraft; | |
} | |
public static void createTasksForRecipes(List<Recipe__c> recipes) { | |
List<Task> newTasks = new List<Task>(); | |
Set<Id> cookBookIds = new Set<Id>(); | |
Set<Id> notDraftRecipes = new Set<Id>(); | |
for (Recipe__c recipe: recipes) { | |
if (!recipe.Draft__c) { | |
notDraftRecipes.add(recipe.Id); | |
} | |
} | |
List<Recipe_Usage__c> recipeUsages = [SELECT Recipe__c, CookBook__c from Recipe_Usage__c where Recipe__c IN :notDraftRecipes]; | |
for(Recipe_Usage__c usage: recipeUsages) { | |
cookBookIds.add(usage.CookBook__c); | |
} | |
List<CookBook__c> cookBooks = [SELECT Id, OwnerId from Cookbook__c WHERE Id in :cookBookIds]; | |
for (CookBook__c book: cookBooks) { | |
Task task = new Task(); | |
task.OwnerId = book.OwnerId; | |
task.WhatId = book.Id; | |
task.ActivityDate = date.today().addDays(7); | |
newTasks.add(task); | |
} | |
insert newTasks; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment