Skip to content

Instantly share code, notes, and snippets.

@brunomichetti
Created July 1, 2022 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunomichetti/247f8da69ffd805589a84a4fd0b3b00a to your computer and use it in GitHub Desktop.
Save brunomichetti/247f8da69ffd805589a84a4fd0b3b00a to your computer and use it in GitHub Desktop.
# factory.py inside users/test
from faker import Faker as FakerClass
from typing import Any, Sequence
from factory import django, Faker, post_generation
from users.models import User, Category
CATEGORIES_VALUES = [x[0] for x in Category.choices]
class UserFactory(django.DjangoModelFactory):
class Meta:
model = User
username = Faker('user_name')
phone_number = Faker('phone_number')
category = Faker('random_element', elements=CATEGORIES_VALUES)
@post_generation
def password(self, create: bool, extracted: Sequence[Any], **kwargs):
password = (
extracted
if extracted
else FakerClass().password(
length=30,
special_chars=True,
digits=True,
upper_case=True,
lower_case=True,
)
)
self.set_password(password)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment