def test_post_with_ingredients_recipe(self, my_recipe_list_url, view_client, make_ingredients_batch): user = UserFactory() view_client.force_login(user) ingredient_objects = make_ingredients_batch(6) amounts = [1.5, 2.3, 0.5, 0.4, 4.3, 5.2] ingredients = [ingredient.pk for ingredient in ingredient_objects] view_client.force_login(user=user) payload = { 'private': True, 'name': 'testuser7', 'recipe': 'recipe7', 'diet_category': 'LACTOSE_FREE', 'ingredients': ingredients, 'amounts': amounts } response = view_client.post(my_recipe_list_url, payload, content_type='application/json') assert response.status_code == status.HTTP_201_CREATED assert response.data.get('name') == payload.get('name') assert response.data.get('recipe') == payload.get('recipe') assert len(response.data.get('ingredients')) == len( payload.get('ingredients'))
def test_put_dayplan(self, dayplan_detail_url, view_client, make_meals_batch): user = UserFactory() view_client.force_login(user) meals = make_meals_batch(3) dayplan = DayPlanFactory(user=user) choices = ['BREAKFAST', 'LUNCH', 'DINNER'] for i in range(3): MealChoiceFactory(day_plan=dayplan, meal=meals[i], meal_choice=choices[i]) date = datetime.now() meals = make_meals_batch(3) payload = { 'date': date, 'user': user.id, 'meals': [meal.pk for meal in meals] } response = view_client.put(dayplan_detail_url(args=[dayplan.pk]), payload, content_type='application/json') assert response.status_code == status.HTTP_200_OK
def test_get_recipe_list(self, my_recipe_list_url, view_client): user = UserFactory() for i in range(3): RecipeFactory(owner=user, private=True) view_client.force_login(user=user) response = view_client.get(my_recipe_list_url) assert response.status_code == status.HTTP_200_OK assert len(response.data.get('results')) == 3
def test_post_with_valid_data_succeeds(self, meal_list_url, view_client): user = UserFactory() meal_data = model_to_dict(MealFactory.build(owner=user)) view_client.force_login(user) response = view_client.post(meal_list_url, meal_data) assert response.status_code == status.HTTP_201_CREATED meal = Meal.objects.get(pk=response.data.get('id')) assert meal.name == meal_data.get('name')
def test_delete_dayplan(self, dayplan_detail_url, view_client): user = UserFactory() view_client.force_login(user) dayplan = DayPlanFactory(user=user) response = view_client.delete(dayplan_detail_url(args=[dayplan.pk])) assert response.status_code == status.HTTP_204_NO_CONTENT dayplans = DayPlan.objects.all() assert len(dayplans) == 0
def test_post_with_valid_data_succeeds(self, recipe_list_url, view_client): user = UserFactory() view_client.force_login(user=user) recipe_data = model_to_dict(RecipeFactory.build()) recipe_data['owner'] = user recipe_data['diet_category'] = 'VEGAN' response = view_client.post(recipe_list_url, recipe_data) assert response.status_code == status.HTTP_201_CREATED recipe = Recipe.objects.get(pk=response.data.get('id')) assert recipe.name == recipe_data.get('name') assert recipe.diet_category == recipe_data.get('diet_category')
def test_post_event(self, view_client): user = UserFactory() meal = MealFactory(owner=user) view_client.force_login(user) data = { "meal_name": meal.name, "choice": "BREAKFAST", "event_date": "2019-05-26 01:19:26" } response = view_client.post(reverse('event'), data) assert response.status_code == status.HTTP_201_CREATED assert json.loads(response.data).get('status') == 'confirmed'
def test_get_dayplan(self, dayplan_detail_url, view_client, make_meals_batch): user = UserFactory() view_client.force_login(user) meals = make_meals_batch(3) dayplan = DayPlanFactory(user=user) for i in range(3): MealChoiceFactory(day_plan=dayplan, meal=meals[i]) response = view_client.get(dayplan_detail_url(args=[dayplan.pk])) assert response.status_code == status.HTTP_200_OK assert response.data.get('id') == dayplan.pk
def test_get_dayplan_all(self, dayplan_list_url, view_client, make_meals_batch): user = UserFactory() view_client.force_login(user) meals = make_meals_batch(3) dayplan = DayPlanFactory(user=user) for i in range(3): MealChoiceFactory(day_plan=dayplan, meal=meals[i]) response = view_client.get(dayplan_list_url) assert response.status_code == status.HTTP_200_OK assert len(response.data.get('results')) == 1
def test_post_with_valid_data_succeeds(self, dayplan_list_url, view_client, make_meals_batch): user = UserFactory() view_client.force_login(user) meals_objects = make_meals_batch(3) meals = [meal.pk for meal in meals_objects] dayplan_data = {'user': user.pk, 'meals': meals} response = view_client.post(dayplan_list_url, dayplan_data, content_type='application/json') assert response.status_code == status.HTTP_201_CREATED dayplan = DayPlan.objects.get(pk=response.data.get('id')) assert len(response.data.get('meals')) == len(dayplan.meals.all()) assert str(dayplan.user.id) == dayplan_data.get('user')
def test_diet_category_with_multiple_recipes(self, meal_list_url, meal_detail_url, view_client): user = UserFactory() view_client.force_login(user=user) rec1 = RecipeFactory(owner=user) rec2 = RecipeFactory(owner=user) meal_data = {'name': 'tess', 'detail': 'tesst', 'diet_category': 'NON-DIET', 'recipes': [rec1.pk, rec2.pk]} response = view_client.post(meal_list_url, meal_data) response = view_client.get(meal_detail_url(args=[response.data.get('id')])) assert response.status_code == status.HTTP_200_OK assert response.data.get('diet_category') == 'VARY' assert len(response.data.get('recipes')) == 2
def test_patch_dayplan(self, dayplan_detail_url, view_client, make_meals_batch): user = UserFactory() view_client.force_login(user) meals = make_meals_batch(3) dayplan = DayPlanFactory(user=user) for i in range(3): MealChoiceFactory(day_plan=dayplan, meal=meals[i]) date = datetime.now() payload = {'date': date} response = view_client.patch(dayplan_detail_url(args=[dayplan.pk]), payload, content_type='application/json') assert response.status_code == status.HTTP_200_OK
def test_diet_category_filter(self, meal_diet_filter_url, meal_list_url, view_client): user = UserFactory() view_client.force_login(user) choices = ['VEGAN', 'RAW', 'LACTOSE_FREE'] for i in range(2): meal_data = {'name': 'tess' + str(i), 'diet_category': choices[i], 'detail': 'tesst' + str(i)} response = view_client.post(meal_list_url, meal_data) assert response.status_code == status.HTTP_201_CREATED response = view_client.get(meal_diet_filter_url, {'diet_category': 'VEGAN'}) assert response.status_code == status.HTTP_200_OK response_data = json.loads(json.dumps(response.data)) assert response_data.get('results')[0].get('diet_category') == 'VEGAN'
def test_put_recipe(self, my_recipe_detail_url, my_recipe_list_url, view_client, make_ingredients_batch, ingredient_list_url): user = UserFactory() view_client.force_login(user) ingredient_objects = make_ingredients_batch(6) amounts = [1.5, 2.3, 0.5, 0.4, 4.3, 5.2] ingredients = [ingredient.pk for ingredient in ingredient_objects] view_client.force_login(user=user) post_payload = { 'private': True, 'name': 'testuser7', 'recipe': 'recipe7', 'diet_category': 'LACTOSE_FREE', 'ingredients': ingredients, 'amounts': amounts } response = view_client.post(my_recipe_list_url, post_payload, content_type='application/json') put_payload = { 'private': True, 'name': 'testuser11', 'recipe': 'recipe11', 'diet_category': 'RAW', 'ingredients': [ingredients[0]], 'amounts': [amounts[1]] } response = view_client.put( my_recipe_detail_url(args=[response.data.get('id')]), put_payload, content_type='application/json') assert response.status_code == status.HTTP_200_OK assert response.data.get('name') == put_payload.get('name') assert response.data.get('recipe') == put_payload.get('recipe') assert response.data.get('diet_category') == 'RAW' assert response.data.get('total_ingredients') == len(ingredients) # check if quantity_total and weight_total for ingredient are calculated response = view_client.get(ingredient_list_url) assert dict( response.data.get('results')[0]).get('quantity_total') is not None assert dict( response.data.get('results')[0]).get('weight_total') is not None
def test_private_get_no_authentication_fail(self, my_recipe_detail_url, view_client, make_my_recipe_user): recipe_user = make_my_recipe_user recipe = recipe_user['recipe'] user = recipe_user['user'] user2 = UserFactory() view_client.force_login(user=user2) response = view_client.get(my_recipe_detail_url(args=[recipe.pk])) assert response.status_code == status.HTTP_404_NOT_FOUND # now we authenticate with the owner's credentials view_client.force_login(user=user) response = view_client.get(my_recipe_detail_url(args=[recipe.pk])) assert response.status_code == status.HTTP_200_OK assert response.data.get('name') == recipe.name assert response.data.get('recipe') == recipe.recipe assert response.data.get('diet_category') == 'RAW'
def test_put_meal(self, meal_detail_url, view_client, meal_list_url): user = UserFactory() view_client.force_login(user=user) rec1 = RecipeFactory(owner=user) meal_data = {'name': 'tess123', 'detail': 'tesst', 'diet_category': 'NON-DIET', 'recipes': rec1.pk} response = view_client.post(meal_list_url, meal_data) rec2 = RecipeFactory(owner=user) rec3 = RecipeFactory(owner=user) payload = {'name': 'testuser71', 'diet_category': 'LACTOSE_FREE', 'detail': 'detail7', 'recipes': [rec2.pk, rec3.pk]} response = view_client.put(meal_detail_url(args=[response.data.get('id')]), payload, content_type='application/json') assert response.status_code == status.HTTP_200_OK assert response.data.get('name') == response.data.get('name') assert response.data.get('detail') == response.data.get('detail') assert response.data.get('diet_category') == 'VARY' assert len(response.data.get('recipes')) == 3
def test_put_recipe(self, recipe_detail_url, recipe_list_url, view_client, make_ingredients_batch): user = UserFactory() view_client.force_login(user) ingredient_objects = make_ingredients_batch(6) amounts = [1.5, 2.3, 0.5, 0.4, 4.3, 5.2] ingredients = [ingredient.pk for ingredient in ingredient_objects] view_client.force_login(user=user) post_payload = { 'name': 'testuser7', 'recipe': 'recipe7', 'diet_category': 'LACTOSE_FREE', 'ingredients': ingredients, 'amounts': amounts } response = view_client.post(recipe_list_url, post_payload, content_type='application/json') put_payload = { 'name': 'testuser11', 'recipe': 'recipe11', 'diet_category': 'RAW', 'ingredients': [ingredients[0]], 'amounts': [amounts[1]] } response = view_client.put( recipe_detail_url(args=[response.data.get('id')]), put_payload, content_type='application/json') assert response.status_code == status.HTTP_200_OK assert response.data.get('name') == put_payload.get('name') assert response.data.get('recipe') == put_payload.get('recipe') assert response.data.get('diet_category') == 'RAW' assert response.data.get('total_ingredients') == len(ingredients) calories = Recipe.objects.aggregate(sum=Sum('ingredients__kcal')) assert calories['sum'] == response.data.get('total_kcals')
def make_recipe_user(): user = UserFactory() recipe = RecipeFactory(owner=user) return {'recipe': recipe, 'user': user}
def make_meal_user(): user = UserFactory() return {'meal': MealFactory(owner=user), 'user': user}
def make_my_recipe_user(): user = UserFactory() recipe = RecipeFactory(owner=user, private=True) return {'recipe': recipe, 'user': user}
def make_meal(): user = UserFactory() return MealFactory(owner=user)