Replaced django.test.utils.patch_logger() with assertLogs().

Thanks Tim Graham for the review.
This commit is contained in:
Claude Paroz 2018-04-28 15:20:27 +02:00 committed by Tim Graham
parent 7d3fe36c62
commit 607970f31c
13 changed files with 97 additions and 157 deletions

View file

@ -1,6 +1,5 @@
from django.db import connection
from django.test import TestCase
from django.test.utils import patch_logger
class SchemaLoggerTests(TestCase):
@ -9,12 +8,11 @@ class SchemaLoggerTests(TestCase):
editor = connection.schema_editor(collect_sql=True)
sql = 'SELECT * FROM foo WHERE id in (%s, %s)'
params = [42, 1337]
with patch_logger('django.db.backends.schema', 'debug', log_kwargs=True) as logger:
with self.assertLogs('django.db.backends.schema', 'DEBUG') as cm:
editor.execute(sql, params)
self.assertEqual(cm.records[0].sql, sql)
self.assertEqual(cm.records[0].params, params)
self.assertEqual(
logger,
[(
'SELECT * FROM foo WHERE id in (%s, %s); (params [42, 1337])',
{'extra': {'sql': sql, 'params': params}},
)]
cm.records[0].getMessage(),
'SELECT * FROM foo WHERE id in (%s, %s); (params [42, 1337])',
)

View file

@ -22,7 +22,7 @@ from django.db.transaction import TransactionManagementError, atomic
from django.test import (
TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature,
)
from django.test.utils import CaptureQueriesContext, isolate_apps, patch_logger
from django.test.utils import CaptureQueriesContext, isolate_apps
from django.utils import timezone
from .fields import (
@ -1573,11 +1573,11 @@ class SchemaTests(TransactionTestCase):
new_field = CharField(max_length=255, unique=True)
new_field.model = Author
new_field.set_attributes_from_name('name')
with patch_logger('django.db.backends.schema', 'debug') as logger_calls:
with self.assertLogs('django.db.backends.schema', 'DEBUG') as cm:
with connection.schema_editor() as editor:
editor.alter_field(Author, Author._meta.get_field('name'), new_field)
# One SQL statement is executed to alter the field.
self.assertEqual(len(logger_calls), 1)
# One SQL statement is executed to alter the field.
self.assertEqual(len(cm.records), 1)
@isolate_apps('schema')
@unittest.skipIf(connection.vendor == 'sqlite', 'SQLite remakes the table on field alteration.')
@ -1606,11 +1606,11 @@ class SchemaTests(TransactionTestCase):
new_field = SlugField(max_length=75, unique=True)
new_field.model = Tag
new_field.set_attributes_from_name('slug')
with patch_logger('django.db.backends.schema', 'debug') as logger_calls:
with self.assertLogs('django.db.backends.schema', 'DEBUG') as cm:
with connection.schema_editor() as editor:
editor.alter_field(Tag, Tag._meta.get_field('slug'), new_field)
# One SQL statement is executed to alter the field.
self.assertEqual(len(logger_calls), 1)
# One SQL statement is executed to alter the field.
self.assertEqual(len(cm.records), 1)
# Ensure that the field is still unique.
Tag.objects.create(title='foo', slug='foo')
with self.assertRaises(IntegrityError):