test_textfield.py 1.7 KB

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