2016-09-13 20:14:49 +08:00
|
|
|
from unittest import skipIf
|
|
|
|
|
2016-03-22 09:06:54 +08:00
|
|
|
from django.core.exceptions import ValidationError
|
2016-09-13 20:14:49 +08:00
|
|
|
from django.db import connection, models
|
2016-03-22 09:06:54 +08:00
|
|
|
from django.test import SimpleTestCase, TestCase
|
|
|
|
|
|
|
|
from .models import Post
|
|
|
|
|
|
|
|
|
|
|
|
class TestCharField(TestCase):
|
|
|
|
|
|
|
|
def test_max_length_passed_to_formfield(self):
|
|
|
|
"""
|
|
|
|
CharField passes its max_length attribute to form fields created using
|
|
|
|
the formfield() method.
|
|
|
|
"""
|
|
|
|
cf1 = models.CharField()
|
|
|
|
cf2 = models.CharField(max_length=1234)
|
|
|
|
self.assertIsNone(cf1.formfield().max_length)
|
|
|
|
self.assertEqual(1234, cf2.formfield().max_length)
|
|
|
|
|
|
|
|
def test_lookup_integer_in_charfield(self):
|
|
|
|
self.assertEqual(Post.objects.filter(title=9).count(), 0)
|
|
|
|
|
2016-10-27 15:53:39 +08:00
|
|
|
@skipIf(connection.vendor == 'mysql', 'Running on MySQL requires utf8mb4 encoding (#18392)')
|
2016-09-13 20:14:49 +08:00
|
|
|
def test_emoji(self):
|
|
|
|
p = Post.objects.create(title='Smile 😀', body='Whatever.')
|
|
|
|
p.refresh_from_db()
|
|
|
|
self.assertEqual(p.title, 'Smile 😀')
|
|
|
|
|
2019-01-01 01:57:35 +08:00
|
|
|
def test_assignment_from_choice_enum(self):
|
|
|
|
class Event(models.TextChoices):
|
|
|
|
C = 'Carnival!'
|
|
|
|
F = 'Festival!'
|
|
|
|
|
|
|
|
p1 = Post.objects.create(title=Event.C, body=Event.F)
|
|
|
|
p1.refresh_from_db()
|
|
|
|
self.assertEqual(p1.title, 'Carnival!')
|
|
|
|
self.assertEqual(p1.body, 'Festival!')
|
|
|
|
self.assertEqual(p1.title, Event.C)
|
|
|
|
self.assertEqual(p1.body, Event.F)
|
|
|
|
p2 = Post.objects.get(title='Carnival!')
|
2019-10-21 13:52:17 +08:00
|
|
|
self.assertEqual(p1, p2)
|
|
|
|
self.assertEqual(p2.title, Event.C)
|
2019-01-01 01:57:35 +08:00
|
|
|
|
2016-03-22 09:06:54 +08:00
|
|
|
|
2020-07-18 19:17:39 +08:00
|
|
|
class TestMethods(SimpleTestCase):
|
|
|
|
def test_deconstruct(self):
|
|
|
|
field = models.CharField()
|
|
|
|
*_, kwargs = field.deconstruct()
|
|
|
|
self.assertEqual(kwargs, {})
|
|
|
|
field = models.CharField(db_collation='utf8_esperanto_ci')
|
|
|
|
*_, kwargs = field.deconstruct()
|
|
|
|
self.assertEqual(kwargs, {'db_collation': 'utf8_esperanto_ci'})
|
|
|
|
|
|
|
|
|
2016-03-22 09:06:54 +08:00
|
|
|
class ValidationTests(SimpleTestCase):
|
|
|
|
|
2019-01-01 01:57:35 +08:00
|
|
|
class Choices(models.TextChoices):
|
|
|
|
C = 'c', 'C'
|
|
|
|
|
2016-03-22 09:06:54 +08:00
|
|
|
def test_charfield_raises_error_on_empty_string(self):
|
|
|
|
f = models.CharField()
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
f.clean('', None)
|
|
|
|
|
|
|
|
def test_charfield_cleans_empty_string_when_blank_true(self):
|
|
|
|
f = models.CharField(blank=True)
|
|
|
|
self.assertEqual('', f.clean('', None))
|
|
|
|
|
|
|
|
def test_charfield_with_choices_cleans_valid_choice(self):
|
|
|
|
f = models.CharField(max_length=1, choices=[('a', 'A'), ('b', 'B')])
|
|
|
|
self.assertEqual('a', f.clean('a', None))
|
|
|
|
|
|
|
|
def test_charfield_with_choices_raises_error_on_invalid_choice(self):
|
|
|
|
f = models.CharField(choices=[('a', 'A'), ('b', 'B')])
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
f.clean('not a', None)
|
|
|
|
|
2019-01-01 01:57:35 +08:00
|
|
|
def test_enum_choices_cleans_valid_string(self):
|
|
|
|
f = models.CharField(choices=self.Choices.choices, max_length=1)
|
|
|
|
self.assertEqual(f.clean('c', None), 'c')
|
|
|
|
|
|
|
|
def test_enum_choices_invalid_input(self):
|
|
|
|
f = models.CharField(choices=self.Choices.choices, max_length=1)
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
f.clean('a', None)
|
|
|
|
|
2016-03-22 09:06:54 +08:00
|
|
|
def test_charfield_raises_error_on_empty_input(self):
|
|
|
|
f = models.CharField(null=False)
|
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
f.clean(None, None)
|