Add model Libros y serializadores
This commit is contained in:
parent
964053e51c
commit
d0a2dfeddc
30
app/libros/migrations/0002_libros.py
Normal file
30
app/libros/migrations/0002_libros.py
Normal file
@ -0,0 +1,30 @@
|
||||
# Generated by Django 3.2.5 on 2021-07-09 16:33
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('libros', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Libros',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(max_length=255)),
|
||||
('genre', models.CharField(max_length=255)),
|
||||
('year', models.CharField(max_length=4)),
|
||||
('author', models.CharField(max_length=255)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Libro',
|
||||
'verbose_name_plural': 'Libros',
|
||||
'ordering': ('-created_at',),
|
||||
},
|
||||
),
|
||||
]
|
@ -1,6 +1,25 @@
|
||||
from django.db import models
|
||||
# app/libros/models.py
|
||||
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.db import models
|
||||
|
||||
|
||||
class CustomUser(AbstractUser):
|
||||
pass
|
||||
|
||||
|
||||
class Libros(models.Model):
|
||||
title = models.CharField(max_length=255)
|
||||
genre = models.CharField(max_length=255)
|
||||
year = models.CharField(max_length=4)
|
||||
author = models.CharField(max_length=255)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ("-created_at",)
|
||||
verbose_name = "Libro"
|
||||
verbose_name_plural = "Libros"
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
15
app/libros/serializers.py
Normal file
15
app/libros/serializers.py
Normal file
@ -0,0 +1,15 @@
|
||||
# app/libros/serializers.py
|
||||
|
||||
from rest_framework import serializers
|
||||
from .models import Libros
|
||||
|
||||
|
||||
class LibroSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Libros
|
||||
fields = "__all__"
|
||||
read_only_fields = (
|
||||
"id",
|
||||
"created_date",
|
||||
"updated_date",
|
||||
)
|
29
tests/libros/test_create_model.py
Normal file
29
tests/libros/test_create_model.py
Normal file
@ -0,0 +1,29 @@
|
||||
# tests/libros/test_create_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
|
31
tests/libros/test_serializers.py
Normal file
31
tests/libros/test_serializers.py
Normal file
@ -0,0 +1,31 @@
|
||||
# tests/libros/test_serializers.py
|
||||
|
||||
from app.libros.serializers import LibroSerializer
|
||||
|
||||
def test_valid_libro_serializer():
|
||||
valid_serializer_data = {
|
||||
"title": "Raising Arizona",
|
||||
"genre": "comedy",
|
||||
"year": "1987",
|
||||
"author": "Ray Bradbury",
|
||||
}
|
||||
serializer = LibroSerializer(data=valid_serializer_data)
|
||||
assert serializer.is_valid()
|
||||
assert serializer.validated_data == valid_serializer_data
|
||||
assert serializer.data == valid_serializer_data
|
||||
assert serializer.errors == {}
|
||||
|
||||
|
||||
def test_invalid_libro_serializer():
|
||||
invalid_serializer_data = {
|
||||
"title": "Soy Leyenda",
|
||||
"author": "Richard Matheson",
|
||||
}
|
||||
serializer = LibroSerializer(data=invalid_serializer_data)
|
||||
assert not serializer.is_valid()
|
||||
assert serializer.validated_data == {}
|
||||
assert serializer.data == invalid_serializer_data
|
||||
assert serializer.errors == {
|
||||
"year": ["This field is required."],
|
||||
"genre": ["This field is required."],
|
||||
}
|
Loading…
Reference in New Issue
Block a user