-
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
b28c852
commit 4bce68e
Showing
14 changed files
with
306 additions
and
24 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
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,19 @@ | ||
# Generated by Django 3.2.25 on 2024-09-06 00:30 | ||
|
||
import core.models | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0006_ingredient_name'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='recipe', | ||
name='image', | ||
field=models.ImageField(null=True, upload_to=core.models.recipe_image_file_path), | ||
), | ||
] |
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 |
---|---|---|
@@ -1,12 +1,22 @@ | ||
from abc import ABC | ||
|
||
from decimal import Decimal | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.test import TestCase | ||
from django.urls import reverse | ||
|
||
from rest_framework import status | ||
from rest_framework.test import APIClient | ||
|
||
from core.models import Recipe, Ingredient, Tag | ||
|
||
|
||
RELATED_FIELD_MAP = { | ||
Ingredient: 'ingredients', | ||
Tag: 'tags', | ||
} | ||
|
||
|
||
def create_user(email='[email protected]', password='testpass123'): | ||
return get_user_model().objects.create(email=email, password=password) | ||
|
@@ -54,14 +64,14 @@ def test_retrieve_model(self): | |
def test_model_limited_to_user(self): | ||
user2 = create_user(email='[email protected]') | ||
self.model.objects.create(user=user2, name="Meat") | ||
tag = self.model.objects.create(user=self.user, name="Fruity") | ||
item = self.model.objects.create(user=self.user, name="Fruity") | ||
|
||
res = self.client.get(self.list_url) | ||
|
||
self.assertEqual(res.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(res.data), 1) | ||
self.assertEqual(res.data[0]['name'], tag.name) | ||
self.assertEqual(res.data[0]['id'], tag.id) | ||
self.assertEqual(res.data[0]['name'], item.name) | ||
self.assertEqual(res.data[0]['id'], item.id) | ||
|
||
def test_update_model(self): | ||
model= self.model.objects.create(user=self.user, name="Name") | ||
|
@@ -85,3 +95,45 @@ def test_delete_model(self): | |
self.assertEqual(res.status_code, status.HTTP_204_NO_CONTENT) | ||
models = self.model.objects.filter(user=self.user) | ||
self.assertFalse(models.exists()) | ||
|
||
def test_filter_model_assigned_to_recipes(self): | ||
obj1 = self.model.objects.create(user=self.user, name="Object 1") | ||
obj2 = self.model.objects.create(user=self.user, name="Object 2") | ||
recipe = Recipe.objects.create( | ||
title='Apple Crumble', | ||
time_minutes=5, | ||
price=Decimal('4.50'), | ||
user=self.user, | ||
) | ||
getattr(recipe, RELATED_FIELD_MAP.get(self.model)).add(obj1) | ||
|
||
res = self.client.get(self.list_url, {'assigned_only': 1}) | ||
|
||
s1 = self.serializer(obj1) | ||
s2 = self.serializer(obj2) | ||
self.assertIn(s1.data, res.data) | ||
self.assertNotIn(s2.data, res.data) | ||
|
||
def test_filtered_objects_unique(self): | ||
obj = self.model.objects.create(user=self.user, name="Object") | ||
self.model.objects.create(user=self.user, name="Different Object") | ||
recipe1 = Recipe.objects.create( | ||
title='Eggs Benedict', | ||
time_minutes=60, | ||
price=Decimal('7.00'), | ||
user=self.user | ||
) | ||
recipe2 = Recipe.objects.create( | ||
title='Herb Eggs', | ||
time_minutes=20, | ||
price=Decimal('4.00'), | ||
user=self.user | ||
) | ||
|
||
getattr(recipe1, RELATED_FIELD_MAP.get(self.model)).add(obj) | ||
getattr(recipe2, RELATED_FIELD_MAP.get(self.model)).add(obj) | ||
|
||
res = self.client.get(self.list_url, {'assigned_only': 1}) | ||
|
||
self.assertEqual(len(res.data), 1) | ||
|
File renamed without changes.
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 |
---|---|---|
|
@@ -2,6 +2,10 @@ | |
Tests for recipe APIs. | ||
""" | ||
from decimal import Decimal | ||
import tempfile | ||
import os | ||
|
||
from PIL import Image | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.test import TestCase | ||
|
@@ -30,6 +34,11 @@ def detail_url(recipe_id): | |
return reverse('recipe:recipe-detail', args=[recipe_id]) | ||
|
||
|
||
def image_upload_url(recipe_id): | ||
"""Create and return an image upload URL""" | ||
return reverse('recipe:recipe-upload-image', args=[recipe_id]) | ||
|
||
|
||
def create_recipe(user, **params): | ||
"""Create and return a same recipe""" | ||
defaults = { | ||
|
@@ -373,3 +382,83 @@ def test_clear_recipe_ingredients(self): | |
|
||
self.assertEqual(res.status_code, status.HTTP_200_OK) | ||
self.assertEqual(recipe.ingredients.count(), 0) | ||
|
||
def test_filter_by_tags(self): | ||
r1 = create_recipe(user=self.user, title = 'Thai vegetable curry') | ||
r2 = create_recipe(user=self.user, title='Aubergine with Tahini') | ||
tag1 = Tag.objects.create(user=self.user, name='Vegan') | ||
tag2 = Tag.objects.create(user=self.user, name='Vegeterian') | ||
r1.tags.add(tag1) | ||
r2.tags.add(tag2) | ||
r3 = create_recipe(user=self.user, title='Fish and Chips') | ||
|
||
params = {'tags': f'{tag1.id},{tag2.id}'} | ||
res = self.client.get(RECIPES_URL, params=params) | ||
|
||
s1 = RecipeSerializer(r1) | ||
s2 = RecipeSerializer(r2) | ||
s3 = RecipeSerializer(r3) | ||
|
||
self.assertEqual(res.status_code, status.HTTP_200_OK) | ||
self.assertIn(s1.data, res.data) | ||
self.assertIn(s2.data, res.data) | ||
self.assertNotIn(s3.data, res.data) | ||
|
||
def test_filter_by_ingredients(self): | ||
r1 = create_recipe(user=self.user, title = 'Thai vegetable curry') | ||
r2 = create_recipe(user=self.user, title='Aubergine with Tahini') | ||
i1 = Ingredient.objects.create(user=self.user, name='Potato') | ||
i2 = Ingredient.objects.create(user=self.user, name='Coconut Milk') | ||
r1.ingredients.add(i1) | ||
r2.ingredients.add(i2) | ||
r3 = create_recipe(user=self.user, title='Fish and Chips') | ||
|
||
params = {'ingredients': f'{i1.id},{i2.id}'} | ||
res = self.client.get(RECIPES_URL, params) | ||
|
||
s1 = RecipeSerializer(r1) | ||
s2 = RecipeSerializer(r2) | ||
s3 = RecipeSerializer(r3) | ||
|
||
self.assertIn(s1.data, res.data) | ||
self.assertIn(s2.data, res.data) | ||
self.assertNotIn(s3.data, res.data) | ||
|
||
|
||
class ImageUploadTests(TestCase): | ||
"""Tests for the image upload API.""" | ||
|
||
def setUp(self): | ||
self.client = APIClient() | ||
self.user = get_user_model().objects.create_user( | ||
'[email protected]', | ||
'password123', | ||
) | ||
self.client.force_authenticate(self.user) | ||
self.recipe = create_recipe(user=self.user) | ||
|
||
def tearDown(self): | ||
self.recipe.image.delete() | ||
|
||
def test_upload_image(self): | ||
"""Test uploading an image to a recipe.""" | ||
url = image_upload_url(self.recipe.id) | ||
with tempfile.NamedTemporaryFile(suffix='.jpg') as image_file: | ||
img = Image.new('RGB', (10, 10)) | ||
img.save(image_file, format='JPEG') | ||
image_file.seek(0) | ||
payload = {'image': image_file} | ||
res = self.client.post(url, payload, format='multipart') | ||
|
||
self.recipe.refresh_from_db() | ||
self.assertEqual(res.status_code, status.HTTP_200_OK) | ||
self.assertIn('image', res.data) | ||
self.assertTrue(os.path.exists(self.recipe.image.path)) | ||
|
||
def test_upload_image_bad_request(self): | ||
url = image_upload_url(self.recipe.id) | ||
payload = {'image': 'bad'} | ||
|
||
res = self.client.post(url, payload, format='multipart') | ||
|
||
self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) |
File renamed without changes.
Oops, something went wrong.