Kaydet (Commit) b39aabc6 authored tarafından Jon Dufresne's avatar Jon Dufresne Kaydeden (comit) Tim Graham

Refs #27795 -- Reworked get_or_create test erroneously mixing bytes and str.

As CharField.to_python() now always calls str(), assigning bytes to a
CharField is no longer correct usage. Doing so results in a warning:

  django/db/models/fields/__init__.py:1061: BytesWarning: str() on a bytes instance

Use a unique constraint violation to trigger the database error instead.

Warning introduced in 301de774.
üst 9ae4362b
...@@ -6,10 +6,8 @@ from threading import Thread ...@@ -6,10 +6,8 @@ from threading import Thread
from django.core.exceptions import FieldError from django.core.exceptions import FieldError
from django.db import DatabaseError, IntegrityError, connection from django.db import DatabaseError, IntegrityError, connection
from django.test import ( from django.test import (
SimpleTestCase, TestCase, TransactionTestCase, ignore_warnings, SimpleTestCase, TestCase, TransactionTestCase, skipUnlessDBFeature,
skipUnlessDBFeature,
) )
from django.utils.encoding import DjangoUnicodeDecodeError
from .models import ( from .models import (
Author, Book, DefaultPerson, ManualPrimaryKeyTest, Person, Profile, Author, Book, DefaultPerson, ManualPrimaryKeyTest, Person, Profile,
...@@ -203,22 +201,18 @@ class GetOrCreateTestsWithManualPKs(TestCase): ...@@ -203,22 +201,18 @@ class GetOrCreateTestsWithManualPKs(TestCase):
formatted_traceback = traceback.format_exc() formatted_traceback = traceback.format_exc()
self.assertIn('obj.save', formatted_traceback) self.assertIn('obj.save', formatted_traceback)
# MySQL emits a warning when broken data is saved
@ignore_warnings(module='django.db.backends.mysql.base')
def test_savepoint_rollback(self): def test_savepoint_rollback(self):
""" """
Regression test for #20463: the database connection should still be The database connection is still usable after a DatabaseError in
usable after a DataError or ProgrammingError in .get_or_create(). get_or_create() (#20463).
""" """
try: Tag.objects.create(text='foo')
Person.objects.get_or_create( with self.assertRaises(DatabaseError):
birthday=date(1970, 1, 1), # pk 123456789 doesn't exist, so the tag object will be created.
defaults={'first_name': b"\xff", 'last_name': b"\xff"}) # Saving triggers a unique constraint violation on 'text'.
except (DatabaseError, DjangoUnicodeDecodeError): Tag.objects.get_or_create(pk=123456789, defaults={'text': 'foo'})
Person.objects.create( # Tag objects can be created after the error.
first_name="Bob", last_name="Ross", birthday=date(1950, 1, 1)) Tag.objects.create(text='bar')
else:
self.skipTest("This backend accepts broken utf-8.")
def test_get_or_create_empty(self): def test_get_or_create_empty(self):
""" """
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment