diff options
| author | Loic Bistuer <loic.bistuer@gmail.com> | 2014-05-18 11:58:16 +0700 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-05-21 07:04:38 -0400 |
| commit | d61b6224b09cb2aa6fb75ccecab246f91553ef7b (patch) | |
| tree | 943857a8cb88ffab6836cd580a1ca6c478a65445 /tests | |
| parent | 1a29675d76de00d6fad9b366fc66e8b6d541c3b6 (diff) | |
[1.7.x] Fixed #22424 -- Fixed handling of default values for TextField/BinaryField on MySQL.
Thanks syphar for the review and suggestions.
Backport of 1d3d01b4f7 from master
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/migrations/test_operations.py | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index 388544cb04..5455610912 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import unittest try: @@ -312,6 +314,115 @@ class OperationTests(MigrationTestBase): operation.database_backwards("test_adfl", editor, new_state, project_state) self.assertColumnNotExists("test_adfl_pony", "height") + def test_add_charfield(self): + """ + Tests the AddField operation on TextField. + """ + project_state = self.set_up_test_model("test_adchfl") + + new_apps = project_state.render() + Pony = new_apps.get_model("test_adchfl", "Pony") + pony = Pony.objects.create(weight=42) + + new_state = self.apply_operations("test_adchfl", project_state, [ + migrations.AddField( + "Pony", + "text", + models.CharField(max_length=10, default="some text"), + ), + migrations.AddField( + "Pony", + "empty", + models.CharField(max_length=10, default=""), + ), + # If not properly quoted digits would be interpreted as an int. + migrations.AddField( + "Pony", + "digits", + models.CharField(max_length=10, default="42"), + ), + ]) + + new_apps = new_state.render() + Pony = new_apps.get_model("test_adchfl", "Pony") + pony = Pony.objects.get(pk=pony.pk) + self.assertEqual(pony.text, "some text") + self.assertEqual(pony.empty, "") + self.assertEqual(pony.digits, "42") + + def test_add_textfield(self): + """ + Tests the AddField operation on TextField. + """ + project_state = self.set_up_test_model("test_adtxtfl") + + new_apps = project_state.render() + Pony = new_apps.get_model("test_adtxtfl", "Pony") + pony = Pony.objects.create(weight=42) + + new_state = self.apply_operations("test_adtxtfl", project_state, [ + migrations.AddField( + "Pony", + "text", + models.TextField(default="some text"), + ), + migrations.AddField( + "Pony", + "empty", + models.TextField(default=""), + ), + # If not properly quoted digits would be interpreted as an int. + migrations.AddField( + "Pony", + "digits", + models.TextField(default="42"), + ), + ]) + + new_apps = new_state.render() + Pony = new_apps.get_model("test_adtxtfl", "Pony") + pony = Pony.objects.get(pk=pony.pk) + self.assertEqual(pony.text, "some text") + self.assertEqual(pony.empty, "") + self.assertEqual(pony.digits, "42") + + def test_add_binaryfield(self): + """ + Tests the AddField operation on TextField/BinaryField. + """ + project_state = self.set_up_test_model("test_adbinfl") + + new_apps = project_state.render() + Pony = new_apps.get_model("test_adbinfl", "Pony") + pony = Pony.objects.create(weight=42) + + new_state = self.apply_operations("test_adbinfl", project_state, [ + migrations.AddField( + "Pony", + "blob", + models.BinaryField(default=b"some text"), + ), + migrations.AddField( + "Pony", + "empty", + models.BinaryField(default=b""), + ), + # If not properly quoted digits would be interpreted as an int. + migrations.AddField( + "Pony", + "digits", + models.BinaryField(default=b"42"), + ), + ]) + + new_apps = new_state.render() + Pony = new_apps.get_model("test_adbinfl", "Pony") + pony = Pony.objects.get(pk=pony.pk) + # SQLite returns buffer/memoryview, cast to bytes for checking. + self.assertEqual(bytes(pony.blob), b"some text") + self.assertEqual(bytes(pony.empty), b"") + self.assertEqual(bytes(pony.digits), b"42") + def test_column_name_quoting(self): """ Column names that are SQL keywords shouldn't cause problems when used |
