93 lines
3.0 KiB
Python
93 lines
3.0 KiB
Python
"""
|
|
Test for models.
|
|
"""
|
|
from unittest.mock import patch
|
|
from decimal import Decimal
|
|
|
|
from django.test import TestCase
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from core import models
|
|
|
|
|
|
def create_user(email='user@example.com', password='testpass123'):
|
|
"""Create and return a new user."""
|
|
return get_user_model().objects.create_user(email, password)
|
|
|
|
|
|
class ModelTests(TestCase):
|
|
"""Test models."""
|
|
|
|
def test_create_user_with_email_successful(self):
|
|
"""Test creating a user with an email is successful."""
|
|
email = 'test@example.com'
|
|
password = 'testpass123'
|
|
user = create_user(email, password)
|
|
self.assertEqual(user.email, email)
|
|
self.assertTrue(user.check_password(password))
|
|
|
|
def test_new_user_email_normalized(self):
|
|
"""Test email is normalized for new users."""
|
|
sample_emails = [
|
|
['test1@EXAMPLE.com', 'test1@example.com'],
|
|
['test2@Example.com', 'test2@example.com'],
|
|
['TEST3@EXAMPLE.COM', 'TEST3@example.com'],
|
|
['test4@example.COM', 'test4@example.com'],
|
|
]
|
|
for email, expected in sample_emails:
|
|
user = create_user(email=email)
|
|
self.assertEqual(user.email, expected)
|
|
|
|
def test_new_user_withouth_email_raises_error(self):
|
|
"""Test that creating a user withouth an email raises a ValueError."""
|
|
with self.assertRaises(ValueError):
|
|
create_user('', 'test123')
|
|
|
|
def test_create_superuser(self):
|
|
"""Test creating a superuser."""
|
|
user = get_user_model().objects.create_superuser(
|
|
'test@example.com',
|
|
'test123',
|
|
)
|
|
self.assertTrue(user.is_superuser)
|
|
self.assertTrue(user.is_staff)
|
|
|
|
def test_create_recipe(self):
|
|
"""Test creating a recipe is successful."""
|
|
user = create_user()
|
|
recipe = models.Recipe.objects.create(
|
|
user=user,
|
|
title='Nombre receta ejemplo',
|
|
time_minutes=5,
|
|
price=Decimal('5.50'),
|
|
description='Descripción de la receta de ejemplo'
|
|
)
|
|
|
|
self.assertEqual(str(recipe), recipe.title)
|
|
|
|
def test_create_tag(self):
|
|
"""Test creating a tag is successful."""
|
|
user = create_user()
|
|
tag = models.Tag.objects.create(user=user, name='Tag1')
|
|
|
|
self.assertEqual(str(tag), tag.name)
|
|
|
|
def test_create_ingredient(self):
|
|
"""Test creating an ingredient is successful."""
|
|
user = create_user()
|
|
ingredient = models.Ingredient.objects.create(
|
|
user=user,
|
|
name='Ingrediente1'
|
|
)
|
|
|
|
self.assertEqual(str(ingredient), ingredient.name)
|
|
|
|
@patch('core.models.uuid.uuid4')
|
|
def test_recipe_file_name_uuid(self, mock_uuid):
|
|
"""Test generating image path."""
|
|
uuid = 'test-uuid'
|
|
mock_uuid.return_value = uuid
|
|
file_path = models.recipe_image_file_path(None, 'example.jpg')
|
|
|
|
self.assertEqual(file_path, f'uploads/recipe/{uuid}.jpg')
|