2023-10-07 16:36:13 -03:00
|
|
|
"""
|
|
|
|
Test for models.
|
|
|
|
"""
|
2023-10-09 01:25:27 -03:00
|
|
|
from decimal import Decimal
|
|
|
|
|
2023-10-07 16:36:13 -03:00
|
|
|
from django.test import TestCase
|
2023-10-08 20:04:34 -03:00
|
|
|
from django.contrib.auth import get_user_model
|
2023-10-07 16:36:13 -03:00
|
|
|
|
2023-10-09 01:25:27 -03:00
|
|
|
from core import models
|
2023-10-07 16:36:13 -03:00
|
|
|
|
2023-10-09 03:25:09 -03:00
|
|
|
|
2023-10-10 00:08:52 -03:00
|
|
|
def create_user(email='user@example.com', password='testpass123'):
|
|
|
|
"""Create and return a new user."""
|
|
|
|
return get_user_model().objects.create_user(email, password)
|
|
|
|
|
|
|
|
|
2023-10-07 16:36:13 -03:00
|
|
|
class ModelTests(TestCase):
|
|
|
|
"""Test models."""
|
|
|
|
|
2023-10-11 01:05:54 -03:00
|
|
|
def test_create_user_with_email_successful(self):
|
|
|
|
"""Test creating a user with an email is successful."""
|
2023-10-07 16:36:13 -03:00
|
|
|
email = 'test@example.com'
|
|
|
|
password = 'testpass123'
|
2023-10-10 00:08:52 -03:00
|
|
|
user = create_user(email, password)
|
2023-10-07 16:36:13 -03:00
|
|
|
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:
|
2023-10-10 00:08:52 -03:00
|
|
|
user = create_user(email=email)
|
2023-10-07 16:36:13 -03:00
|
|
|
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):
|
2023-10-10 00:08:52 -03:00
|
|
|
create_user('', 'test123')
|
2023-10-07 16:36:13 -03:00
|
|
|
|
|
|
|
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)
|
2023-10-09 01:25:27 -03:00
|
|
|
|
|
|
|
def test_create_recipe(self):
|
|
|
|
"""Test creating a recipe is successful."""
|
2023-10-10 00:08:52 -03:00
|
|
|
user = create_user()
|
2023-10-09 01:25:27 -03:00
|
|
|
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)
|
2023-10-10 00:08:52 -03:00
|
|
|
|
|
|
|
def test_create_tag(self):
|
2023-10-11 01:05:54 -03:00
|
|
|
"""Test creating a tag is successful."""
|
2023-10-10 00:08:52 -03:00
|
|
|
user = create_user()
|
|
|
|
tag = models.Tag.objects.create(user=user, name='Tag1')
|
|
|
|
|
|
|
|
self.assertEqual(str(tag), tag.name)
|
2023-10-11 01:05:54 -03:00
|
|
|
|
|
|
|
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)
|