0

What's the best way

class BankLoanApplicationFile(TestCase):

    """Test cases for loan_application.py file."""

    def setUp(self) -> None:
        """Set up configuration."""
    
        self.loan_product = mock.Mock(spec=LoanProduct)
        self.loan_type = LoanType.objects.create()
        self.loan_usage = LoanUsage.objects.create()
        self.profession = Profession.objects.create(name='Architekt/in')

     def tearDown(self) -> None:
         self.loan_type.delete()
         self.loan_usage.delete()

This is the error :

django.db.utils.IntegrityError: duplicate key value violates unique constraint.

Whats the best way of ignoring this error with Django tests, I have read several post about a similar issue, but I didn't get a solution

EDIT :

Below are the models :

class CommonLoanOptions(models.Model):

    name = models.CharField(_('Name'), max_length=64, unique=True)
    is_active = models.BooleanField(_('Is Active'), default=True)

    class Meta:
        abstract = True

    def __str__(self):
        return f'{self.name}'


class LoanProduct(CommonLoanOptions):

    """LoanProduct Model."""

    class Meta:
        verbose_name = _('Loan Product')
        verbose_name_plural = _('Loan Products')
        ordering = ('id',)

class Profession(CommonLoanOptions):

    """Profession Model."""

    class Meta:
        verbose_name = _('Profession')
        verbose_name_plural = _('Professions')
        ordering = ('name',)

class LoanUsage(CommonLoanOptions):

    """LoanUsage Model."""

    class Meta:
        verbose_name = _('Loan Usage')
        verbose_name_plural = _('Loan Usages')
Lutaaya Huzaifah Idris
  • 3,596
  • 8
  • 38
  • 77

1 Answers1

0

There are two possible ways to resolve this.

  1. (Recommended) Give a value to the fields while creating the record:

    E.g.: self.loan_usage = LoanUsage.objects.create(name="any_name")

  2. In models.py, change the name field as:

    name = models.CharField(_('Name'), max_length=64, unique=True, blank=True)