Skip to content

Commit

Permalink
Write services tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkjellid committed Jun 14, 2024
1 parent 7e575f4 commit f259d9f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 9 deletions.
1 change: 1 addition & 0 deletions nest/recipes/plans/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def _get_products_dataframe(self) -> pl.DataFrame:
quantity=item.portion_quantity,
from_unit=item.portion_quantity_unit,
to_unit=product.unit,
piece_weight=product.unit_quantity,
)

if converted_quantity is None or product.unit_quantity is None:
Expand Down
4 changes: 2 additions & 2 deletions nest/recipes/plans/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.7 on 2024-06-12 13:23
# Generated by Django 4.2.7 on 2024-06-14 11:58

from django.db import migrations, models
import django.db.models.deletion
Expand All @@ -21,7 +21,7 @@ class Migration(migrations.Migration):
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created time')),
('updated_at', models.DateTimeField(auto_now=True, verbose_name='modified time')),
('title', models.CharField(max_length=50)),
('description', models.TextField(blank=True, max_length=100)),
('description', models.TextField(blank=True, max_length=100, null=True)),
('slug', models.SlugField()),
('from_date', models.DateTimeField(blank=True, null=True)),
('home', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='homes.home')),
Expand Down
2 changes: 1 addition & 1 deletion nest/recipes/plans/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RecipePlan(BaseModel):
"""

title = models.CharField(max_length=50)
description = models.TextField(max_length=100, blank=True)
description = models.TextField(max_length=100, blank=True, null=True)
slug = models.SlugField(max_length=50)
from_date = models.DateTimeField(blank=True, null=True)
home = models.ForeignKey(
Expand Down
87 changes: 81 additions & 6 deletions tests/recipes/test_plans_services.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,85 @@
def test_service_create_weekly_recipe_plan_for_home():
assert False
from decimal import Decimal

import pytest
from django.utils import timezone
from nest.recipes.plans.algorithm import PlanDistributor
from nest.recipes.plans.models import RecipePlan, RecipePlanItem
from nest.recipes.plans.services import create_recipe_plan, _create_recipe_plan_items
from tests.factories.records import RecipeDetailRecordFactory

def test_service_create_recipe_plan(immediate_on_commit):
assert False
pytestmark = pytest.mark.django_db


def test_service__create_recipe_plan_items():
assert False
def test_service_create_recipe_plan(
django_assert_num_queries, immediate_on_commit, mocker
):
"""
Test that the create_recipe_plan runs creates the plan instance and runs
the required methods to create a full plan.
"""
grace_period_weeks = 1
budget = Decimal("100.00")
num_items = 1
num_pescatarian = 1
num_vegetarian = 1
applicable_recipes = []

applicable_recipes_mock = mocker.patch(
"nest.recipes.plans.services.find_recipes_applicable_for_plan",
return_value=applicable_recipes,
)
create_items_mock = mocker.patch(
"nest.recipes.plans.services._create_recipe_plan_items"
)
distr_mock = mocker.patch.object(PlanDistributor, "create_plan", return_value=[])

initial_count = RecipePlan.objects.count()

with django_assert_num_queries(3), immediate_on_commit:
create_recipe_plan(
title="Example title",
description=None,
budget=budget,
num_items=num_items,
num_pescatarian=num_pescatarian,
num_vegetarian=num_vegetarian,
grace_period_weeks=grace_period_weeks,
from_date=timezone.now(),
)

# Assert that a new plan is created.
assert RecipePlan.objects.count() == initial_count + 1

# Assert that mocks are called accordingly.
applicable_recipes_mock.assert_called_once_with(
grace_period_weeks=grace_period_weeks,
)
distr_mock.assert_called_once()
create_items_mock.assert_called_once()


@pytest.mark.recipes(
recipe1={"title": " Recipe 1"},
recipe2={"title": " Recipe 2"},
recipe3={"title": " Recipe 3"},
)
@pytest.mark.recipe_plan(title="My plan")
def test_service__create_recipe_plan_items(
django_assert_num_queries, recipes, recipe_plan
):
"""
Test that the _create_recipe_plan_items creates plan items associated to the correct
recipe plan.
"""
recipe_records = [
RecipeDetailRecordFactory.build(id=recipe.id) for recipe in recipes.values()
]

initial_count = RecipePlanItem.objects.filter(recipe_plan_id=recipe_plan.id).count()

with django_assert_num_queries(2):
_create_recipe_plan_items(plan_id=recipe_plan.id, recipes=recipe_records)

assert RecipePlanItem.objects.filter(
recipe_plan_id=recipe_plan.id
).count() == initial_count + len(recipe_records)

0 comments on commit f259d9f

Please sign in to comment.