Translate english
This commit is contained in:
parent
997ad1f3e8
commit
d04d21ae5b
@ -3,4 +3,4 @@ from django.apps import AppConfig
|
|||||||
|
|
||||||
class LibrosConfig(AppConfig):
|
class LibrosConfig(AppConfig):
|
||||||
default_auto_field = "django.db.models.BigAutoField"
|
default_auto_field = "django.db.models.BigAutoField"
|
||||||
name = "app.libros"
|
name = "app.library"
|
@ -1,17 +1,18 @@
|
|||||||
# Generated by Django 3.2.5 on 2021-07-09 16:33
|
# Generated by Django 3.2.5 on 2021-07-14 14:16
|
||||||
|
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
('libros', '0001_initial'),
|
|
||||||
]
|
]
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
migrations.CreateModel(
|
migrations.CreateModel(
|
||||||
name='Libros',
|
name='Book',
|
||||||
fields=[
|
fields=[
|
||||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
('title', models.CharField(max_length=255)),
|
('title', models.CharField(max_length=255)),
|
||||||
@ -22,9 +23,9 @@ class Migration(migrations.Migration):
|
|||||||
('updated_at', models.DateTimeField(auto_now=True)),
|
('updated_at', models.DateTimeField(auto_now=True)),
|
||||||
],
|
],
|
||||||
options={
|
options={
|
||||||
'verbose_name': 'Libro',
|
'verbose_name': 'Book',
|
||||||
'verbose_name_plural': 'Libros',
|
'verbose_name_plural': 'Books',
|
||||||
'ordering': ('-created_at',),
|
'ordering': ('created_at',),
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
]
|
]
|
@ -1,14 +1,9 @@
|
|||||||
# app/libros/models.py
|
# app/Library/models.py
|
||||||
|
|
||||||
from django.contrib.auth.models import AbstractUser
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
class CustomUser(AbstractUser):
|
class Book(models.Model):
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class Libros(models.Model):
|
|
||||||
title = models.CharField(max_length=255)
|
title = models.CharField(max_length=255)
|
||||||
genre = models.CharField(max_length=255)
|
genre = models.CharField(max_length=255)
|
||||||
year = models.CharField(max_length=4)
|
year = models.CharField(max_length=4)
|
||||||
@ -17,9 +12,9 @@ class Libros(models.Model):
|
|||||||
updated_at = models.DateTimeField(auto_now=True)
|
updated_at = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ("-created_at",)
|
ordering = ("created_at",)
|
||||||
verbose_name = "Libro"
|
verbose_name = "Book"
|
||||||
verbose_name_plural = "Libros"
|
verbose_name_plural = "Books"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.title
|
return self.title
|
@ -1,12 +1,12 @@
|
|||||||
# app/libros/serializers.py
|
# app/Library/serializers.py
|
||||||
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from .models import Libros
|
from .models import Book
|
||||||
|
|
||||||
|
|
||||||
class LibroSerializer(serializers.ModelSerializer):
|
class BookSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Libros
|
model = Book
|
||||||
fields = "__all__"
|
fields = "__all__"
|
||||||
read_only_fields = (
|
read_only_fields = (
|
||||||
"id",
|
"id",
|
11
app/library/urls.py
Normal file
11
app/library/urls.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# app/Library/urls.py
|
||||||
|
|
||||||
|
from django.urls import path
|
||||||
|
from app.library.views import ping, BookList, BookDetails
|
||||||
|
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path("ping/", ping, name="ping"),
|
||||||
|
path("api/book/", BookList.as_view(), name="book_list"),
|
||||||
|
path("api/book/<int:pk>/", BookDetails.as_view(), name="book_details"),
|
||||||
|
]
|
@ -1,11 +1,11 @@
|
|||||||
# app/libros/views.py
|
# app/Library/views.py
|
||||||
|
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from .serializers import LibroSerializer
|
from .serializers import BookSerializer
|
||||||
from .models import Libros
|
from .models import Book
|
||||||
|
|
||||||
|
|
||||||
def ping(request):
|
def ping(request):
|
||||||
@ -13,42 +13,42 @@ def ping(request):
|
|||||||
return JsonResponse(data)
|
return JsonResponse(data)
|
||||||
|
|
||||||
|
|
||||||
class LibrosList(APIView):
|
class BookList(APIView):
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
libros = Libros.objects.all().order_by("created_at")
|
books = Book.objects.all()
|
||||||
serializer = LibroSerializer(libros, many=True)
|
serializer = BookSerializer(books, many=True)
|
||||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
serializer = LibroSerializer(data=request.data)
|
serializer = BookSerializer(data=request.data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
serializer.save()
|
serializer.save()
|
||||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
|
||||||
class LibrosDetails(APIView):
|
class BookDetails(APIView):
|
||||||
def get(self, request, pk):
|
def get(self, request, pk):
|
||||||
libro = Libros.objects.filter(pk=pk).first()
|
book = Book.objects.filter(pk=pk).first()
|
||||||
if libro:
|
if book:
|
||||||
serializer = LibroSerializer(libro)
|
serializer = BookSerializer(book)
|
||||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
return Response(status=status.HTTP_404_NOT_FOUND)
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
def put(self, request, pk):
|
def put(self, request, pk):
|
||||||
libro = Libros.objects.filter(pk=pk).first()
|
book = Book.objects.filter(pk=pk).first()
|
||||||
serializer = LibroSerializer(libro, data=request.data)
|
serializer = BookSerializer(book, data=request.data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
serializer.save()
|
serializer.save()
|
||||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
elif not libro:
|
elif not book:
|
||||||
return Response(serializer.data, status=status.HTTP_404_NOT_FOUND)
|
return Response(serializer.data, status=status.HTTP_404_NOT_FOUND)
|
||||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
def delete(self, request, pk):
|
def delete(self, request, pk):
|
||||||
libro = Libros.objects.filter(pk=pk).first()
|
book = Book.objects.filter(pk=pk).first()
|
||||||
if libro:
|
if book:
|
||||||
serializer = LibroSerializer(libro)
|
serializer = BookSerializer(book)
|
||||||
libro.delete()
|
book.delete()
|
||||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
return Response(status=status.HTTP_404_NOT_FOUND)
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
@ -1,44 +0,0 @@
|
|||||||
# Generated by Django 3.2.5 on 2021-07-04 20:09
|
|
||||||
|
|
||||||
import django.contrib.auth.models
|
|
||||||
import django.contrib.auth.validators
|
|
||||||
from django.db import migrations, models
|
|
||||||
import django.utils.timezone
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
|
||||||
|
|
||||||
initial = True
|
|
||||||
|
|
||||||
dependencies = [
|
|
||||||
('auth', '0012_alter_user_first_name_max_length'),
|
|
||||||
]
|
|
||||||
|
|
||||||
operations = [
|
|
||||||
migrations.CreateModel(
|
|
||||||
name='CustomUser',
|
|
||||||
fields=[
|
|
||||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
|
||||||
('password', models.CharField(max_length=128, verbose_name='password')),
|
|
||||||
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
|
|
||||||
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
|
|
||||||
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
|
|
||||||
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
|
|
||||||
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
|
|
||||||
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
|
|
||||||
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
|
|
||||||
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
|
|
||||||
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
|
|
||||||
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
|
|
||||||
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
|
|
||||||
],
|
|
||||||
options={
|
|
||||||
'verbose_name': 'user',
|
|
||||||
'verbose_name_plural': 'users',
|
|
||||||
'abstract': False,
|
|
||||||
},
|
|
||||||
managers=[
|
|
||||||
('objects', django.contrib.auth.models.UserManager()),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
]
|
|
@ -1,11 +0,0 @@
|
|||||||
# app/libros/urls.py
|
|
||||||
|
|
||||||
from django.urls import path
|
|
||||||
from app.libros.views import *
|
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
|
||||||
path("ping/", ping, name="ping"),
|
|
||||||
path("api/libros/", LibrosList.as_view(), name="libros_list"),
|
|
||||||
path("api/libros/<int:pk>/", LibrosDetails.as_view(), name="libros_details"),
|
|
||||||
]
|
|
@ -6,7 +6,7 @@ import sys
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Run administrative tasks."""
|
"""Run administrative tasks."""
|
||||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proyecto.settings")
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proyect.settings")
|
||||||
try:
|
try:
|
||||||
from django.core.management import execute_from_command_line
|
from django.core.management import execute_from_command_line
|
||||||
except ImportError as exc:
|
except ImportError as exc:
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
ASGI config for proyecto project.
|
ASGI config for proyect project.
|
||||||
|
|
||||||
It exposes the ASGI callable as a module-level variable named ``application``.
|
It exposes the ASGI callable as a module-level variable named ``application``.
|
||||||
|
|
||||||
@ -11,6 +11,6 @@ import os
|
|||||||
|
|
||||||
from django.core.asgi import get_asgi_application
|
from django.core.asgi import get_asgi_application
|
||||||
|
|
||||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proyecto.settings")
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proyect.settings")
|
||||||
|
|
||||||
application = get_asgi_application()
|
application = get_asgi_application()
|
@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Django settings for proyecto project.
|
Django settings for proyect project.
|
||||||
|
|
||||||
Generated by 'django-admin startproject' using Django 3.2.5.
|
Generated by 'django-admin startproject' using Django 3.2.5.
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ INSTALLED_APPS = [
|
|||||||
"django.contrib.messages",
|
"django.contrib.messages",
|
||||||
"django.contrib.staticfiles",
|
"django.contrib.staticfiles",
|
||||||
"rest_framework", # nuevo
|
"rest_framework", # nuevo
|
||||||
"app.libros", # nuevo
|
"app.library", # nuevo
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
@ -51,7 +51,7 @@ MIDDLEWARE = [
|
|||||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||||
]
|
]
|
||||||
|
|
||||||
ROOT_URLCONF = "proyecto.urls"
|
ROOT_URLCONF = "proyect.urls"
|
||||||
|
|
||||||
TEMPLATES = [
|
TEMPLATES = [
|
||||||
{
|
{
|
||||||
@ -69,7 +69,7 @@ TEMPLATES = [
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
WSGI_APPLICATION = "proyecto.wsgi.application"
|
WSGI_APPLICATION = "proyect.wsgi.application"
|
||||||
|
|
||||||
|
|
||||||
# Database
|
# Database
|
||||||
@ -109,11 +109,11 @@ LANGUAGE_CODE = "en-us"
|
|||||||
|
|
||||||
TIME_ZONE = "UTC"
|
TIME_ZONE = "UTC"
|
||||||
|
|
||||||
USE_I18N = True
|
USE_I18N = False
|
||||||
|
|
||||||
USE_L10N = True
|
USE_L10N = False
|
||||||
|
|
||||||
USE_TZ = True
|
USE_TZ = False
|
||||||
|
|
||||||
|
|
||||||
# Static files (CSS, JavaScript, Images)
|
# Static files (CSS, JavaScript, Images)
|
||||||
@ -125,5 +125,3 @@ STATIC_URL = "/static/"
|
|||||||
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
|
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
|
||||||
|
|
||||||
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
||||||
|
|
||||||
AUTH_USER_MODEL = "libros.CustomUser"
|
|
8
proyect/urls.py
Normal file
8
proyect/urls.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# proyect/urls.py
|
||||||
|
|
||||||
|
from django.contrib import admin
|
||||||
|
from django.urls import path, include
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path("", include("app.library.urls")),
|
||||||
|
]
|
@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
WSGI config for proyecto project.
|
WSGI config for proyect project.
|
||||||
|
|
||||||
It exposes the WSGI callable as a module-level variable named ``application``.
|
It exposes the WSGI callable as a module-level variable named ``application``.
|
||||||
|
|
||||||
@ -11,6 +11,6 @@ import os
|
|||||||
|
|
||||||
from django.core.wsgi import get_wsgi_application
|
from django.core.wsgi import get_wsgi_application
|
||||||
|
|
||||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proyecto.settings")
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proyect.settings")
|
||||||
|
|
||||||
application = get_wsgi_application()
|
application = get_wsgi_application()
|
@ -1,9 +0,0 @@
|
|||||||
# proyecto/urls.py
|
|
||||||
|
|
||||||
from django.contrib import admin
|
|
||||||
from django.urls import path, include
|
|
||||||
|
|
||||||
urlpatterns = [
|
|
||||||
path("admin/", admin.site.urls),
|
|
||||||
path("", include("app.libros.urls")),
|
|
||||||
]
|
|
@ -1,5 +1,5 @@
|
|||||||
[pytest]
|
[pytest]
|
||||||
DJANGO_SETTINGS_MODULE = proyecto.settings
|
DJANGO_SETTINGS_MODULE = proyect.settings
|
||||||
|
|
||||||
# Opcional, pero recomendado
|
# Opcional, pero recomendado
|
||||||
python_files = tests.py test_*.py *_tests.py
|
python_files = tests.py test_*.py *_tests.py
|
28
tests/library/test_model.py
Normal file
28
tests/library/test_model.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
# tests/Library/test_model.py
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from app.library.models import Book
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_book_model():
|
||||||
|
|
||||||
|
## Given
|
||||||
|
book = Book(
|
||||||
|
title="The foundation",
|
||||||
|
genre="Science fiction",
|
||||||
|
year="1951",
|
||||||
|
author="Isaac Asimov",
|
||||||
|
)
|
||||||
|
book.save()
|
||||||
|
|
||||||
|
## When
|
||||||
|
|
||||||
|
## Then
|
||||||
|
assert book.title == "The foundation"
|
||||||
|
assert book.genre == "Science fiction"
|
||||||
|
assert book.year == "1951"
|
||||||
|
assert book.author == "Isaac Asimov"
|
||||||
|
assert book.created_at
|
||||||
|
assert book.updated_at
|
||||||
|
assert str(book) == book.title
|
@ -1,6 +1,6 @@
|
|||||||
# tests/libros/test_serializers.py
|
# tests/Library/test_serializers.py
|
||||||
|
|
||||||
from app.libros.serializers import LibroSerializer
|
from app.library.serializers import BookSerializer
|
||||||
|
|
||||||
|
|
||||||
def test_valid_libro_serializer():
|
def test_valid_libro_serializer():
|
||||||
@ -10,7 +10,7 @@ def test_valid_libro_serializer():
|
|||||||
"year": "1987",
|
"year": "1987",
|
||||||
"author": "Ray Bradbury",
|
"author": "Ray Bradbury",
|
||||||
}
|
}
|
||||||
serializer = LibroSerializer(data=valid_serializer_data)
|
serializer = BookSerializer(data=valid_serializer_data)
|
||||||
assert serializer.is_valid()
|
assert serializer.is_valid()
|
||||||
assert serializer.validated_data == valid_serializer_data
|
assert serializer.validated_data == valid_serializer_data
|
||||||
assert serializer.data == valid_serializer_data
|
assert serializer.data == valid_serializer_data
|
||||||
@ -19,10 +19,10 @@ def test_valid_libro_serializer():
|
|||||||
|
|
||||||
def test_invalid_libro_serializer():
|
def test_invalid_libro_serializer():
|
||||||
invalid_serializer_data = {
|
invalid_serializer_data = {
|
||||||
"title": "Soy Leyenda",
|
"title": "I am legend",
|
||||||
"author": "Richard Matheson",
|
"author": "Richard Matheson",
|
||||||
}
|
}
|
||||||
serializer = LibroSerializer(data=invalid_serializer_data)
|
serializer = BookSerializer(data=invalid_serializer_data)
|
||||||
assert not serializer.is_valid()
|
assert not serializer.is_valid()
|
||||||
assert serializer.validated_data == {}
|
assert serializer.validated_data == {}
|
||||||
assert serializer.data == invalid_serializer_data
|
assert serializer.data == invalid_serializer_data
|
@ -1,7 +1,7 @@
|
|||||||
# tests/libros/test_views.py
|
# tests/Library/test_views.py
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from app.libros.models import Libros
|
from app.library.models import Book
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
|
|
||||||
@ -9,15 +9,15 @@ from django.urls import reverse
|
|||||||
def test_add_book(client):
|
def test_add_book(client):
|
||||||
|
|
||||||
# Given
|
# Given
|
||||||
libros = Libros.objects.all()
|
book = Book.objects.all()
|
||||||
assert len(libros) == 0
|
assert len(book) == 0
|
||||||
|
|
||||||
# When
|
# When
|
||||||
resp = client.post(
|
resp = client.post(
|
||||||
reverse("libros_list"),
|
reverse("book_list"),
|
||||||
{
|
{
|
||||||
"title": "El fin de la eternidad",
|
"title": "The End of Eternity",
|
||||||
"genre": "Ciencia Ficción",
|
"genre": "Sciencie Fiction",
|
||||||
"author": "Isaac Asimov",
|
"author": "Isaac Asimov",
|
||||||
"year": "1955",
|
"year": "1955",
|
||||||
},
|
},
|
||||||
@ -26,36 +26,36 @@ def test_add_book(client):
|
|||||||
|
|
||||||
# Then
|
# Then
|
||||||
assert resp.status_code == 201
|
assert resp.status_code == 201
|
||||||
assert resp.data["title"] == "El fin de la eternidad"
|
assert resp.data["title"] == "The End of Eternity"
|
||||||
|
|
||||||
libros = Libros.objects.all()
|
book = Book.objects.all()
|
||||||
assert len(libros) == 1
|
assert len(book) == 1
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_get_single_book(client):
|
def test_get_single_book(client):
|
||||||
|
|
||||||
# Given
|
# Given
|
||||||
libro = Libros.objects.create(
|
book = Book.objects.create(
|
||||||
title="El fin de la eternidad",
|
title="The End of Eternity",
|
||||||
genre="Ciencia Ficción",
|
genre="Sciencie Fiction",
|
||||||
author="Isaac Asimov",
|
author="Isaac Asimov",
|
||||||
year="1955",
|
year="1955",
|
||||||
)
|
)
|
||||||
|
|
||||||
# When
|
# When
|
||||||
resp = client.get(reverse("libros_details", kwargs={"pk": libro.id}))
|
resp = client.get(reverse("book_details", kwargs={"pk": book.id}))
|
||||||
|
|
||||||
# Then
|
# Then
|
||||||
assert resp.status_code == 200
|
assert resp.status_code == 200
|
||||||
assert resp.data["title"] == "El fin de la eternidad"
|
assert resp.data["title"] == "The End of Eternity"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_get_single_book_incorrect_id(client):
|
def test_get_single_book_incorrect_id(client):
|
||||||
|
|
||||||
# When
|
# When
|
||||||
resp = client.get(reverse("libros_details", kwargs={"pk": 99}))
|
resp = client.get(reverse("book_details", kwargs={"pk": 99}))
|
||||||
|
|
||||||
# Then
|
# Then
|
||||||
assert resp.status_code == 404
|
assert resp.status_code == 404
|
||||||
@ -66,50 +66,50 @@ def test_get_all_books(client, faker):
|
|||||||
|
|
||||||
# Given
|
# Given
|
||||||
def create_random_book():
|
def create_random_book():
|
||||||
return Libros.objects.create(
|
return Book.objects.create(
|
||||||
title=faker.name(),
|
title=faker.name(),
|
||||||
genre=faker.job(),
|
genre=faker.job(),
|
||||||
author=faker.name_nonbinary(),
|
author=faker.name_nonbinary(),
|
||||||
year=faker.year(),
|
year=faker.year(),
|
||||||
)
|
)
|
||||||
|
|
||||||
libro_1 = create_random_book()
|
book_1 = create_random_book()
|
||||||
libro_2 = create_random_book()
|
book_2 = create_random_book()
|
||||||
|
|
||||||
# When
|
# When
|
||||||
resp = client.get(reverse("libros_list"))
|
resp = client.get(reverse("book_list"))
|
||||||
|
|
||||||
# Then
|
# Then
|
||||||
assert resp.status_code == 200
|
assert resp.status_code == 200
|
||||||
assert resp.data[0]["title"] == libro_1.title
|
assert resp.data[0]["title"] == book_1.title
|
||||||
assert resp.data[1]["title"] == libro_2.title
|
assert resp.data[1]["title"] == book_2.title
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_remove_book(client):
|
def test_remove_book(client):
|
||||||
|
|
||||||
# Given
|
# Given
|
||||||
libro = Libros.objects.create(
|
book = Book.objects.create(
|
||||||
title="El fin de la eternidad",
|
title="The End of Eternity",
|
||||||
genre="Ciencia Ficción",
|
genre="Sciencie Fiction",
|
||||||
author="Isaac Asimov",
|
author="Isaac Asimov",
|
||||||
year="1955",
|
year="1955",
|
||||||
)
|
)
|
||||||
## Check exist
|
## Check exist
|
||||||
resp_detail = client.get(reverse("libros_details", kwargs={"pk": libro.id}))
|
resp_detail = client.get(reverse("book_details", kwargs={"pk": book.id}))
|
||||||
assert resp_detail.status_code == 200
|
assert resp_detail.status_code == 200
|
||||||
assert resp_detail.data["title"] == "El fin de la eternidad"
|
assert resp_detail.data["title"] == "The End of Eternity"
|
||||||
|
|
||||||
# When
|
# When
|
||||||
resp_delete = client.delete(reverse("libros_details", kwargs={"pk": libro.id}))
|
resp_delete = client.delete(reverse("book_details", kwargs={"pk": book.id}))
|
||||||
resp_list = client.get(reverse("libros_list"))
|
resp_list = client.get(reverse("book_list"))
|
||||||
rest_new_detail = client.get(reverse("libros_details", kwargs={"pk": libro.id}))
|
rest_new_detail = client.get(reverse("book_details", kwargs={"pk": book.id}))
|
||||||
|
|
||||||
# Then
|
# Then
|
||||||
## Check status delete
|
## Check status delete
|
||||||
assert resp_delete.status_code == 200
|
assert resp_delete.status_code == 200
|
||||||
## Check return delete
|
## Check return delete
|
||||||
assert resp_delete.data["title"] == "El fin de la eternidad"
|
assert resp_delete.data["title"] == "The End of Eternity"
|
||||||
## Check status list
|
## Check status list
|
||||||
assert resp_list.status_code == 200
|
assert resp_list.status_code == 200
|
||||||
## Check not item list
|
## Check not item list
|
||||||
@ -121,15 +121,15 @@ def test_remove_book(client):
|
|||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_remove_book_incorrect_id(client):
|
def test_remove_book_incorrect_id(client):
|
||||||
# Given
|
# Given
|
||||||
libro = Libros.objects.create(
|
book = Book.objects.create(
|
||||||
title="El fin de la eternidad",
|
title="The End of Eternity",
|
||||||
genre="Ciencia Ficción",
|
genre="Sciencie Fiction",
|
||||||
author="Isaac Asimov",
|
author="Isaac Asimov",
|
||||||
year="1955",
|
year="1955",
|
||||||
)
|
)
|
||||||
|
|
||||||
# When
|
# When
|
||||||
resp = client.delete(reverse("libros_details", kwargs={"pk": 99}))
|
resp = client.delete(reverse("book_details", kwargs={"pk": 99}))
|
||||||
|
|
||||||
# Then
|
# Then
|
||||||
assert resp.status_code == 404
|
assert resp.status_code == 404
|
||||||
@ -139,19 +139,19 @@ def test_remove_book_incorrect_id(client):
|
|||||||
def test_update_book(client):
|
def test_update_book(client):
|
||||||
|
|
||||||
# Given
|
# Given
|
||||||
libro = Libros.objects.create(
|
book = Book.objects.create(
|
||||||
title="El fin de la eternidad",
|
title="The End of Eternity",
|
||||||
genre="Ciencia Ficción",
|
genre="Sciencie Fiction",
|
||||||
author="Isaac Asimov",
|
author="Isaac Asimov",
|
||||||
year="1955",
|
year="1955",
|
||||||
)
|
)
|
||||||
|
|
||||||
# When
|
# When
|
||||||
resp = client.put(
|
resp = client.put(
|
||||||
reverse("libros_details", kwargs={"pk": libro.id}),
|
reverse("book_details", kwargs={"pk": book.id}),
|
||||||
{
|
{
|
||||||
"title": "Dune",
|
"title": "Dune",
|
||||||
"genre": "Ciencia Ficción",
|
"genre": "Sciencie Fiction",
|
||||||
"author": "Frank Herbert",
|
"author": "Frank Herbert",
|
||||||
"year": "1965",
|
"year": "1965",
|
||||||
},
|
},
|
||||||
@ -161,40 +161,40 @@ def test_update_book(client):
|
|||||||
# Then
|
# Then
|
||||||
assert resp.status_code == 200
|
assert resp.status_code == 200
|
||||||
assert resp.data["title"] == "Dune"
|
assert resp.data["title"] == "Dune"
|
||||||
assert resp.data["genre"] == "Ciencia Ficción"
|
assert resp.data["genre"] == "Sciencie Fiction"
|
||||||
assert resp.data["author"] == "Frank Herbert"
|
assert resp.data["author"] == "Frank Herbert"
|
||||||
assert resp.data["year"] == "1965"
|
assert resp.data["year"] == "1965"
|
||||||
|
|
||||||
resp_detail = client.get(reverse("libros_details", kwargs={"pk": libro.id}))
|
resp_detail = client.get(reverse("book_details", kwargs={"pk": book.id}))
|
||||||
assert resp_detail.status_code == 200
|
assert resp_detail.status_code == 200
|
||||||
assert resp_detail.data["title"] == "Dune"
|
assert resp_detail.data["title"] == "Dune"
|
||||||
assert resp_detail.data["genre"] == "Ciencia Ficción"
|
assert resp_detail.data["genre"] == "Sciencie Fiction"
|
||||||
assert resp_detail.data["author"] == "Frank Herbert"
|
assert resp_detail.data["author"] == "Frank Herbert"
|
||||||
assert resp_detail.data["year"] == "1965"
|
assert resp_detail.data["year"] == "1965"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_update_book_incorrect_id(client):
|
def test_update_book_incorrect_id(client):
|
||||||
resp = client.put(reverse("libros_details", kwargs={"pk": 99}))
|
resp = client.put(reverse("book_details", kwargs={"pk": 99}))
|
||||||
assert resp.status_code == 404
|
assert resp.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_update_book_invalid_json(client):
|
def test_update_book_invalid_json(client):
|
||||||
# Given
|
# Given
|
||||||
libro = Libros.objects.create(
|
book = Book.objects.create(
|
||||||
title="El fin de la eternidad",
|
title="The End of Eternity",
|
||||||
genre="Ciencia Ficción",
|
genre="Sciencie Fiction",
|
||||||
author="Isaac Asimov",
|
author="Isaac Asimov",
|
||||||
year="1955",
|
year="1955",
|
||||||
)
|
)
|
||||||
|
|
||||||
# When
|
# When
|
||||||
resp = client.put(
|
resp = client.put(
|
||||||
reverse("libros_details", kwargs={"pk": libro.id}),
|
reverse("book_details", kwargs={"pk": book.id}),
|
||||||
{
|
{
|
||||||
"foo": "Dune",
|
"foo": "Dune",
|
||||||
"boo": "Ciencia Ficción",
|
"boo": "Sciencie Fiction",
|
||||||
},
|
},
|
||||||
content_type="application/json",
|
content_type="application/json",
|
||||||
)
|
)
|
@ -1,30 +0,0 @@
|
|||||||
# tests/libros/test_model.py
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from app.libros.models import Libros
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
|
||||||
def test_libros_model():
|
|
||||||
|
|
||||||
## Given
|
|
||||||
# Creamos un nuevo libro en la base de datos
|
|
||||||
libro = Libros(
|
|
||||||
title="La fundación",
|
|
||||||
genre="Ciencia ficción",
|
|
||||||
year="1951",
|
|
||||||
author="Isaac Asimov",
|
|
||||||
)
|
|
||||||
libro.save()
|
|
||||||
|
|
||||||
## When
|
|
||||||
|
|
||||||
## Then
|
|
||||||
assert libro.title == "La fundación"
|
|
||||||
assert libro.genre == "Ciencia ficción"
|
|
||||||
assert libro.year == "1951"
|
|
||||||
assert libro.author == "Isaac Asimov"
|
|
||||||
assert libro.created_at
|
|
||||||
assert libro.updated_at
|
|
||||||
assert str(libro) == libro.title
|
|
@ -1,3 +0,0 @@
|
|||||||
# Las funciones deben empezar por "test_"
|
|
||||||
def test_titulo():
|
|
||||||
assert "canción de hielo y fuego" != "juego de tronos"
|
|
Loading…
Reference in New Issue
Block a user