test_deprecated_fields.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from unittest import skipUnless
  2. from django.core import checks
  3. from django.db import connection, models
  4. from django.test import SimpleTestCase
  5. from django.test.utils import isolate_apps
  6. @isolate_apps("invalid_models_tests")
  7. class DeprecatedFieldsTests(SimpleTestCase):
  8. def test_IPAddressField_deprecated(self):
  9. class IPAddressModel(models.Model):
  10. ip = models.IPAddressField()
  11. model = IPAddressModel()
  12. self.assertEqual(
  13. model.check(),
  14. [
  15. checks.Error(
  16. "IPAddressField has been removed except for support in "
  17. "historical migrations.",
  18. hint="Use GenericIPAddressField instead.",
  19. obj=IPAddressModel._meta.get_field("ip"),
  20. id="fields.E900",
  21. )
  22. ],
  23. )
  24. def test_CommaSeparatedIntegerField_deprecated(self):
  25. class CommaSeparatedIntegerModel(models.Model):
  26. csi = models.CommaSeparatedIntegerField(max_length=64)
  27. model = CommaSeparatedIntegerModel()
  28. self.assertEqual(
  29. model.check(),
  30. [
  31. checks.Error(
  32. "CommaSeparatedIntegerField is removed except for support in "
  33. "historical migrations.",
  34. hint=(
  35. "Use "
  36. "CharField(validators=[validate_comma_separated_integer_list]) "
  37. "instead."
  38. ),
  39. obj=CommaSeparatedIntegerModel._meta.get_field("csi"),
  40. id="fields.E901",
  41. )
  42. ],
  43. )
  44. def test_nullbooleanfield_deprecated(self):
  45. class NullBooleanFieldModel(models.Model):
  46. nb = models.NullBooleanField()
  47. model = NullBooleanFieldModel()
  48. self.assertEqual(
  49. model.check(),
  50. [
  51. checks.Error(
  52. "NullBooleanField is removed except for support in historical "
  53. "migrations.",
  54. hint="Use BooleanField(null=True, blank=True) instead.",
  55. obj=NullBooleanFieldModel._meta.get_field("nb"),
  56. id="fields.E903",
  57. ),
  58. ],
  59. )
  60. @skipUnless(connection.vendor == "postgresql", "PostgreSQL specific SQL")
  61. def test_postgres_jsonfield_deprecated(self):
  62. from django.contrib.postgres.fields import JSONField
  63. class PostgresJSONFieldModel(models.Model):
  64. field = JSONField()
  65. self.assertEqual(
  66. PostgresJSONFieldModel.check(),
  67. [
  68. checks.Error(
  69. "django.contrib.postgres.fields.JSONField is removed except "
  70. "for support in historical migrations.",
  71. hint="Use django.db.models.JSONField instead.",
  72. obj=PostgresJSONFieldModel._meta.get_field("field"),
  73. id="fields.E904",
  74. ),
  75. ],
  76. )
  77. @skipUnless(connection.vendor == "postgresql", "PostgreSQL specific SQL")
  78. def test_postgres_ci_fields_deprecated(self):
  79. from django.contrib.postgres.fields import (
  80. ArrayField,
  81. CICharField,
  82. CIEmailField,
  83. CITextField,
  84. )
  85. class PostgresCIFieldsModel(models.Model):
  86. ci_char = CICharField(max_length=255)
  87. ci_email = CIEmailField()
  88. ci_text = CITextField()
  89. array_ci_text = ArrayField(CITextField())
  90. self.assertEqual(
  91. PostgresCIFieldsModel.check(),
  92. [
  93. checks.Error(
  94. "django.contrib.postgres.fields.CICharField is removed except for "
  95. "support in historical migrations.",
  96. hint=(
  97. 'Use CharField(db_collation="…") with a case-insensitive '
  98. "non-deterministic collation instead."
  99. ),
  100. obj=PostgresCIFieldsModel._meta.get_field("ci_char"),
  101. id="fields.E905",
  102. ),
  103. checks.Error(
  104. "django.contrib.postgres.fields.CIEmailField is removed except for "
  105. "support in historical migrations.",
  106. hint=(
  107. 'Use EmailField(db_collation="…") with a case-insensitive '
  108. "non-deterministic collation instead."
  109. ),
  110. obj=PostgresCIFieldsModel._meta.get_field("ci_email"),
  111. id="fields.E906",
  112. ),
  113. checks.Error(
  114. "django.contrib.postgres.fields.CITextField is removed except for "
  115. "support in historical migrations.",
  116. hint=(
  117. 'Use TextField(db_collation="…") with a case-insensitive '
  118. "non-deterministic collation instead."
  119. ),
  120. obj=PostgresCIFieldsModel._meta.get_field("ci_text"),
  121. id="fields.E907",
  122. ),
  123. checks.Error(
  124. "Base field for array has errors:\n"
  125. " django.contrib.postgres.fields.CITextField is removed except "
  126. "for support in historical migrations. (fields.E907)",
  127. obj=PostgresCIFieldsModel._meta.get_field("array_ci_text"),
  128. id="postgres.E001",
  129. ),
  130. ],
  131. )