Last active
March 26, 2020 01:40
-
-
Save forcethesales/9252e8416732187aa701bfd94b4ce212 to your computer and use it in GitHub Desktop.
Week 6 Homework
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 RecipeTriggerHandler_Test { | |
@TestSetup | |
static void dataCreation(){ | |
// create difficult recipe | |
Recipe__c rec = new Recipe__c(); | |
rec.Name = 'Test Recipe 3'; | |
rec.Active_Time__c = 2; | |
rec.Description__c = 'recipe'; | |
rec.Active_Time_Units__c = 'Hours'; | |
rec.Servings__c = 20; | |
rec.Name__c= 'Test Recipe 3'; | |
insert rec; | |
//create moderate complexity recipe | |
Recipe__c reci = new Recipe__c(); | |
reci.Name = 'Test Recipe 2'; | |
reci.Active_Time__c = 30; | |
reci.Description__c = 'recipe'; | |
reci.Active_Time_Units__c = 'Minutes'; | |
reci.Servings__c = 5; | |
reci.Name__c= 'Test Recipe 2'; | |
insert reci; | |
//create simple complexity recipe | |
Recipe__c recip = new Recipe__c(); | |
recip.Name = 'Test Recipe 1'; | |
recip.Active_Time__c = 30; | |
recip.Description__c = 'recipe'; | |
recip.Active_Time_Units__c = 'Minutes'; | |
recip.Servings__c = 2; | |
recip.Name__c= 'Test Recipe 1'; | |
insert recip; | |
//create cookbooks | |
List <Cookbook__c> newBooks = new List <Cookbook__c> (); | |
Cookbook__c book = new Cookbook__c(); | |
book.Name = 'Barney Loves to Eat'; | |
newBooks.add(book); | |
Cookbook__c book2 = new Cookbook__c(); | |
book2.Name = 'Wilma Loves to Cook'; | |
newBooks.add(book2); | |
insert newBooks; | |
//create recipe usages | |
List <Recipe_Usage__c> newUsages = new List <Recipe_usage__c> (); | |
Recipe_usage__c usage = new Recipe_usage__c(); | |
usage.Recipe__c = Rec.Id; | |
usage.Cookbook__c = book.Id; | |
newUsages.add(usage); | |
Recipe_usage__c usage2 = new Recipe_usage__c(); | |
usage2.Recipe__c = Rec.Id; | |
usage2.Cookbook__c = book2.Id; | |
newUsages.add(usage2); | |
insert newUsages; | |
} | |
@isTest static void testBruteForceRecipeCreation() { | |
List <Recipe__c> rcps = new List<Recipe__c>(); | |
Boolean booboo=false; | |
Test.startTest(); | |
rcps = [SELECT Id FROM Recipe__c]; | |
if (rcps.size() >0 ){ | |
booboo= true; | |
} | |
System.assertEquals(booboo, true, 'Was expecting to find at least one recipe'); | |
Test.stopTest(); | |
} | |
@isTest static void testAtTestSetupMethodsRule(){ | |
List<Recipe_Usage__c> usages = [SELECT Id, Recipe__c, Cookbook__c FROM Recipe_Usage__c]; | |
System.assertEquals(2, usages.size(), 'Expected test to find 2 recipe usages'); | |
} | |
@isTest static void testDraftRecipes1() { | |
//Positive Test to see if Recipe 2 remains not a draft | |
Recipe__c reckies = [SELECT Id, Draft__c, Active_Time__c, Name FROM Recipe__c WHERE Name = 'Test Recipe 2']; | |
//declare a boolean | |
Boolean trueFalse = false; | |
Test.startTest(); | |
reckies.Draft__c=trueFalse; | |
system.assertEquals(false, trueFalse, 'draft expected to be false'); | |
Test.StopTest(); | |
} | |
@isTest static void testDraftRecipes2() { | |
//positive test see if record becomes draft when updated so that it should be marked a draft | |
Recipe__c reckies = [SELECT Id, Draft__c, Active_Time__c, Name FROM Recipe__c WHERE Name = 'Test Recipe 2']; | |
//declare a boolean | |
system.debug('value of Draft before update:' + reckies.Draft__c); | |
//Boolean trueFalse = false; | |
Test.StartTest(); | |
//by making the Active Time null the recipe should be marked Draft when updated | |
reckies.Active_Time__c = null; | |
update reckies; | |
Test.stopTest(); | |
Recipe__c afterReckies = [SELECT Id, Draft__c, Active_Time__c FROM Recipe__c WHERE Name = 'Test Recipe 2']; | |
//set the value of Draft to the boolean | |
//afterReckies.Draft__c = trueFalse; | |
System.Debug('Draft field value after udpate: ' + afterReckies.Draft__c); | |
System.Debug(afterReckies); | |
System.assertEquals(true, afterReckies.Draft__c, 'Was expecting to find the recipe to be a draft.'); | |
} | |
@isTest static void recipeComplexity() { | |
List <Recipe__c> listRecipes = new List<Recipe__c>(); | |
listRecipes = [SELECT Id, Name, Complexity__c FROM Recipe__c]; | |
Test.StartTest(); | |
For (Recipe__c recipe:listRecipes){ | |
if(recipe.Name=='Test Recipe 2') { | |
system.assertEquals(recipe.Complexity__c='Moderate', 'Moderate', 'was expecting Moderate'); | |
system.debug('====recipe is 2, so it is moderate'); | |
} else if(recipe.Name=='Test Recipe 1') { | |
system.assertEquals(recipe.Complexity__c='Simple', 'Simple', 'was expecting simple'); | |
system.debug('====recipe is 1, so it is simple but it is '+ recipe.Complexity__c); | |
} | |
else if(recipe.Name=='Test Recipe 3') | |
system.debug('====recipe should be difficult, but recipe 3 is ' + recipe.Complexity__c ); | |
system.assertEquals(recipe.Complexity__c='Difficult', 'Difficult','was expecting Difficult'); | |
} | |
Test.stopTest(); | |
} | |
//Test that tasks are created when a recipe is updated. | |
@isTest static void recipeAfter() { | |
Recipe__c rec = new Recipe__c(); | |
rec = [SELECT Id, Name, Draft__c FROM Recipe__c WHERE Name = 'Test Recipe 3']; | |
rec.Name = 'Bonanza Cookie'; | |
update rec; | |
system.debug('recipe variable rec: ' + rec); | |
Recipe__c reck = [SELECT Draft__c FROM Recipe__c WHERE Name = 'Bonanza Cookie']; | |
List <Task> listTasks = new List<Task>(); | |
listTasks = [SELECT Id, Subject, Description, WhatId FROM Task]; | |
List <Id> whatIdList = new List <Id>(); | |
system.debug('ListTask:'+listTasks); | |
Test.StartTest(); | |
for (Task tisket:listTasks){ | |
whatIdList.add(tisket.WhatId); | |
} | |
string RecipeId= whatIdList[0]; | |
string RecipeId2= whatIdList[1]; | |
System.AssertEquals(RecipeId, rec.Id, 'Excpected the recipe id and whatId are the same'); | |
System.AssertEquals(RecipeId2, rec.Id, 'Excpected the recipe id and whatId are the same'); | |
System.assertEquals(2, listTasks.size(), 'Expected test to find 2 tasks'); | |
//NEGATIVE TEST: make sure when a field is changed that the record doesn't become a draft | |
System.AssertEquals(reck.Draft__c,false, 'Expected draft to remain false on this update.'); | |
Test.stopTest(); | |
System.Debug('the what id: '+Recipeid + ' should equal the recipe id :' + rec.id); | |
} | |
//Test that throws an error if Name__c is blank. | |
@isTest static void blankNameTest() { | |
// create recipe with no Name__c | |
Recipe__c rec = new Recipe__c(); | |
rec.Name = 'Test Recipe 4'; | |
rec.Active_Time__c = 2; | |
rec.Description__c = 'recipe'; | |
rec.Active_Time_Units__c = 'Hours'; | |
rec.Servings__c = 20; | |
// Run the Test | |
Test.startTest(); | |
try { | |
insert rec; | |
} catch (Exception e) { | |
// Did we get the expected error? | |
System.assertEquals(e.getTypeName(), 'System.DmlException'); | |
Test.stopTest(); | |
} | |
} | |
} |
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 PositivePermission_tests { | |
@TestSetup | |
static void testSetup(){ | |
Account a = TestFactory.getAccount('I can see your private object', true); | |
Private_Object__c poPo = new Private_Object__c(account__c = a.id, notes__c = 'foo'); | |
insert poPo; | |
} | |
@isTest static void PermissionSetTest_Positive() { | |
//User u = TestFactory.generateUser('standard user'); | |
User u2 = TestFactory.generateUser('Custom User'); | |
//Private_Object__c privateObj = new Private_Object__c(); | |
//privateObj = [SELECT Id, OwnerId FROM Private_Object__c]; | |
//privateObj.OwnerId = u.id; | |
//update privateObj; | |
System.runAs(u2){ | |
Private_Object__c[] pos; | |
Test.startTest(); | |
pos = [SELECT Id, Account__c, notes__c FROM Private_Object__c]; | |
Test.stopTest(); | |
system.assert(pos.size() == 1, 'the user with the custom user profile can see all Private Objects'); | |
} | |
} | |
} |
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
//Trailhead Write Negative Tests | |
@isTest | |
public class Calculator_Tests { | |
@isTest static Void additionPositiveTest(){ | |
Integer a = 45, b=42; | |
Test.startTest(); | |
calculator.addition(a,b); | |
Test.stopTest(); | |
system.assertEquals(calculator.addition(a, b),87, 'expected 87'); | |
system.debug(a + b); | |
} | |
@isTest static Void subtractionPositiveTest(){ | |
Integer a = 45, b=42; | |
Test.startTest(); | |
calculator.subtraction(a, b); | |
Test.stopTest(); | |
system.assertEquals(calculator.subtraction(a, b),3, 'expected 3'); | |
system.debug(a - b); | |
} | |
@isTest static Void multiplyPositiveTest(){ | |
Integer a = 45, b=42; | |
Test.startTest(); | |
system.assertEquals(calculator.multiply(a, b),1890, 'expected 1890'); | |
Test.stopTest(); | |
system.debug(a * b); | |
} | |
@isTest static Void multiplyNegativeTestA(){ | |
List<calculator> calculators = new List<calculator>(); | |
Integer a = 0, b=42; | |
Boolean exceptionA = false; | |
Test.startTest(); | |
try{ | |
calculator.multiply(a, b); | |
} catch(calculator.calculatorException cce){ | |
if(cce.getMessage().equalsIgnoreCase('It doesn\'t make sense to multiply by zero')){ | |
exceptionA = true; | |
} | |
} | |
Test.stopTest(); | |
system.assert(exceptionA, 'Calulator should have thrown an exception'); | |
} | |
@isTest static Void dividePositiveTest(){ | |
Integer a = 1890, b=42; | |
Test.startTest(); | |
calculator.divide(a,b); | |
Test.stopTest(); | |
system.assertEquals(calculator.divide(a, b),45, 'expected 45'); | |
system.debug(a / b); | |
} | |
@isTest static Void divideNegativeTestA(){ | |
Integer a = 42, b=0; | |
Boolean excepshun = false; | |
Test.startTest(); | |
try{ | |
calculator.divide(a, b); | |
} catch(calculator.calculatorException cce){ | |
if(cce.getMessage().equalsIgnoreCase('you still can\'t divide by zero')){ | |
excepshun = true; | |
} | |
} | |
Test.stopTest(); | |
system.assert(excepshun, 'Calculator should have thrown an exception'); | |
system.debug('Exception' + excepshun); | |
} | |
@isTest static Void divideNegativeTestB(){ | |
//try for the other exception | |
Integer e= -22, d =22; | |
Decimal returnValue = e / d; | |
Boolean eekception = false; | |
Test.startTest(); | |
try{ | |
calculator.divide(e, d); | |
} catch(calculator.calculatorException cce2){ | |
if(cce2.getMessage().equalsIgnoreCase('Division returned a negative value.' + returnValue)){ | |
eekception = true; | |
system.debug('Exception ' + eekception); | |
} | |
} | |
Test.stopTest(); | |
system.assert(eekception, 'Calculator should have thrown an exception'); | |
} | |
} |
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, before update, after insert, after update) { | |
if (Trigger.isInsert) { | |
if(Trigger.isBefore){ | |
system.debug('entering the trigger isBefore'); | |
RecipeTriggerHandler.onBeforeInsert(trigger.new); | |
RecipeTriggerHandler.recipeComplexity(trigger.new); | |
} | |
} | |
//Had to add this section to update the Recipe and make it a draft | |
if (Trigger.isBefore) { | |
if(Trigger.isUpdate){ | |
system.debug('entering the trigger isBefore'); | |
RecipeTriggerHandler.updateRecipes(trigger.new); | |
} | |
} | |
if (Trigger.isUpdate && Trigger.isAfter) { | |
//isUpdate doesn't mean "after update". it would also work in the before. so we need to specify. trigger.isAfter and trigger.isUpdate | |
// Instantiate the Handler and set Trigger records | |
system.debug('entering the trigger'); | |
RecipeTriggerHandler.recipeAfter(Trigger.new); | |
//RecipeTriggerHandler.updateRecipes(Trigger.new); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment