test_charfield.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from django.core.exceptions import ValidationError
  2. from django.db import models
  3. from django.test import SimpleTestCase, TestCase
  4. from .models import Post
  5. class TestCharField(TestCase):
  6. def test_max_length_passed_to_formfield(self):
  7. """
  8. CharField passes its max_length attribute to form fields created using
  9. the formfield() method.
  10. """
  11. cf1 = models.CharField()
  12. cf2 = models.CharField(max_length=1234)
  13. self.assertIsNone(cf1.formfield().max_length)
  14. self.assertEqual(1234, cf2.formfield().max_length)
  15. def test_lookup_integer_in_charfield(self):
  16. self.assertEqual(Post.objects.filter(title=9).count(), 0)
  17. def test_emoji(self):
  18. p = Post.objects.create(title="Smile 😀", body="Whatever.")
  19. p.refresh_from_db()
  20. self.assertEqual(p.title, "Smile 😀")
  21. def test_assignment_from_choice_enum(self):
  22. class Event(models.TextChoices):
  23. C = "Carnival!"
  24. F = "Festival!"
  25. p1 = Post.objects.create(title=Event.C, body=Event.F)
  26. p1.refresh_from_db()
  27. self.assertEqual(p1.title, "Carnival!")
  28. self.assertEqual(p1.body, "Festival!")
  29. self.assertEqual(p1.title, Event.C)
  30. self.assertEqual(p1.body, Event.F)
  31. p2 = Post.objects.get(title="Carnival!")
  32. self.assertEqual(p1, p2)
  33. self.assertEqual(p2.title, Event.C)
  34. class TestMethods(SimpleTestCase):
  35. def test_deconstruct(self):
  36. field = models.CharField()
  37. *_, kwargs = field.deconstruct()
  38. self.assertEqual(kwargs, {})
  39. field = models.CharField(db_collation="utf8_esperanto_ci")
  40. *_, kwargs = field.deconstruct()
  41. self.assertEqual(kwargs, {"db_collation": "utf8_esperanto_ci"})
  42. class ValidationTests(SimpleTestCase):
  43. class Choices(models.TextChoices):
  44. C = "c", "C"
  45. def test_charfield_raises_error_on_empty_string(self):
  46. f = models.CharField()
  47. msg = "This field cannot be blank."
  48. with self.assertRaisesMessage(ValidationError, msg):
  49. f.clean("", None)
  50. def test_charfield_cleans_empty_string_when_blank_true(self):
  51. f = models.CharField(blank=True)
  52. self.assertEqual("", f.clean("", None))
  53. def test_charfield_with_choices_cleans_valid_choice(self):
  54. f = models.CharField(max_length=1, choices=[("a", "A"), ("b", "B")])
  55. self.assertEqual("a", f.clean("a", None))
  56. def test_charfield_with_choices_raises_error_on_invalid_choice(self):
  57. f = models.CharField(choices=[("a", "A"), ("b", "B")])
  58. msg = "Value 'not a' is not a valid choice."
  59. with self.assertRaisesMessage(ValidationError, msg):
  60. f.clean("not a", None)
  61. def test_enum_choices_cleans_valid_string(self):
  62. f = models.CharField(choices=self.Choices, max_length=1)
  63. self.assertEqual(f.clean("c", None), "c")
  64. def test_enum_choices_invalid_input(self):
  65. f = models.CharField(choices=self.Choices, max_length=1)
  66. msg = "Value 'a' is not a valid choice."
  67. with self.assertRaisesMessage(ValidationError, msg):
  68. f.clean("a", None)
  69. def test_charfield_raises_error_on_empty_input(self):
  70. f = models.CharField(null=False)
  71. msg = "This field cannot be null."
  72. with self.assertRaisesMessage(ValidationError, msg):
  73. f.clean(None, None)
  74. def test_callable_choices(self):
  75. def get_choices():
  76. return {str(i): f"Option {i}" for i in range(3)}
  77. f = models.CharField(max_length=1, choices=get_choices)
  78. for i in get_choices():
  79. with self.subTest(i=i):
  80. self.assertEqual(i, f.clean(i, None))
  81. with self.assertRaises(ValidationError):
  82. f.clean("A", None)
  83. with self.assertRaises(ValidationError):
  84. f.clean("3", None)