Last active
May 6, 2024 16:16
-
-
Save joshuadavidthomas/f1aa45b7c8616228b573a07708736635 to your computer and use it in GitHub Desktop.
`factory-boy` vs `model-bakery`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
import itertools | |
import pytest | |
from model_bakery import baker | |
from .factories import ActivityFactory | |
@pytest.fixture | |
def activity_factory(db): | |
""" | |
Test fixture using externally defined factory for Activity model. | |
""" | |
return ActivityFactory() | |
@pytest.fixture | |
def activity_baker(faker, db): | |
""" | |
Test fixture for Activity model using `model-bakery`. No need for defining any | |
other files or classes to accomplish the same thing as above. | |
`model-bakery` automatically introspects the Model and generates dummy values | |
for any required field. To reach parity with the `ActivityFactory`, we will explicitly | |
set the `description` and `price` as well, since `model-bakery` would normally ignore | |
them (`null` and/or `blank` being equal to `True`). | |
""" | |
return baker.make( | |
"activities.Activity", | |
category=itertools.cycle(Activity.CATEGORY_CHOICES), | |
description=faker.word(), | |
price=faker.pyfloat(left_digits=4, right_digits=2, positive=True) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
from factory import Faker | |
from factory.django import DjangoModelFactory | |
from factory.fuzzy import FuzzyChoice | |
from .models import Activity | |
class ActivityFactory(DjangoModelFactory): | |
class Meta: | |
model = Activity | |
description = Faker("word") | |
category = FuzzyChoice(Activity.CATEGORY_CHOICES, getter=lambda c: c[0]) | |
price = Faker("pyfloat", left_digits=4, right_digits=2, positive=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
from django.db import models | |
from django.utils.translation import gettext_lazy as _ | |
class Activity(models.Model): | |
CATEGORY_CHOICES = ( | |
("A", _("Angling")), | |
("H", _("Hunting")), | |
("S", _("School")), | |
("CB", _("Cast N Blast")), | |
("T", _("Transportation")), | |
("M", _("Misc")), | |
("O", _("Other")), | |
) | |
name = models.CharField(max_length=255, help_text=_("The name of the activity.")) | |
description = models.CharField(max_length=255, null=True, blank=True) | |
category = models.CharField(choices=CATEGORY_CHOICES, max_length=255) | |
price = models.DecimalField(decimal_places=2, default=0.0, max_digits=7, null=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
requirements.in: