Merge branch 'RecipeIngredientTest' into 'main'
Made tests for the RecipeIngredient class Closes #20 See merge request cse1105/2025-2026/teams/csep-team-76!29
This commit is contained in:
commit
c56344d88a
4 changed files with 98 additions and 4 deletions
|
|
@ -50,6 +50,10 @@ public class Ingredient {
|
|||
+ carbsPer100g * KCAL_PER_GRAM_CARBS
|
||||
+ fatPer100g * KCAL_PER_GRAM_FAT;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@ public class RecipeIngredient {
|
|||
|
||||
public double amountInBaseUnit() {
|
||||
Unit unit = getUnit();
|
||||
if (unit == null || unit.isFormal() || unit.conversionFactor <= 0) {
|
||||
if (unit == null || !unit.isFormal() || unit.conversionFactor <= 0) {
|
||||
return 0.0;
|
||||
}
|
||||
return amount * unit.conversionFactor;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,95 @@
|
|||
package commons;
|
||||
//should i do it or wait for lines for next week
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class RecipeIngredientTest {
|
||||
|
||||
private RecipeIngredient gram;
|
||||
private RecipeIngredient kilogram;
|
||||
private RecipeIngredient milliliter;
|
||||
private RecipeIngredient liter;
|
||||
private RecipeIngredient teaspoon;
|
||||
private RecipeIngredient tablespoon;
|
||||
private RecipeIngredient cup;
|
||||
private RecipeIngredient piece;
|
||||
private RecipeIngredient pinch;
|
||||
private RecipeIngredient handful;
|
||||
private RecipeIngredient toTaste;
|
||||
private RecipeIngredient invalid;
|
||||
|
||||
@BeforeEach
|
||||
void setup(){
|
||||
Recipe recipe = new Recipe();
|
||||
Ingredient ingredient = new Ingredient();
|
||||
gram = new RecipeIngredient(recipe,ingredient,1,"GRAM");
|
||||
kilogram = new RecipeIngredient(recipe,ingredient,1,"KILOGRAM");
|
||||
|
||||
milliliter = new RecipeIngredient(recipe,ingredient,1,"MILLILITER");
|
||||
liter = new RecipeIngredient(recipe,ingredient,1,"LITER");
|
||||
teaspoon = new RecipeIngredient(recipe,ingredient,1,"TEASPOON");
|
||||
tablespoon = new RecipeIngredient(recipe,ingredient,1,"TABLESPOON");
|
||||
cup = new RecipeIngredient(recipe,ingredient,1,"CUP");
|
||||
piece = new RecipeIngredient(recipe,ingredient,1,"PIECE");
|
||||
pinch = new RecipeIngredient(recipe,ingredient,1,"PINCH");
|
||||
handful = new RecipeIngredient(recipe,ingredient,1,"HANDFUL");
|
||||
toTaste = new RecipeIngredient(recipe,ingredient,1,"TO_TASTE");
|
||||
|
||||
invalid = new RecipeIngredient(recipe,ingredient,1,"INVALID");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void getFormalUnitTest(){
|
||||
assertEquals(Unit.GRAM, gram.getUnit());
|
||||
assertEquals(Unit.KILOGRAM, kilogram.getUnit());
|
||||
assertEquals(Unit.MILLILITER, milliliter.getUnit());
|
||||
assertEquals(Unit.LITER, liter.getUnit());
|
||||
assertEquals(Unit.TEASPOON, teaspoon.getUnit());
|
||||
assertEquals(Unit.TABLESPOON, tablespoon.getUnit());
|
||||
assertEquals(Unit.CUP, cup.getUnit());
|
||||
|
||||
assertEquals(Unit.PIECE, piece.getUnit());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getInformalUnitTest(){
|
||||
assertEquals(Unit.PINCH, pinch.getUnit());
|
||||
assertEquals(Unit.HANDFUL, handful.getUnit());
|
||||
assertEquals(Unit.TO_TASTE, toTaste.getUnit());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getUnknownUnitTest(){
|
||||
assertNull(invalid.getUnit());
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertFormalToBaseUnit(){
|
||||
assertEquals(1000, kilogram.amountInBaseUnit());
|
||||
|
||||
assertEquals(1,milliliter.amountInBaseUnit());
|
||||
assertEquals(1000.0,liter.amountInBaseUnit());
|
||||
assertEquals(15.0,tablespoon.amountInBaseUnit());
|
||||
assertEquals(5,teaspoon.amountInBaseUnit());
|
||||
assertEquals(240.0,cup.amountInBaseUnit());
|
||||
|
||||
assertEquals(1.0,piece.amountInBaseUnit());
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertInformalToBaseUnit(){
|
||||
assertEquals(0,pinch.amountInBaseUnit());
|
||||
assertEquals(0,handful.amountInBaseUnit());
|
||||
assertEquals(0, toTaste.amountInBaseUnit());
|
||||
}
|
||||
|
||||
@Test
|
||||
void convertUnknownToBaseUnit(){
|
||||
assertEquals(0,invalid.amountInBaseUnit());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -134,8 +134,7 @@ public class IngredientController {
|
|||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
// TODO: Refactor to use setters
|
||||
updated.id = id;
|
||||
updated.setId(id);
|
||||
Ingredient savedIngredient = ingredientRepository.save(updated);
|
||||
messagingTemplate.convertAndSend(Topics.INGREDIENTS, new CreateIngredientMessage(savedIngredient));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue