Last active
May 25, 2023 22:17
-
-
Save asmitakhare/83d194f90668ba51bb83dcac6ae08fbc to your computer and use it in GitHub Desktop.
Final Project
This file contains hidden or 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
public inherited sharing class RecipeController { | |
@AuraEnabled | |
public static List <Ingredient__c> generateGroceryList(Id recipeId){ | |
//load the ingredients and return them | |
List<Ingredient__c> ingredientList = [Select ID, Name, Measurement__c, Measurement_Type__c, Notes__c From Ingredient__c Where Recipe__c = :recipeId]; | |
return ingredientList; | |
} | |
@AuraEnabled | |
public static void addIngredient(String ingredientName, Integer measurementAmount, String measurementType, ID recipeId){ | |
//Insert the ingredient | |
Ingredient__c ingredient = new Ingredient__c (Name = ingredientName, | |
Measurement__c = measurementAmount, | |
Measurement_Type__c = measurementType, | |
Id = recipeId ); | |
insert ingredient; | |
System.debug('adding all ingredient from recipe :' +recipeId); | |
} | |
@AuraEnabled | |
public static List < Ingredient__c > scaleRecipeForServings (ID recipeId, Decimal desiredServings) { | |
//Scale the recipe and return the list of scaled ingredients | |
Recipe__c r = [Select Id, Servings__c From Recipe__c Where Id = :recipeId]; | |
List<Ingredient__c> ingList = [Select ID, Name, Measurement__c, Measurement_Type__c, Notes__c From Ingredient__c | |
Where Recipe__c = :r.Id]; | |
Decimal scale = desiredServings/r.Servings__c; | |
for(Ingredient__c ingr : ingList){ | |
ingr.Measurement__c = scale*ingr.Measurement__c; | |
} | |
return ingList; | |
} | |
@AuraEnabled(Cacheable=true) | |
public static Recipe__c[] getAllRecipes() { | |
return [ | |
SELECT | |
Id, | |
Name, | |
Draft__c, | |
Active_Time__c, | |
Active_Time_Units__c, | |
Complexity__c, | |
Needs_Review__c, | |
Possible_Duplicate__c, | |
Season__c | |
FROM Recipe__c | |
ORDER BY Name | |
LIMIT 50 | |
]; | |
} | |
@AuraEnabled(Cacheable=true) | |
public static Recipe__c[] searchRecipes(String searchTerm) { | |
// Return all recipes when no search term | |
searchTerm = searchTerm.trim(); | |
if (searchTerm == '') { | |
System.debug('returning: '+getAllRecipes()); | |
return getAllRecipes(); | |
} | |
// Prepare query paramters | |
searchTerm = '%' + searchTerm + '%'; | |
// Execute search query | |
return [ | |
SELECT | |
Id, | |
Name, | |
Draft__c, | |
Active_Time__c, | |
Active_Time_Units__c, | |
Complexity__c, | |
Needs_Review__c, | |
Possible_Duplicate__c, | |
Season__c | |
FROM Recipe__c | |
WHERE Name LIKE :searchTerm | |
ORDER BY Name | |
LIMIT 50 | |
]; | |
} | |
} |
This file contains hidden or 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 | |
public with sharing class RecipeControllerTest { | |
@isTest | |
static void testGenerateGroceryList() { | |
List<Recipe__c> r = TestFactory.generateRecipe(1); | |
Test.startTest(); | |
List<Ingredient__c> lstIngre = RecipeController.generateGroceryList( | |
r[0].Id | |
); | |
Test.stopTest(); | |
System.assert( | |
lstIngre.size() != 0, | |
'List of inegredienet shouldn not be null' | |
); | |
} | |
@isTest | |
static void testAddIngredient() { | |
Recipe__c rec = new Recipe__c(); | |
rec.Name = 'Recipe'; | |
rec.Active_time__c = 1; | |
rec.Active_Time_Units__c = 'hours'; | |
rec.Description__c = 'Testfactory recipe test'; | |
insert rec; | |
List<Ingredient__c> lstIng = TestFactory.generateAndInsertIngredients(8, rec.Id ); | |
Test.startTest(); | |
Map<Id, Ingredient__c> mapIng = new Map<Id, Ingredient__c>( | |
[SELECT Id, Name FROM Ingredient__c] | |
); | |
for (Ingredient__c ing : lstIng) { | |
if (!mapIng.containsKey(ing.Id)) { | |
Assert.fail('Could not find intgredient from generated list'); | |
} | |
} | |
Test.stopTest(); | |
} | |
@isTest | |
static void testScaleRecipeForServings(){ | |
Recipe__c rec = new Recipe__c(); | |
rec.Name = 'Recipe'; | |
rec.Active_time__c = 1; | |
rec.Active_Time_Units__c = 'hours'; | |
rec.Description__c = 'Testfactory recipe test'; | |
rec.Servings__c = 6; | |
insert rec; | |
Ingredient__c newIngre = new Ingredient__c(); | |
newIngre.Name = 'Ingredient'; | |
newIngre.Measurement__c = 4; | |
newIngre.Measurement_Type__c = 'cups'; | |
newIngre.Recipe__c = rec.Id; | |
insert newIngre; | |
Test.startTest(); | |
List<Ingredient__c> lstingr = RecipeController.scaleRecipeForServings (rec.Id, 12); | |
Test.stopTest(); | |
system.assertEquals(8, lstingr[0].Measurement__c, 'measurment does not scale'); | |
} | |
@isTest | |
static void testGetAllRecipes(){ | |
List<Recipe__c> lstRec = TestFactory.generateRecipe(1); | |
Test.startTest(); | |
Recipe__c[]rec = RecipeController.getAllRecipes(); | |
Test.stopTest(); | |
system.assert(rec.size() != 0, 'Could not find Recipie records'); | |
} | |
@isTest | |
static void testSearchAllRecipes(){ | |
List<Recipe__c> lstRec = TestFactory.generateRecipe(5); | |
Test.startTest(); | |
Recipe__c[]rec = RecipeController.searchRecipes(''); | |
Test.stopTest(); | |
system.assertEquals(lstRec.size(), rec.size(), 'Recipe records did not find'); | |
} | |
} |
This file contains hidden or 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 | |
public class TestFactory { | |
//creating and returning list of ingredients and count number of ingredients | |
public static List<Ingredient__c> generateIngredients( | |
Integer numOfIngredients, | |
Id recipeId | |
) { | |
List<Ingredient__c> lstIngre = new List<Ingredient__c>(); | |
for (Integer i = 0; i < numOfIngredients; i++) { | |
Ingredient__c newIngre = new Ingredient__c(); | |
newIngre.Name = 'Ingredient' + i; | |
newIngre.Measurement__c = 1; | |
newIngre.Measurement_Type__c = 'cups'; | |
lstIngre.add(newIngre); | |
} | |
return lstIngre; | |
} | |
public static List<Ingredient__c> generateAndInsertIngredients( | |
Integer numOfIngredients, | |
Id recipeId | |
) { | |
List<Ingredient__c> lstIngre = generateIngredients( | |
numOfIngredients, | |
recipeId | |
); | |
insert lstIngre; | |
return lstIngre; | |
} | |
//creating inserting and returning thr list of recipes and counting number of recipes | |
public static List<Recipe__c> generateRecipe(Integer numOfRecipe) { | |
List<Recipe__c> recipes = new List<Recipe__c>(); | |
for (Integer i = 0; i < numOfRecipe; i++) { | |
Recipe__c rec = new Recipe__c(); | |
rec.Name = 'Recipe' + i; | |
rec.Active_time__c = 1; | |
rec.Active_Time_Units__c = 'hours'; | |
rec.Description__c = 'Testfactory recipe test'; | |
recipes.add(rec); | |
} | |
insert recipes; | |
List<Ingredient__c> lstIngredient = new List<Ingredient__c>(); | |
for (Recipe__c r : recipes) { | |
List<Ingredient__c> ingredients = generateIngredients( | |
5 + | |
Integer.valueof((Math.random() * 6)), // creating ingredient randomly between 5 and 10 | |
r.Id | |
); | |
lstIngredient.addAll(ingredients); | |
} | |
insert lstIngredient; | |
return recipes; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment