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')
3
  • Can you share your models.py file? Commented Jul 20, 2022 at 12:06
  • @BhavyaPeshavaria, I have added them Commented Jul 20, 2022 at 12:09
  • I go the issue, I was supposed to import from django.test import TestCase instead of from unittest import TestCase Commented Jul 20, 2022 at 13:19

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Still no luck, same error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.