2017-06-13 02:18:37 +08:00
|
|
|
from django import forms
|
2020-12-11 01:00:57 +08:00
|
|
|
from django.db import models
|
2020-07-18 19:17:39 +08:00
|
|
|
from django.test import SimpleTestCase, TestCase
|
2016-03-22 09:06:54 +08:00
|
|
|
|
|
|
|
from .models import Post
|
|
|
|
|
|
|
|
|
|
|
|
class TextFieldTests(TestCase):
|
|
|
|
|
|
|
|
def test_max_length_passed_to_formfield(self):
|
|
|
|
"""
|
|
|
|
TextField passes its max_length attribute to form fields created using
|
|
|
|
their formfield() method.
|
|
|
|
"""
|
|
|
|
tf1 = models.TextField()
|
|
|
|
tf2 = models.TextField(max_length=2345)
|
|
|
|
self.assertIsNone(tf1.formfield().max_length)
|
|
|
|
self.assertEqual(2345, tf2.formfield().max_length)
|
|
|
|
|
2017-06-13 02:18:37 +08:00
|
|
|
def test_choices_generates_select_widget(self):
|
|
|
|
"""A TextField with choices uses a Select widget."""
|
|
|
|
f = models.TextField(choices=[('A', 'A'), ('B', 'B')])
|
|
|
|
self.assertIsInstance(f.formfield().widget, forms.Select)
|
|
|
|
|
2016-03-22 09:06:54 +08:00
|
|
|
def test_to_python(self):
|
|
|
|
"""TextField.to_python() should return a string."""
|
|
|
|
f = models.TextField()
|
|
|
|
self.assertEqual(f.to_python(1), '1')
|
|
|
|
|
|
|
|
def test_lookup_integer_in_textfield(self):
|
|
|
|
self.assertEqual(Post.objects.filter(body=24).count(), 0)
|
2016-09-13 20:14:49 +08:00
|
|
|
|
|
|
|
def test_emoji(self):
|
|
|
|
p = Post.objects.create(title='Whatever', body='Smile 😀.')
|
|
|
|
p.refresh_from_db()
|
|
|
|
self.assertEqual(p.body, 'Smile 😀.')
|
2020-07-18 19:17:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestMethods(SimpleTestCase):
|
|
|
|
def test_deconstruct(self):
|
|
|
|
field = models.TextField()
|
|
|
|
*_, kwargs = field.deconstruct()
|
|
|
|
self.assertEqual(kwargs, {})
|
|
|
|
field = models.TextField(db_collation='utf8_esperanto_ci')
|
|
|
|
*_, kwargs = field.deconstruct()
|
|
|
|
self.assertEqual(kwargs, {'db_collation': 'utf8_esperanto_ci'})
|