Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add unique feature arg in faker factory #997

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tlconnor
Copy link

@tlconnor tlconnor commented Feb 2, 2023

This pull request follows on from the abandoned #820. The code is simplified and follows the same pattern used when specifying the locale for Faker.

Since Faker 4.9.0 there has been support to generate unique values. This is really helpful when dealing with unique constraint on a field generated by factory boy. The test can be flaky if you use faker without the "unique" feature on an ORM field with an unique constraint.

The usage with factory boy is simple as this:

class UserFactory(fatory.Factory):
            class Meta:
                model = User

            arrival = factory.Faker(
                'date_between_dates',
                date_start=datetime.date(2020, 1, 1),
                date_end=datetime.date(2020, 5, 31),
                unique=True  # The generated date is guaranteed to be unique inside the test execution.
            )

The unique keyword can be passed on every faker providers. If True the faker object passes through the Faker Unique Proxy, making sure the generated value has not been already generated before. Note that the default unique keyword value is False.

Credit goes to @arthurHamon2 for the idea and the original pull request.

Since Faker 4.9.0 there has been support to generate unique values. This is really helpful when
dealing with unique constraint on a field generated by factory boy. The test can be flaky if you use
faker without the "unique" feature on an ORM field with an unique constraint.

The usage with factory boy is simple as this:

```
class UserFactory(fatory.Factory):
            class Meta:
                model = User

            arrival = factory.Faker(
                'date_between_dates',
                date_start=datetime.date(2020, 1, 1),
                date_end=datetime.date(2020, 5, 31),
                unique=True  # The generated date is guaranteed to be unique inside the test execution.
            )
```

The unique keyword can be passed on every faker providers. If `True` the faker object passes through
the Faker Unique Proxy, making sure the generated value has not been already generated before. Note
that the default unique keyword value is `False`.

Co-authored-by: Arthur Hamon <arthur@lumapps.com>
@kingbuzzman
Copy link
Contributor

@tlconnor merge #999 into your branch to test your changes

@kingbuzzman
Copy link
Contributor

@tlconnor NM, its been merged to master. Sync.

@HosseyNJF
Copy link

Hi. Is this PR ready to merge? I'd really love this feature to be present and I can help if it's needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants