0

I have set a unique constraint on email field of my Member model. Now while writing unit tests, my tests fail due to expiring unique constraint.

def setUp(self):
    self.car_provider = mommy.make(Member, username="car_provider")
    self.car_provider.set_password("12345678")
    self.car_provider.save()

    self.applicant = mommy.make(Member, username="applicant")
    self.applicant.set_password("12345678")
    self.applicant.save()

I get following error: "django.db.utils.IntegrityError: duplicate key value violates unique constraint "account_member_email_a727987b_uniq" DETAIL: Key (email)=() already exists."

1 Answers1

0

If you set up email as a unique field you actually must have a test for that case and handle that exception somehow.

But to make this test run you should either provide an explicit value for email field in every mommy.make() call. Or you can populate it with randomly generated values with _fill_optional option.

Like:

self.car_provider = mommy.make(Member, username="car_provider", _fill_optional=['email'])
Artem Kolontay
  • 840
  • 1
  • 10
  • 16