summaryrefslogtreecommitdiff
path: root/tests/schema
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2014-05-07 13:46:23 -0700
committerAndrew Godwin <andrew@aeracode.org>2014-05-07 13:47:02 -0700
commitd8bf415ab29ef3ef843286e69c646ebcce58c172 (patch)
tree4c7b6fba90d7cb58a2000011e4a2a4158831507a /tests/schema
parentf67433e74b06bbeb9b6bba0a9db5fb33512bdb43 (diff)
[1.7.x] Fixed #22581: Pass default values for schema through get_db_prep_save()
Diffstat (limited to 'tests/schema')
-rw-r--r--tests/schema/tests.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index 0ab5bb0363..55b2ee5ce3 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -231,6 +231,40 @@ class SchemaTests(TransactionTestCase):
else:
self.assertEqual(field_type, 'BooleanField')
+ def test_add_field_default_transform(self):
+ """
+ Tests adding fields to models with a default that is not directly
+ valid in the database (#22581)
+ """
+ class TestTransformField(IntegerField):
+ # Weird field that saves the count of items in its value
+ def get_default(self):
+ return self.default
+ def get_prep_value(self, value):
+ if value is None:
+ return 0
+ return len(value)
+ # Create the table
+ with connection.schema_editor() as editor:
+ editor.create_model(Author)
+ # Add some rows of data
+ Author.objects.create(name="Andrew", height=30)
+ Author.objects.create(name="Andrea")
+ # Add the field with a default it needs to cast (to string in this case)
+ new_field = TestTransformField(default={1:2})
+ new_field.set_attributes_from_name("thing")
+ with connection.schema_editor() as editor:
+ editor.add_field(
+ Author,
+ new_field,
+ )
+ # Ensure the field is there
+ columns = self.column_classes(Author)
+ field_type, field_info = columns['thing']
+ self.assertEqual(field_type, 'IntegerField')
+ # Make sure the values were transformed correctly
+ self.assertEqual(Author.objects.extra(where=["thing = 1"]).count(), 2)
+
def test_alter(self):
"""
Tests simple altering of fields