Removed redundant model field choices tests.

This commit is contained in:
Matthias Kestenholz 2019-03-23 17:04:39 +01:00 committed by Tim Graham
parent 2ee1e1a174
commit ea60b7bc74
5 changed files with 8 additions and 50 deletions

View File

@ -1,25 +0,0 @@
"""
Specifying 'choices' for a field
Most fields take a ``choices`` parameter, which should be a tuple of tuples
specifying which are the valid values for that field.
For each field that has ``choices``, a model instance gets a
``get_fieldname_display()`` method, where ``fieldname`` is the name of the
field. This method returns the "human-readable" value of the field.
"""
from django.db import models
from django.utils.translation import gettext_lazy as _
class Person(models.Model):
GENDER_CHOICES = (
('M', _('Male')),
('F', _('Female')),
)
name = models.CharField(max_length=20)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
def __str__(self):
return self.name

View File

@ -1,25 +0,0 @@
from django.test import TestCase
from .models import Person
class ChoicesTests(TestCase):
def test_display(self):
a = Person.objects.create(name='Adrian', gender='M')
s = Person.objects.create(name='Sara', gender='F')
self.assertEqual(a.gender, 'M')
self.assertEqual(s.gender, 'F')
self.assertEqual(a.get_gender_display(), 'Male')
self.assertEqual(s.get_gender_display(), 'Female')
# If the value for the field doesn't correspond to a valid choice,
# the value itself is provided as a display value.
a.gender = ''
self.assertEqual(a.get_gender_display(), '')
a.gender = 'U'
self.assertEqual(a.get_gender_display(), 'U')
# _get_FIELD_display() coerces lazy strings.
self.assertIsInstance(a.get_gender_display(), str)

View File

@ -12,6 +12,7 @@ from django.db.models.fields.files import ImageField, ImageFieldFile
from django.db.models.fields.related import (
ForeignKey, ForeignObject, ManyToManyField, OneToOneField,
)
from django.utils.translation import gettext_lazy as _
try:
from PIL import Image
@ -46,6 +47,7 @@ class Whiz(models.Model):
)
),
(0, 'Other'),
(5, _('translated')),
)
c = models.IntegerField(choices=CHOICES, null=True)

View File

@ -162,6 +162,12 @@ class GetFieldDisplayTests(SimpleTestCase):
self.assertEqual(Whiz(c='').get_c_display(), '') # Empty value
self.assertEqual(WhizDelayed(c=0).get_c_display(), 'Other') # Delayed choices
def test_get_FIELD_display_translated(self):
"""A translated display value is coerced to str."""
val = Whiz(c=5).get_c_display()
self.assertIsInstance(val, str)
self.assertEqual(val, 'translated')
def test_iterator_choices(self):
"""
get_choices() works with Iterators.