Option to make Faker return unique values
See original GitHub issueI see random test failures because e.g. factory.Faker('company')
returns duplicate values (usually after a few hundred calls, but as low as the second call). To remedy this, I wrote a subclass of Faker
that keeps track of values returned so it can ensure uniqueness. The code is fairly trivial:
from factory.faker import Faker
class UniqueFaker(Faker):
"""
A Faker that keeps track of returned values so it can ensure uniqueness.
"""
def __init__(self, *args, **kwargs):
super(UniqueFaker, self).__init__(*args, **kwargs)
self._values = {None}
def generate(self, extra_kwargs):
value = None
while value in self._values:
value = super(UniqueFaker, self).generate(extra_kwargs)
self._values.add(value)
return value
Is there any interest in either adding this subclass to factoryboy, or integrating the functionality into Faker
itself?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:12
- Comments:11 (3 by maintainers)
Top Results From Across the Web
How to get unique values from faker? - php - Stack Overflow
My solution to this problem is to use a loop to make unique() functional. $max = 10; for($c=1; $c<= ...
Read more >Unique - Faker
Generates a unique result using the results of the given method. ... The function used to determine whether a value was already returned....
Read more >Factory Faker Unique() Across All Tests? - Laracasts
Hey all, I'm using Faker\Generator to create sample data, in this case State Codes: define(\App\Statecode::class, function (Faker $faker) { return [ 'id' ...
Read more >Welcome to Faker's documentation! — Faker 15.3.4 ...
Through use of the .unique property on the generator, you can guarantee that any generated values are unique for this specific instance. ......
Read more >Laravel Faker: Seed Unique Values in Factories - YouTube
I want to show a few examples of how Faker's library unique () actually works: with a single field, or with a pivot...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@danihodovic you can use
generate
to imperatively create a value in your sequence:Oh, that’s (the OP) almost what I did:
The idea is to override the method that calls the
faker
’sformat
method, copy the contents and addunique
.Non-performant? Meaning, slow? There seems to be nothing that suggests that. I’d say subclassing is the best workaround I could find. Not that it means
factory-boy
should follow that path.Sounds like a different use case. I wonder if one’d want to reproduce test failures for such tests…
AFAICS, nobody’s suggesting enforcing uniqueness globally. Usually that’s needed for database fields with a unique constraint.
Doesn’t work since
factory-boy==3.1.0
, and infactory-boy==3.2.0
generate()
was altogether removed (?).