- Notifications
You must be signed in to change notification settings - Fork40.7k
Open
Description
public with sharing class RecipeTriggerHandler_Test {
@isTeststatic void validateRecipes() { List<Recipe__c> newRecipes = new List<Recipe__c>(); Recipe__c recipe1 = new Recipe__c(); recipe1.Name = 'Recipe 1'; recipe1.Active_Time__c = 10; recipe1.Description__c = 'Description 1'; recipe1.Active_Time_Units__c = 'Minutes'; recipe1.Servings__c = 4; newRecipes.add(recipe1); RecipeTriggerHandler.validateRecipes(newRecipes); System.assertEquals(false, newRecipes[0].Draft__c, 'Recipe 1 should not be a draft');}@istest
private static void testComplexityPositive() {
List<Recipe__c> testRecipes = new List<Recipe__c>{ new Recipe__c( Name = 'SimpleTestRecipe', Active_Time__c = 1, Active_Time_Units__c = 'Minutes', Description__c = 'Test Description.', Servings__c = 4 ), new Recipe__c( Name = 'ModerateTestRecipe', Active_Time__c = 1, Active_Time_Units__c = 'Minutes', Description__c = 'Test Description.', Servings__c = 8 ), new Recipe__c( Name = 'DifficultTestRecipe', Active_Time__c = 3, Active_Time_Units__c = 'Hours', Description__c = 'Test Description.', Servings__c = 8 )};Test.startTest();insert testRecipes;Test.stopTest();// Query back by name to assert expected valuesMap<String, Recipe__c> results = new Map<String, Recipe__c>( [SELECT Name, Complexity__c FROM Recipe__c WHERE Name IN :new List<String>{ 'SimpleTestRecipe', 'ModerateTestRecipe', 'DifficultTestRecipe' }]);System.assertEquals('Simple', results.get('SimpleTestRecipe').Complexity__c, 'Expected Simple');System.assertEquals('Moderate', results.get('ModerateTestRecipe').Complexity__c, 'Expected Moderate');System.assertEquals('Difficult', results.get('DifficultTestRecipe').Complexity__c, 'Expected Difficult');}
@IsTeststatic void testReviewTaskPositive() { // Create Cookbook Cookbook__c book = new Cookbook__c(Name = 'TestCookbook'); insert book; // Create Recipe Recipe__c rec = new Recipe__c( Name = 'TestRecipe', Active_Time__c = 3, Active_Time_Units__c = 'Hours', Description__c = 'Test Description.', Servings__c = 8, Contains_Allergens__c = false ); insert rec; // Link recipe → cookbook Recipe_Usage__c usage = new Recipe_Usage__c( Cookbook__c = book.Id, Recipe__c = rec.Id ); insert usage; // Trigger the logic by updating to Contains_Allergens__c = true rec.Contains_Allergens__c = true; Test.startTest(); update rec; Test.stopTest(); // Query the created task List<Task> taskList = [ SELECT Id, WhatId, ActivityDate FROM Task ]; System.assertEquals(1, taskList.size(), 'One task should be created'); System.assertEquals(book.Id, taskList[0].WhatId, 'Task should be related to cookbook'); System.assertEquals(System.today().addDays(7), taskList[0].ActivityDate, 'Due date should be in 7 days');}//Negative Test
@istest
static void testNoTaskIfAlreadyTrue() {
// Create Cookbook Cookbook__c book = new Cookbook__c(Name = 'Book2'); insert book; // Recipe already has allergens = true Recipe__c rec = new Recipe__c( Name = 'Recipe2', Contains_Allergens__c = true ); insert rec; // Link recipe → cookbook insert new Recipe_Usage__c(Cookbook__c = book.Id, Recipe__c = rec.Id); // Update something unrelated rec.Description__c = 'Updated description'; Test.startTest(); update rec; Test.stopTest(); System.assertEquals( 0, [SELECT COUNT() FROM Task], 'No task should be created if allergens was already true' );}//Nagative Test 2@IsTeststatic void testNoTaskIfNotUsedInCookbook() { // Recipe initially allergens false Recipe__c rec = new Recipe__c( Name = 'RecipeNoUse', Contains_Allergens__c = false ); insert rec; // Update → set allergens to true but no usage record exists rec.Contains_Allergens__c = true; Test.startTest(); update rec; Test.stopTest(); System.assertEquals( 0, [SELECT COUNT() FROM Task], 'No task should be created if recipe is not linked to any cookbook' );}//Bulk Test@IsTeststatic void testBulkProcessing() { Cookbook__c book = new Cookbook__c(Name = 'BulkBook'); insert book; List<Recipe__c> recipes = new List<Recipe__c>(); for (Integer i = 0; i < 5; i++) { recipes.add(new Recipe__c( Name = 'BulkRecipe ' + i, Contains_Allergens__c = false )); } insert recipes; // Link all recipes to the same cookbook List<Recipe_Usage__c> usages = new List<Recipe_Usage__c>(); for (Recipe__c r : recipes) { usages.add(new Recipe_Usage__c(Cookbook__c = book.Id, Recipe__c = r.Id)); } insert usages; // Update all recipes → set allergens true for (Recipe__c r : recipes) { r.Contains_Allergens__c = true; } Test.startTest(); update recipes; Test.stopTest(); Integer countTasks = [SELECT COUNT() FROM Task]; System.assertEquals(5, countTasks, 'Should create one task per recipe in bulk mode');}}
Metadata
Metadata
Assignees
Labels
No labels