mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-10 16:47:59 -05:00
Squashed commit of shoppinglist_v2
This commit is contained in:
@@ -57,7 +57,19 @@ def obj_tree_1(request, space_1):
|
||||
except AttributeError:
|
||||
params = {}
|
||||
objs = []
|
||||
inherit = params.pop('inherit', False)
|
||||
objs.extend(FoodFactory.create_batch(3, space=space_1, **params))
|
||||
|
||||
# set all foods to inherit everything
|
||||
if inherit:
|
||||
inherit = Food.inheritable_fields
|
||||
Through = Food.objects.filter(space=space_1).first().inherit_fields.through
|
||||
for i in inherit:
|
||||
Through.objects.bulk_create([
|
||||
Through(food_id=x, foodinheritfield_id=i.id)
|
||||
for x in Food.objects.filter(space=space_1).values_list('id', flat=True)
|
||||
])
|
||||
|
||||
objs[0].move(objs[1], node_location)
|
||||
objs[1].move(objs[2], node_location)
|
||||
return Food.objects.get(id=objs[1].id) # whenever you move/merge a tree it's safest to re-get the object
|
||||
@@ -471,8 +483,8 @@ def test_tree_filter(obj_tree_1, obj_2, obj_3, u1_s1):
|
||||
@pytest.mark.parametrize("obj_tree_1, field, inherit, new_val", [
|
||||
({'has_category': True, 'inherit': True}, 'supermarket_category', True, 'cat_1'),
|
||||
({'has_category': True, 'inherit': False}, 'supermarket_category', False, 'cat_1'),
|
||||
({'ignore_shopping': True, 'inherit': True}, 'ignore_shopping', True, 'false'),
|
||||
({'ignore_shopping': True, 'inherit': False}, 'ignore_shopping', False, 'false'),
|
||||
({'food_onhand': True, 'inherit': True}, 'food_onhand', True, 'false'),
|
||||
({'food_onhand': True, 'inherit': False}, 'food_onhand', False, 'false'),
|
||||
], indirect=['obj_tree_1']) # indirect=True populates magic variable request.param of obj_tree_1 with the parameter
|
||||
def test_inherit(request, obj_tree_1, field, inherit, new_val, u1_s1):
|
||||
with scope(space=obj_tree_1.space):
|
||||
@@ -496,47 +508,17 @@ def test_inherit(request, obj_tree_1, field, inherit, new_val, u1_s1):
|
||||
assert (getattr(child, field) == new_val) == inherit
|
||||
|
||||
|
||||
@pytest.mark.parametrize("obj_tree_1, field, inherit, new_val", [
|
||||
({'has_category': True, 'inherit': True, }, 'supermarket_category', True, 'cat_1'),
|
||||
({'ignore_shopping': True, 'inherit': True, }, 'ignore_shopping', True, 'false'),
|
||||
], indirect=['obj_tree_1'])
|
||||
# This is more about the model than the API - should this be moved to a different test?
|
||||
def test_ignoreinherit_field(request, obj_tree_1, field, inherit, new_val, u1_s1):
|
||||
with scope(space=obj_tree_1.space):
|
||||
parent = obj_tree_1.get_parent()
|
||||
child = obj_tree_1.get_descendants()[0]
|
||||
obj_tree_1.ignore_inherit.add(FoodInheritField.objects.get(field=field))
|
||||
new_val = request.getfixturevalue(new_val)
|
||||
|
||||
# change parent to a new value
|
||||
setattr(parent, field, new_val)
|
||||
with scope(space=parent.space):
|
||||
parent.save() # trigger post-save signal
|
||||
# get the objects again because values are cached
|
||||
obj_tree_1 = Food.objects.get(id=obj_tree_1.id)
|
||||
# inheritance is blocked - should not get new value
|
||||
assert getattr(obj_tree_1, field) != new_val
|
||||
|
||||
setattr(obj_tree_1, field, new_val)
|
||||
with scope(space=parent.space):
|
||||
obj_tree_1.save() # trigger post-save signal
|
||||
# get the objects again because values are cached
|
||||
child = Food.objects.get(id=child.id)
|
||||
# inherit with child should still work
|
||||
assert getattr(child, field) == new_val
|
||||
|
||||
|
||||
@pytest.mark.parametrize("obj_tree_1", [
|
||||
({'has_category': True, 'inherit': False, 'ignore_shopping': True}),
|
||||
({'has_category': True, 'inherit': False, 'food_onhand': True}),
|
||||
], indirect=['obj_tree_1'])
|
||||
def test_reset_inherit(obj_tree_1, space_1):
|
||||
with scope(space=space_1):
|
||||
space_1.food_inherit.add(*Food.inherit_fields.values_list('id', flat=True)) # set default inherit fields
|
||||
space_1.food_inherit.add(*Food.inheritable_fields.values_list('id', flat=True)) # set default inherit fields
|
||||
parent = obj_tree_1.get_parent()
|
||||
child = obj_tree_1.get_descendants()[0]
|
||||
obj_tree_1.ignore_shopping = False
|
||||
assert parent.ignore_shopping == child.ignore_shopping
|
||||
assert parent.ignore_shopping != obj_tree_1.ignore_shopping
|
||||
obj_tree_1.food_onhand = False
|
||||
assert parent.food_onhand == child.food_onhand
|
||||
assert parent.food_onhand != obj_tree_1.food_onhand
|
||||
assert parent.supermarket_category != child.supermarket_category
|
||||
assert parent.supermarket_category != obj_tree_1.supermarket_category
|
||||
|
||||
@@ -545,5 +527,5 @@ def test_reset_inherit(obj_tree_1, space_1):
|
||||
obj_tree_1 = Food.objects.get(id=obj_tree_1.id)
|
||||
parent = obj_tree_1.get_parent()
|
||||
child = obj_tree_1.get_descendants()[0]
|
||||
assert parent.ignore_shopping == obj_tree_1.ignore_shopping == child.ignore_shopping
|
||||
assert parent.food_onhand == obj_tree_1.food_onhand == child.food_onhand
|
||||
assert parent.supermarket_category == obj_tree_1.supermarket_category == child.supermarket_category
|
||||
|
||||
@@ -3,6 +3,8 @@ from datetime import timedelta
|
||||
|
||||
import factory
|
||||
import pytest
|
||||
# work around for bug described here https://stackoverflow.com/a/70312265/15762829
|
||||
from django.conf import settings
|
||||
from django.contrib import auth
|
||||
from django.forms import model_to_dict
|
||||
from django.urls import reverse
|
||||
@@ -14,6 +16,11 @@ from cookbook.models import Food, Ingredient, ShoppingListEntry, Step
|
||||
from cookbook.tests.factories import (IngredientFactory, MealPlanFactory, RecipeFactory,
|
||||
StepFactory, UserFactory)
|
||||
|
||||
if settings.DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2',
|
||||
'django.db.backends.postgresql']:
|
||||
from django.db.backends.postgresql.features import DatabaseFeatures
|
||||
DatabaseFeatures.can_defer_constraint_checks = False
|
||||
|
||||
SHOPPING_LIST_URL = 'api:shoppinglistentry-list'
|
||||
SHOPPING_RECIPE_URL = 'api:recipe-shopping'
|
||||
|
||||
@@ -43,7 +50,7 @@ def recipe(request, space_1, u1_s1):
|
||||
# steps__food_recipe_count = params.get('steps__food_recipe_count', {})
|
||||
params['created_by'] = params.get('created_by', auth.get_user(u1_s1))
|
||||
params['space'] = space_1
|
||||
return RecipeFactory.create(**params)
|
||||
return RecipeFactory(**params)
|
||||
|
||||
# return RecipeFactory.create(
|
||||
# steps__recipe_count=steps__recipe_count,
|
||||
@@ -178,27 +185,24 @@ def test_shopping_recipe_edit(request, recipe, sle_count, use_mealplan, u1_s1, u
|
||||
|
||||
|
||||
@pytest.mark.parametrize("user2, sle_count", [
|
||||
({'mealplan_autoadd_shopping': False}, (0, 17)),
|
||||
({'mealplan_autoinclude_related': False}, (8, 8)),
|
||||
({'mealplan_autoexclude_onhand': False}, (19, 19)),
|
||||
({'mealplan_autoexclude_onhand': False, 'mealplan_autoinclude_related': False}, (9, 9)),
|
||||
({'mealplan_autoadd_shopping': False}, (0, 18)),
|
||||
({'mealplan_autoinclude_related': False}, (9, 9)),
|
||||
({'mealplan_autoexclude_onhand': False}, (20, 20)),
|
||||
({'mealplan_autoexclude_onhand': False, 'mealplan_autoinclude_related': False}, (10, 10)),
|
||||
], indirect=['user2'])
|
||||
@pytest.mark.parametrize("use_mealplan", [(False), (True), ])
|
||||
@pytest.mark.parametrize("recipe", [({'steps__recipe_count': 1})], indirect=['recipe'])
|
||||
def test_shopping_recipe_userpreference(recipe, sle_count, use_mealplan, user2):
|
||||
with scopes_disabled():
|
||||
user = auth.get_user(user2)
|
||||
# setup recipe with 10 ingredients, 1 step recipe with 10 ingredients, 2 food onhand(from recipe and step_recipe), 1 food ignore shopping
|
||||
# setup recipe with 10 ingredients, 1 step recipe with 10 ingredients, 2 food onhand(from recipe and step_recipe)
|
||||
ingredients = Ingredient.objects.filter(step__recipe=recipe)
|
||||
food = Food.objects.get(id=ingredients[2].food.id)
|
||||
food.on_hand = True
|
||||
food.food_onhand = True
|
||||
food.save()
|
||||
food = recipe.steps.filter(type=Step.RECIPE).first().step_recipe.steps.first().ingredients.first().food
|
||||
food = Food.objects.get(id=food.id)
|
||||
food.on_hand = True
|
||||
food.save()
|
||||
food = Food.objects.get(id=ingredients[4].food.id)
|
||||
food.ignore_shopping = True
|
||||
food.food_onhand = True
|
||||
food.save()
|
||||
|
||||
if use_mealplan:
|
||||
|
||||
@@ -112,30 +112,29 @@ def test_preference_delete(u1_s1, u2_s1):
|
||||
|
||||
|
||||
def test_default_inherit_fields(u1_s1, u1_s2, space_1, space_2):
|
||||
food_inherit_fields = Food.inherit_fields.all()
|
||||
|
||||
r = u1_s1.get(
|
||||
reverse(DETAIL_URL, args={auth.get_user(u1_s1).id}),
|
||||
)
|
||||
food_inherit_fields = Food.inheritable_fields
|
||||
assert len([x.field for x in food_inherit_fields]) > 0
|
||||
|
||||
# by default space food will not inherit any fields, so all of them will be ignored
|
||||
assert space_1.food_inherit.all().count() == 0
|
||||
assert len([x.field for x in food_inherit_fields]) == len([x['field'] for x in json.loads(r.content)['food_ignore_default']]) > 0
|
||||
|
||||
# inherit all possible fields
|
||||
with scope(space=space_1):
|
||||
space_1.food_inherit.add(*Food.inherit_fields.values_list('id', flat=True))
|
||||
r = u1_s1.get(
|
||||
reverse(DETAIL_URL, args={auth.get_user(u1_s1).id}),
|
||||
)
|
||||
assert len([x['field'] for x in json.loads(r.content)['food_inherit_default']]) == 0
|
||||
|
||||
assert space_1.food_inherit.all().count() == Food.inherit_fields.all().count() > 0
|
||||
# now by default, food is not ignoring inheritance on any field
|
||||
assert len([x['field'] for x in json.loads(r.content)['food_ignore_default']]) == 0
|
||||
# inherit all possible fields
|
||||
with scope(space=space_1):
|
||||
space_1.food_inherit.add(*Food.inheritable_fields.values_list('id', flat=True))
|
||||
|
||||
# other spaces and users in those spaced not effected
|
||||
assert space_1.food_inherit.all().count() == Food.inheritable_fields.count() > 0
|
||||
# now by default, food is inheriting all of the possible fields
|
||||
r = u1_s1.get(
|
||||
reverse(DETAIL_URL, args={auth.get_user(u1_s1).id}),
|
||||
)
|
||||
assert len([x['field'] for x in json.loads(r.content)['food_inherit_default']]) == space_1.food_inherit.all().count()
|
||||
|
||||
# other spaces and users in those spaces not effected
|
||||
r = u1_s2.get(
|
||||
reverse(DETAIL_URL, args={auth.get_user(u1_s2).id}),
|
||||
)
|
||||
assert space_2.food_inherit.all().count() == 0
|
||||
assert len([x.field for x in food_inherit_fields]) == len([x['field'] for x in json.loads(r.content)['food_ignore_default']]) > 0
|
||||
assert space_2.food_inherit.all().count() == 0 == len([x['field'] for x in json.loads(r.content)['food_inherit_default']])
|
||||
|
||||
Reference in New Issue
Block a user