diff options
| author | Matthew Schinckel <matt@schinckel.net> | 2016-04-18 11:25:15 +0930 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-04-19 10:17:29 -0400 |
| commit | 799e81ef6bc8cbb944dd3594a2646a594a04b44d (patch) | |
| tree | 77c09a788f5b1acea2cc5d2a84034f41d169a8ed /tests | |
| parent | f5b8e9b2820177dae2bf392867641d097438f85e (diff) | |
[1.9.x] Fixed #26475 -- Added functools.partial() support to migrations autodetector.
Backport of 5402f3ab09413a571fd9d3aa27f6c76ec42ff891 from master
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/migrations/test_autodetector.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index e634b549f9..521aa8a4a0 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import functools import re from django.apps import apps @@ -657,6 +658,59 @@ class AutodetectorTests(TestCase): self.assertOperationTypes(changes, 'testapp', 0, ["AlterField"]) self.assertOperationAttributes(changes, "testapp", 0, 0, name="name", preserve_default=True) + def test_supports_functools_partial(self): + def _content_file_name(instance, filename, key, **kwargs): + return '{}/{}'.format(instance, filename) + + def content_file_name(key, **kwargs): + return functools.partial(_content_file_name, key, **kwargs) + + # An unchanged partial reference. + before = self.make_project_state([ModelState("testapp", "Author", [ + ("id", models.AutoField(primary_key=True)), + ("file", models.FileField(max_length=200, upload_to=content_file_name('file'))), + ])]) + after = self.make_project_state([ModelState("testapp", "Author", [ + ("id", models.AutoField(primary_key=True)), + ("file", models.FileField(max_length=200, upload_to=content_file_name('file'))), + ])]) + autodetector = MigrationAutodetector(before, after) + changes = autodetector._detect_changes() + self.assertNumberMigrations(changes, 'testapp', 0) + + # A changed partial reference. + args_changed = self.make_project_state([ModelState("testapp", "Author", [ + ("id", models.AutoField(primary_key=True)), + ("file", models.FileField(max_length=200, upload_to=content_file_name('other-file'))), + ])]) + autodetector = MigrationAutodetector(before, args_changed) + changes = autodetector._detect_changes() + self.assertNumberMigrations(changes, 'testapp', 1) + self.assertOperationTypes(changes, 'testapp', 0, ['AlterField']) + # Can't use assertOperationFieldAttributes because we need the + # deconstructed version, i.e., the exploded func/args/keywords rather + # than the partial: we don't care if it's not the same instance of the + # partial, only if it's the same source function, args, and keywords. + value = changes['testapp'][0].operations[0].field.upload_to + self.assertEqual( + (_content_file_name, ('other-file',), {}), + (value.func, value.args, value.keywords) + ) + + kwargs_changed = self.make_project_state([ModelState("testapp", "Author", [ + ("id", models.AutoField(primary_key=True)), + ("file", models.FileField(max_length=200, upload_to=content_file_name('file', spam='eggs'))), + ])]) + autodetector = MigrationAutodetector(before, kwargs_changed) + changes = autodetector._detect_changes() + self.assertNumberMigrations(changes, 'testapp', 1) + self.assertOperationTypes(changes, 'testapp', 0, ['AlterField']) + value = changes['testapp'][0].operations[0].field.upload_to + self.assertEqual( + (_content_file_name, ('file',), {'spam': 'eggs'}), + (value.func, value.args, value.keywords) + ) + @mock.patch('django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration', side_effect=AssertionError("Should not have prompted for not null addition")) def test_alter_field_to_not_null_with_default(self, mocked_ask_method): |
