Skip to content

Commit

Permalink
Write recipe fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkjellid committed Jan 23, 2024
1 parent 5740aa4 commit b9bca7f
Showing 1 changed file with 270 additions and 5 deletions.
275 changes: 270 additions & 5 deletions tests/recipes/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
from typing import Any, Callable, TypedDict

import pytest

from decimal import Decimal
from nest.products.core.models import Product
from nest.recipes.core.enums import RecipeDifficulty, RecipeStatus
from nest.recipes.core.models import Recipe
from nest.recipes.ingredients.models import RecipeIngredient, RecipeIngredientItemGroup

from nest.recipes.ingredients.models import (
RecipeIngredient,
RecipeIngredientItemGroup,
RecipeIngredientItem,
)
from nest.recipes.steps.enums import RecipeStepType
from nest.recipes.steps.models import RecipeStep
from nest.units.models import Unit
from ..factories.fixtures import (
get_instance,
get_spec_for_instance,
instance,
instances,
)
from datetime import timedelta
from ..products.fixtures import ProductSpec


Expand Down Expand Up @@ -303,8 +310,266 @@ def _get_recipe_ingredient_spec(slug: str) -> RecipeIngredientItemGroupSpec:
return _get_recipe_ingredient_spec


# TODO: RecipeIngredientItem
@pytest.fixture
def get_recipe_ingredient_item_group(
create_recipe_ingredient_item_group: CreateRecipeIngredientItemGroup,
get_recipe_ingredient_item_group_spec: Callable[
[str], RecipeIngredientItemGroupSpec
],
) -> Callable[[str], RecipeIngredientItemGroup]:
recipe_ingredient_item_groups: dict[str, RecipeIngredientItemGroup] = {}

def get_or_create_recipe_ingredient_item_group(
slug: str
) -> RecipeIngredientItemGroup:
return get_instance(
slug=slug,
instances=recipe_ingredient_item_groups,
create_callback=create_recipe_ingredient_item_group,
get_spec_callback=get_recipe_ingredient_item_group_spec,
)

return get_or_create_recipe_ingredient_item_group


@pytest.fixture
def recipe_ingredient_item_group(
request: pytest.FixtureRequest,
create_recipe_ingredient_item_group: CreateRecipeIngredientItemGroup,
default_recipe_ingredient_item_group_spec: RecipeIngredientItemGroupSpec,
) -> RecipeIngredientItemGroup:
return instance(
create_callback=create_recipe_ingredient_item_group,
default_spec=default_recipe_ingredient_item_group_spec,
request=request,
marker="recipe_ingredient_item_group",
)


@pytest.fixture
def recipe_ingredient_item_groups(
request: pytest.FixtureRequest,
get_recipe_ingredient_item_group: Callable[[str], RecipeIngredientItemGroup],
) -> dict[str, RecipeIngredientItemGroup]:
return instances(
request=request,
markers="recipe_ingredient_item_groups",
get_instance_callback=get_recipe_ingredient_item_group,
)


class RecipeStepSpec(TypedDict, total=False):
recipe: dict[str, RecipeSpec]
number: int
duration: timedelta
instruction: str
step_type: RecipeStepType


CreateRecipeStep = Callable[[RecipeStepSpec], RecipeStep]


@pytest.fixture
def create_recipe_step(
db: Any, get_recipe: Callable[[str], Recipe]
) -> CreateRecipeStep:
def _create_recipe_step(spec: RecipeStepSpec) -> RecipeStep:
recipe_from_spec = get_recipe(spec["recipe"])
recipe_step = RecipeStep.objects.create(
recipe=recipe_from_spec,
number=spec["number"],
duration=spec["duration"],
instruction=spec["instruction"],
step_type=spec["step_type"],
)

return recipe_step

return _create_recipe_step


@pytest.fixture
def default_recipe_step_spec(
request: pytest.FixtureRequest, recipe: Recipe
) -> RecipeStepSpec:
return RecipeStepSpec(
recipe=recipe,
number=1,
duration=timedelta(minutes=15),
instruction="A sample instruction",
step_type=RecipeStepType.COOKING,
)


@pytest.fixture
def get_recipe_step_spec(
default_recipe_step_spec: RecipeStepSpec, request: pytest.FixtureRequest
) -> RecipeStepSpec:
def _get_recipe_step_spec(slug: str) -> RecipeStepSpec:
return get_spec_for_instance(
slug=slug,
default_spec=default_recipe_step_spec,
request=request,
marker="recipe_step",
)

return _get_recipe_step_spec


@pytest.fixture
def get_recipe_step(
create_recipe_step: CreateRecipeStep,
get_recipe_step_spec: Callable[[str], RecipeStepSpec],
) -> Callable[[str], RecipeStep]:
recipe_steps: dict[str, RecipeStep] = {}

def get_or_create_recipe_step(slug: str) -> RecipeStep:
return get_instance(
slug=slug,
instances=recipe_steps,
create_callback=create_recipe_step,
get_spec_callback=get_recipe_step_spec,
)

return get_or_create_recipe_step


@pytest.fixture
def recipe_step(
request: pytest.FixtureRequest,
create_recipe_step: CreateRecipeStep,
default_recipe_step_spec: RecipeStepSpec,
) -> RecipeStep:
return instance(
create_callback=create_recipe_step,
default_spec=default_recipe_step_spec,
request=request,
marker="recipe_step",
)


@pytest.fixture
def recipe_steps(
get_recipe: Callable[[str], RecipeStep], request: pytest.FixtureRequest
) -> dict[str, RecipeStep]:
return instances(
request=request,
markers="recipe_steps",
get_instance_callback=get_recipe,
)


class RecipeIngredientItemSpec(TypedDict, total=False):
...
ingredient_group: dict[str, RecipeIngredientItemGroup]
ingredient: dict[str, RecipeIngredientSpec]
step: dict[str, RecipeStepSpec]
additional_info: str | None
portion_quantity: Decimal
portion_quantity_unit: str


CreateRecipeIngredientItem = Callable[[RecipeIngredientItemSpec], RecipeIngredientItem]


@pytest.fixture
def create_recipe_ingredient_item(
db: Any,
get_recipe_ingredient_item_group: Callable[[str], RecipeIngredientItemGroup],
get_recipe_ingredient: Callable[[str], RecipeIngredient],
get_recipe_step: Callable[[str], RecipeStep],
get_unit: Callable[[str], Unit],
) -> CreateRecipeIngredientItem:
def _create_recipe_ingredient_item(spec: RecipeIngredientItemSpec):
ingredient_group_from_spec = get_recipe_ingredient_item_group(
spec.pop("ingredient_group")
)
ingredient_from_spec = get_recipe_ingredient(spec.pop("ingredient"))
step_from_spec = get_recipe_step(spec.pop("step"))
portion_unit_from_spec = get_unit(spec.pop("portion_quantity_unit"))

return RecipeIngredientItem.objects.create(
ingredient_group=ingredient_group_from_spec,
ingredient=ingredient_from_spec,
step=step_from_spec,
portion_quantity_unit=portion_unit_from_spec,
**spec,
)

return _create_recipe_ingredient_item


@pytest.fixture
def default_recipe_ingredient_item_spec(
request: pytest.FixtureRequest,
recipe_ingredient_item_group: RecipeIngredientItemGroup,
recipe_ingredient: RecipeIngredient,
recipe_step: RecipeStep,
) -> RecipeIngredientItem:
return RecipeIngredientItemSpec(
ingredient_group=recipe_ingredient_item_group,
ingredient=recipe_ingredient,
step=recipe_step,
portion_quantity_unit="g",
portion_quantity=Decimal("100.00"),
additional_info=None,
)


@pytest.fixture
def get_recipe_ingredient_item_spec(
default_recipe_ingredient_item_spec: RecipeIngredientItemSpec,
request: pytest.FixtureRequest,
) -> RecipeIngredientItemSpec:
def _get_recipe_ingredient_item_spec(slug: str) -> RecipeIngredientItemSpec:
return get_spec_for_instance(
slug=slug,
default_spec=default_recipe_ingredient_item_spec,
request=request,
marker="recipe_ingredient_item",
)

return _get_recipe_ingredient_item_spec


@pytest.fixture
def get_recipe_ingredient_item(
create_recipe_ingredient_item: CreateRecipeIngredientItem,
get_recipe_ingredient_item_spec: Callable[[str], RecipeIngredientItemSpec],
) -> Callable[[str], RecipeIngredientItem]:
recipe_ingredient_items: dict[str, RecipeIngredientItem] = {}

def get_or_create_recipe_ingredient_item(slug: str) -> RecipeIngredientItem:
return get_instance(
slug=slug,
instances=recipe_ingredient_items,
create_callback=create_recipe_ingredient_item,
get_spec_callback=get_recipe_ingredient_item_spec,
)

return get_or_create_recipe_ingredient_item


@pytest.fixture
def recipe_ingredient_item(
request: pytest.FixtureRequest,
create_recipe_ingredient_item: CreateRecipeIngredientItem,
default_recipe_ingredient_item_spec: RecipeIngredientItemSpec,
) -> RecipeIngredientItem:
return instance(
create_callback=create_recipe_ingredient_item,
default_spec=default_recipe_ingredient_item_spec,
request=request,
marker="recipe_ingredient_item",
)


@pytest.fixture
def recipe_ingredient_items(
get_recipe_ingredient_item: Callable[[str], RecipeIngredientItem],
request: pytest.FixtureRequest,
) -> dict[str, RecipeIngredientItem]:
return instances(
request=request,
markers="recipe_ingredient_items",
get_instance_callback=get_recipe_ingredient_item,
)

0 comments on commit b9bca7f

Please sign in to comment.