From ea60b7bc7464808e34e3cb0aac04455fdd8545eb Mon Sep 17 00:00:00 2001 From: Matthias Kestenholz Date: Sat, 23 Mar 2019 17:04:39 +0100 Subject: [PATCH] Removed redundant model field choices tests. --- tests/choices/__init__.py | 0 tests/choices/models.py | 25 ------------------------- tests/choices/tests.py | 25 ------------------------- tests/model_fields/models.py | 2 ++ tests/model_fields/tests.py | 6 ++++++ 5 files changed, 8 insertions(+), 50 deletions(-) delete mode 100644 tests/choices/__init__.py delete mode 100644 tests/choices/models.py delete mode 100644 tests/choices/tests.py diff --git a/tests/choices/__init__.py b/tests/choices/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/tests/choices/models.py b/tests/choices/models.py deleted file mode 100644 index 37ef8daf606..00000000000 --- a/tests/choices/models.py +++ /dev/null @@ -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 diff --git a/tests/choices/tests.py b/tests/choices/tests.py deleted file mode 100644 index 88b8bf7fe2d..00000000000 --- a/tests/choices/tests.py +++ /dev/null @@ -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) diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py index 93e424a2aa8..cbf3ec26ea6 100644 --- a/tests/model_fields/models.py +++ b/tests/model_fields/models.py @@ -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) diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py index f55175aa9e9..bb82c7b93da 100644 --- a/tests/model_fields/tests.py +++ b/tests/model_fields/tests.py @@ -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.