Skip to content

Commit

Permalink
added test of patching recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
iameraj committed Feb 8, 2024
1 parent 1c729a1 commit bdfce23
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions app/recipe/test/testRecipeAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient
from core.models import Recipe
from core.models import Recipe
from recipe.serializers import RecipeSerializer, RecipeDetailSerializer

RECIPE_URL = reverse("recipe:recipe-list")
Expand All @@ -32,10 +32,12 @@ def create_recipe(user, **params):
recipe = Recipe.objects.create(user=user, **defaults)
return recipe


def create_user(**params):
"""Create and return a new user"""
return get_user_model().objects.create_user(**params)


class PublicRecipeAPITests(TestCase):
"""
Tests unauthenticated recipe endpoints
Expand All @@ -58,7 +60,9 @@ class PrivateRecipeAPITests(TestCase):

def setUp(self):
self.client = APIClient()
self.user = create_user(email="[email protected]", password= "testpassword")
self.user = create_user(
email="[email protected]", password="testpassword"
)
self.client.force_authenticate(user=self.user)

def test_retrieve_recipes(self):
Expand Down Expand Up @@ -102,3 +106,22 @@ def test_create_recipe(self):
for k, v in payload.items():
self.assertEqual(getattr(recipe, k), v)
self.assertEqual(recipe.user, self.user)

def test_partial_update(self):
"""Test patrial update"""

original_price = Decimal("4.64")
recipe = create_recipe(
user=self.user, title="sample title", price=original_price
)

payload = {
"title": "New_title",
}

res = self.client.patch(detail_url(recipe.id), payload)

self.assertEqual(res.status_code, status.HTTP_200_OK)
recipe.refresh_from_db()
self.assertEqual(recipe.title, payload["title"])
self.assertEqual(recipe.price, original_price)

0 comments on commit bdfce23

Please sign in to comment.