2014-01-20 19:01:14 +08:00
|
|
|
import unittest
|
|
|
|
|
2014-02-22 21:29:43 +08:00
|
|
|
from django.core.checks import Error, Warning as DjangoWarning
|
2014-01-20 19:01:14 +08:00
|
|
|
from django.db import connection, models
|
2018-11-27 03:05:02 +08:00
|
|
|
from django.test import SimpleTestCase, skipIfDBFeature
|
2015-11-17 13:39:28 +08:00
|
|
|
from django.test.utils import isolate_apps, override_settings
|
2018-05-30 09:43:38 +08:00
|
|
|
from django.utils.functional import lazy
|
2014-05-18 00:54:34 +08:00
|
|
|
from django.utils.timezone import now
|
2018-01-25 12:21:13 +08:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
|
2015-11-17 13:39:28 +08:00
|
|
|
@isolate_apps('invalid_models_tests')
|
|
|
|
class AutoFieldTests(SimpleTestCase):
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def test_valid_case(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
id = models.AutoField(primary_key=True)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('id')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def test_primary_key(self):
|
|
|
|
# primary_key must be True. Refs #12467.
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.AutoField(primary_key=False)
|
|
|
|
|
|
|
|
# Prevent Django from autocreating `id` AutoField, which would
|
|
|
|
# result in an error, because a model must have exactly one
|
|
|
|
# AutoField.
|
|
|
|
another = models.IntegerField(primary_key=True)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [
|
2014-01-20 10:45:21 +08:00
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
'AutoFields must set primary_key=True.',
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E100',
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
|
2015-11-17 13:39:28 +08:00
|
|
|
@isolate_apps('invalid_models_tests')
|
2018-11-27 03:05:02 +08:00
|
|
|
class CharFieldTests(SimpleTestCase):
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def test_valid_field(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
choices=[
|
|
|
|
('1', 'item1'),
|
|
|
|
('2', 'item2'),
|
|
|
|
],
|
2018-03-16 17:54:34 +08:00
|
|
|
db_index=True,
|
|
|
|
)
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def test_missing_max_length(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField()
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [
|
2014-01-20 10:45:21 +08:00
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"CharFields must define a 'max_length' attribute.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E120',
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def test_negative_max_length(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(max_length=-1)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [
|
2014-01-20 10:45:21 +08:00
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"'max_length' must be a positive integer.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E121',
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def test_bad_max_length_value(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(max_length="bad")
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [
|
2014-01-20 10:45:21 +08:00
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"'max_length' must be a positive integer.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E121',
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-05-19 17:43:06 +08:00
|
|
|
def test_str_max_length_value(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(max_length='20')
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [
|
2015-05-19 17:43:06 +08:00
|
|
|
Error(
|
|
|
|
"'max_length' must be a positive integer.",
|
|
|
|
obj=field,
|
|
|
|
id='fields.E121',
|
|
|
|
),
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2015-05-19 17:43:06 +08:00
|
|
|
|
2017-04-25 06:49:31 +08:00
|
|
|
def test_str_max_length_type(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(max_length=True)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [
|
2017-04-25 06:49:31 +08:00
|
|
|
Error(
|
|
|
|
"'max_length' must be a positive integer.",
|
|
|
|
obj=field,
|
|
|
|
id='fields.E121'
|
|
|
|
),
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2017-04-25 06:49:31 +08:00
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def test_non_iterable_choices(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(max_length=10, choices='bad')
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [
|
2014-01-20 10:45:21 +08:00
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"'choices' must be an iterable (e.g., a list or tuple).",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E004',
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2018-01-24 23:16:08 +08:00
|
|
|
def test_non_iterable_choices_two_letters(self):
|
|
|
|
"""Two letters isn't a valid choice pair."""
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(max_length=10, choices=['ab'])
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
self.assertEqual(field.check(), [
|
|
|
|
Error(
|
|
|
|
"'choices' must be an iterable containing (actual value, "
|
|
|
|
"human readable name) tuples.",
|
|
|
|
obj=field,
|
|
|
|
id='fields.E005',
|
|
|
|
),
|
|
|
|
])
|
|
|
|
|
2016-08-17 02:23:30 +08:00
|
|
|
def test_iterable_of_iterable_choices(self):
|
2017-01-19 15:39:46 +08:00
|
|
|
class ThingItem:
|
2016-08-17 02:23:30 +08:00
|
|
|
def __init__(self, value, display):
|
|
|
|
self.value = value
|
|
|
|
self.display = display
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return (x for x in [self.value, self.display])
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return 2
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Things:
|
2016-08-17 02:23:30 +08:00
|
|
|
def __iter__(self):
|
|
|
|
return (x for x in [ThingItem(1, 2), ThingItem(3, 4)])
|
|
|
|
|
|
|
|
class ThingWithIterableChoices(models.Model):
|
|
|
|
thing = models.CharField(max_length=100, blank=True, choices=Things())
|
|
|
|
|
|
|
|
self.assertEqual(ThingWithIterableChoices._meta.get_field('thing').check(), [])
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def test_choices_containing_non_pairs(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(max_length=10, choices=[(1, 2, 3), (1, 2, 3)])
|
|
|
|
|
2018-08-22 04:35:25 +08:00
|
|
|
class Model2(models.Model):
|
|
|
|
field = models.IntegerField(choices=[0])
|
|
|
|
|
|
|
|
for model in (Model, Model2):
|
|
|
|
with self.subTest(model.__name__):
|
|
|
|
field = model._meta.get_field('field')
|
|
|
|
self.assertEqual(field.check(), [
|
|
|
|
Error(
|
|
|
|
"'choices' must be an iterable containing (actual "
|
|
|
|
"value, human readable name) tuples.",
|
|
|
|
obj=field,
|
|
|
|
id='fields.E005',
|
|
|
|
),
|
|
|
|
])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2018-01-25 12:21:13 +08:00
|
|
|
def test_choices_containing_lazy(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(max_length=10, choices=[['1', _('1')], ['2', _('2')]])
|
|
|
|
|
|
|
|
self.assertEqual(Model._meta.get_field('field').check(), [])
|
|
|
|
|
2018-05-30 09:43:38 +08:00
|
|
|
def test_lazy_choices(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(max_length=10, choices=lazy(lambda: [[1, '1'], [2, '2']], tuple)())
|
|
|
|
|
|
|
|
self.assertEqual(Model._meta.get_field('field').check(), [])
|
|
|
|
|
2018-01-24 23:16:08 +08:00
|
|
|
def test_choices_named_group(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(
|
|
|
|
max_length=10, choices=[
|
|
|
|
['knights', [['L', 'Lancelot'], ['G', 'Galahad']]],
|
|
|
|
['wizards', [['T', 'Tim the Enchanter']]],
|
|
|
|
['R', 'Random character'],
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(Model._meta.get_field('field').check(), [])
|
|
|
|
|
2017-11-30 14:33:24 +08:00
|
|
|
def test_choices_named_group_non_pairs(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(
|
|
|
|
max_length=10,
|
|
|
|
choices=[['knights', [['L', 'Lancelot', 'Du Lac']]]],
|
|
|
|
)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
self.assertEqual(field.check(), [
|
|
|
|
Error(
|
|
|
|
"'choices' must be an iterable containing (actual value, "
|
|
|
|
"human readable name) tuples.",
|
|
|
|
obj=field,
|
|
|
|
id='fields.E005',
|
|
|
|
),
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_choices_named_group_bad_structure(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(
|
|
|
|
max_length=10, choices=[
|
|
|
|
['knights', [
|
|
|
|
['Noble', [['G', 'Galahad']]],
|
|
|
|
['Combative', [['L', 'Lancelot']]],
|
|
|
|
]],
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
self.assertEqual(field.check(), [
|
|
|
|
Error(
|
|
|
|
"'choices' must be an iterable containing (actual value, "
|
|
|
|
"human readable name) tuples.",
|
|
|
|
obj=field,
|
|
|
|
id='fields.E005',
|
|
|
|
),
|
|
|
|
])
|
|
|
|
|
2018-01-25 12:21:13 +08:00
|
|
|
def test_choices_named_group_lazy(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(
|
|
|
|
max_length=10, choices=[
|
|
|
|
[_('knights'), [['L', _('Lancelot')], ['G', _('Galahad')]]],
|
|
|
|
['R', _('Random character')],
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(Model._meta.get_field('field').check(), [])
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def test_bad_db_index_value(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(max_length=10, db_index='bad')
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [
|
2014-01-20 10:45:21 +08:00
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"'db_index' must be None, True or False.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E006',
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2017-03-06 00:50:33 +08:00
|
|
|
def test_bad_validators(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(max_length=10, validators=[True])
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
self.assertEqual(field.check(), [
|
|
|
|
Error(
|
|
|
|
"All 'validators' must be callable.",
|
|
|
|
hint=(
|
|
|
|
"validators[0] (True) isn't a function or instance of a "
|
|
|
|
"validator class."
|
|
|
|
),
|
|
|
|
obj=field,
|
|
|
|
id='fields.E008',
|
|
|
|
),
|
|
|
|
])
|
|
|
|
|
2014-01-20 19:01:14 +08:00
|
|
|
@unittest.skipUnless(connection.vendor == 'mysql',
|
|
|
|
"Test valid only for MySQL")
|
2014-01-20 10:45:21 +08:00
|
|
|
def test_too_long_char_field_under_mysql(self):
|
|
|
|
from django.db.backends.mysql.validation import DatabaseValidation
|
|
|
|
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(unique=True, max_length=256)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2016-12-01 05:10:22 +08:00
|
|
|
validator = DatabaseValidation(connection=connection)
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(validator.check_field(field), [
|
2014-01-20 10:45:21 +08:00
|
|
|
Error(
|
2014-03-03 14:31:55 +08:00
|
|
|
'MySQL does not allow unique CharFields to have a max_length > 255.',
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 14:31:55 +08:00
|
|
|
id='mysql.E001',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
|
2015-11-17 13:39:28 +08:00
|
|
|
@isolate_apps('invalid_models_tests')
|
2018-11-27 03:05:02 +08:00
|
|
|
class DateFieldTests(SimpleTestCase):
|
2017-07-11 16:53:22 +08:00
|
|
|
maxDiff = None
|
2014-02-22 21:29:43 +08:00
|
|
|
|
|
|
|
def test_auto_now_and_auto_now_add_raise_error(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field0 = models.DateTimeField(auto_now=True, auto_now_add=True, default=now)
|
|
|
|
field1 = models.DateTimeField(auto_now=True, auto_now_add=False, default=now)
|
|
|
|
field2 = models.DateTimeField(auto_now=False, auto_now_add=True, default=now)
|
|
|
|
field3 = models.DateTimeField(auto_now=True, auto_now_add=True, default=None)
|
2014-05-18 00:54:34 +08:00
|
|
|
|
2014-02-22 21:29:43 +08:00
|
|
|
expected = []
|
|
|
|
checks = []
|
|
|
|
for i in range(4):
|
|
|
|
field = Model._meta.get_field('field%d' % i)
|
|
|
|
expected.append(Error(
|
|
|
|
"The options auto_now, auto_now_add, and default "
|
|
|
|
"are mutually exclusive. Only one of these options "
|
|
|
|
"may be present.",
|
|
|
|
obj=field,
|
|
|
|
id='fields.E160',
|
|
|
|
))
|
|
|
|
checks.extend(field.check())
|
|
|
|
self.assertEqual(checks, expected)
|
|
|
|
|
|
|
|
def test_fix_default_value(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field_dt = models.DateField(default=now())
|
|
|
|
field_d = models.DateField(default=now().date())
|
|
|
|
field_now = models.DateField(default=now)
|
|
|
|
|
|
|
|
field_dt = Model._meta.get_field('field_dt')
|
|
|
|
field_d = Model._meta.get_field('field_d')
|
|
|
|
field_now = Model._meta.get_field('field_now')
|
|
|
|
errors = field_dt.check()
|
|
|
|
errors.extend(field_d.check())
|
|
|
|
errors.extend(field_now.check()) # doesn't raise a warning
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(errors, [
|
2014-02-22 21:29:43 +08:00
|
|
|
DjangoWarning(
|
|
|
|
'Fixed default value provided.',
|
|
|
|
hint='It seems you set a fixed date / time / datetime '
|
|
|
|
'value as default for this field. This may not be '
|
|
|
|
'what you want. If you want to have the current date '
|
|
|
|
'as default, use `django.utils.timezone.now`',
|
|
|
|
obj=field_dt,
|
|
|
|
id='fields.W161',
|
|
|
|
),
|
|
|
|
DjangoWarning(
|
|
|
|
'Fixed default value provided.',
|
|
|
|
hint='It seems you set a fixed date / time / datetime '
|
|
|
|
'value as default for this field. This may not be '
|
|
|
|
'what you want. If you want to have the current date '
|
|
|
|
'as default, use `django.utils.timezone.now`',
|
|
|
|
obj=field_d,
|
|
|
|
id='fields.W161',
|
|
|
|
)
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2014-02-22 21:29:43 +08:00
|
|
|
|
|
|
|
@override_settings(USE_TZ=True)
|
|
|
|
def test_fix_default_value_tz(self):
|
|
|
|
self.test_fix_default_value()
|
|
|
|
|
|
|
|
|
2015-11-17 13:39:28 +08:00
|
|
|
@isolate_apps('invalid_models_tests')
|
2018-11-27 03:05:02 +08:00
|
|
|
class DateTimeFieldTests(SimpleTestCase):
|
2017-07-11 16:53:22 +08:00
|
|
|
maxDiff = None
|
2014-02-22 21:29:43 +08:00
|
|
|
|
|
|
|
def test_fix_default_value(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field_dt = models.DateTimeField(default=now())
|
|
|
|
field_d = models.DateTimeField(default=now().date())
|
|
|
|
field_now = models.DateTimeField(default=now)
|
|
|
|
|
|
|
|
field_dt = Model._meta.get_field('field_dt')
|
|
|
|
field_d = Model._meta.get_field('field_d')
|
|
|
|
field_now = Model._meta.get_field('field_now')
|
|
|
|
errors = field_dt.check()
|
|
|
|
errors.extend(field_d.check())
|
|
|
|
errors.extend(field_now.check()) # doesn't raise a warning
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(errors, [
|
2014-02-22 21:29:43 +08:00
|
|
|
DjangoWarning(
|
|
|
|
'Fixed default value provided.',
|
|
|
|
hint='It seems you set a fixed date / time / datetime '
|
|
|
|
'value as default for this field. This may not be '
|
|
|
|
'what you want. If you want to have the current date '
|
|
|
|
'as default, use `django.utils.timezone.now`',
|
|
|
|
obj=field_dt,
|
|
|
|
id='fields.W161',
|
|
|
|
),
|
|
|
|
DjangoWarning(
|
|
|
|
'Fixed default value provided.',
|
|
|
|
hint='It seems you set a fixed date / time / datetime '
|
|
|
|
'value as default for this field. This may not be '
|
|
|
|
'what you want. If you want to have the current date '
|
|
|
|
'as default, use `django.utils.timezone.now`',
|
|
|
|
obj=field_d,
|
|
|
|
id='fields.W161',
|
|
|
|
)
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2014-02-22 21:29:43 +08:00
|
|
|
|
|
|
|
@override_settings(USE_TZ=True)
|
|
|
|
def test_fix_default_value_tz(self):
|
|
|
|
self.test_fix_default_value()
|
|
|
|
|
|
|
|
|
2015-11-17 13:39:28 +08:00
|
|
|
@isolate_apps('invalid_models_tests')
|
|
|
|
class DecimalFieldTests(SimpleTestCase):
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def test_required_attributes(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.DecimalField()
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [
|
2014-01-20 10:45:21 +08:00
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"DecimalFields must define a 'decimal_places' attribute.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E130',
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"DecimalFields must define a 'max_digits' attribute.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E132',
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def test_negative_max_digits_and_decimal_places(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.DecimalField(max_digits=-1, decimal_places=-1)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [
|
2014-01-20 10:45:21 +08:00
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"'decimal_places' must be a non-negative integer.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E131',
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"'max_digits' must be a positive integer.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E133',
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def test_bad_values_of_max_digits_and_decimal_places(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.DecimalField(max_digits="bad", decimal_places="bad")
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [
|
2014-01-20 10:45:21 +08:00
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"'decimal_places' must be a non-negative integer.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E131',
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"'max_digits' must be a positive integer.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E133',
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def test_decimal_places_greater_than_max_digits(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.DecimalField(max_digits=9, decimal_places=10)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [
|
2014-01-20 10:45:21 +08:00
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"'max_digits' must be greater or equal to 'decimal_places'.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E134',
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def test_valid_field(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.DecimalField(max_digits=10, decimal_places=10)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
|
2015-11-17 13:39:28 +08:00
|
|
|
@isolate_apps('invalid_models_tests')
|
|
|
|
class FileFieldTests(SimpleTestCase):
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2016-11-30 22:11:44 +08:00
|
|
|
def test_valid_default_case(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.FileField()
|
|
|
|
|
|
|
|
self.assertEqual(Model._meta.get_field('field').check(), [])
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def test_valid_case(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.FileField(upload_to='somewhere')
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def test_primary_key(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.FileField(primary_key=False, upload_to='somewhere')
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [
|
2014-01-20 10:45:21 +08:00
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"'primary_key' is not a valid argument for a FileField.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E201',
|
2014-01-20 10:45:21 +08:00
|
|
|
)
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2016-11-27 02:23:03 +08:00
|
|
|
def test_upload_to_starts_with_slash(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.FileField(upload_to='/somewhere')
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
self.assertEqual(field.check(), [
|
|
|
|
Error(
|
|
|
|
"FileField's 'upload_to' argument must be a relative path, not "
|
|
|
|
"an absolute path.",
|
|
|
|
obj=field,
|
|
|
|
id='fields.E202',
|
|
|
|
hint='Remove the leading slash.',
|
|
|
|
)
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_upload_to_callable_not_checked(self):
|
|
|
|
def callable(instance, filename):
|
|
|
|
return '/' + filename
|
|
|
|
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.FileField(upload_to=callable)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
self.assertEqual(field.check(), [])
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-11-17 13:39:28 +08:00
|
|
|
@isolate_apps('invalid_models_tests')
|
|
|
|
class FilePathFieldTests(SimpleTestCase):
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def test_forbidden_files_and_folders(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.FilePathField(allow_files=False, allow_folders=False)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [
|
2014-01-20 10:45:21 +08:00
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
"FilePathFields must have either 'allow_files' or 'allow_folders' set to True.",
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E140',
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
|
2015-11-17 13:39:28 +08:00
|
|
|
@isolate_apps('invalid_models_tests')
|
|
|
|
class GenericIPAddressFieldTests(SimpleTestCase):
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def test_non_nullable_blank(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.GenericIPAddressField(null=False, blank=True)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(field.check(), [
|
2014-01-20 10:45:21 +08:00
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
('GenericIPAddressFields cannot have blank=True if null=False, '
|
|
|
|
'as blank values are stored as nulls.'),
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E150',
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
|
2015-11-17 13:39:28 +08:00
|
|
|
@isolate_apps('invalid_models_tests')
|
|
|
|
class ImageFieldTests(SimpleTestCase):
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def test_pillow_installed(self):
|
|
|
|
try:
|
2014-03-21 22:54:53 +08:00
|
|
|
from PIL import Image # NOQA
|
|
|
|
except ImportError:
|
2014-01-20 10:45:21 +08:00
|
|
|
pillow_installed = False
|
|
|
|
else:
|
|
|
|
pillow_installed = True
|
|
|
|
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.ImageField(upload_to='somewhere')
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
errors = field.check()
|
|
|
|
expected = [] if pillow_installed else [
|
|
|
|
Error(
|
2014-03-03 18:18:39 +08:00
|
|
|
'Cannot use ImageField because Pillow is not installed.',
|
2018-04-18 06:19:29 +08:00
|
|
|
hint=('Get Pillow at https://pypi.org/project/Pillow/ '
|
2014-10-19 09:01:56 +08:00
|
|
|
'or run command "pip install Pillow".'),
|
2014-01-20 10:45:21 +08:00
|
|
|
obj=field,
|
2014-03-03 18:18:39 +08:00
|
|
|
id='fields.E210',
|
2014-01-20 10:45:21 +08:00
|
|
|
),
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
2014-03-13 19:32:20 +08:00
|
|
|
|
|
|
|
|
2015-11-17 13:39:28 +08:00
|
|
|
@isolate_apps('invalid_models_tests')
|
|
|
|
class IntegerFieldTests(SimpleTestCase):
|
2014-11-18 22:03:13 +08:00
|
|
|
|
|
|
|
def test_max_length_warning(self):
|
|
|
|
class Model(models.Model):
|
2017-12-11 23:35:19 +08:00
|
|
|
integer = models.IntegerField(max_length=2)
|
|
|
|
biginteger = models.BigIntegerField(max_length=2)
|
|
|
|
smallinteger = models.SmallIntegerField(max_length=2)
|
|
|
|
positiveinteger = models.PositiveIntegerField(max_length=2)
|
|
|
|
positivesmallinteger = models.PositiveSmallIntegerField(max_length=2)
|
|
|
|
|
|
|
|
for field in Model._meta.get_fields():
|
|
|
|
if field.auto_created:
|
|
|
|
continue
|
|
|
|
with self.subTest(name=field.name):
|
|
|
|
self.assertEqual(field.check(), [
|
|
|
|
DjangoWarning(
|
|
|
|
"'max_length' is ignored when used with %s." % field.__class__.__name__,
|
|
|
|
hint="Remove 'max_length' from field",
|
|
|
|
obj=field,
|
|
|
|
id='fields.W122',
|
|
|
|
)
|
|
|
|
])
|
2014-11-18 22:03:13 +08:00
|
|
|
|
|
|
|
|
2015-11-17 13:39:28 +08:00
|
|
|
@isolate_apps('invalid_models_tests')
|
2018-11-27 03:05:02 +08:00
|
|
|
class TimeFieldTests(SimpleTestCase):
|
2017-07-11 16:53:22 +08:00
|
|
|
maxDiff = None
|
2014-03-13 19:32:20 +08:00
|
|
|
|
2014-02-22 21:29:43 +08:00
|
|
|
def test_fix_default_value(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field_dt = models.TimeField(default=now())
|
|
|
|
field_t = models.TimeField(default=now().time())
|
|
|
|
field_now = models.DateField(default=now)
|
|
|
|
|
|
|
|
field_dt = Model._meta.get_field('field_dt')
|
|
|
|
field_t = Model._meta.get_field('field_t')
|
|
|
|
field_now = Model._meta.get_field('field_now')
|
|
|
|
errors = field_dt.check()
|
|
|
|
errors.extend(field_t.check())
|
|
|
|
errors.extend(field_now.check()) # doesn't raise a warning
|
2017-07-11 16:53:22 +08:00
|
|
|
self.assertEqual(errors, [
|
2014-02-22 21:29:43 +08:00
|
|
|
DjangoWarning(
|
|
|
|
'Fixed default value provided.',
|
|
|
|
hint='It seems you set a fixed date / time / datetime '
|
|
|
|
'value as default for this field. This may not be '
|
|
|
|
'what you want. If you want to have the current date '
|
|
|
|
'as default, use `django.utils.timezone.now`',
|
|
|
|
obj=field_dt,
|
|
|
|
id='fields.W161',
|
|
|
|
),
|
|
|
|
DjangoWarning(
|
|
|
|
'Fixed default value provided.',
|
|
|
|
hint='It seems you set a fixed date / time / datetime '
|
|
|
|
'value as default for this field. This may not be '
|
|
|
|
'what you want. If you want to have the current date '
|
|
|
|
'as default, use `django.utils.timezone.now`',
|
|
|
|
obj=field_t,
|
|
|
|
id='fields.W161',
|
2014-03-13 19:32:20 +08:00
|
|
|
)
|
2017-07-11 16:53:22 +08:00
|
|
|
])
|
2014-03-13 19:32:20 +08:00
|
|
|
|
2014-02-22 21:29:43 +08:00
|
|
|
@override_settings(USE_TZ=True)
|
|
|
|
def test_fix_default_value_tz(self):
|
|
|
|
self.test_fix_default_value()
|
2017-05-23 23:02:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
@isolate_apps('invalid_models_tests')
|
2018-11-27 03:05:02 +08:00
|
|
|
class TextFieldTests(SimpleTestCase):
|
2017-05-23 23:02:40 +08:00
|
|
|
|
|
|
|
@skipIfDBFeature('supports_index_on_text_field')
|
|
|
|
def test_max_length_warning(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
value = models.TextField(db_index=True)
|
|
|
|
field = Model._meta.get_field('value')
|
|
|
|
field_type = field.db_type(connection)
|
|
|
|
self.assertEqual(field.check(), [
|
|
|
|
DjangoWarning(
|
|
|
|
'%s does not support a database index on %s columns.'
|
|
|
|
% (connection.display_name, field_type),
|
|
|
|
hint=(
|
|
|
|
"An index won't be created. Silence this warning if you "
|
|
|
|
"don't care about it."
|
|
|
|
),
|
|
|
|
obj=field,
|
|
|
|
id='fields.W162',
|
|
|
|
)
|
|
|
|
])
|