Skip to content

Instantly share code, notes, and snippets.

@niavesper
Last active April 14, 2021 15:55
Show Gist options
  • Save niavesper/0cc4fa26117a82947e9edae251df33cb to your computer and use it in GitHub Desktop.
Save niavesper/0cc4fa26117a82947e9edae251df33cb to your computer and use it in GitHub Desktop.
@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%' AND Draft__c = TRUE];
// 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%' AND Draft__c = TRUE];
// 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%' AND Draft__c = TRUE];
// 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%'AND Draft__c = TRUE];
// 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');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment