Feat/add unique feature arg in faker factory#997
Open
tlconnor wants to merge 1 commit intoFactoryBoy:masterfrom
Open
Feat/add unique feature arg in faker factory#997tlconnor wants to merge 1 commit intoFactoryBoy:masterfrom
tlconnor wants to merge 1 commit intoFactoryBoy:masterfrom
Conversation
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>
Merged
Contributor
Contributor
|
@tlconnor NM, its been merged to |
|
Hi. Is this PR ready to merge? I'd really love this feature to be present and I can help if it's needed |
|
@tlconnor @kingbuzzman |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
The unique keyword can be passed on every faker providers. If
Truethe 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 isFalse.Credit goes to @arthurHamon2 for the idea and the original pull request.