Reputation: 21
Use Factory-boy for retrieve operation without use the DB for testing case.
I have this simple model:
class Student(models.Model):
name = models.CharField(max_length=20) `
To get all: Student.objects.all()
With Factory-boy:
class StudentFactory(factory.django.DjangoModelFactory):
class Meta:
model = Student
Is there a way to make StudentFactory.objects.all() ?
When I call the method all() in my factory, I would like to return a list of QuerySet created by me. Example: [QuerySet_1, QuerySet_2] # Not Database.
With that, I can change my data from DB to memory in my test.
Upvotes: 1
Views: 325
Reputation: 968
You may be looking for the methods create_batch and build_batch, depending on whether you want to save the newly generated instances in the test
database or not.
Here's an example which I copy-pasted and adapted from factory-boy documentation:
# --- models.py
class StudentFactory(factory.django.DjangoModelFactory):
class Meta:
model = Student
# --- test_student_factory.py
from . import factories
def make_objects():
factories.StudentFactory.create_batch(size=50)
Upvotes: 1