Add fake data

This commit is contained in:
Andros Fenollosa 2021-07-14 16:46:34 +02:00
parent d04d21ae5b
commit aeb5199bdc
10 changed files with 658 additions and 24 deletions

View File

@ -25,6 +25,12 @@ python3 manage.py migrate
python3 manage.py runserver
```
## Fake data
``` bash
python3 manage.py runscript create_books
```
## Test
``` bash

View File

@ -1,4 +1,4 @@
# Generated by Django 3.2.5 on 2021-07-14 14:16
# Generated by Django 3.2.5 on 2021-07-14 14:37
from django.db import migrations, models
@ -16,7 +16,7 @@ class Migration(migrations.Migration):
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)),
('country', 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)),

View File

@ -5,7 +5,7 @@ from django.db import models
class Book(models.Model):
title = models.CharField(max_length=255)
genre = models.CharField(max_length=255)
country = 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)

View File

@ -37,8 +37,9 @@ INSTALLED_APPS = [
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"rest_framework", # nuevo
"app.library", # nuevo
"rest_framework",
"app.library",
"django_extensions",
]
MIDDLEWARE = [

View File

@ -1,5 +1,6 @@
django
djangorestframework
pytest-django
pytest
Faker
Django==3.2.5
djangorestframework==3.12.4
django-extensions==3.1.3
Faker==8.10.1
pytest==6.2.4
pytest-django==4.4.0

24
scripts/create_books.py Normal file
View File

@ -0,0 +1,24 @@
from app.library.models import Book
import json
def run():
print('Working')
# Delete old books
Book.objects.all().delete()
# Read file
with open("scripts/example_books.json", "r") as file_books:
books = json.loads(file_books.read())
# Create books
for book in books:
Book(
title=book['title'],
author=book['author'],
country=book['country'],
year=book['year'],
).save()
print('Finish!')

602
scripts/example_books.json Normal file
View File

@ -0,0 +1,602 @@
[
{
"author": "Chinua Achebe",
"country": "Nigeria",
"title": "Things Fall Apart",
"year": 1958
},
{
"author": "Hans Christian Andersen",
"country": "Denmark",
"title": "Fairy tales",
"year": 1836
},
{
"author": "Dante Alighieri",
"country": "Italy",
"title": "The Divine Comedy",
"year": 1315
},
{
"author": "Unknown",
"country": "Sumer and Akkadian Empire",
"title": "The Epic Of Gilgamesh",
"year": -1700
},
{
"author": "Unknown",
"country": "Achaemenid Empire",
"title": "The Book Of Job",
"year": -600
},
{
"author": "Unknown",
"country": "India/Iran/Iraq/Egypt/Tajikistan",
"title": "One Thousand and One Nights",
"year": 1200
},
{
"author": "Unknown",
"country": "Iceland",
"title": "Nj\u00e1l's Saga",
"year": 1350
},
{
"author": "Jane Austen",
"country": "United Kingdom",
"title": "Pride and Prejudice",
"year": 1813
},
{
"author": "Honor\u00e9 de Balzac",
"country": "France",
"title": "Le P\u00e8re Goriot",
"year": 1835
},
{
"author": "Samuel Beckett",
"country": "Republic of Ireland",
"title": "Molloy, Malone Dies, The Unnamable, the trilogy",
"year": 1952
},
{
"author": "Giovanni Boccaccio",
"country": "Italy",
"title": "The Decameron",
"year": 1351
},
{
"author": "Jorge Luis Borges",
"country": "Argentina",
"title": "Ficciones",
"year": 1965
},
{
"author": "Emily Bront\u00eb",
"country": "United Kingdom",
"title": "Wuthering Heights",
"year": 1847
},
{
"author": "Albert Camus",
"country": "Algeria, French Empire",
"title": "The Stranger",
"year": 1942
},
{
"author": "Paul Celan",
"country": "Romania, France",
"title": "Poems",
"year": 1952
},
{
"author": "Louis-Ferdinand C\u00e9line",
"country": "France",
"title": "Journey to the End of the Night",
"year": 1932
},
{
"author": "Miguel de Cervantes",
"country": "Spain",
"title": "Don Quijote De La Mancha",
"year": 1610
},
{
"author": "Geoffrey Chaucer",
"country": "England",
"title": "The Canterbury Tales",
"year": 1450
},
{
"author": "Anton Chekhov",
"country": "Russia",
"title": "Stories",
"year": 1886
},
{
"author": "Joseph Conrad",
"country": "United Kingdom",
"title": "Nostromo",
"year": 1904
},
{
"author": "Charles Dickens",
"country": "United Kingdom",
"title": "Great Expectations",
"year": 1861
},
{
"author": "Denis Diderot",
"country": "France",
"title": "Jacques the Fatalist",
"year": 1796
},
{
"author": "Alfred D\u00f6blin",
"country": "Germany",
"title": "Berlin Alexanderplatz",
"year": 1929
},
{
"author": "Fyodor Dostoevsky",
"country": "Russia",
"title": "Crime and Punishment",
"year": 1866
},
{
"author": "Fyodor Dostoevsky",
"country": "Russia",
"title": "The Idiot",
"year": 1869
},
{
"author": "Fyodor Dostoevsky",
"country": "Russia",
"title": "The Possessed",
"year": 1872
},
{
"author": "Fyodor Dostoevsky",
"country": "Russia",
"title": "The Brothers Karamazov",
"year": 1880
},
{
"author": "George Eliot",
"country": "United Kingdom",
"title": "Middlemarch",
"year": 1871
},
{
"author": "Ralph Ellison",
"country": "United States",
"title": "Invisible Man",
"year": 1952
},
{
"author": "Euripides",
"country": "Greece",
"title": "Medea",
"year": -431
},
{
"author": "William Faulkner",
"country": "United States",
"title": "Absalom, Absalom!",
"year": 1936
},
{
"author": "William Faulkner",
"country": "United States",
"title": "The Sound and the Fury",
"year": 1929
},
{
"author": "Gustave Flaubert",
"country": "France",
"title": "Madame Bovary",
"year": 1857
},
{
"author": "Gustave Flaubert",
"country": "France",
"title": "Sentimental Education",
"year": 1869
},
{
"author": "Federico Garc\u00eda Lorca",
"country": "Spain",
"title": "Gypsy Ballads",
"year": 1928
},
{
"author": "Gabriel Garc\u00eda M\u00e1rquez",
"country": "Colombia",
"title": "One Hundred Years of Solitude",
"year": 1967
},
{
"author": "Gabriel Garc\u00eda M\u00e1rquez",
"country": "Colombia",
"title": "Love in the Time of Cholera",
"year": 1985
},
{
"author": "Johann Wolfgang von Goethe",
"country": "Saxe-Weimar",
"title": "Faust",
"year": 1832
},
{
"author": "Nikolai Gogol",
"country": "Russia",
"title": "Dead Souls",
"year": 1842
},
{
"author": "G\u00fcnter Grass",
"country": "Germany",
"title": "The Tin Drum",
"year": 1959
},
{
"author": "Jo\u00e3o Guimar\u00e3es Rosa",
"country": "Brazil",
"title": "The Devil to Pay in the Backlands",
"year": 1956
},
{
"author": "Knut Hamsun",
"country": "Norway",
"title": "Hunger",
"year": 1890
},
{
"author": "Ernest Hemingway",
"country": "United States",
"title": "The Old Man and the Sea",
"year": 1952
},
{
"author": "Homer",
"country": "Greece",
"title": "Iliad",
"year": -735
},
{
"author": "Homer",
"country": "Greece",
"title": "Odyssey",
"year": -800
},
{
"author": "Henrik Ibsen",
"country": "Norway",
"title": "A Doll's House",
"year": 1879
},
{
"author": "James Joyce",
"country": "Irish Free State",
"title": "Ulysses",
"year": 1922
},
{
"author": "Franz Kafka",
"country": "Czechoslovakia",
"title": "Stories",
"year": 1924
},
{
"author": "Franz Kafka",
"country": "Czechoslovakia",
"title": "The Trial",
"year": 1925
},
{
"author": "Franz Kafka",
"country": "Czechoslovakia",
"title": "The Castle",
"year": 1926
},
{
"author": "K\u0101lid\u0101sa",
"country": "India",
"title": "The recognition of Shakuntala",
"year": 150
},
{
"author": "Yasunari Kawabata",
"country": "Japan",
"title": "The Sound of the Mountain",
"year": 1954
},
{
"author": "Nikos Kazantzakis",
"country": "Greece",
"title": "Zorba the Greek",
"year": 1946
},
{
"author": "D. H. Lawrence",
"country": "United Kingdom",
"title": "Sons and Lovers",
"year": 1913
},
{
"author": "Halld\u00f3r Laxness",
"country": "Iceland",
"title": "Independent People",
"year": 1934
},
{
"author": "Giacomo Leopardi",
"country": "Italy",
"title": "Poems",
"year": 1818
},
{
"author": "Doris Lessing",
"country": "United Kingdom",
"title": "The Golden Notebook",
"year": 1962
},
{
"author": "Astrid Lindgren",
"country": "Sweden",
"title": "Pippi Longstocking",
"year": 1945
},
{
"author": "Lu Xun",
"country": "China",
"title": "Diary of a Madman",
"year": 1918
},
{
"author": "Naguib Mahfouz",
"country": "Egypt",
"title": "Children of Gebelawi",
"year": 1959
},
{
"author": "Thomas Mann",
"country": "Germany",
"title": "Buddenbrooks",
"year": 1901
},
{
"author": "Thomas Mann",
"country": "Germany",
"title": "The Magic Mountain",
"year": 1924
},
{
"author": "Herman Melville",
"country": "United States",
"title": "Moby Dick",
"year": 1851
},
{
"author": "Michel de Montaigne",
"country": "France",
"title": "Essays",
"year": 1595
},
{
"author": "Elsa Morante",
"country": "Italy",
"title": "History",
"year": 1974
},
{
"author": "Toni Morrison",
"country": "United States",
"title": "Beloved",
"year": 1987
},
{
"author": "Murasaki Shikibu",
"country": "Japan",
"title": "The Tale of Genji",
"year": 1006
},
{
"author": "Robert Musil",
"country": "Austria",
"title": "The Man Without Qualities",
"year": 1931
},
{
"author": "Vladimir Nabokov",
"country": "Russia/United States",
"title": "Lolita",
"year": 1955
},
{
"author": "George Orwell",
"country": "United Kingdom",
"title": "Nineteen Eighty-Four",
"year": 1949
},
{
"author": "Ovid",
"country": "Roman Empire",
"title": "Metamorphoses",
"year": 100
},
{
"author": "Fernando Pessoa",
"country": "Portugal",
"title": "The Book of Disquiet",
"year": 1928
},
{
"author": "Edgar Allan Poe",
"country": "United States",
"title": "Tales",
"year": 1950
},
{
"author": "Marcel Proust",
"country": "France",
"title": "In Search of Lost Time",
"year": 1920
},
{
"author": "Fran\u00e7ois Rabelais",
"country": "France",
"title": "Gargantua and Pantagruel",
"year": 1533
},
{
"author": "Juan Rulfo",
"country": "Mexico",
"title": "Pedro P\u00e1ramo",
"year": 1955
},
{
"author": "Rumi",
"country": "Sultanate of Rum",
"title": "The Masnavi",
"year": 1236
},
{
"author": "Salman Rushdie",
"country": "United Kingdom, India",
"title": "Midnight's Children",
"year": 1981
},
{
"author": "Saadi",
"country": "Persia, Persian Empire",
"title": "Bostan",
"year": 1257
},
{
"author": "Tayeb Salih",
"country": "Sudan",
"title": "Season of Migration to the North",
"year": 1966
},
{
"author": "Jos\u00e9 Saramago",
"country": "Portugal",
"title": "Blindness",
"year": 1995
},
{
"author": "William Shakespeare",
"country": "England",
"title": "Hamlet",
"year": 1603
},
{
"author": "William Shakespeare",
"country": "England",
"title": "King Lear",
"year": 1608
},
{
"author": "William Shakespeare",
"country": "England",
"title": "Othello",
"year": 1609
},
{
"author": "Sophocles",
"country": "Greece",
"title": "Oedipus the King",
"year": -430
},
{
"author": "Stendhal",
"country": "France",
"title": "The Red and the Black",
"year": 1830
},
{
"author": "Laurence Sterne",
"country": "England",
"title": "The Life And Opinions of Tristram Shandy",
"year": 1760
},
{
"author": "Italo Svevo",
"country": "Italy",
"title": "Confessions of Zeno",
"year": 1923
},
{
"author": "Jonathan Swift",
"country": "Ireland",
"title": "Gulliver's Travels",
"year": 1726
},
{
"author": "Leo Tolstoy",
"country": "Russia",
"title": "War and Peace",
"year": 1867
},
{
"author": "Leo Tolstoy",
"country": "Russia",
"title": "Anna Karenina",
"year": 1877
},
{
"author": "Leo Tolstoy",
"country": "Russia",
"title": "The Death of Ivan Ilyich",
"year": 1886
},
{
"author": "Mark Twain",
"country": "United States",
"title": "The Adventures of Huckleberry Finn",
"year": 1884
},
{
"author": "Valmiki",
"country": "India",
"title": "Ramayana",
"year": -450
},
{
"author": "Virgil",
"country": "Roman Empire",
"title": "The Aeneid",
"year": -23
},
{
"author": "Vyasa",
"country": "India",
"title": "Mahabharata",
"year": -700
},
{
"author": "Walt Whitman",
"country": "United States",
"title": "Leaves of Grass",
"year": 1855
},
{
"author": "Virginia Woolf",
"country": "United Kingdom",
"title": "Mrs Dalloway",
"year": 1925
},
{
"author": "Virginia Woolf",
"country": "United Kingdom",
"title": "To the Lighthouse",
"year": 1927
},
{
"author": "Marguerite Yourcenar",
"country": "France/Belgium",
"title": "Memoirs of Hadrian",
"year": 1951
}
]

View File

@ -10,7 +10,7 @@ def test_book_model():
## Given
book = Book(
title="The foundation",
genre="Science fiction",
country="eeuu",
year="1951",
author="Isaac Asimov",
)
@ -20,7 +20,7 @@ def test_book_model():
## Then
assert book.title == "The foundation"
assert book.genre == "Science fiction"
assert book.country == "eeuu"
assert book.year == "1951"
assert book.author == "Isaac Asimov"
assert book.created_at

View File

@ -6,7 +6,7 @@ from app.library.serializers import BookSerializer
def test_valid_libro_serializer():
valid_serializer_data = {
"title": "Raising Arizona",
"genre": "comedy",
"country": "eeuu",
"year": "1987",
"author": "Ray Bradbury",
}
@ -28,5 +28,5 @@ def test_invalid_libro_serializer():
assert serializer.data == invalid_serializer_data
assert serializer.errors == {
"year": ["This field is required."],
"genre": ["This field is required."],
"country": ["This field is required."],
}

View File

@ -17,7 +17,7 @@ def test_add_book(client):
reverse("book_list"),
{
"title": "The End of Eternity",
"genre": "Sciencie Fiction",
"country": "eeuu",
"author": "Isaac Asimov",
"year": "1955",
},
@ -38,7 +38,7 @@ def test_get_single_book(client):
# Given
book = Book.objects.create(
title="The End of Eternity",
genre="Sciencie Fiction",
country="eeuu",
author="Isaac Asimov",
year="1955",
)
@ -68,7 +68,7 @@ def test_get_all_books(client, faker):
def create_random_book():
return Book.objects.create(
title=faker.name(),
genre=faker.job(),
country=faker.country(),
author=faker.name_nonbinary(),
year=faker.year(),
)
@ -91,7 +91,7 @@ def test_remove_book(client):
# Given
book = Book.objects.create(
title="The End of Eternity",
genre="Sciencie Fiction",
country="eeuu",
author="Isaac Asimov",
year="1955",
)
@ -123,7 +123,7 @@ def test_remove_book_incorrect_id(client):
# Given
book = Book.objects.create(
title="The End of Eternity",
genre="Sciencie Fiction",
country="eeuu",
author="Isaac Asimov",
year="1955",
)
@ -141,7 +141,7 @@ def test_update_book(client):
# Given
book = Book.objects.create(
title="The End of Eternity",
genre="Sciencie Fiction",
country="eeuu",
author="Isaac Asimov",
year="1955",
)
@ -151,7 +151,7 @@ def test_update_book(client):
reverse("book_details", kwargs={"pk": book.id}),
{
"title": "Dune",
"genre": "Sciencie Fiction",
"country": "eeuu",
"author": "Frank Herbert",
"year": "1965",
},
@ -161,14 +161,14 @@ def test_update_book(client):
# Then
assert resp.status_code == 200
assert resp.data["title"] == "Dune"
assert resp.data["genre"] == "Sciencie Fiction"
assert resp.data["country"] == "eeuu"
assert resp.data["author"] == "Frank Herbert"
assert resp.data["year"] == "1965"
resp_detail = client.get(reverse("book_details", kwargs={"pk": book.id}))
assert resp_detail.status_code == 200
assert resp_detail.data["title"] == "Dune"
assert resp_detail.data["genre"] == "Sciencie Fiction"
assert resp_detail.data["country"] == "eeuu"
assert resp_detail.data["author"] == "Frank Herbert"
assert resp_detail.data["year"] == "1965"
@ -184,7 +184,7 @@ def test_update_book_invalid_json(client):
# Given
book = Book.objects.create(
title="The End of Eternity",
genre="Sciencie Fiction",
country="eeuu",
author="Isaac Asimov",
year="1955",
)