-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91368fd
commit 719f48d
Showing
9 changed files
with
303 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Generated by Django 3.2.25 on 2024-05-27 17:20 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0004_rename_tag_recipe_tags'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Ingredient', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('name', models.CharField(max_length=255)), | ||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
migrations.AddField( | ||
model_name='recipe', | ||
name='ingredients', | ||
field=models.ManyToManyField(to='core.Ingredient'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
""" | ||
Tests for the ingredients API. | ||
""" | ||
from django.contrib.auth import get_user_model | ||
from django.urls import reverse | ||
from django.test import TestCase | ||
|
||
from rest_framework import status | ||
from rest_framework.test import APIClient | ||
|
||
from core.models import Ingredient | ||
|
||
from recipe.serializers import IngredientSerializer | ||
|
||
INGREDIENT_URL = reverse('recipe:ingredient-list') | ||
|
||
|
||
def detail_url(ingredient_id): | ||
"""Create and return ingredient detail id.""" | ||
return reverse('recipe:ingredient-detail', args=[ingredient_id]) | ||
|
||
|
||
def create_user(email='[email protected]', password='testuser123'): | ||
"""Create and return a user.""" | ||
return get_user_model().objects.create_user(email=email, password=password) | ||
|
||
|
||
class PublicIngredientApiTest(TestCase): | ||
"""Test unauthorized API requests.""" | ||
|
||
def setUp(self): | ||
self.client = APIClient() | ||
|
||
def test_auth_required(self): | ||
"""Test authentication required to list ingredients.""" | ||
res = self.client.get(INGREDIENT_URL) | ||
|
||
self.assertEqual(res.status_code, status.HTTP_401_UNAUTHORIZED) | ||
|
||
|
||
class PrivateIngredientApiTest(TestCase): | ||
"""Test unauthenticated API requests.""" | ||
|
||
def setUp(self): | ||
self.user = create_user() | ||
self.client = APIClient() | ||
self.client.force_authenticate(self.user) | ||
|
||
def test_retrieve_ingredient(self): | ||
"""Test retrieving a list of ingredients.""" | ||
Ingredient.objects.create(user=self.user, name='Kale') | ||
Ingredient.objects.create(user=self.user, name='Vanilla') | ||
|
||
res = self.client.get(INGREDIENT_URL) | ||
|
||
ingredients = Ingredient.objects.order_by('-name') | ||
serializer = IngredientSerializer(ingredients, many=True) | ||
|
||
self.assertEqual(res.status_code, status.HTTP_200_OK) | ||
self.assertEqual(res.data, serializer.data) | ||
|
||
def test_ingredient_limited_to_user(self): | ||
"""Test list of ingredients limited to authenticated user.""" | ||
|
||
user2 = create_user(email='[email protected]') | ||
Ingredient.objects.create(user=user2, name='Salt') | ||
ingredient = Ingredient.objects.create(user=self.user, name='Pepper') | ||
|
||
res = self.client.get(INGREDIENT_URL) | ||
|
||
self.assertEqual(res.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(res.data), 1) | ||
self.assertEqual(res.data[0]['name'], ingredient.name) | ||
self.assertEqual(res.data[0]['id'], ingredient.id) | ||
|
||
def test_update_ingredient(self): | ||
"""Test updating ingredient.""" | ||
ingredient = Ingredient.objects.create( | ||
user=self.user, name='Jollof Rice') | ||
|
||
payload = {'name': 'Nigerian Jollof Rice'} | ||
url = detail_url(ingredient.id) | ||
res = self.client.patch(url, payload) | ||
|
||
self.assertEqual(res.status_code, status.HTTP_200_OK) | ||
ingredient.refresh_from_db() | ||
self.assertEqual(ingredient.name, payload['name']) | ||
|
||
def test_delete_ingredient(self): | ||
"""Test deleting ingredient.""" | ||
ingredient = Ingredient.objects.create( | ||
user=self.user, name='Egusi Soup') | ||
|
||
url = detail_url(ingredient.id) | ||
res = self.client.delete(url) | ||
|
||
self.assertEqual(res.status_code, status.HTTP_204_NO_CONTENT) | ||
ingredients = Ingredient.objects.filter(user=self.user) | ||
self.assertFalse(ingredients.exists()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.