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.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
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.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
|
||||||
|
|
||||||
public class RecipeIngredientTest {
|
public class RecipeIngredientTest {
|
||||||
|
|
||||||
private RecipeIngredient gram;
|
private Map<String, FormalIngredient> ingredientUnitMap;
|
||||||
private RecipeIngredient kilogram;
|
private Map<String, VagueIngredient> ingredientDescriptorMap;
|
||||||
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 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
|
@BeforeEach
|
||||||
void setup(){
|
void setup(){
|
||||||
Recipe recipe = new Recipe();
|
ingredientUnitMap = new HashMap<>();
|
||||||
Ingredient ingredient = new Ingredient();
|
ingredientDescriptorMap = new HashMap<>();
|
||||||
gram = new RecipeIngredient(recipe,ingredient,1,"GRAM");
|
List.of("g", "kg", "ml", "l", "tbsp", "cup")
|
||||||
kilogram = new RecipeIngredient(recipe,ingredient,1,"KILOGRAM");
|
.forEach(u -> ingredientUnitMap.put(u, getFormal(u)));
|
||||||
|
List.of("a sprinkle of", "some", "bits of", "a few")
|
||||||
milliliter = new RecipeIngredient(recipe,ingredient,1,"MILLILITER");
|
.forEach(d -> ingredientDescriptorMap.put(d, getVague(d)));
|
||||||
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
|
@Test
|
||||||
void getInformalUnitTest(){
|
void testInstantiateFormalIngredient() {
|
||||||
assertEquals(Unit.PINCH, pinch.getUnit());
|
Ingredient ingredient = new Ingredient("Bread", 1, 2, 3);
|
||||||
assertEquals(Unit.HANDFUL, handful.getUnit());
|
FormalIngredient fi = new FormalIngredient(ingredient, 1.0, "g");
|
||||||
assertEquals(Unit.TO_TASTE, toTaste.getUnit());
|
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
|
@Test
|
||||||
void getUnknownUnitTest(){
|
void testFormalIngredientScaleByFactor() {
|
||||||
assertNull(invalid.getUnit());
|
ingredientUnitMap.replaceAll(
|
||||||
}
|
(_, b) -> b.scaleBy(2)
|
||||||
|
);
|
||||||
@Test
|
// Each amount is doubled after the mapping
|
||||||
void convertFormalToBaseUnit(){
|
assertEquals(ingredientUnitMap.size(), ingredientUnitMap.values().stream()
|
||||||
assertEquals(1000, kilogram.amountInBaseUnit());
|
.filter(i -> i.getAmount() == 2.0).count());
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
|
@ -12,6 +13,17 @@ class RecipeTest {
|
||||||
|
|
||||||
Recipe recipe;
|
Recipe recipe;
|
||||||
static final Long RECIPE_ID = 1L;
|
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
|
@BeforeEach
|
||||||
void setupRecipe() {
|
void setupRecipe() {
|
||||||
|
|
@ -42,8 +54,7 @@ class RecipeTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getIngredientsAddThrow() {
|
void getIngredientsAddThrow() {
|
||||||
// TODO: Change to actual Ingredient class later
|
assertThrows(UnsupportedOperationException.class, () -> recipe.getIngredients().add(testIngredient));
|
||||||
assertThrows(UnsupportedOperationException.class, () -> recipe.getIngredients().add("Lasagna"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -53,8 +64,10 @@ class RecipeTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void setIngredients() {
|
void setIngredients() {
|
||||||
// TODO: Change to actual Ingredient class later
|
List<RecipeIngredient> ingredients =
|
||||||
List<String> ingredients = new ArrayList<>(List.of("Chocolate", "Flour", "Egg"));
|
Stream.of("Chocolate", "Flour", "Egg")
|
||||||
|
.map(RecipeTest::getTemplate)
|
||||||
|
.toList();
|
||||||
recipe.setIngredients(ingredients);
|
recipe.setIngredients(ingredients);
|
||||||
|
|
||||||
assertEquals(recipe.getIngredients(), ingredients);
|
assertEquals(recipe.getIngredients(), ingredients);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package commons;
|
package commons;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
|
@ -11,64 +12,55 @@ class UnitTest {
|
||||||
private static final double KILOGRAMS = 1000.0;
|
private static final double KILOGRAMS = 1000.0;
|
||||||
private static final double MILLILITERS = 1.0;
|
private static final double MILLILITERS = 1.0;
|
||||||
private static final double LITERS = 1000.0;
|
private static final double LITERS = 1000.0;
|
||||||
private static final double TABLESPOONS = 15.0;
|
private static final double TABLESPOONS = 14.0;
|
||||||
private static final double TEASPOONS = 5.0;
|
private static final double CUPS = 225.0;
|
||||||
private static final double CUPS = 240.0;
|
|
||||||
private static final double PIECES = 1.0;
|
|
||||||
|
|
||||||
private static final double NoMeaningfulValue = 0.0;
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void formalUnitMarkedFormal(){
|
void formalUnitMarkedFormal(){
|
||||||
assertTrue(Unit.GRAM.isFormal());
|
assertTrue(Unit.GRAMME.isFormal());
|
||||||
assertTrue(Unit.KILOGRAM.isFormal());
|
assertTrue(Unit.KILOGRAMME.isFormal());
|
||||||
assertTrue(Unit.LITER.isFormal());
|
assertTrue(Unit.LITRE.isFormal());
|
||||||
assertTrue(Unit.CUP.isFormal());
|
assertTrue(Unit.CUP.isFormal());
|
||||||
assertTrue(Unit.MILLILITER.isFormal());
|
assertTrue(Unit.MILLILITRE.isFormal());
|
||||||
assertTrue(Unit.PIECE.isFormal());
|
|
||||||
assertTrue(Unit.TABLESPOON.isFormal());
|
assertTrue(Unit.TABLESPOON.isFormal());
|
||||||
assertTrue(Unit.TEASPOON.isFormal());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void informalUnitAreNotFormal() {
|
void informalUnitAreNotFormal() {
|
||||||
assertFalse(Unit.PINCH.isFormal());
|
assertFalse(Unit.INFORMAL.isFormal());
|
||||||
assertFalse(Unit.HANDFUL.isFormal());
|
|
||||||
assertFalse(Unit.TO_TASTE.isFormal());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void conversionIsCorrect() {
|
void conversionIsCorrect() {
|
||||||
assertEquals(GRAMS, Unit.GRAM.conversionFactor);
|
assertEquals(GRAMS, Unit.GRAMME.conversionFactor);
|
||||||
assertEquals(KILOGRAMS, Unit.KILOGRAM.conversionFactor);
|
assertEquals(KILOGRAMS, Unit.KILOGRAMME.conversionFactor);
|
||||||
assertEquals(LITERS, Unit.LITER.conversionFactor);
|
assertEquals(LITERS, Unit.LITRE.conversionFactor);
|
||||||
assertEquals(CUPS, Unit.CUP.conversionFactor);
|
assertEquals(CUPS, Unit.CUP.conversionFactor);
|
||||||
assertEquals(MILLILITERS, Unit.MILLILITER.conversionFactor);
|
assertEquals(MILLILITERS, Unit.MILLILITRE.conversionFactor);
|
||||||
assertEquals(PIECES, Unit.PIECE.conversionFactor);
|
|
||||||
assertEquals(TABLESPOONS, Unit.TABLESPOON.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
|
@Test
|
||||||
void toStringReturnsName(){
|
void toStringReturnsName(){
|
||||||
assertEquals("GRAM", Unit.GRAM.toString());
|
assertEquals("g", Unit.GRAMME.toString());
|
||||||
assertEquals("KILOGRAM", Unit.KILOGRAM.toString());
|
assertEquals("kg", Unit.KILOGRAMME.toString());
|
||||||
assertEquals("LITER", Unit.LITER.toString());
|
assertEquals("l", Unit.LITRE.toString());
|
||||||
assertEquals("CUP", Unit.CUP.toString());
|
assertEquals("cup", Unit.CUP.toString());
|
||||||
assertEquals("MILLILITER", Unit.MILLILITER.toString());
|
assertEquals("ml", Unit.MILLILITRE.toString());
|
||||||
assertEquals("PIECE", Unit.PIECE.toString());
|
assertEquals("tbsp", Unit.TABLESPOON.toString());
|
||||||
assertEquals("TABLESPOON", Unit.TABLESPOON.toString());
|
}
|
||||||
assertEquals("TEASPOON", Unit.TEASPOON.toString());
|
@Test
|
||||||
|
void testFromSuffixCreatesSomeUnit() {
|
||||||
assertEquals("PINCH", Unit.PINCH.toString());
|
assertTrue(Unit.fromString("g").isPresent());
|
||||||
assertEquals("HANDFUL", Unit.HANDFUL.toString());
|
}
|
||||||
assertEquals("TO_TASTE", Unit.TO_TASTE.toString());
|
@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