mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-06 06:38:03 -05:00
pytest shopping user preferences
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -84,3 +84,4 @@ cookbook/static/vue
|
|||||||
vue/webpack-stats.json
|
vue/webpack-stats.json
|
||||||
cookbook/templates/sw.js
|
cookbook/templates/sw.js
|
||||||
.prettierignore
|
.prettierignore
|
||||||
|
vue/tsconfig.json
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ from django.utils import timezone
|
|||||||
from django_scopes import scopes_disabled
|
from django_scopes import scopes_disabled
|
||||||
from pytest_factoryboy import LazyFixture, register
|
from pytest_factoryboy import LazyFixture, register
|
||||||
|
|
||||||
from cookbook.models import Ingredient, ShoppingListEntry
|
from cookbook.models import Food, Ingredient, ShoppingListEntry
|
||||||
from cookbook.tests.factories import MealPlanFactory, RecipeFactory, UserFactory
|
from cookbook.tests.factories import MealPlanFactory, RecipeFactory, StepFactory, UserFactory
|
||||||
|
|
||||||
SHOPPING_LIST_URL = 'api:shoppinglistentry-list'
|
SHOPPING_LIST_URL = 'api:shoppinglistentry-list'
|
||||||
SHOPPING_RECIPE_URL = 'api:recipe-shopping'
|
SHOPPING_RECIPE_URL = 'api:recipe-shopping'
|
||||||
@@ -24,11 +24,10 @@ def user2(request, u1_s1):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
params = {}
|
params = {}
|
||||||
user = auth.get_user(u1_s1)
|
user = auth.get_user(u1_s1)
|
||||||
user.userpreference.mealplan_autoadd_shopping = True
|
user.userpreference.mealplan_autoadd_shopping = params.get('mealplan_autoadd_shopping', True)
|
||||||
user.userpreference.mealplan_autoinclude_related = True
|
user.userpreference.mealplan_autoinclude_related = params.get('mealplan_autoinclude_related', True)
|
||||||
key = list(params)[0]
|
user.userpreference.mealplan_autoexclude_onhand = params.get('mealplan_autoexclude_onhand', True)
|
||||||
setattr(user.userpreference, key, params[key])
|
user.userpreference.save()
|
||||||
user.userpreference.save
|
|
||||||
return u1_s1
|
return u1_s1
|
||||||
|
|
||||||
|
|
||||||
@@ -106,6 +105,7 @@ def test_shopping_recipe_method(request, arg, recipe, sle_count, u1_s1, u2_s1):
|
|||||||
@pytest.mark.parametrize("use_mealplan", [(False), (True), ])
|
@pytest.mark.parametrize("use_mealplan", [(False), (True), ])
|
||||||
def test_shopping_recipe_edit(request, recipe, sle_count, use_mealplan, u1_s1, u2_s1):
|
def test_shopping_recipe_edit(request, recipe, sle_count, use_mealplan, u1_s1, u2_s1):
|
||||||
# tests editing shopping list via recipe or mealplan
|
# tests editing shopping list via recipe or mealplan
|
||||||
|
with scopes_disabled():
|
||||||
user = auth.get_user(u1_s1)
|
user = auth.get_user(u1_s1)
|
||||||
user2 = auth.get_user(u2_s1)
|
user2 = auth.get_user(u2_s1)
|
||||||
user.userpreference.mealplan_autoinclude_related = True
|
user.userpreference.mealplan_autoinclude_related = True
|
||||||
@@ -174,26 +174,54 @@ def test_shopping_recipe_edit(request, recipe, sle_count, use_mealplan, u1_s1, u
|
|||||||
assert len(json.loads(u2_s1.get(reverse(SHOPPING_LIST_URL)).content)) == sle_count
|
assert len(json.loads(u2_s1.get(reverse(SHOPPING_LIST_URL)).content)) == sle_count
|
||||||
|
|
||||||
|
|
||||||
# auto add shopping, no
|
|
||||||
# auto include related, no
|
|
||||||
# auto ignore onhand
|
|
||||||
# ignore shopping food
|
|
||||||
@pytest.mark.parametrize("user2, sle_count", [
|
@pytest.mark.parametrize("user2, sle_count", [
|
||||||
({'mealplan_autoadd_shopping': False}, 0),
|
({'mealplan_autoadd_shopping': False}, (0, 17)),
|
||||||
({'mealplan_autoinclude_related': False}, 9),
|
({'mealplan_autoinclude_related': False}, (7, 7)),
|
||||||
({'mealplan_autoexclude_onhand': True}, 27), # shopping list from recipe with StepRecipe
|
({'mealplan_autoexclude_onhand': False}, (19, 19)),
|
||||||
], indirect=['user2'])
|
], indirect=['user2'])
|
||||||
@pytest.mark.parametrize("use_mealplan", [(False), (True), ])
|
@pytest.mark.parametrize("use_mealplan", [(False), (True), ])
|
||||||
@pytest.mark.parametrize("recipe", [({'steps__recipe_count': 1})], indirect=['recipe'])
|
@pytest.mark.parametrize("recipe", [({'steps__recipe_count': 1})], indirect=['recipe'])
|
||||||
def test_shopping_recipe_userpreference(request, recipe, sle_count, use_mealplan, user2):
|
def test_shopping_recipe_userpreference(recipe, sle_count, use_mealplan, user2):
|
||||||
with scopes_disabled():
|
with scopes_disabled():
|
||||||
user = auth.get_user(user2)
|
user = auth.get_user(user2)
|
||||||
# setup recipe with 10 ingredients, 1 step recipe with 10 ingredients, 1 food onhand, 1 food ignore shopping
|
# setup recipe with 10 ingredients, 1 step recipe with 10 ingredients, 2 food onhand(from recipe and step_recipe), 1 food ignore shopping
|
||||||
recipe.step.all()
|
ingredients = Ingredient.objects.filter(step__recipe=recipe)
|
||||||
|
food = Food.objects.get(id=ingredients[0].food.id)
|
||||||
|
food.on_hand = True
|
||||||
|
food.save()
|
||||||
|
food = Step.objects.filter(type=Step.RECIPE).first().ingredients.first()
|
||||||
|
food.on_hand = True
|
||||||
|
food.save()
|
||||||
|
food = Food.objects.get(id=ingredients[2].food.id)
|
||||||
|
food.ignore_shopping = True
|
||||||
|
food.save()
|
||||||
|
|
||||||
|
if use_mealplan:
|
||||||
|
mealplan = MealPlanFactory(space=recipe.space, created_by=user, servings=recipe.servings, recipe=recipe)
|
||||||
|
assert len(json.loads(user2.get(reverse(SHOPPING_LIST_URL)).content)) == sle_count[0]
|
||||||
|
else:
|
||||||
|
user2.put(reverse(SHOPPING_RECIPE_URL, args={recipe.id}))
|
||||||
|
assert len(json.loads(user2.get(reverse(SHOPPING_LIST_URL)).content)) == sle_count[1]
|
||||||
|
|
||||||
|
|
||||||
def test_shopping_recipe_mixed_authors(request, user2):
|
def test_shopping_recipe_mixed_authors(u1_s1, u2_s1):
|
||||||
assert 1 == 1
|
with scopes_disabled():
|
||||||
|
user1 = auth.get_user(u1_s1)
|
||||||
|
user2 = auth.get_user(u2_s1)
|
||||||
|
space = user1.userpreference.space
|
||||||
|
user3 = UserFactory(space=space)
|
||||||
|
recipe1 = RecipeFactory(created_by=user1, space=space)
|
||||||
|
recipe2 = RecipeFactory(created_by=user2, space=space)
|
||||||
|
recipe3 = RecipeFactory(created_by=user3, space=space)
|
||||||
|
food = Food.objects.get(id=recipe1.steps.first().ingredients.first().food.id)
|
||||||
|
food.recipe = recipe2
|
||||||
|
food.save()
|
||||||
|
recipe1.steps.add(StepFactory(step_recipe=recipe3, ingredients__count=0, space=space))
|
||||||
|
recipe1.save()
|
||||||
|
|
||||||
|
u1_s1.put(reverse(SHOPPING_RECIPE_URL, args={recipe1.id}))
|
||||||
|
assert len(json.loads(u1_s1.get(reverse(SHOPPING_LIST_URL)).content)) == 29
|
||||||
|
assert len(json.loads(u2_s1.get(reverse(SHOPPING_LIST_URL)).content)) == 0
|
||||||
|
|
||||||
|
|
||||||
# TODO test creating shopping list from recipe that includes recipes from multiple users
|
# TODO test creating shopping list from recipe that includes recipes from multiple users
|
||||||
|
|||||||
@@ -267,7 +267,10 @@ class StepFactory(factory.django.DjangoModelFactory):
|
|||||||
if not create:
|
if not create:
|
||||||
return
|
return
|
||||||
if kwargs.get('has_recipe', False):
|
if kwargs.get('has_recipe', False):
|
||||||
step_recipe = RecipeFactory(space=self.space)
|
self.step_recipe = RecipeFactory(space=self.space)
|
||||||
|
self.type = Step.RECIPE
|
||||||
|
elif extracted:
|
||||||
|
self.step_recipe = extracted
|
||||||
self.type = Step.RECIPE
|
self.type = Step.RECIPE
|
||||||
|
|
||||||
@factory.post_generation
|
@factory.post_generation
|
||||||
@@ -340,7 +343,7 @@ class RecipeFactory(factory.django.DjangoModelFactory):
|
|||||||
self.steps.add(StepFactory(space=self.space, ingredients__food_recipe_count=ing_recipe_count))
|
self.steps.add(StepFactory(space=self.space, ingredients__food_recipe_count=ing_recipe_count))
|
||||||
if num_recipe_steps > 0:
|
if num_recipe_steps > 0:
|
||||||
for j in range(num_recipe_steps):
|
for j in range(num_recipe_steps):
|
||||||
self.steps.add(StepFactory(space=self.space, step_recipe__has_recipe=True))
|
self.steps.add(StepFactory(space=self.space, step_recipe__has_recipe=True, ingredients__count=0))
|
||||||
if extracted and (num_steps + num_recipe_steps == 0):
|
if extracted and (num_steps + num_recipe_steps == 0):
|
||||||
for step in extracted:
|
for step in extracted:
|
||||||
self.steps.add(step)
|
self.steps.add(step)
|
||||||
|
|||||||
Reference in New Issue
Block a user