refactor(test/commons): refactor tests for commons models
This commit is contained in:
parent
554d769305
commit
73c42303c4
3 changed files with 83 additions and 116 deletions
|
|
@ -2,94 +2,56 @@ package commons;
|
|||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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;
|
||||
private Map<String, FormalIngredient> ingredientUnitMap;
|
||||
private Map<String, VagueIngredient> ingredientDescriptorMap;
|
||||
|
||||
private FormalIngredient getFormal(String unit) {
|
||||
Ingredient ingredient = new Ingredient("Bread", 1, 2, 3);
|
||||
return new FormalIngredient(ingredient, 1.0, unit);
|
||||
}
|
||||
private VagueIngredient getVague(String descriptor) {
|
||||
Ingredient ingredient = new Ingredient("Bread", 1, 2, 3);
|
||||
return new VagueIngredient(ingredient, descriptor);
|
||||
}
|
||||
@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());
|
||||
ingredientUnitMap = new HashMap<>();
|
||||
ingredientDescriptorMap = new HashMap<>();
|
||||
List.of("g", "kg", "ml", "l", "tbsp", "cup")
|
||||
.forEach(u -> ingredientUnitMap.put(u, getFormal(u)));
|
||||
List.of("a sprinkle of", "some", "bits of", "a few")
|
||||
.forEach(d -> ingredientDescriptorMap.put(d, getVague(d)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getInformalUnitTest(){
|
||||
assertEquals(Unit.PINCH, pinch.getUnit());
|
||||
assertEquals(Unit.HANDFUL, handful.getUnit());
|
||||
assertEquals(Unit.TO_TASTE, toTaste.getUnit());
|
||||
void testInstantiateFormalIngredient() {
|
||||
Ingredient ingredient = new Ingredient("Bread", 1, 2, 3);
|
||||
FormalIngredient fi = new FormalIngredient(ingredient, 1.0, "g");
|
||||
assertEquals(ingredient, fi.getIngredient());
|
||||
}
|
||||
@Test
|
||||
void testInstantiateVagueIngredient() {
|
||||
Ingredient ingredient = new Ingredient("Bread", 1, 2, 3);
|
||||
VagueIngredient fi = new VagueIngredient(ingredient, "some");
|
||||
assertEquals("some", fi.getDescription());
|
||||
}
|
||||
|
||||
@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());
|
||||
void testFormalIngredientScaleByFactor() {
|
||||
ingredientUnitMap.replaceAll(
|
||||
(_, b) -> b.scaleBy(2)
|
||||
);
|
||||
// Each amount is doubled after the mapping
|
||||
assertEquals(ingredientUnitMap.size(), ingredientUnitMap.values().stream()
|
||||
.filter(i -> i.getAmount() == 2.0).count());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
|
@ -12,6 +13,17 @@ class RecipeTest {
|
|||
|
||||
Recipe recipe;
|
||||
static final Long RECIPE_ID = 1L;
|
||||
static final RecipeIngredient testIngredient = new FormalIngredient(
|
||||
new Ingredient("Bread", 1, 2, 3),
|
||||
1.0,
|
||||
"g"
|
||||
);
|
||||
|
||||
static RecipeIngredient getTemplate(String name) {
|
||||
return new VagueIngredient(
|
||||
new Ingredient(name, 1.0, 1.0, 1.0),
|
||||
"some");
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setupRecipe() {
|
||||
|
|
@ -42,8 +54,7 @@ class RecipeTest {
|
|||
|
||||
@Test
|
||||
void getIngredientsAddThrow() {
|
||||
// TODO: Change to actual Ingredient class later
|
||||
assertThrows(UnsupportedOperationException.class, () -> recipe.getIngredients().add("Lasagna"));
|
||||
assertThrows(UnsupportedOperationException.class, () -> recipe.getIngredients().add(testIngredient));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -53,8 +64,10 @@ class RecipeTest {
|
|||
|
||||
@Test
|
||||
void setIngredients() {
|
||||
// TODO: Change to actual Ingredient class later
|
||||
List<String> ingredients = new ArrayList<>(List.of("Chocolate", "Flour", "Egg"));
|
||||
List<RecipeIngredient> ingredients =
|
||||
Stream.of("Chocolate", "Flour", "Egg")
|
||||
.map(RecipeTest::getTemplate)
|
||||
.toList();
|
||||
recipe.setIngredients(ingredients);
|
||||
|
||||
assertEquals(recipe.getIngredients(), ingredients);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package commons;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
|
@ -11,64 +12,55 @@ class UnitTest {
|
|||
private static final double KILOGRAMS = 1000.0;
|
||||
private static final double MILLILITERS = 1.0;
|
||||
private static final double LITERS = 1000.0;
|
||||
private static final double TABLESPOONS = 15.0;
|
||||
private static final double TEASPOONS = 5.0;
|
||||
private static final double CUPS = 240.0;
|
||||
private static final double PIECES = 1.0;
|
||||
|
||||
private static final double NoMeaningfulValue = 0.0;
|
||||
private static final double TABLESPOONS = 14.0;
|
||||
private static final double CUPS = 225.0;
|
||||
|
||||
|
||||
@Test
|
||||
void formalUnitMarkedFormal(){
|
||||
assertTrue(Unit.GRAM.isFormal());
|
||||
assertTrue(Unit.KILOGRAM.isFormal());
|
||||
assertTrue(Unit.LITER.isFormal());
|
||||
assertTrue(Unit.GRAMME.isFormal());
|
||||
assertTrue(Unit.KILOGRAMME.isFormal());
|
||||
assertTrue(Unit.LITRE.isFormal());
|
||||
assertTrue(Unit.CUP.isFormal());
|
||||
assertTrue(Unit.MILLILITER.isFormal());
|
||||
assertTrue(Unit.PIECE.isFormal());
|
||||
assertTrue(Unit.MILLILITRE.isFormal());
|
||||
assertTrue(Unit.TABLESPOON.isFormal());
|
||||
assertTrue(Unit.TEASPOON.isFormal());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void informalUnitAreNotFormal() {
|
||||
assertFalse(Unit.PINCH.isFormal());
|
||||
assertFalse(Unit.HANDFUL.isFormal());
|
||||
assertFalse(Unit.TO_TASTE.isFormal());
|
||||
assertFalse(Unit.INFORMAL.isFormal());
|
||||
}
|
||||
|
||||
@Test
|
||||
void conversionIsCorrect() {
|
||||
assertEquals(GRAMS, Unit.GRAM.conversionFactor);
|
||||
assertEquals(KILOGRAMS, Unit.KILOGRAM.conversionFactor);
|
||||
assertEquals(LITERS, Unit.LITER.conversionFactor);
|
||||
assertEquals(GRAMS, Unit.GRAMME.conversionFactor);
|
||||
assertEquals(KILOGRAMS, Unit.KILOGRAMME.conversionFactor);
|
||||
assertEquals(LITERS, Unit.LITRE.conversionFactor);
|
||||
assertEquals(CUPS, Unit.CUP.conversionFactor);
|
||||
assertEquals(MILLILITERS, Unit.MILLILITER.conversionFactor);
|
||||
assertEquals(PIECES, Unit.PIECE.conversionFactor);
|
||||
assertEquals(MILLILITERS, Unit.MILLILITRE.conversionFactor);
|
||||
assertEquals(TABLESPOONS, Unit.TABLESPOON.conversionFactor);
|
||||
assertEquals(TEASPOONS, Unit.TEASPOON.conversionFactor);
|
||||
|
||||
assertEquals(NoMeaningfulValue, Unit.PINCH.conversionFactor);
|
||||
assertEquals(NoMeaningfulValue, Unit.HANDFUL.conversionFactor);
|
||||
assertEquals(NoMeaningfulValue, Unit.TO_TASTE.conversionFactor);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void toStringReturnsName(){
|
||||
assertEquals("GRAM", Unit.GRAM.toString());
|
||||
assertEquals("KILOGRAM", Unit.KILOGRAM.toString());
|
||||
assertEquals("LITER", Unit.LITER.toString());
|
||||
assertEquals("CUP", Unit.CUP.toString());
|
||||
assertEquals("MILLILITER", Unit.MILLILITER.toString());
|
||||
assertEquals("PIECE", Unit.PIECE.toString());
|
||||
assertEquals("TABLESPOON", Unit.TABLESPOON.toString());
|
||||
assertEquals("TEASPOON", Unit.TEASPOON.toString());
|
||||
|
||||
assertEquals("PINCH", Unit.PINCH.toString());
|
||||
assertEquals("HANDFUL", Unit.HANDFUL.toString());
|
||||
assertEquals("TO_TASTE", Unit.TO_TASTE.toString());
|
||||
assertEquals("g", Unit.GRAMME.toString());
|
||||
assertEquals("kg", Unit.KILOGRAMME.toString());
|
||||
assertEquals("l", Unit.LITRE.toString());
|
||||
assertEquals("cup", Unit.CUP.toString());
|
||||
assertEquals("ml", Unit.MILLILITRE.toString());
|
||||
assertEquals("tbsp", Unit.TABLESPOON.toString());
|
||||
}
|
||||
@Test
|
||||
void testFromSuffixCreatesSomeUnit() {
|
||||
assertTrue(Unit.fromString("g").isPresent());
|
||||
}
|
||||
@Test
|
||||
void testFromSuffixCreatesCorrectUnit() {
|
||||
assertEquals(Optional.of(Unit.GRAMME), Unit.fromString("g"));
|
||||
}
|
||||
@Test
|
||||
void testFromNonExistentSuffixCreatesNoUnit() {
|
||||
assertFalse(Unit.fromString("joe").isPresent());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue