|
| 1 | +import sys |
| 2 | +import os |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) |
| 6 | + |
| 7 | +import RecipeSucuk |
| 8 | +import Ingredient as IngredientModule |
| 9 | +from Ingredient import Ingredient |
| 10 | +from RecipeSucuk import findRecipes |
| 11 | + |
| 12 | + |
| 13 | +# ── Hilfsfunktionen ──────────────────────────────────────────── |
| 14 | + |
| 15 | +def makeRawRecipe(id: int, name: str, description: str = "") -> dict: |
| 16 | + return {"id": id, "name": name, "description": description} |
| 17 | + |
| 18 | +def makeRawIngredient(name: str, amount: float = 1.0) -> dict: |
| 19 | + return {"name": name, "amount": amount, "amountType": "g"} |
| 20 | + |
| 21 | +def mockIngredients(mapping: dict): |
| 22 | + return lambda rid: mapping.get(rid, []) |
| 23 | + |
| 24 | +def runFindRecipes(rawRecipes, ingredientMapping, searchIngredients, index=0): |
| 25 | + """Hilfsfunktion: patcht beide DB-Aufrufe und ruft findRecipes auf.""" |
| 26 | + with patch.object(RecipeSucuk, "getAllRecipes", return_value=rawRecipes), \ |
| 27 | + patch.object(IngredientModule, "getAllIngredientsForRecipe", |
| 28 | + side_effect=mockIngredients(ingredientMapping)): |
| 29 | + return findRecipes(searchIngredients, index) |
| 30 | + |
| 31 | + |
| 32 | +# ── Tests ────────────────────────────────────────────────────── |
| 33 | + |
| 34 | +class TestFindRecipesKeinTreffer: |
| 35 | + def testLeereDB(self): |
| 36 | + result = runFindRecipes([], {}, [Ingredient("Mehl", 1)]) |
| 37 | + assert result == [] |
| 38 | + |
| 39 | + def testKeineUebereinstimmung(self): |
| 40 | + raw = [makeRawRecipe(1, "Pasta")] |
| 41 | + ing = {1: [makeRawIngredient("Nudeln")]} |
| 42 | + result = runFindRecipes(raw, ing, [Ingredient("Mehl", 1)]) |
| 43 | + assert len(result) == 1 |
| 44 | + assert result[0].getRating() == 0.0 |
| 45 | + |
| 46 | + def testLeereZutatenliste(self): |
| 47 | + raw = [makeRawRecipe(1, "Pasta"), makeRawRecipe(2, "Pizza")] |
| 48 | + ing = {1: [makeRawIngredient("Nudeln")], 2: [makeRawIngredient("Teig")]} |
| 49 | + result = runFindRecipes(raw, ing, []) |
| 50 | + assert len(result) == 2 |
| 51 | + |
| 52 | + |
| 53 | +class TestFindRecipesTreffer: |
| 54 | + def testEinTreffer(self): |
| 55 | + raw = [makeRawRecipe(1, "Pasta")] |
| 56 | + ing = {1: [makeRawIngredient("Nudeln"), makeRawIngredient("Salz")]} |
| 57 | + result = runFindRecipes(raw, ing, [Ingredient("Nudeln", 1)]) |
| 58 | + assert len(result) == 1 |
| 59 | + assert result[0].getName() == "Pasta" |
| 60 | + assert result[0].getRating() == 0.5 # 1 von 2 Zutaten |
| 61 | + |
| 62 | + def testMehrereTrefferSortierungNachRating(self): |
| 63 | + raw = [makeRawRecipe(1, "Pasta"), makeRawRecipe(2, "Pizza")] |
| 64 | + ing = { |
| 65 | + 1: [makeRawIngredient("Nudeln"), makeRawIngredient("Salz")], |
| 66 | + 2: [makeRawIngredient("Teig")], |
| 67 | + } |
| 68 | + # Nudeln trifft Pasta (1/2 = 0.5), Teig trifft Pizza (1/1 = 1.0) |
| 69 | + result = runFindRecipes(raw, ing, [Ingredient("Nudeln", 1), Ingredient("Teig", 1)]) |
| 70 | + assert result[0].getName() == "Pizza" # Rating 1.0 |
| 71 | + assert result[1].getName() == "Pasta" # Rating 0.5 |
| 72 | + |
| 73 | + def testRatingBerechnung(self): |
| 74 | + raw = [makeRawRecipe(1, "Pasta")] |
| 75 | + ing = {1: [makeRawIngredient("A"), makeRawIngredient("B"), makeRawIngredient("C")]} |
| 76 | + result = runFindRecipes(raw, ing, [Ingredient("A", 1), Ingredient("B", 1)]) |
| 77 | + assert round(result[0].getRating(), 4) == round(2 / 3, 4) # 2 von 3 Zutaten |
| 78 | + |
| 79 | + def testAlleZutatenTreffen(self): |
| 80 | + raw = [makeRawRecipe(1, "Pasta")] |
| 81 | + ing = {1: [makeRawIngredient("Nudeln"), makeRawIngredient("Salz")]} |
| 82 | + result = runFindRecipes(raw, ing, [Ingredient("Nudeln", 1), Ingredient("Salz", 1)]) |
| 83 | + assert result[0].getRating() == 1.0 |
| 84 | + |
| 85 | + |
| 86 | +class TestFindRecipesPaginierung: |
| 87 | + def _buildRecipes(self, n: int): |
| 88 | + raw = [makeRawRecipe(i, f"Rezept{i}") for i in range(n)] |
| 89 | + ing = {i: [makeRawIngredient(f"Zutat{i}")] for i in range(n)} |
| 90 | + return raw, ing |
| 91 | + |
| 92 | + def testErsteSeite(self): |
| 93 | + raw, ing = self._buildRecipes(15) |
| 94 | + assert len(runFindRecipes(raw, ing, [])) == 12 |
| 95 | + |
| 96 | + def testZweiteSeite(self): |
| 97 | + raw, ing = self._buildRecipes(15) |
| 98 | + assert len(runFindRecipes(raw, ing, [], index=1)) == 3 |
| 99 | + |
| 100 | + def testLeereSeite(self): |
| 101 | + raw, ing = self._buildRecipes(5) |
| 102 | + assert runFindRecipes(raw, ing, [], index=1) == [] |
| 103 | + |
| 104 | + def testGenauZwoelfRezepte(self): |
| 105 | + raw, ing = self._buildRecipes(12) |
| 106 | + assert len(runFindRecipes(raw, ing, [], index=0)) == 12 |
| 107 | + assert runFindRecipes(raw, ing, [], index=1) == [] |
0 commit comments