2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2011-07-13 17:35:51 +08:00
|
|
|
|
2019-03-28 17:18:30 +08:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from .models import PersonWithCustomMaxLengths, PersonWithDefaultMaxLengths
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2007-09-20 07:33:57 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
class MaxLengthArgumentsTests(unittest.TestCase):
|
|
|
|
|
2013-10-27 03:15:03 +08:00
|
|
|
def verify_max_length(self, model, field, length):
|
|
|
|
self.assertEqual(model._meta.get_field(field).max_length, length)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2007-09-20 07:33:57 +08:00
|
|
|
def test_default_max_lengths(self):
|
2014-07-01 02:34:45 +08:00
|
|
|
self.verify_max_length(PersonWithDefaultMaxLengths, 'email', 254)
|
2007-09-20 07:33:57 +08:00
|
|
|
self.verify_max_length(PersonWithDefaultMaxLengths, 'vcard', 100)
|
|
|
|
self.verify_max_length(PersonWithDefaultMaxLengths, 'homepage', 200)
|
|
|
|
self.verify_max_length(PersonWithDefaultMaxLengths, 'avatar', 100)
|
|
|
|
|
2008-08-02 12:56:11 +08:00
|
|
|
def test_custom_max_lengths(self):
|
2009-05-08 02:06:22 +08:00
|
|
|
self.verify_max_length(PersonWithCustomMaxLengths, 'email', 250)
|
|
|
|
self.verify_max_length(PersonWithCustomMaxLengths, 'vcard', 250)
|
|
|
|
self.verify_max_length(PersonWithCustomMaxLengths, 'homepage', 250)
|
|
|
|
self.verify_max_length(PersonWithCustomMaxLengths, 'avatar', 250)
|
2007-09-20 07:33:57 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2019-03-28 17:18:30 +08:00
|
|
|
class MaxLengthORMTests(TestCase):
|
2007-09-20 07:33:57 +08:00
|
|
|
|
|
|
|
def test_custom_max_lengths(self):
|
|
|
|
args = {
|
|
|
|
"email": "someone@example.com",
|
|
|
|
"vcard": "vcard",
|
|
|
|
"homepage": "http://example.com/",
|
|
|
|
"avatar": "me.jpg"
|
|
|
|
}
|
|
|
|
|
|
|
|
for field in ("email", "vcard", "homepage", "avatar"):
|
|
|
|
new_args = args.copy()
|
2013-11-03 05:02:56 +08:00
|
|
|
new_args[field] = "X" * 250 # a value longer than any of the default fields could hold.
|
2007-09-20 07:33:57 +08:00
|
|
|
p = PersonWithCustomMaxLengths.objects.create(**new_args)
|
2013-07-01 20:22:27 +08:00
|
|
|
self.assertEqual(getattr(p, field), ("X" * 250))
|