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