🩹(factory) handle email uniqueness
When generating a batch of users using Faker, there's a possibility of generating multiple users with the same email address. This breaches the uniqueness constraint set on the email field, leading to flaky tests that may fail when random behavior coincides unfavorably. Implemented a method inspired by Identity's model to ensure unique email addresses when creating user batches with Faker. Updated relevant tests for improved stability.
This commit is contained in:
committed by
aleb_the_flash
parent
99cee241f9
commit
97752e1d5f
@@ -124,6 +124,7 @@ class UserFactory(factory.django.DjangoModelFactory):
|
||||
|
||||
class Meta:
|
||||
model = models.User
|
||||
django_get_or_create = ("email",)
|
||||
|
||||
email = factory.Faker("email")
|
||||
language = factory.fuzzy.FuzzyChoice([lang[0] for lang in settings.LANGUAGES])
|
||||
|
||||
@@ -36,13 +36,13 @@ def test_models_users_email_unique():
|
||||
with pytest.raises(
|
||||
ValidationError, match="User with this Email address already exists."
|
||||
):
|
||||
factories.UserFactory(email=user.email)
|
||||
models.User.objects.create(email=user.email)
|
||||
|
||||
|
||||
def test_models_users_email_several_null():
|
||||
"""Several users with a null value for the "email" field can co-exist."""
|
||||
factories.UserFactory(email=None)
|
||||
factories.UserFactory(email=None)
|
||||
models.User.objects.create(email=None, password="foo.")
|
||||
|
||||
assert models.User.objects.filter(email__isnull=True).count() == 2
|
||||
|
||||
|
||||
Reference in New Issue
Block a user