| 1 |
from django import forms |
| 2 |
from django.db import models |
| 3 |
from django.test import SimpleTestCase, TestCase |
| 4 |
|
| 5 |
from .models import Post |
| 6 |
|
| 7 |
|
| 8 |
class TextFieldTests(TestCase): |
| 9 |
def test_max_length_passed_to_formfield(self): |
| 10 |
""" |
| 11 |
TextField passes its max_length attribute to form fields created using |
| 12 |
their formfield() method. |
| 13 |
""" |
| 14 |
tf1 = models.TextField() |
| 15 |
tf2 = models.TextField(max_length=2345) |
| 16 |
self.assertIsNone(tf1.formfield().max_length) |
| 17 |
self.assertEqual(2345, tf2.formfield().max_length) |
| 18 |
|
| 19 |
def test_choices_generates_select_widget(self): |
| 20 |
"""A TextField with choices uses a Select widget.""" |
| 21 |
f = models.TextField(choices=[("A", "A"), ("B", "B")]) |
| 22 |
self.assertIsInstance(f.formfield().widget, forms.Select) |
| 23 |
|
| 24 |
def test_to_python(self): |
| 25 |
"""TextField.to_python() should return a string.""" |
| 26 |
f = models.TextField() |
| 27 |
self.assertEqual(f.to_python(1), "1") |
| 28 |
|
| 29 |
def test_lookup_integer_in_textfield(self): |
| 30 |
self.assertEqual(Post.objects.filter(body=24).count(), 0) |
| 31 |
|
| 32 |
def test_emoji(self): |
| 33 |
p = Post.objects.create(title="Whatever", body="Smile 😀.") |
| 34 |
p.refresh_from_db() |
| 35 |
self.assertEqual(p.body, "Smile 😀.") |
| 36 |
|
| 37 |
|
| 38 |
class TestMethods(SimpleTestCase): |
| 39 |
def test_deconstruct(self): |
| 40 |
field = models.TextField() |
| 41 |
*_, kwargs = field.deconstruct() |
| 42 |
self.assertEqual(kwargs, {}) |
| 43 |
field = models.TextField(db_collation="utf8_esperanto_ci") |
| 44 |
*_, kwargs = field.deconstruct() |
| 45 |
self.assertEqual(kwargs, {"db_collation": "utf8_esperanto_ci"}) |