🩹(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:
Lebaud Antoine
2024-03-21 11:37:23 +01:00
committed by aleb_the_flash
parent 99cee241f9
commit 97752e1d5f
2 changed files with 3 additions and 2 deletions

View File

@@ -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])

View File

@@ -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