Tested that get_or_create raises IntegrityError.

It used to raise "DatabaseError: no such savepoint" with the old
transaction management. Closes #15117.
This commit is contained in:
Aymeric Augustin 2013-04-28 17:15:41 +02:00
parent 90fe9141de
commit 3a4276ffc3
2 changed files with 19 additions and 2 deletions

View File

@ -24,3 +24,7 @@ class Person(models.Model):
class ManualPrimaryKeyTest(models.Model):
id = models.IntegerField(primary_key=True)
data = models.CharField(max_length=100)
class Profile(models.Model):
person = models.ForeignKey(Person, primary_key=True)

View File

@ -4,9 +4,9 @@ from datetime import date
import traceback
from django.db import IntegrityError
from django.test import TestCase
from django.test import TestCase, TransactionTestCase
from .models import Person, ManualPrimaryKeyTest
from .models import Person, ManualPrimaryKeyTest, Profile
class GetOrCreateTests(TestCase):
@ -64,3 +64,16 @@ class GetOrCreateTests(TestCase):
formatted_traceback = traceback.format_exc()
self.assertIn('obj.save', formatted_traceback)
class GetOrCreateTransactionTests(TransactionTestCase):
def test_get_or_create_integrityerror(self):
# Regression test for #15117. Requires a TransactionTestCase on
# databases that delay integrity checks until the end of transactions,
# otherwise the exception is never raised.
try:
Profile.objects.get_or_create(person=Person(id=1))
except IntegrityError:
pass
else:
self.skipTest("This backend does not support integrity checks.")