From 7b4123462d703255d185f84f2d60a7531deb6fb7 Mon Sep 17 00:00:00 2001 From: smilerz Date: Fri, 13 Aug 2021 15:22:22 -0500 Subject: [PATCH] ran and fixed tests --- cookbook/serializer.py | 8 ++++++-- cookbook/tests/api/test_api_recipe.py | 4 ++-- cookbook/tests/api/test_api_recipe_book_entry.py | 4 ++-- cookbook/tests/conftest.py | 2 +- cookbook/views/api.py | 2 +- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/cookbook/serializer.py b/cookbook/serializer.py index b48e66aa3..8b6cd7740 100644 --- a/cookbook/serializer.py +++ b/cookbook/serializer.py @@ -284,7 +284,9 @@ class FoodSerializer(UniqueFieldsMixin, WritableNestedModelSerializer): supermarket_category = SupermarketCategorySerializer(allow_null=True, required=False) def create(self, validated_data): - obj, created = Food.objects.get_or_create(name=validated_data['name'].strip(), space=self.context['request'].space) + validated_data['name'] = validated_data['name'].strip() + validated_data['space'] = self.context['request'].space + obj, created = Food.objects.get_or_create(validated_data) return obj def update(self, instance, validated_data): @@ -456,9 +458,11 @@ class RecipeBookEntrySerializer(serializers.ModelSerializer): def create(self, validated_data): book = validated_data['book'] + recipe = validated_data['recipe'] if not book.get_owner() == self.context['request'].user: raise NotFound(detail=None, code=None) - return super().create(validated_data) + obj, created = RecipeBookEntry.objects.get_or_create(book=book, recipe=recipe) + return obj class Meta: model = RecipeBookEntry diff --git a/cookbook/tests/api/test_api_recipe.py b/cookbook/tests/api/test_api_recipe.py index 419c2353e..da98dae54 100644 --- a/cookbook/tests/api/test_api_recipe.py +++ b/cookbook/tests/api/test_api_recipe.py @@ -4,16 +4,16 @@ import pytest from django.urls import reverse from django_scopes import scopes_disabled -from cookbook.models import Food, Ingredient, Step, Recipe +from cookbook.models import Recipe LIST_URL = 'api:recipe-list' DETAIL_URL = 'api:recipe-detail' + # TODO need to add extensive tests against recipe search to go through all of the combinations of parameters # probably needs to include a far more extensive set of initial recipes to effectively test results # and to ensure that all parts of the code are exercised. # TODO should probably consider adding code coverage plugin to the test suite - @pytest.mark.parametrize("arg", [ ['a_u', 403], ['g1_s1', 200], diff --git a/cookbook/tests/api/test_api_recipe_book_entry.py b/cookbook/tests/api/test_api_recipe_book_entry.py index 89bf2ba06..11d1523ea 100644 --- a/cookbook/tests/api/test_api_recipe_book_entry.py +++ b/cookbook/tests/api/test_api_recipe_book_entry.py @@ -11,7 +11,7 @@ LIST_URL = 'api:recipebookentry-list' DETAIL_URL = 'api:recipebookentry-detail' -@pytest.fixture() +@pytest.fixture def obj_1(space_1, u1_s1, recipe_1_s1): b = RecipeBook.objects.create(name='test_1', created_by=auth.get_user(u1_s1), space=space_1) @@ -100,7 +100,7 @@ def test_add_duplicate(u1_s1, obj_1): {'book': obj_1.book.pk, 'recipe': obj_1.recipe.pk}, content_type='application/json' ) - assert r.status_code == 400 + assert r.status_code == 201 def test_delete(u1_s1, u1_s2, obj_1): diff --git a/cookbook/tests/conftest.py b/cookbook/tests/conftest.py index 54d87c1c9..a9d82c97b 100644 --- a/cookbook/tests/conftest.py +++ b/cookbook/tests/conftest.py @@ -7,7 +7,7 @@ from django.contrib import auth from django.contrib.auth.models import User, Group from django_scopes import scopes_disabled -from cookbook.models import Space, Recipe, Step, Ingredient, Food, Unit, Storage +from cookbook.models import Space, Recipe, Step, Ingredient, Food, Unit # hack from https://github.com/raphaelm/django-scopes to disable scopes for all fixtures diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 191209da9..7986bee26 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -222,7 +222,7 @@ class TreeMixin(FuzzyFilterMixin): content = {'error': True, 'msg': _('Cannot merge with child object!')} return Response(content, status=status.HTTP_403_FORBIDDEN) ######################################################################## - # this needs abstracted to update steps instead of recipes for food merge + # TODO this needs abstracted to update steps instead of recipes for food merge ######################################################################## recipes = Recipe.objects.filter(**{"%ss" % self.basename: source}, space=self.request.space)