I am trying to write TestCase with django factory and this lill bit advance testcase writing..
This is my models.py below:
class Author(models.Model):
name = models.CharField(max_length=25)
def __str__(self):
return self.name
class Book(models.Model):
author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='author')
title = models.CharField(max_length=200)
body = models.TextField()
def __str__ (self):
return self.title
and this is my tests.py below
from django.test import TestCase
from library.models import Author, Book
import factory
# Create factory for model
class AuthorFactory(factory.django.DjangoModelFactory):
class Meta:
model = Author
name = 'author name'
class BookFactory(factory.django.DjangoModelFactory):
class Meta:
model = Book
author = factory.SubFactory(AuthorFactory)
title = 'i am title'
body = 'i am body'
# Start TestCase here
class TestSingleBook(TestCase):
def setUp(self):
self.author = AuthorFactory.create_batch(
10,
name='Jhon Doe'
)
self.book = BookFactory.create_batch(
author=self.author
)
def test_single_book_get(self):
# compare here
above code, my testcase class is class TestSingleBook(TestCase): i hope you noticed it..
I want to create 10 Author and create 10 book with those 10 Author, and then i will compare it in assertEqual
if i create multiple book with single author, i can craft the test easily but i dont want it...
My Problem is Nested/Multiple...
Create multiple author and then create multiple book with those author...
is there anyone who solved these kind of problem before?
Can anyone help me in this case? if you dont get above all these, feel free to ask me regarding this..