def obj_tree_1(request, space_1): try: params = request.param # request.param is a magic variable 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
def test_makenow_sibling_substitute(recipes, makenow_recipe, user1, space_1): request = type('', (object, ), {'space': space_1, 'user': user1})() search = RecipeSearch(request, makenow='true') with scope(space=space_1): food = Food.objects.filter( ingredient__step__recipe=makenow_recipe.id).first() onhand_user = food.onhand_users.first() food.onhand_users.clear() food.substitute_siblings = True food.save() assert search.get_queryset(Recipe.objects.all()).count() == 0 new_parent = FoodFactory.create(space=space_1) new_sibling = FoodFactory.create(space=space_1, onhand_users=[onhand_user]) new_sibling.move(new_parent, 'first-child') food.move(new_parent, 'first-child') assert Food.objects.filter(ingredient__step__recipe=makenow_recipe.id, onhand_users__isnull=False).count() == 9 assert Food.objects.filter(ingredient__step__recipe=makenow_recipe.id, depth=2).count() == 1 search = search.get_queryset(Recipe.objects.all()) assert search.count() == 1 assert search.first().id == makenow_recipe.id
def test_makenow_substitute(recipes, makenow_recipe, user1, space_1): request = type('', (object, ), {'space': space_1, 'user': user1})() search = RecipeSearch(request, makenow='true') with scope(space=space_1): food = Food.objects.filter( ingredient__step__recipe=makenow_recipe.id).first() onhand_user = food.onhand_users.first() food.onhand_users.clear() assert search.get_queryset(Recipe.objects.all()).count() == 0 food.substitute.add( FoodFactory.create(space=space_1, onhand_users=[onhand_user])) assert Food.objects.filter(ingredient__step__recipe=makenow_recipe.id, onhand_users__isnull=False).count() == 9 assert Food.objects.filter(ingredient__step__recipe=makenow_recipe.id, substitute__isnull=False).count() == 1 search = search.get_queryset(Recipe.objects.all()) assert search.count() == 1 assert search.first().id == makenow_recipe.id
def test_shopping_food_share(u1_s1, u2_s1, food, space_1): with scope(space=space_1): user1 = auth.get_user(u1_s1) user2 = auth.get_user(u2_s1) food2 = FoodFactory(space=space_1) r = u1_s1.put(reverse(SHOPPING_FOOD_URL, args={food.id})) r = u2_s1.put(reverse(SHOPPING_FOOD_URL, args={food2.id})) sl_1 = json.loads(u1_s1.get(reverse(SHOPPING_LIST_URL)).content) sl_2 = json.loads(u2_s1.get(reverse(SHOPPING_LIST_URL)).content) assert len(sl_1) == 1 assert len(sl_2) == 1 sl_1[0]['created_by']['id'] == user1.id sl_2[0]['created_by']['id'] == user2.id with scopes_disabled(): user1.userpreference.shopping_share.add(user2) user1.userpreference.save() assert len(json.loads(u1_s1.get(reverse(SHOPPING_LIST_URL)).content)) == 1 assert len(json.loads(u2_s1.get(reverse(SHOPPING_LIST_URL)).content)) == 2
def food(request, space_1, u1_s1): return FoodFactory(space=space_1)
def found_recipe(request, space_1, accent, unaccent, u1_s1, u2_s1): user1 = auth.get_user(u1_s1) user2 = auth.get_user(u2_s1) days_3 = timezone.now() - timedelta(days=3) days_15 = timezone.now() - timedelta(days=15) days_30 = timezone.now() - timedelta(days=30) if request.param.get('createdon', None): recipe1 = RecipeFactory.create(space=space_1, created_at=days_3) recipe2 = RecipeFactory.create(space=space_1, created_at=days_30) recipe3 = RecipeFactory.create(space=space_1, created_at=days_15) else: recipe1 = RecipeFactory.create(space=space_1) recipe2 = RecipeFactory.create(space=space_1) recipe3 = RecipeFactory.create(space=space_1) obj1 = None obj2 = None if request.param.get('food', None): obj1 = FoodFactory.create(name=unaccent, space=space_1) obj2 = FoodFactory.create(name=accent, space=space_1) recipe1.steps.first().ingredients.add(IngredientFactory.create(food=obj1)) recipe2.steps.first().ingredients.add(IngredientFactory.create(food=obj2)) recipe3.steps.first().ingredients.add(IngredientFactory.create(food=obj1), IngredientFactory.create(food=obj2)) if request.param.get('keyword', None): obj1 = KeywordFactory.create(name=unaccent, space=space_1) obj2 = KeywordFactory.create(name=accent, space=space_1) recipe1.keywords.add(obj1) recipe2.keywords.add(obj2) recipe3.keywords.add(obj1, obj2) recipe1.name = unaccent recipe2.name = accent recipe1.save() recipe2.save() if request.param.get('book', None): obj1 = RecipeBookEntryFactory.create(recipe=recipe1).book obj2 = RecipeBookEntryFactory.create(recipe=recipe2).book RecipeBookEntryFactory.create(recipe=recipe3, book=obj1) RecipeBookEntryFactory.create(recipe=recipe3, book=obj2) if request.param.get('unit', None): obj1 = UnitFactory.create(name=unaccent, space=space_1) obj2 = UnitFactory.create(name=accent, space=space_1) recipe1.steps.first().ingredients.add(IngredientFactory.create(unit=obj1)) recipe2.steps.first().ingredients.add(IngredientFactory.create(unit=obj2)) recipe3.steps.first().ingredients.add(IngredientFactory.create(unit=obj1), IngredientFactory.create(unit=obj2)) if request.param.get('name', None): recipe1.name = unaccent recipe2.name = accent recipe1.save() recipe2.save() if request.param.get('description', None): recipe1.description = unaccent recipe2.description = accent recipe1.save() recipe2.save() if request.param.get('instruction', None): i1 = recipe1.steps.first() i2 = recipe2.steps.first() i1.instruction = unaccent i2.instruction = accent i1.save() i2.save() if request.param.get('viewedon', None): ViewLogFactory.create(recipe=recipe1, created_by=user1, created_at=days_3, space=space_1) ViewLogFactory.create(recipe=recipe2, created_by=user1, created_at=days_30, space=space_1) ViewLogFactory.create(recipe=recipe3, created_by=user2, created_at=days_15, space=space_1) if request.param.get('cookedon', None): CookLogFactory.create(recipe=recipe1, created_by=user1, created_at=days_3, space=space_1) CookLogFactory.create(recipe=recipe2, created_by=user1, created_at=days_30, space=space_1) CookLogFactory.create(recipe=recipe3, created_by=user2, created_at=days_15, space=space_1) if request.param.get('timescooked', None): CookLogFactory.create_batch(5, recipe=recipe1, created_by=user1, space=space_1) CookLogFactory.create(recipe=recipe2, created_by=user1, space=space_1) CookLogFactory.create_batch(3, recipe=recipe3, created_by=user2, space=space_1) if request.param.get('rating', None): CookLogFactory.create(recipe=recipe1, created_by=user1, rating=5.0, space=space_1) CookLogFactory.create(recipe=recipe2, created_by=user1, rating=1.0, space=space_1) CookLogFactory.create(recipe=recipe3, created_by=user2, rating=3.0, space=space_1) return (recipe1, recipe2, recipe3, obj1, obj2, request.param)