Skip to content

Commit

Permalink
Fix n+1 and write test for new selector
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkjellid committed Jun 19, 2024
1 parent d9eb5bc commit 9fc15df
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
6 changes: 5 additions & 1 deletion nest/recipes/steps/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ def get_step_ingredient_items_for_steps(

step_ingredient_items = RecipeStepIngredientItem.objects.filter(
step_id__in=step_ids
).select_related("ingredient_item__ingredient__product__unit")
).select_related(
"ingredient_item__ingredient_group",
"ingredient_item__ingredient__product__unit",
"ingredient_item__portion_quantity_unit",
)

for step_ingredient_item in step_ingredient_items:
if step_ingredient_item.ingredient_item is None:
Expand Down
55 changes: 53 additions & 2 deletions tests/recipes/test_steps_selectors.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,53 @@
def test_selector_get_step_ingredient_items_for_steps():
assert False
import pytest

from nest.recipes.steps.selectors import get_step_ingredient_items_for_steps


@pytest.mark.products(
product1={"name": "product1"},
product2={"name": "product2"},
product3={"name": "product3"},
)
@pytest.mark.recipe_ingredients(
ingredient1={"title": "Ingredient 1", "product": "product1"},
ingredient2={"title": "Ingredient 2", "product": "product2"},
ingredient3={"title": "Ingredient 3", "product": "product3"},
)
@pytest.mark.recipe_ingredient_item_groups(
group1={"title": "Group 1"},
group2={"title": "Group 2"},
)
@pytest.mark.recipe_ingredient_items(
item1={"ingredient_group": "group1", "ingredient": "ingredient1"},
item2={"ingredient_group": "group1", "ingredient": "ingredient2"},
item3={"ingredient_group": "group2", "ingredient": "ingredient3"},
)
@pytest.mark.recipe_steps(
step1={"number": 1},
step2={"number": 2},
step3={"number": 3},
step4={"number": 4},
)
@pytest.mark.recipe_step_ingredient_items(
step_item1={"step": "step1", "ingredient_item": "item1"},
step_item2={"step": "step1", "ingredient_item": "item2"},
step_item3={"step": "step2", "ingredient_item": "item1"},
step_item4={"step": "step4", "ingredient_item": "item3"},
)
def test_selector_get_step_ingredient_items_for_steps(
recipe_steps, recipe_step_ingredient_items, django_assert_num_queries
):
"""
Test that the get_step_ingredient_items_for_steps selector correctly retrieves
result within query limits.
"""
step_ids = [step.id for step in recipe_steps.values()]

with django_assert_num_queries(1):
step_items = get_step_ingredient_items_for_steps(step_ids=step_ids)

assert len(step_items.keys()) == len(step_ids)
assert len(step_items[recipe_steps["step1"].id]) == 2
assert len(step_items[recipe_steps["step2"].id]) == 1
assert len(step_items[recipe_steps["step3"].id]) == 0
assert len(step_items[recipe_steps["step4"].id]) == 1

0 comments on commit 9fc15df

Please sign in to comment.