0
0
Djangoframework~20 mins

Factory Boy for test data in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Factory Boy Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Factory Boy factory produce?
Given the following factory definition, what will be the value of user.email after user = UserFactory() is executed?
Django
import factory
from myapp.models import User

class UserFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = User

    username = factory.Sequence(lambda n: f'user{n}')
    email = factory.LazyAttribute(lambda obj: f'{obj.username}@example.com')

user = UserFactory()
A"user0@example.com"
BRaises an AttributeError
C"user1@example.com"
D"user@example.com"
Attempts:
2 left
💡 Hint
Look at how the username is generated and how email uses it.
📝 Syntax
intermediate
2:00remaining
Which factory definition has a syntax error?
Identify the factory definition that will cause a syntax error when run.
A
class ProductFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Product
    price = factory.Faker('random_number', digits=3)
B
)3=stigid ,'rebmun_modnar'(rekaF.yrotcaf = ecirp    
tcudorP = ledom        
:ateM ssalc    
:)yrotcaFledoMognajD.ognajd.yrotcaf(yrotcaFtcudorP ssalc
C
class ProductFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Product
    name = factory.Faker('word')
    price = factory.Faker('random_number', digits=3)
D
class ProductFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Product
    name = factory.Faker('word')
Attempts:
2 left
💡 Hint
Check the commas between arguments in function calls.
state_output
advanced
2:00remaining
What is the value of order.total after creation?
Given this factory and model, what will order.total be after order = OrderFactory()?
Django
import factory
from myapp.models import Order

class OrderFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Order

    quantity = 3
    price_per_item = 10
    total = factory.LazyAttribute(lambda o: o.quantity * o.price_per_item)

order = OrderFactory()
A0
BNone
CRaises AttributeError
D30
Attempts:
2 left
💡 Hint
Look at how total is calculated using LazyAttribute.
🔧 Debug
advanced
2:00remaining
Why does this factory raise a RecursionError?
Examine the factory below. Why does creating an instance with profile = ProfileFactory() cause a RecursionError?
Django
import factory
from myapp.models import Profile, User

class UserFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = User

    username = factory.Sequence(lambda n: f'user{n}')

class ProfileFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Profile

    user = factory.SubFactory(UserFactory)
    friend = factory.SubFactory('myapp.factories.ProfileFactory')

profile = ProfileFactory()
ABecause UserFactory is missing a required field
BBecause factory.SubFactory requires a callable, not a string
CBecause ProfileFactory references itself in friend causing infinite recursion
DBecause Profile model does not have a friend field
Attempts:
2 left
💡 Hint
Look at the friend field in ProfileFactory.
🧠 Conceptual
expert
3:00remaining
Which option correctly uses Factory Boy to create related objects with different attributes?
You want to create a Book with two Author objects, each having different names. Which factory setup achieves this correctly?
Django
from myapp.models import Book, Author
import factory

class AuthorFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Author

    name = factory.Sequence(lambda n: f'Author {n}')

class BookFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Book

    title = 'My Book'
    authors = factory.RelatedFactoryList(AuthorFactory, related_name='book', size=2)
AUse SubFactory twice in BookFactory for authors with fixed names
BUse RelatedFactoryList with size=2 and AuthorFactory with Sequence for names
CUse RelatedFactoryList with size=2 but AuthorFactory has fixed name 'Author'
DUse a post_generation hook to add two authors with different names manually
Attempts:
2 left
💡 Hint
Think about how to create multiple related objects with unique attributes.