ADR 0001: Faker Shortcuts
October 2023 . Eduardo Oliveira
Context
When we introduced the faker inside this project, we decided to use only one faker.Faker instance for all model generations. The reason for this is to grant the faker.unique generated values are really unique for various models context with the possibility to clear the already seen values.
To use only one faker.Fake instance we decided to parse this as the first argument of the functions to generate field values. To made it more easy to write generator for model fields, we need to write shortcuts for the faker.
Decision Drivers
- Easy to set the methods in the
@faker_fieldsdecorator. - The shortcuts can receive arguments to configure the faker methods.
- Must include the most part of standard providers of the faker.
- The shortcuts should be used to directly set the value to an field.
Decision
We decided to write shortcuts (this name is an inspiration from the original django.shortcuts package) with the format bellow, with the optional named **kwargs:
def country_code(fake, representation='alpha-2'):
"""Generate a country code"""
return fake.country_code(representation=representation)
@faker_fields decorator:
from django.db import models
from rest_framework_supertest import shortcuts
from rest_framework_supertest.models.decorators import faker_fields
@faker_fields(
title=shortcuts.name,
mime=(shortcuts.mime_type, { 'category': 'audio' })
)
class Book(models.Model):
title = models.CharField()
mime = models.TextField()
Related Issues
When this ADR was created, we have some related issues for this decision:
- #21 [feature] create an shortcut module and define methods to basic faker methods
- #45 [feature] add support for parsing arguments to fake function
- #47 [documentation] explain better arguments of faker shortcuts
- #62 [feature] create shortcuts for file generation faker with real support for directly set model files
- #63 [feature] create json shortcuts