2016-03-22 09:06:54 +08:00
|
|
|
from django import forms
|
|
|
|
from django.db import models
|
|
|
|
from django.test import SimpleTestCase, TestCase
|
2015-04-16 06:28:49 +08:00
|
|
|
from django.utils.encoding import force_str
|
2009-04-12 13:32:23 +08:00
|
|
|
|
2013-05-03 22:02:10 +08:00
|
|
|
from .models import (
|
2016-03-22 09:06:54 +08:00
|
|
|
Foo, RenamedField, VerboseNameField, Whiz, WhizIter, WhizIterEmpty,
|
2015-01-28 20:35:27 +08:00
|
|
|
)
|
2009-04-12 13:32:23 +08:00
|
|
|
|
2009-05-28 13:46:09 +08:00
|
|
|
|
2016-03-22 09:06:54 +08:00
|
|
|
class BasicFieldTests(TestCase):
|
|
|
|
|
2010-03-07 09:50:58 +08:00
|
|
|
def test_show_hidden_initial(self):
|
|
|
|
"""
|
2016-03-22 09:06:54 +08:00
|
|
|
Fields with choices respect show_hidden_initial as a kwarg to
|
|
|
|
formfield().
|
2010-03-07 09:50:58 +08:00
|
|
|
"""
|
|
|
|
choices = [(0, 0), (1, 1)]
|
|
|
|
model_field = models.Field(choices=choices)
|
|
|
|
form_field = model_field.formfield(show_hidden_initial=True)
|
|
|
|
self.assertTrue(form_field.show_hidden_initial)
|
|
|
|
|
|
|
|
form_field = model_field.formfield(show_hidden_initial=False)
|
|
|
|
self.assertFalse(form_field.show_hidden_initial)
|
|
|
|
|
2011-05-03 19:51:37 +08:00
|
|
|
def test_field_repr(self):
|
|
|
|
"""
|
2016-03-22 09:06:54 +08:00
|
|
|
__repr__() of a field displays its name.
|
2011-05-03 19:51:37 +08:00
|
|
|
"""
|
|
|
|
f = Foo._meta.get_field('a')
|
|
|
|
self.assertEqual(repr(f), '<django.db.models.fields.CharField: a>')
|
|
|
|
f = models.fields.CharField()
|
|
|
|
self.assertEqual(repr(f), '<django.db.models.fields.CharField>')
|
|
|
|
|
2011-08-13 19:53:42 +08:00
|
|
|
def test_field_name(self):
|
|
|
|
"""
|
2016-03-22 09:06:54 +08:00
|
|
|
A defined field name (name="fieldname") is used instead of the model
|
|
|
|
model's attribute name (modelname).
|
2011-08-13 19:53:42 +08:00
|
|
|
"""
|
|
|
|
instance = RenamedField()
|
|
|
|
self.assertTrue(hasattr(instance, 'get_fieldname_display'))
|
|
|
|
self.assertFalse(hasattr(instance, 'get_modelname_display'))
|
|
|
|
|
2012-09-02 00:32:27 +08:00
|
|
|
def test_field_verbose_name(self):
|
|
|
|
m = VerboseNameField
|
2015-02-22 02:27:49 +08:00
|
|
|
for i in range(1, 24):
|
2016-03-22 09:06:54 +08:00
|
|
|
self.assertEqual(m._meta.get_field('field%d' % i).verbose_name, 'verbose field%d' % i)
|
2012-09-02 00:32:27 +08:00
|
|
|
|
|
|
|
self.assertEqual(m._meta.get_field('id').verbose_name, 'verbose pk')
|
|
|
|
|
2013-08-30 11:44:37 +08:00
|
|
|
def test_choices_form_class(self):
|
2016-03-22 09:06:54 +08:00
|
|
|
"""Can supply a custom choices form class to Field.formfield()"""
|
2013-08-30 11:44:37 +08:00
|
|
|
choices = [('a', 'a')]
|
2013-02-24 02:01:38 +08:00
|
|
|
field = models.CharField(choices=choices)
|
2013-08-30 11:44:37 +08:00
|
|
|
klass = forms.TypedMultipleChoiceField
|
|
|
|
self.assertIsInstance(field.formfield(choices_form_class=klass), klass)
|
2013-02-24 02:01:38 +08:00
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def test_field_str(self):
|
|
|
|
f = Foo._meta.get_field('a')
|
2016-03-22 09:06:54 +08:00
|
|
|
self.assertEqual(force_str(f), 'model_fields.Foo.a')
|
2015-02-12 14:28:24 +08:00
|
|
|
|
2016-01-10 07:05:57 +08:00
|
|
|
|
2016-03-22 09:06:54 +08:00
|
|
|
class ChoicesTests(SimpleTestCase):
|
2016-01-10 07:05:57 +08:00
|
|
|
|
2009-04-11 03:54:14 +08:00
|
|
|
def test_choices_and_field_display(self):
|
|
|
|
"""
|
2016-03-22 09:06:54 +08:00
|
|
|
get_choices() interacts with get_FIELD_display() to return the expected
|
|
|
|
values.
|
2009-04-11 03:54:14 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(Whiz(c=1).get_c_display(), 'First') # A nested value
|
|
|
|
self.assertEqual(Whiz(c=0).get_c_display(), 'Other') # A top level value
|
|
|
|
self.assertEqual(Whiz(c=9).get_c_display(), 9) # Invalid value
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(Whiz(c=None).get_c_display()) # Blank value
|
2009-04-11 03:54:14 +08:00
|
|
|
self.assertEqual(Whiz(c='').get_c_display(), '') # Empty value
|
2009-04-12 13:32:23 +08:00
|
|
|
|
2014-07-28 05:39:40 +08:00
|
|
|
def test_iterator_choices(self):
|
|
|
|
"""
|
2016-03-22 09:06:54 +08:00
|
|
|
get_choices() works with Iterators.
|
2014-07-28 05:39:40 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(WhizIter(c=1).c, 1) # A nested value
|
|
|
|
self.assertEqual(WhizIter(c=9).c, 9) # Invalid value
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(WhizIter(c=None).c) # Blank value
|
2014-07-28 05:39:40 +08:00
|
|
|
self.assertEqual(WhizIter(c='').c, '') # Empty value
|
|
|
|
|
|
|
|
def test_empty_iterator_choices(self):
|
|
|
|
"""
|
2016-03-22 09:06:54 +08:00
|
|
|
get_choices() works with empty iterators.
|
2014-07-28 05:39:40 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(WhizIterEmpty(c="a").c, "a") # A nested value
|
|
|
|
self.assertEqual(WhizIterEmpty(c="b").c, "b") # Invalid value
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(WhizIterEmpty(c=None).c) # Blank value
|
2014-07-28 05:39:40 +08:00
|
|
|
self.assertEqual(WhizIterEmpty(c='').c, '') # Empty value
|