From 3a4276ffc35326f20e95890b50a67aebeabc9ad0 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sun, 28 Apr 2013 17:15:41 +0200 Subject: [PATCH] Tested that get_or_create raises IntegrityError. It used to raise "DatabaseError: no such savepoint" with the old transaction management. Closes #15117. --- tests/get_or_create/models.py | 4 ++++ tests/get_or_create/tests.py | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/tests/get_or_create/models.py b/tests/get_or_create/models.py index 678f5a401c..82905de4f8 100644 --- a/tests/get_or_create/models.py +++ b/tests/get_or_create/models.py @@ -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) diff --git a/tests/get_or_create/tests.py b/tests/get_or_create/tests.py index 1e300fbb4d..e9cce9bbde 100644 --- a/tests/get_or_create/tests.py +++ b/tests/get_or_create/tests.py @@ -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.")