Skip to content

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

@halbow
Copy link

halbow commented Nov 6, 2024

@tlconnor @kingbuzzman
Would you still be open/interrested to merge this feature ? :)
I can to take the PR and rebase it if it helps !

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.

4 participants