Skip to content

Commit

Permalink
Write two more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkjellid committed Jun 19, 2024
1 parent 9fc15df commit 3915af0
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 8 deletions.
6 changes: 3 additions & 3 deletions nest/recipes/steps/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ def _find_step_id_for_step_item(
def create_or_update_recipe_step_ingredient_items(
recipe_id: int, steps: list[Step]
) -> None:
recipe_steps = RecipeStep.objects.filter(recipe_id=recipe_id)
recipe_ingredient_items = RecipeIngredientItem.objects.filter(
ingredient_group__recipe_id=recipe_id
recipe_steps = list(RecipeStep.objects.filter(recipe_id=recipe_id))
recipe_ingredient_items = list(
RecipeIngredientItem.objects.filter(ingredient_group__recipe_id=recipe_id)
)
existing_relations = list(
RecipeStepIngredientItem.objects.filter(step__recipe_id=recipe_id)
Expand Down
144 changes: 139 additions & 5 deletions tests/recipes/test_steps_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

import pytest

from nest.recipes.ingredients.models import RecipeIngredientItem
from nest.recipes.ingredients.services import IngredientItem
from nest.recipes.steps.enums import RecipeStepType
from nest.recipes.steps.models import RecipeStep
from nest.recipes.steps.services import Step, create_or_update_recipe_steps
from nest.recipes.steps.services import (
Step,
create_or_update_recipe_steps,
_find_step_id_for_step_item,
_find_ingredient_item_id_for_step_item,
)


@pytest.mark.recipe
Expand Down Expand Up @@ -110,12 +116,140 @@ def test_service_create_or_update_recipe_steps(
)


def test__find_ingredient_item_id_for_step_item():
assert False
@pytest.mark.products(
product1={"name": "Product 1"},
product2={"name": "Product 2"},
product3={"name": "Product 3"},
)
@pytest.mark.recipe_ingredients(
ingredient1={"title": "Green peppers", "product": "product1"},
ingredient2={"title": "Cod", "product": "product2"},
ingredient3={"title": "Parsly", "product": "product3"},
)
@pytest.mark.recipe_ingredient_items(
item1={"ingredient": "ingredient1"},
item2={"ingredient": "ingredient2"},
item3={"ingredient": "ingredient3"},
)
@pytest.mark.parametrize("item", ("item1", "item2", "item3", "other"))
def test__find_ingredient_item_id_for_step_item(
item, recipe_ingredient_items, django_assert_num_queries
):
"""
Test that the _find_ingredient_item_id_for_step_item service util is able to find
the correct ingredient item id for a given ingredient item.
"""
default_item = recipe_ingredient_items["item1"]
current_item = recipe_ingredient_items[item] if item != "other" else None

item_id = getattr(current_item, "id", None) # Default None - important!
ingredient_id = getattr(current_item, "ingredient_id", default_item.ingredient_id)
portion_quantity = getattr(
current_item, "portion_quantity", default_item.portion_quantity
)
portion_quantity_unit_id = getattr(
current_item, "portion_quantity_unit_id", default_item.portion_quantity_unit_id
)
additional_info = getattr(
current_item, "additional_info", default_item.additional_info
)

db_items = list(RecipeIngredientItem.objects.all())

def test__find_step_id_for_step_item():
assert False
with django_assert_num_queries(0):
found_item_id = _find_ingredient_item_id_for_step_item(
item=IngredientItem(
id=item_id,
ingredient=ingredient_id,
portion_quantity=portion_quantity,
portion_quantity_unit=portion_quantity_unit_id,
additional_info=additional_info,
),
recipe_ingredient_items=db_items,
)

# In the run with "other", we do not send in the id, to see if we're able to find
# the item based on the given mapping (which is a copy of the default_step attrs)
if item == "other":
assert found_item_id == default_item.id
else:
assert found_item_id == current_item.id

with pytest.raises(StopIteration):
_find_ingredient_item_id_for_step_item(
item=IngredientItem(
id=None,
ingredient=999,
portion_quantity=10,
portion_quantity_unit=portion_quantity_unit_id,
additional_info=additional_info,
),
recipe_ingredient_items=db_items,
)


@pytest.mark.recipe_steps(
step1={"number": 1},
step2={"number": 2},
step3={"number": 3},
)
@pytest.mark.parametrize("step", ("step1", "step2", "step3", "other"))
def test__find_step_id_for_step_item(
step,
recipe_steps,
recipe_ingredient_items,
django_assert_num_queries,
):
"""
Test that the _find_step_id_for_step_item service util is able to find the correct
step instance based on given item.
"""

default_step = recipe_steps["step1"]
current_step = recipe_steps[step] if step != "other" else None

step_id = getattr(current_step, "id", None) # Default None - important!
number = getattr(current_step, "number", default_step.number)
duration_in_minutes = (
getattr(current_step, "duration", default_step.duration).seconds / 60
)
instruction = getattr(current_step, "instruction", default_step.instruction)
step_type = getattr(current_step, "step_type", default_step.step_type).value

db_steps = list(RecipeStep.objects.all())

with django_assert_num_queries(0):
found_step_id = _find_step_id_for_step_item(
step=Step(
id=step_id,
number=number,
duration=duration_in_minutes,
instruction=instruction,
step_type=step_type,
ingredient_items=[],
),
recipe_steps=db_steps,
)

# In the run with "other", we do not send in the id, to see if we're able to find
# the step based on the given mapping (which is a copy of the default_step attrs)
if step == "other":
assert found_step_id == default_step.id
else:
assert found_step_id == current_step.id

with pytest.raises(StopIteration):
_find_step_id_for_step_item(
step=Step(
id=None,
number=99999,
duration=12343123,
instruction="This step does not exist",
step_type=step_type,
ingredient_items=[],
),
recipe_steps=db_steps,
)


def test_service_create_or_update_recipe_step_ingredient_items():
Expand Down

0 comments on commit 3915af0

Please sign in to comment.