-
Notifications
You must be signed in to change notification settings - Fork 0
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
Provide standalone access to UserFactory username generation #20
Comments
Had some time to think about this 😂 This week I've wished for a unique generator for strings when working with Factory Boy. I'm not satisfied with using Sequences to guarantee unique values - especially for stringy values which are not meant to contain numbers. I've started work on uFaker to add functionality to Faker to make it easier to generate unique values and or exclude undesired ones. Quick recap:
So, back to the original question:
Do you care about values in the database already? If no - then would If yes - then What I'm thinking of for the "yes I care about existing values in the database" situation is a recipe that uses a ban list: from factory_djoy import CleanModelFactory, ufake
from project.accounts import Account
class AccountFactory(CleanModelFactory):
class Meta:
model = Account
@lazy_attribute
def username(self):
all_usernames = Account.objects.values_list('username', flat=True)
return ufake.user_name(ban=all_usernames) What do you think? Would the recipe be powerful enough for what you're looking for? Either way, my plan is to build out uFaker to the API in its README and then replace the username generation in this library with the recipe above. |
I'm so glad that you got to this within 3 years, I was worried we'd go into the 4th year..... 😆 I understand your thinking and I think I do care about existing values in the database sometimes. So a ban list does not sound like a bad idea. |
Just a note to self with an example for testing. Given a class Game(models.Model):
server_id = models.IntegerField(unique=True)
code = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now=True) Then we would have a factory (note the change of import name from the from factory import Faker
from factory_djoy import CleanModelFactory, uFaker
from games.models import Game
class GameFactory(CleanModelFactory):
class Meta:
model = Game
server_id = uFaker('pyint') # This guarantees unique values for this ID
code = Faker('word') # We don't care about uniqueness here. This would then mean that a large number of valid It also means that we can easily test what happens when there are missing values without the chance of collision, but maintaining some randomness: def test_missing():
"""
When no games can be found, serializer is valid, but `_game` is `None`.
"""
GameFactory.create_batch(3, server_id__ban=[29])
serializer = GameSearchSerializer(data={'server_id': 29})
result = serializer.is_valid()
assert result is True, serializer.errors
assert serializer._game is None |
When using a custom user model I'd love to still use your unique username generation.
Would it be possible to provide the Username generator as it's own standalone module/provider?
Something like:
The text was updated successfully, but these errors were encountered: