diff options
author | David Wobrock <david.wobrock@gmail.com> | 2022-07-02 19:55:37 +0200 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-07-08 07:05:55 +0200 |
commit | 41019e48bbf082c985e6ba3bad34d118b903bff1 (patch) | |
tree | 63e08889847484c233e690c81375f7a4e73eec18 /tests | |
parent | 57793b47657ace966ce8ce96d801ac0d85e5efc6 (diff) | |
download | django-41019e48bbf082c985e6ba3bad34d118b903bff1.tar.gz |
Refs #27236 -- Added generic mechanism to handle the deprecation of migration operations.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/check_framework/test_migrations.py | 101 | ||||
-rw-r--r-- | tests/db_functions/migrations/0001_setup_extensions.py | 17 | ||||
-rw-r--r-- | tests/postgres_tests/migrations/0001_setup_extensions.py | 33 |
3 files changed, 137 insertions, 14 deletions
diff --git a/tests/check_framework/test_migrations.py b/tests/check_framework/test_migrations.py new file mode 100644 index 0000000000..0b00690e77 --- /dev/null +++ b/tests/check_framework/test_migrations.py @@ -0,0 +1,101 @@ +from django.core import checks +from django.db import migrations +from django.db.migrations.operations.base import Operation +from django.test import TestCase + + +class DeprecatedMigrationOperationTests(TestCase): + def test_default_operation(self): + class MyOperation(Operation): + system_check_deprecated_details = {} + + my_operation = MyOperation() + + class Migration(migrations.Migration): + operations = [my_operation] + + self.assertEqual( + Migration("name", "app_label").check(), + [ + checks.Warning( + msg="MyOperation has been deprecated.", + obj=my_operation, + id="migrations.WXXX", + ) + ], + ) + + def test_user_specified_details(self): + class MyOperation(Operation): + system_check_deprecated_details = { + "msg": "This operation is deprecated and will be removed soon.", + "hint": "Use something else.", + "id": "migrations.W999", + } + + my_operation = MyOperation() + + class Migration(migrations.Migration): + operations = [my_operation] + + self.assertEqual( + Migration("name", "app_label").check(), + [ + checks.Warning( + msg="This operation is deprecated and will be removed soon.", + obj=my_operation, + hint="Use something else.", + id="migrations.W999", + ) + ], + ) + + +class RemovedMigrationOperationTests(TestCase): + def test_default_operation(self): + class MyOperation(Operation): + system_check_removed_details = {} + + my_operation = MyOperation() + + class Migration(migrations.Migration): + operations = [my_operation] + + self.assertEqual( + Migration("name", "app_label").check(), + [ + checks.Error( + msg=( + "MyOperation has been removed except for support in historical " + "migrations." + ), + obj=my_operation, + id="migrations.EXXX", + ) + ], + ) + + def test_user_specified_details(self): + class MyOperation(Operation): + system_check_removed_details = { + "msg": "Support for this operation is gone.", + "hint": "Use something else.", + "id": "migrations.E999", + } + + my_operation = MyOperation() + + class Migration(migrations.Migration): + operations = [my_operation] + + self.assertEqual( + Migration("name", "app_label").check(), + [ + checks.Error( + msg="Support for this operation is gone.", + obj=my_operation, + hint="Use something else.", + id="migrations.E999", + ) + ], + ) diff --git a/tests/db_functions/migrations/0001_setup_extensions.py b/tests/db_functions/migrations/0001_setup_extensions.py index 0289055499..5909a96eb8 100644 --- a/tests/db_functions/migrations/0001_setup_extensions.py +++ b/tests/db_functions/migrations/0001_setup_extensions.py @@ -1,11 +1,22 @@ -from unittest import mock - from django.db import migrations +from django.db.migrations.operations.base import Operation + + +class DummyOperation(Operation): + def state_forwards(self, app_label, state): + pass + + def database_forwards(self, app_label, schema_editor, from_state, to_state): + pass + + def database_backwards(self, app_label, schema_editor, from_state, to_state): + pass + try: from django.contrib.postgres.operations import CryptoExtension except ImportError: - CryptoExtension = mock.Mock() + CryptoExtension = DummyOperation class Migration(migrations.Migration): diff --git a/tests/postgres_tests/migrations/0001_setup_extensions.py b/tests/postgres_tests/migrations/0001_setup_extensions.py index 090abf9649..3edeff7b82 100644 --- a/tests/postgres_tests/migrations/0001_setup_extensions.py +++ b/tests/postgres_tests/migrations/0001_setup_extensions.py @@ -1,6 +1,17 @@ -from unittest import mock - from django.db import connection, migrations +from django.db.migrations.operations.base import Operation + + +class DummyOperation(Operation): + def state_forwards(self, app_label, state): + pass + + def database_forwards(self, app_label, schema_editor, from_state, to_state): + pass + + def database_backwards(self, app_label, schema_editor, from_state, to_state): + pass + try: from django.contrib.postgres.operations import ( @@ -15,14 +26,14 @@ try: UnaccentExtension, ) except ImportError: - BloomExtension = mock.Mock() - BtreeGinExtension = mock.Mock() - BtreeGistExtension = mock.Mock() - CITextExtension = mock.Mock() - CreateExtension = mock.Mock() - HStoreExtension = mock.Mock() - TrigramExtension = mock.Mock() - UnaccentExtension = mock.Mock() + BloomExtension = DummyOperation + BtreeGinExtension = DummyOperation + BtreeGistExtension = DummyOperation + CITextExtension = DummyOperation + CreateExtension = DummyOperation + HStoreExtension = DummyOperation + TrigramExtension = DummyOperation + UnaccentExtension = DummyOperation needs_crypto_extension = False else: needs_crypto_extension = ( @@ -41,7 +52,7 @@ class Migration(migrations.Migration): # dash in its name. CreateExtension("uuid-ossp"), # CryptoExtension is required for RandomUUID() on PostgreSQL < 13. - CryptoExtension() if needs_crypto_extension else mock.Mock(), + CryptoExtension() if needs_crypto_extension else DummyOperation(), HStoreExtension(), TrigramExtension(), UnaccentExtension(), |