summaryrefslogtreecommitdiff
path: root/tests/get_or_create/tests.py
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2013-05-22 10:56:06 +0200
committerAymeric Augustin <aymeric.augustin@m4x.org>2013-05-22 10:56:06 +0200
commit0e51d8eb66121b2558a38c9f4e366df781046873 (patch)
tree219f60c5070122e94e4a419f6e7442c8c8312d3c /tests/get_or_create/tests.py
parentadeec00979d1b365535b8f26fda4a5f7173e975d (diff)
Fixed #20463 -- Made get_or_create more robust.
When an exception other than IntegrityError was raised, get_or_create could fail and leave the database connection in an unusable state. Thanks UloPe for the report.
Diffstat (limited to 'tests/get_or_create/tests.py')
-rw-r--r--tests/get_or_create/tests.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/tests/get_or_create/tests.py b/tests/get_or_create/tests.py
index 5117a2f915..5a5956bf74 100644
--- a/tests/get_or_create/tests.py
+++ b/tests/get_or_create/tests.py
@@ -2,14 +2,16 @@ from __future__ import absolute_import
from datetime import date
import traceback
+import warnings
-from django.db import IntegrityError
+from django.db import IntegrityError, DatabaseError
from django.test import TestCase, TransactionTestCase
from .models import Person, ManualPrimaryKeyTest, Profile, Tag, Thing
class GetOrCreateTests(TestCase):
+
def test_get_or_create(self):
p = Person.objects.create(
first_name='John', last_name='Lennon', birthday=date(1940, 10, 9)
@@ -64,6 +66,22 @@ class GetOrCreateTests(TestCase):
formatted_traceback = traceback.format_exc()
self.assertIn('obj.save', formatted_traceback)
+ def test_savepoint_rollback(self):
+ # Regression test for #20463: the database connection should still be
+ # usable after a DataError or ProgrammingError in .get_or_create().
+ try:
+ # Hide warnings when broken data is saved with a warning (MySQL).
+ with warnings.catch_warnings():
+ warnings.simplefilter('ignore')
+ Person.objects.get_or_create(
+ birthday=date(1970, 1, 1),
+ defaults={'first_name': "\xff", 'last_name': "\xff"})
+ except DatabaseError:
+ Person.objects.create(
+ first_name="Bob", last_name="Ross", birthday=date(1950, 1, 1))
+ else:
+ self.skipTest("This backend accepts broken utf-8.")
+
class GetOrCreateTransactionTests(TransactionTestCase):