Skip to content

Commit

Permalink
update additonal recipe tests for app robust
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelusc committed Apr 20, 2024
1 parent 88b79c6 commit e97df1f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 8 deletions.
77 changes: 69 additions & 8 deletions app/recipe/tests/test_recipe_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ def create_recipe(user, **params):
recipe = Recipe.objects.create(user=user, **defaults)
return recipe

# helper function to create a user


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


class PublicRecipeAPITests(TestCase):
"""Test unauthenticated recipe API requests."""
Expand All @@ -57,10 +64,8 @@ class PrivateRecipeApiTests(TestCase):

def setUp(self):
self.client = APIClient()
self.user = get_user_model().objects.create_user(
'[email protected]',
'password123',
)
self.user = create_user(
email='[email protected]', password='password123')
self.client.force_authenticate(self.user)

def test_retrieve_recipes(self):
Expand All @@ -79,10 +84,7 @@ def test_retrieve_recipes(self):

def test_recipes_limited_to_user(self):
"""Test list of recipes is limited to authenticated user."""
user2 = get_user_model().objects.create_user(
'[email protected]',
'password123',
)
user2 = create_user(email='[email protected]', password='testpass123')
create_recipe(user=user2)
create_recipe(user=self.user)

Expand Down Expand Up @@ -119,3 +121,62 @@ def test_create_recipe(self):
for key, value in payload.items():
self.assertEqual(getattr(recipe, key), value)
self.assertEqual(recipe.user, self.user)

def test_partial_update(self):
"""Test partial update of recipe."""
original_link = 'https://example.com/recipe.pdf'
recipe = create_recipe(
user=self.user,
title='Sample recipe title',
link=original_link,
)

payload = {'title': 'New recipe title'}
url = detail_url(recipe.id)
res = self.client.patch(url, payload)

self.assertEqual(res.status_code, status.HTTP_200_OK)
recipe.refresh_from_db()
self.assertEqual(recipe.title, payload['title'])
self.assertEqual(recipe.link, original_link)
self.assertEqual(recipe.user, self.user)

def test_full_update(self):
"""Test full update of recipe."""
recipe = create_recipe(
user=self.user,
title='Sample recipe title',
description='Sample description',
link='https://example.com/recipe.pdf',
)

payload = {
'title': 'Updated recipe title',
'time_minutes': 13,
'price': Decimal('17.99'),
'description': 'Updated description',
'link': 'https://example.com/recipe2.pdf',
}
url = detail_url(recipe.id)
res = self.client.put(url, payload)

self.assertEqual(res.status_code, status.HTTP_200_OK)
recipe.refresh_from_db()
for key, value in payload.items():
self.assertEqual(getattr(recipe, key), value)
self.assertEqual(recipe.user, self.user)

def test_update_user_returns_error(self):
"""Test changing the recipe user results in an error"""
new_user = create_user(
email='[email protected]',
password='testpass123'
)
recipe = create_recipe(user=self.user)

payload = {'user': new_user.id}
url = detail_url(recipe.id)
self.client.patch(url, payload)

recipe.refresh_from_db()
self.assertEqual(recipe.user, self.user)
4 changes: 4 additions & 0 deletions app/recipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ def get_serializer_class(self):
if self.action == 'list':
return serializers.RecipeSerializer
return self.serializer_class

def perform_create(self, serializer):
"""Create a new recipe."""
serializer.save(user=self.request.user)

0 comments on commit e97df1f

Please sign in to comment.