test_migrations.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from django.core import checks
  2. from django.db import migrations
  3. from django.db.migrations.operations.base import Operation
  4. from django.test import TestCase
  5. class DeprecatedMigrationOperationTests(TestCase):
  6. def test_default_operation(self):
  7. class MyOperation(Operation):
  8. system_check_deprecated_details = {}
  9. my_operation = MyOperation()
  10. class Migration(migrations.Migration):
  11. operations = [my_operation]
  12. self.assertEqual(
  13. Migration("name", "app_label").check(),
  14. [
  15. checks.Warning(
  16. msg="MyOperation has been deprecated.",
  17. obj=my_operation,
  18. id="migrations.WXXX",
  19. )
  20. ],
  21. )
  22. def test_user_specified_details(self):
  23. class MyOperation(Operation):
  24. system_check_deprecated_details = {
  25. "msg": "This operation is deprecated and will be removed soon.",
  26. "hint": "Use something else.",
  27. "id": "migrations.W999",
  28. }
  29. my_operation = MyOperation()
  30. class Migration(migrations.Migration):
  31. operations = [my_operation]
  32. self.assertEqual(
  33. Migration("name", "app_label").check(),
  34. [
  35. checks.Warning(
  36. msg="This operation is deprecated and will be removed soon.",
  37. obj=my_operation,
  38. hint="Use something else.",
  39. id="migrations.W999",
  40. )
  41. ],
  42. )
  43. class RemovedMigrationOperationTests(TestCase):
  44. def test_default_operation(self):
  45. class MyOperation(Operation):
  46. system_check_removed_details = {}
  47. my_operation = MyOperation()
  48. class Migration(migrations.Migration):
  49. operations = [my_operation]
  50. self.assertEqual(
  51. Migration("name", "app_label").check(),
  52. [
  53. checks.Error(
  54. msg=(
  55. "MyOperation has been removed except for support in historical "
  56. "migrations."
  57. ),
  58. obj=my_operation,
  59. id="migrations.EXXX",
  60. )
  61. ],
  62. )
  63. def test_user_specified_details(self):
  64. class MyOperation(Operation):
  65. system_check_removed_details = {
  66. "msg": "Support for this operation is gone.",
  67. "hint": "Use something else.",
  68. "id": "migrations.E999",
  69. }
  70. my_operation = MyOperation()
  71. class Migration(migrations.Migration):
  72. operations = [my_operation]
  73. self.assertEqual(
  74. Migration("name", "app_label").check(),
  75. [
  76. checks.Error(
  77. msg="Support for this operation is gone.",
  78. obj=my_operation,
  79. hint="Use something else.",
  80. id="migrations.E999",
  81. )
  82. ],
  83. )