Skip to content

Instantly share code, notes, and snippets.

@thoroc
Last active November 29, 2022 20:47
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 thoroc/9b3722fbcba8a4834dbe8b96e14e8f17 to your computer and use it in GitHub Desktop.
Save thoroc/9b3722fbcba8a4834dbe8b96e14e8f17 to your computer and use it in GitHub Desktop.
Example: FactoryBoy use with Faker Custom Provider
# 1. Create custom provider
from datetime import datetime
from faker.providers import BaseProvider
class PersonProvider(BaseProvider):
__provider__ = "person_title"
__provider__ = "person_first_name"
__provider__ = "person_email"
__provider__ = "person_dob"
def person_title(self, is_male):
if is_male:
return self.generator.prefix_male()
return self.generator.prefix_female()
def person_first_name(self, is_male):
if is_male:
return self.generator.first_name_male()
return self.generate.first_name_female()
def person_email(self, first: str, last: str, domain: str):
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
return f"test-{first.lower()}.{last.lower()}-{timestamp}@{domain}"
def person_dob(self, is_underage: bool):
if is_underage:
return self.generator.date_of_birth()
return self.generator.date_of_birth(minimum_age=18)
# 2. Add provider to instance of Faker from factory
from faker import Faker
faker = Faker("en_GB")
faker.add_provider(PersonProvider)
# 3. Declare your model
from dataclasses import dataclass
from dataclasses_json import dataclass_json
@dataclass_json
@dataclass
class Person:
title: str
first: str
last: str
email: str
date_of_birth: str
# 4. Use the new provider
import factory
from factory.fuzzy import FuzzyChoice
class PersonFactory(factory.Factory):
class Meta:
model = Person
class Params:
is_male = FuzzyChoice([True, False])
is_underage = FuzzyChoice([True, False])
domain = factory.LazyAttribute(lambda _: faker.free_email_domain())
title = factory.LazyAttribute(lambda o: faker.person_title(is_male=o.is_male))
first = factory.LazyAttribute(lambda o: faker.person_first_name(is_male=o.is_male))
last = factory.LazyAttribute(lambda _: faker.last_name())
email = factory.LazyAttribute(lambda o: faker.person_email(o.first, o.last, domain=o.domain))
date_of_birth = factory.LazyAttribute(lambda o: faker.person_dob(is_underage=o.is_underage).strftime("%Y-%m-%d"))
persons = PersonFactory.create_batch(10, is_underage=False, is_male=True)
for person in persons:
print(person.to_json())

Usage

Example 1

men = PersonFactory.create_batch(5, is_underage=False, is_male=True)
for m in men:
  print(m.to_json())

Output

{"title": "Dr", "first": "Charles", "last": "Davis", "email": "test-charles.davis-20221113_231706@hotmail.com", "date_of_birth": "2000-01-03"}
{"title": "Mr", "first": "Julian", "last": "Bartlett", "email": "test-julian.bartlett-20221113_231706@yahoo.com", "date_of_birth": "1940-05-12"}
{"title": "Dr", "first": "Michael", "last": "Lamb", "email": "test-michael.lamb-20221113_231706@outlook.com", "date_of_birth": "1942-10-23"}
{"title": "Mr", "first": "Gavin", "last": "Hunt", "email": "test-gavin.hunt-20221113_231706@gmail.com", "date_of_birth": "1925-12-16"}
{"title": "Mr", "first": "Gerard", "last": "Griffin", "email": "test-gerard.griffin-20221113_231706@yahoo.com", "date_of_birth": "1931-03-06"}

Example 2

women = PersonFactory.create_batch(5, is_underage=False, is_male=False)
for w in women:
  print(w.to_json())

Output

"title": "Miss", "first": "Margaret", "last": "Cooper", "email": "test-margaret.cooper-20221113_231706@yahoo.com", "date_of_birth": "1972-02-12"}
{"title": "Mrs", "first": "Heather", "last": "Cook", "email": "test-heather.cook-20221113_231706@yahoo.co.uk", "date_of_birth": "1939-09-27"}
{"title": "Miss", "first": "Annette", "last": "Butcher", "email": "test-annette.butcher-20221113_231706@hotmail.co.uk", "date_of_birth": "1977-02-15"}
{"title": "Mrs", "first": "Chelsea", "last": "Smith", "email": "test-chelsea.smith-20221113_231706@hotmail.com", "date_of_birth": "1949-08-31"}
{"title": "Dr", "first": "Kirsty", "last": "Godfrey", "email": "test-kirsty.godfrey-20221113_231706@hotmail.com", "date_of_birth": "1998-01-08"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment