Skip to content

Instantly share code, notes, and snippets.

@forcethesales
Last active December 21, 2020 17:21
Show Gist options
  • Save forcethesales/4defbf75e1bcda87d2247eeb574fcbce to your computer and use it in GitHub Desktop.
Save forcethesales/4defbf75e1bcda87d2247eeb574fcbce to your computer and use it in GitHub Desktop.
RAD II Final Project
public class RecipeHelper {
public static void addIngredient (String ingredientName, Integer measurementAmount, String measurementType, ID recipeId) {
//code
//Your code should create an Ingredient SObject in memory and insert it in the database.
//The ingredient should have the recipe id provided as its parent.
Ingredient__c ingred = new Ingredient__c();
ingred.Name = ingredientName;
ingred.Calories__c = 44;
ingred.Measurement__c=measurementAmount;
ingred.Measurement_Type__c = measurementType;
ingred.Recipe__c = recipeId;
insert ingred;
}
public static List<String> generateGroceryList(ID recipeId){
//This method does something very cool, it makes a list of the ingredients you need, along with their quantities.
//For now, your method should be able to return a list of strings representing ingredient names.
List <Ingredient__c> newIngredients = [SELECT Name from Ingredient__c WHERE Recipe__c = :recipeId];
List < String > groceryList = new List < String >();
for (Ingredient__c i:newIngredients) {
groceryList.add(i.Name);
}
system.debug('Grocery list: ' + groceryList);
return GroceryList;
}
public static void scaleRecipeForServings (ID recipeId, Integer desiredServings){
List <Ingredient__c> myIngredients = [SELECT Name,Measurement__c, Measurement_Type__c from Ingredient__c WHERE Recipe__c = :recipeId];
List < String > scaledList = new List < String >();
Recipe__c rec = [SELECT Servings__c FROM Recipe__c WHERE ID =:recipeID];
//throw error if desired servings is 0
if (desiredServings==0) {
//Add error only works in triggers, so this won't work
//could add a custom exception. could change from void to return something, and then the error would block
//the error is only going to be thrown if we do a DML statement//
// see what Prasanna Did
rec.addError('ERROR: desired servings is 0');
system.debug('Error it is 0');}
Decimal ratio = desiredServings/rec.Servings__c;
system.debug('ratio:' + ratio);
Decimal newAmount;
//new Decimal = Divide desiredservings by Servings. multiply Measurement by that
//Loop through ingredients. multiply measurement by new Decimal.
for (Ingredient__c i:myIngredients) {
newAmount = i.Measurement__c * ratio;
system.debug('New Amount:' + newAmount);
scaledList.add(newAmount + ' ' + i.Measurement_type__c + ' ' +i.Name);
}
system.debug('New list of amounts ' + scaledList);
}
}
@isTest
public class RecipeHelperTest {
//need to add test for recipeRecordType
//
@TestSetup
static void createRecipe(){
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 = 4;
rec.Name__c= 'Test Recipe 3';
insert rec;
}
//This test is to make sure the ingredient is added to the database with the recipe Id as parent.
@isTest static void addIngredientPositiveTest() {
Recipe__c rec = [SELECT Id from Recipe__c];
Test.startTest();
RecipeHelper.addIngredient('Wheat Flour', 4, 'Cups', rec.Id);
Test.stopTest();
Ingredient__c ing = [SELECT Recipe__c,Measurement__c,Measurement_Type__c,Name FROM Ingredient__c];
System.AssertEquals(rec.Id,ing.Recipe__c, 'expected recipeID');
System.AssertEquals(4, ing.Measurement__c,'expected 4');
System.AssertEquals('Cups',ing.Measurement_Type__c, 'I expected Cups');
System.AssertEquals( 'Wheat Flour', ing.Name,'expected wheat flour');
System.debug('Recipe Id from ingredient: ' +ing.recipe__c);
}
//This test is to make sure the ingredient is added to the database with the recipe Id as parent only ONCE.
@isTest static void addIngredientOneTimeTest() {
Recipe__c rec = [SELECT Id from Recipe__c];
Test.startTest();
RecipeHelper.addIngredient('Wheat Flour', 4, 'Cups', rec.Id);
Test.stopTest();
List<Ingredient__c> ing = [SELECT Recipe__c,Measurement__c,Measurement_Type__c,Name FROM Ingredient__c];
System.Assertequals(1,ing.size(),'expect only one recipe');
}
@isTest static void groceryListTest() {
//setup Data. lookup recipe ID and add 3 ingredients using addIngredient method
Recipe__c rec = [SELECT Id from Recipe__c];
RecipeHelper.addIngredient('Walnuts', 1, 'Cups', rec.Id);
RecipeHelper.addIngredient('Milk', 4, 'Tbl', rec.Id);
RecipeHelper.addIngredient('Oil', 1, 'Tbl', rec.Id);
//add the ingredients to a list for comparison
List <String> ingreds = new List <String> ();
ingreds.add('Walnuts');
ingreds.add('Milk');
ingreds.add('Oil');
Test.startTest();
//This returns a list of the ingredients because the method returns a list.
List<String> groceryList = RecipeHelper.generateGroceryList(rec.id);
Test.stopTest();
System.AssertEquals(ingreds, groceryList,'expected walnut, milk, oil');
}
@isTest static void scaleRecipeForServingsTest() {
//setup Data. lookup recipe ID and add 3 ingredients using addIngredient method
Recipe__c rec = [SELECT Id from Recipe__c];
RecipeHelper.addIngredient('Walnuts', 1, 'Cups', rec.Id);
RecipeHelper.addIngredient('Milk', 4, 'Tbl', rec.Id);
RecipeHelper.addIngredient('Oil', 1, 'Tbl', rec.Id);
Test.startTest();
RecipeHelper.scaleRecipeForServings(rec.id,8);
Test.stopTest();
}
@isTest static void scaleRecipeForServingsNegativeTest() {
//setup Data. lookup recipe ID and add 3 ingredients using addIngredient method
Recipe__c rec = [SELECT Id from Recipe__c];
RecipeHelper.addIngredient('Walnuts', 1, 'Cups', rec.Id);
RecipeHelper.addIngredient('Milk', 4, 'Tbl', rec.Id);
RecipeHelper.addIngredient('Oil', 1, 'Tbl', rec.Id);
Boolean bools=false;
Test.startTest();
try {
RecipeHelper.scaleRecipeForServings(rec.id,0);
} catch (Exception e) {
if(e.getMessage().equalsIgnoreCase('ERROR: desired servings is 0')) {
bools = true;
} system.debug('this is the error:' + e.getmessage());
}
Test.stopTest();
// Did we get the expected error?
System.assertEquals(true,bools, 'Expected error because desired servings is 0.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment