| 1 |
import unittest |
| 2 |
|
| 3 |
from django.test import TestCase |
| 4 |
|
| 5 |
from .models import PersonWithCustomMaxLengths, PersonWithDefaultMaxLengths |
| 6 |
|
| 7 |
|
| 8 |
class MaxLengthArgumentsTests(unittest.TestCase): |
| 9 |
def verify_max_length(self, model, field, length): |
| 10 |
self.assertEqual(model._meta.get_field(field).max_length, length) |
| 11 |
|
| 12 |
def test_default_max_lengths(self): |
| 13 |
self.verify_max_length(PersonWithDefaultMaxLengths, "email", 254) |
| 14 |
self.verify_max_length(PersonWithDefaultMaxLengths, "vcard", 100) |
| 15 |
self.verify_max_length(PersonWithDefaultMaxLengths, "homepage", 200) |
| 16 |
self.verify_max_length(PersonWithDefaultMaxLengths, "avatar", 100) |
| 17 |
|
| 18 |
def test_custom_max_lengths(self): |
| 19 |
self.verify_max_length(PersonWithCustomMaxLengths, "email", 250) |
| 20 |
self.verify_max_length(PersonWithCustomMaxLengths, "vcard", 250) |
| 21 |
self.verify_max_length(PersonWithCustomMaxLengths, "homepage", 250) |
| 22 |
self.verify_max_length(PersonWithCustomMaxLengths, "avatar", 250) |
| 23 |
|
| 24 |
|
| 25 |
class MaxLengthORMTests(TestCase): |
| 26 |
def test_custom_max_lengths(self): |
| 27 |
args = { |
| 28 |
"email": "[email protected]", |
| 29 |
"vcard": "vcard", |
| 30 |
"homepage": "http://example.com/", |
| 31 |
"avatar": "me.jpg", |
| 32 |
} |
| 33 |
|
| 34 |
for field in ("email", "vcard", "homepage", "avatar"): |
| 35 |
new_args = args.copy() |
| 36 |
new_args[field] = ( |
| 37 |
"X" * 250 |
| 38 |
) # a value longer than any of the default fields could hold. |
| 39 |
p = PersonWithCustomMaxLengths.objects.create(**new_args) |
| 40 |
self.assertEqual(getattr(p, field), ("X" * 250)) |