Added BaseDatabaseSchemaEditor._effective_default() to allow testing without a connection.
This commit is contained in:
parent
e62f6e0968
commit
9cac10eee4
|
@ -199,8 +199,9 @@ class BaseDatabaseSchemaEditor:
|
||||||
'requires_literal_defaults must provide a prepare_default() method'
|
'requires_literal_defaults must provide a prepare_default() method'
|
||||||
)
|
)
|
||||||
|
|
||||||
def effective_default(self, field):
|
@classmethod
|
||||||
"""Return a field's effective database default value."""
|
def _effective_default(self, field):
|
||||||
|
# This method allows testing its logic without a connection.
|
||||||
if field.has_default():
|
if field.has_default():
|
||||||
default = field.get_default()
|
default = field.get_default()
|
||||||
elif not field.null and field.blank and field.empty_strings_allowed:
|
elif not field.null and field.blank and field.empty_strings_allowed:
|
||||||
|
@ -219,8 +220,11 @@ class BaseDatabaseSchemaEditor:
|
||||||
default = timezone.now()
|
default = timezone.now()
|
||||||
else:
|
else:
|
||||||
default = None
|
default = None
|
||||||
# Convert the value so it can be sent to the database.
|
return default
|
||||||
return field.get_db_prep_save(default, self.connection)
|
|
||||||
|
def effective_default(self, field):
|
||||||
|
"""Return a field's effective database default value."""
|
||||||
|
return field.get_db_prep_save(self._effective_default(field), self.connection)
|
||||||
|
|
||||||
def quote_value(self, value):
|
def quote_value(self, value):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
from django.db import connection, models
|
from django.db import models
|
||||||
from django.test import TestCase
|
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
||||||
|
from django.test import SimpleTestCase
|
||||||
|
|
||||||
|
|
||||||
class SchemaEditorTests(TestCase):
|
class SchemaEditorTests(SimpleTestCase):
|
||||||
|
|
||||||
def test_effective_default_callable(self):
|
def test_effective_default_callable(self):
|
||||||
"""SchemaEditor.effective_default() shouldn't call callable defaults."""
|
"""SchemaEditor.effective_default() shouldn't call callable defaults."""
|
||||||
|
@ -14,9 +15,5 @@ class SchemaEditorTests(TestCase):
|
||||||
def _get_default(self):
|
def _get_default(self):
|
||||||
return self.default
|
return self.default
|
||||||
|
|
||||||
def get_db_prep_save(self, default, connection):
|
|
||||||
return default
|
|
||||||
|
|
||||||
field = MyCharField(max_length=1, default=MyStr)
|
field = MyCharField(max_length=1, default=MyStr)
|
||||||
with connection.schema_editor() as editor:
|
self.assertEqual(BaseDatabaseSchemaEditor._effective_default(field), MyStr)
|
||||||
self.assertEqual(editor.effective_default(field), MyStr)
|
|
||||||
|
|
Loading…
Reference in New Issue