test_charfield.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from unittest import skipIf
  2. from django.core.exceptions import ValidationError
  3. from django.db import connection, models
  4. from django.test import SimpleTestCase, TestCase
  5. from .models import Post
  6. class TestCharField(TestCase):
  7. def test_max_length_passed_to_formfield(self):
  8. """
  9. CharField passes its max_length attribute to form fields created using
  10. the formfield() method.
  11. """
  12. cf1 = models.CharField()
  13. cf2 = models.CharField(max_length=1234)
  14. self.assertIsNone(cf1.formfield().max_length)
  15. self.assertEqual(1234, cf2.formfield().max_length)
  16. def test_lookup_integer_in_charfield(self):
  17. self.assertEqual(Post.objects.filter(title=9).count(), 0)
  18. @skipIf(connection.vendor == 'mysql', 'Running on MySQL requires utf8mb4 encoding (#18392)')
  19. def test_emoji(self):
  20. p = Post.objects.create(title='Smile 😀', body='Whatever.')
  21. p.refresh_from_db()
  22. self.assertEqual(p.title, 'Smile 😀')
  23. def test_assignment_from_choice_enum(self):
  24. class Event(models.TextChoices):
  25. C = 'Carnival!'
  26. F = 'Festival!'
  27. p1 = Post.objects.create(title=Event.C, body=Event.F)
  28. p1.refresh_from_db()
  29. self.assertEqual(p1.title, 'Carnival!')
  30. self.assertEqual(p1.body, 'Festival!')
  31. self.assertEqual(p1.title, Event.C)
  32. self.assertEqual(p1.body, Event.F)
  33. p2 = Post.objects.get(title='Carnival!')
  34. self.assertEquals(p1, p2)
  35. self.assertEquals(p2.title, Event.C)
  36. class ValidationTests(SimpleTestCase):
  37. class Choices(models.TextChoices):
  38. C = 'c', 'C'
  39. def test_charfield_raises_error_on_empty_string(self):
  40. f = models.CharField()
  41. with self.assertRaises(ValidationError):
  42. f.clean('', None)
  43. def test_charfield_cleans_empty_string_when_blank_true(self):
  44. f = models.CharField(blank=True)
  45. self.assertEqual('', f.clean('', None))
  46. def test_charfield_with_choices_cleans_valid_choice(self):
  47. f = models.CharField(max_length=1, choices=[('a', 'A'), ('b', 'B')])
  48. self.assertEqual('a', f.clean('a', None))
  49. def test_charfield_with_choices_raises_error_on_invalid_choice(self):
  50. f = models.CharField(choices=[('a', 'A'), ('b', 'B')])
  51. with self.assertRaises(ValidationError):
  52. f.clean('not a', None)
  53. def test_enum_choices_cleans_valid_string(self):
  54. f = models.CharField(choices=self.Choices.choices, max_length=1)
  55. self.assertEqual(f.clean('c', None), 'c')
  56. def test_enum_choices_invalid_input(self):
  57. f = models.CharField(choices=self.Choices.choices, max_length=1)
  58. with self.assertRaises(ValidationError):
  59. f.clean('a', None)
  60. def test_charfield_raises_error_on_empty_input(self):
  61. f = models.CharField(null=False)
  62. with self.assertRaises(ValidationError):
  63. f.clean(None, None)