2014-01-20 02:45:21 +00:00
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-01-20 11:01:14 +00:00
|
|
|
import unittest
|
|
|
|
|
2014-02-22 13:29:43 +00:00
|
|
|
from django.core.checks import Error, Warning as DjangoWarning
|
2014-01-20 11:01:14 +00:00
|
|
|
from django.db import connection, models
|
2014-02-22 13:29:43 +00:00
|
|
|
from django.test.utils import override_settings
|
2014-05-17 16:54:34 +00:00
|
|
|
from django.utils.timezone import now
|
2014-01-20 02:45:21 +00:00
|
|
|
|
|
|
|
from .base import IsolatedModelsTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class AutoFieldTests(IsolatedModelsTestCase):
|
|
|
|
|
|
|
|
def test_valid_case(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
id = models.AutoField(primary_key=True)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('id')
|
|
|
|
errors = field.check()
|
|
|
|
expected = []
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
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')
|
|
|
|
errors = field.check()
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
'AutoFields must set primary_key=True.',
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E100',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
|
|
|
|
class BooleanFieldTests(IsolatedModelsTestCase):
|
|
|
|
|
|
|
|
def test_nullable_boolean_field(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.BooleanField(null=True)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
errors = field.check()
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
'BooleanFields do not accept null values.',
|
2014-01-20 02:45:21 +00:00
|
|
|
hint='Use a NullBooleanField instead.',
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E110',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
|
|
|
|
class CharFieldTests(IsolatedModelsTestCase):
|
|
|
|
|
|
|
|
def test_valid_field(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
choices=[
|
|
|
|
('1', 'item1'),
|
|
|
|
('2', 'item2'),
|
|
|
|
],
|
|
|
|
db_index=True)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
errors = field.check()
|
|
|
|
expected = []
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_missing_max_length(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField()
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
errors = field.check()
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
"CharFields must define a 'max_length' attribute.",
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E120',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_negative_max_length(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(max_length=-1)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
errors = field.check()
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
"'max_length' must be a positive integer.",
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E121',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_bad_max_length_value(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(max_length="bad")
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
errors = field.check()
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
"'max_length' must be a positive integer.",
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E121',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_non_iterable_choices(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(max_length=10, choices='bad')
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
errors = field.check()
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
"'choices' must be an iterable (e.g., a list or tuple).",
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E004',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_choices_containing_non_pairs(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(max_length=10, choices=[(1, 2, 3), (1, 2, 3)])
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
errors = field.check()
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
"'choices' must be an iterable containing (actual value, human readable name) tuples.",
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E005',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
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')
|
|
|
|
errors = field.check()
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
"'db_index' must be None, True or False.",
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E006',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
2014-01-20 11:01:14 +00:00
|
|
|
@unittest.skipUnless(connection.vendor == 'mysql',
|
|
|
|
"Test valid only for MySQL")
|
2014-01-20 02:45:21 +00: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')
|
|
|
|
validator = DatabaseValidation(connection=None)
|
|
|
|
errors = validator.check_field(field)
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 06:31:55 +00:00
|
|
|
'MySQL does not allow unique CharFields to have a max_length > 255.',
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 06:31:55 +00:00
|
|
|
id='mysql.E001',
|
2014-01-20 02:45:21 +00:00
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
|
2014-02-22 13:29:43 +00:00
|
|
|
class DateFieldTests(IsolatedModelsTestCase):
|
|
|
|
|
|
|
|
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-17 16:54:34 +00:00
|
|
|
|
2014-02-22 13:29:43 +00: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.",
|
|
|
|
hint=None,
|
|
|
|
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
|
|
|
|
expected = [
|
|
|
|
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',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
maxDiff = self.maxDiff
|
|
|
|
self.maxDiff = None
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
self.maxDiff = maxDiff
|
|
|
|
|
|
|
|
@override_settings(USE_TZ=True)
|
|
|
|
def test_fix_default_value_tz(self):
|
|
|
|
self.test_fix_default_value()
|
|
|
|
|
|
|
|
|
|
|
|
class DateTimeFieldTests(IsolatedModelsTestCase):
|
|
|
|
|
|
|
|
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
|
|
|
|
expected = [
|
|
|
|
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',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
maxDiff = self.maxDiff
|
|
|
|
self.maxDiff = None
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
self.maxDiff = maxDiff
|
|
|
|
|
|
|
|
@override_settings(USE_TZ=True)
|
|
|
|
def test_fix_default_value_tz(self):
|
|
|
|
self.test_fix_default_value()
|
|
|
|
|
|
|
|
|
2014-01-20 02:45:21 +00:00
|
|
|
class DecimalFieldTests(IsolatedModelsTestCase):
|
|
|
|
|
|
|
|
def test_required_attributes(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.DecimalField()
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
errors = field.check()
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
"DecimalFields must define a 'decimal_places' attribute.",
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E130',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
"DecimalFields must define a 'max_digits' attribute.",
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E132',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
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')
|
|
|
|
errors = field.check()
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
"'decimal_places' must be a non-negative integer.",
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E131',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
"'max_digits' must be a positive integer.",
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E133',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
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')
|
|
|
|
errors = field.check()
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
"'decimal_places' must be a non-negative integer.",
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E131',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
"'max_digits' must be a positive integer.",
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E133',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
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')
|
|
|
|
errors = field.check()
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
"'max_digits' must be greater or equal to 'decimal_places'.",
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E134',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_valid_field(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.DecimalField(max_digits=10, decimal_places=10)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
errors = field.check()
|
|
|
|
expected = []
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
|
|
|
|
class FileFieldTests(IsolatedModelsTestCase):
|
|
|
|
|
|
|
|
def test_valid_case(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.FileField(upload_to='somewhere')
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
errors = field.check()
|
|
|
|
expected = []
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_unique(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.FileField(unique=False, upload_to='somewhere')
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
errors = field.check()
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
"'unique' is not a valid argument for a FileField.",
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E200',
|
2014-01-20 02:45:21 +00:00
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
def test_primary_key(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.FileField(primary_key=False, upload_to='somewhere')
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
errors = field.check()
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
"'primary_key' is not a valid argument for a FileField.",
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E201',
|
2014-01-20 02:45:21 +00:00
|
|
|
)
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
|
|
|
|
class FilePathFieldTests(IsolatedModelsTestCase):
|
|
|
|
|
|
|
|
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')
|
|
|
|
errors = field.check()
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
"FilePathFields must have either 'allow_files' or 'allow_folders' set to True.",
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E140',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
|
|
|
|
class GenericIPAddressFieldTests(IsolatedModelsTestCase):
|
|
|
|
|
|
|
|
def test_non_nullable_blank(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.GenericIPAddressField(null=False, blank=True)
|
|
|
|
|
|
|
|
field = Model._meta.get_field('field')
|
|
|
|
errors = field.check()
|
|
|
|
expected = [
|
|
|
|
Error(
|
2014-03-03 10:18:39 +00:00
|
|
|
('GenericIPAddressFields cannot have blank=True if null=False, '
|
|
|
|
'as blank values are stored as nulls.'),
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=None,
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E150',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
|
|
|
|
|
|
|
|
class ImageFieldTests(IsolatedModelsTestCase):
|
|
|
|
|
|
|
|
def test_pillow_installed(self):
|
|
|
|
try:
|
2014-03-21 14:54:53 +00:00
|
|
|
from PIL import Image # NOQA
|
|
|
|
except ImportError:
|
2014-01-20 02:45:21 +00: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 10:18:39 +00:00
|
|
|
'Cannot use ImageField because Pillow is not installed.',
|
2014-01-20 02:45:21 +00:00
|
|
|
hint=('Get Pillow at https://pypi.python.org/pypi/Pillow '
|
|
|
|
'or run command "pip install pillow".'),
|
|
|
|
obj=field,
|
2014-03-03 10:18:39 +00:00
|
|
|
id='fields.E210',
|
2014-01-20 02:45:21 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
self.assertEqual(errors, expected)
|
2014-03-13 11:32:20 +00:00
|
|
|
|
|
|
|
|
2014-02-22 13:29:43 +00:00
|
|
|
class TimeFieldTests(IsolatedModelsTestCase):
|
2014-03-13 11:32:20 +00:00
|
|
|
|
2014-02-22 13:29:43 +00: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
|
|
|
|
expected = [
|
|
|
|
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 11:32:20 +00:00
|
|
|
)
|
2014-02-22 13:29:43 +00:00
|
|
|
]
|
|
|
|
maxDiff = self.maxDiff
|
|
|
|
self.maxDiff = None
|
|
|
|
self.assertEqual(errors, expected)
|
|
|
|
self.maxDiff = maxDiff
|
2014-03-13 11:32:20 +00:00
|
|
|
|
2014-02-22 13:29:43 +00:00
|
|
|
@override_settings(USE_TZ=True)
|
|
|
|
def test_fix_default_value_tz(self):
|
|
|
|
self.test_fix_default_value()
|