Skip to content

Instantly share code, notes, and snippets.

@kamalpreet-rad
Created November 6, 2023 04:22
Show Gist options
  • Save kamalpreet-rad/ce1c93eb26bf94e1386062da1bcaaf4b to your computer and use it in GitHub Desktop.
Save kamalpreet-rad/ce1c93eb26bf94e1386062da1bcaaf4b to your computer and use it in GitHub Desktop.
Week Five Homework
@isTest
private class RecipeTriggerHandler_Test {
@isTest
static void testCheckRecipeInformation_Positive() {
// name, active time/units, description and servings should all be reqired on final recipes
// if any of these are missing, it should be flagged as a draft.SELECT FROM AcceptedEventRelation
// create recipe without all required fields and verify it is marked as a draft
List<Recipe__c> recList = new List<Recipe__c>();
Recipe__c rec1 = new Recipe__c (
Name='Test Recipe1'
);
Recipe__c rec2 = new Recipe__c (
Name='Test Recipe2',
Description__c = ' Test description'
);
recList.add(rec1);
recList.add(rec2);
Test.startTest();
insert recList;
Test.stopTest();
// Query for the inserted Recipe SObject
Recipe__c insertedRecipe = [SELECT ID, Draft__c FROM Recipe__c WHERE Name='Test Recipe2' LIMIT 1];
System.assertEquals(true, insertedRecipe.Draft__c, 'A Recipe without all required fields is Draft');
}
@isTest
static void testCheckRecipeInformation_Negative() {
//If a recipe is inserted WITH all required fields, it should not automatically be marked as draft
Recipe__c rec = new Recipe__c (
Name='Test Recipe',
Active_Time__c = 1,
Active_Time_Units__c = 'Minutes',
Description__c = 'Test Description.',
Servings__c = 4
);
Test.startTest();
insert rec;
Test.stopTest();
// Query for the inserted Recipe SObject
Recipe__c insertedRecipe = [SELECT ID, Draft__c FROM Recipe__c WHERE Name='Test Recipe' LIMIT 1];
System.assertEquals(false, insertedRecipe.Draft__c, 'A Recipe with all required fields should NOT be marked as Draft');
}
@isTest
static void testComplexityRate() {
List < Recipe__c > testRecipes = new List < Recipe__c >();
// Create a Recipe that will be considered Simple
Recipe__c rec1 = new Recipe__c (
Name='SimpleTestRecipe',
Active_Time__c = 1,
Active_Time_Units__c = 'Minutes',
Description__c = 'Test Description.',
Servings__c = 4
);
// Create a Recipe that will be considered Medium
Recipe__c rec2 = new Recipe__c (
Name='ModerateTestRecipe',
Active_Time__c = 1,
Active_Time_Units__c = 'Minutes',
Description__c = 'Test Description.',
Servings__c = 8
);
testRecipes.add(rec2);
// Create a Recipe that will be considered Medium
Recipe__c rec3 = new Recipe__c (
Name='DifficultTestRecipe',
Active_Time__c = 3,
Active_Time_Units__c = 'Hours',
Description__c = 'Test Description.',
Servings__c = 8
);
testRecipes.add(rec3);
Test.startTest();
Insert testRecipes;
Test.stopTest();
// Query for inserted Recipes
List < Recipe__c > recipes = [SELECT ID, Name, Complexity__c FROM Recipe__c];
for (Recipe__c rec: recipes) {
if (rec.Name=='SimpleTestRecipe') {
System.assertEquals('Simple', rec.Complexity__c, 'Recipe should be marked as Simple');
} else if (rec.Name=='ModerateTestRecipe') {
System.assertEquals('Moderate', rec.Complexity__c, 'Recipe should be marked as Moderate');
} else if (rec.Name=='DifficultTestRecipe') {
System.assertEquals('Difficult', rec.Complexity__c, 'Recipe should be marked as Difficult');
}
}
}
@isTest // create a recipe that meets the criteria of being non-draft and used in at least one cookbook
static void testCreateReviewTask_Positive() {
// Create Cookbook
Cookbook__c book = new Cookbook__c(Name='TestCookbook');
insert book;
//Test recipes records are inserted
List < Recipe__c > testRecipes = new List < Recipe__c >();
// Create Recipe
Recipe__c rec1 = new Recipe__c (
Name='TestRecipe1',
Active_Time__c = 3,
Active_Time_Units__c = 'Hours',
Description__c = 'Test Description.',
Servings__c = 8
);
testRecipes.add(rec1);
// Create Recipe
Recipe__c rec2 = new Recipe__c (
Name='TestRecipe2',
Active_Time__c = 3,
Active_Time_Units__c = 'Hours',
Description__c = 'Test Description.',
Servings__c = 8
);
testRecipes.add(rec2);
insert testRecipes;
// create junction object to indicate this recipe is in this cookbook
List < Recipe_Usage__c > usages = new List < Recipe_Usage__c >();
Recipe_Usage__c usage1 = new Recipe_Usage__c (
Cookbook__c=book.Id,
Recipe__c=rec1.Id
);
usages.add(usage1);
Recipe_Usage__c usage2 = new Recipe_Usage__c (
Cookbook__c=book.Id,
Recipe__c=rec2.Id
);
usages.add(usage2);
insert usages;
// Now update the recipe so that a review task is generated
List < Recipe__c > updatedRecipes = new List < Recipe__c >();
rec1.Contains_Allergins__c = true;
rec2.Contains_Allergins__c = true;
updatedRecipes.add(rec1);
updatedRecipes.add(rec2);
Test.startTest();
update updatedRecipes;
Test.stopTest();
// query for the task
List < Task > taskList = [SELECT ID, WhatId, ActivityDate FROM Task];
System.assertEquals(1, taskList.size(), 'There should be one task to review the cookbook.');
System.assertEquals(book.Id, taskList[0].WhatId, 'The task should be on the Cookbook record.');
System.assertEquals(System.today().addDays(7),taskList[0].ActivityDate, 'The task should be due in one week.');
}
@isTest
static void testCreateReviewTask_Negative() {
// create a recipe that meets the criteria of being non-draft and used in at least one cookbook
// Create Cookbook
Cookbook__c book = new Cookbook__c(Name='TestCookbook');
insert book;
// Create 2 Recipes that will be in the cookbook, it will be marked draft reviews as required fields are missing
List < Recipe__c > recipes = new List < Recipe__c >();
Recipe__c rec = new Recipe__c (
Name='TestRecipe',
Active_Time__c = 3,
Active_Time_Units__c = 'Hours',
Servings__c = 8
);
recipes.add(rec);
Recipe__c rec2 = new Recipe__c (
Name='TestRecipe2',
Active_Time__c = 1,
Description__c = 'Another Test Description.',
Servings__c = 4
);
recipes.add(rec2);
insert recipes;
// create junction objects to indicate these recipes are in this cookbook
List < Recipe_Usage__c > usages = new List < Recipe_Usage__c >();
Recipe_Usage__c usage = new Recipe_Usage__c (
Cookbook__c=book.Id,
Recipe__c=rec.Id
);
usages.add(usage);
Recipe_Usage__c usage2 = new Recipe_Usage__c (
Cookbook__c=book.Id,
Recipe__c=rec2.Id
);
usages.add(usage2);
insert usages;
// Now update both recipes, however since the reivew will be in draft, no task will be created
List < Recipe__c > updatedRecipes = new List < Recipe__c >();
rec.Contains_Allergins__c = true;
rec2.Contains_Allergins__c = true;
updatedRecipes.add(rec);
updatedRecipes.add(rec2);
Test.startTest();
update updatedRecipes;
Test.stopTest();
// query for the task
List < Task > taskList = [SELECT ID, WhatId FROM Task];
System.assertEquals(0, taskList.size(), 'No Task is created as there were no Review Usage records present.');
}
}
@adam17amo
Copy link

Great job overall! Just a few notes.

For testCheckRecipeInformation_Positive(), I think it makes sense to just use one record here instead of 2 since you're not doing anything with the other one. I'd also recommend using the Assert class instead of the System.assertEquals methods since it's more modern and a bit easier to read.

The next two tests look very similar to the WeekFourHomeworkCheck. It looks like in testComplexityRate(), the simple recipe isn't actually getting added to the list or being inserted. I'd also suggest not capitalizing the word "insert" in line 84 since it is an Apex keyword.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment