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