diff options
author | Mike Fogel <mike@fogel.ca> | 2014-04-11 23:58:56 -0400 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2014-04-30 07:56:52 -0400 |
commit | 7fd1b35ed794e7d0a2a18f6ac168adba25a7de23 (patch) | |
tree | 6faef8b31515c3206b271abbd971f8923248e026 /tests/field_subclassing | |
parent | 8ec388a69da13a8a5cf82604a26abe13be7dd1cb (diff) | |
download | django-7fd1b35ed794e7d0a2a18f6ac168adba25a7de23.tar.gz |
Fixed #22537 -- Add tests and improved docs for field subclass with choices.
Diffstat (limited to 'tests/field_subclassing')
-rw-r--r-- | tests/field_subclassing/fields.py | 5 | ||||
-rw-r--r-- | tests/field_subclassing/models.py | 12 | ||||
-rw-r--r-- | tests/field_subclassing/tests.py | 14 |
3 files changed, 28 insertions, 3 deletions
diff --git a/tests/field_subclassing/fields.py b/tests/field_subclassing/fields.py index d96ce8d873..c2158824df 100644 --- a/tests/field_subclassing/fields.py +++ b/tests/field_subclassing/fields.py @@ -20,6 +20,11 @@ class Small(object): def __str__(self): return '%s%s' % (force_text(self.first), force_text(self.second)) + def __eq__(self, other): + if isinstance(other, self.__class__): + return self.first == other.first and self.second == other.second + return False + class SmallField(six.with_metaclass(models.SubfieldBase, models.Field)): """ diff --git a/tests/field_subclassing/models.py b/tests/field_subclassing/models.py index c2f7e4f66b..3ed465cd7f 100644 --- a/tests/field_subclassing/models.py +++ b/tests/field_subclassing/models.py @@ -5,7 +5,7 @@ Tests for field subclassing. from django.db import models from django.utils.encoding import force_text -from .fields import SmallField, SmallerField, JSONField +from .fields import Small, SmallField, SmallerField, JSONField from django.utils.encoding import python_2_unicode_compatible @@ -22,5 +22,15 @@ class OtherModel(models.Model): data = SmallerField() +class ChoicesModel(models.Model): + SMALL_AB = Small('a', 'b') + SMALL_CD = Small('c', 'd') + SMALL_CHOICES = ( + (SMALL_AB, str(SMALL_AB)), + (SMALL_CD, str(SMALL_CD)), + ) + data = SmallField('small field', choices=SMALL_CHOICES) + + class DataModel(models.Model): data = JSONField() diff --git a/tests/field_subclassing/tests.py b/tests/field_subclassing/tests.py index 5f1dbac8a3..5c695a455c 100644 --- a/tests/field_subclassing/tests.py +++ b/tests/field_subclassing/tests.py @@ -2,12 +2,12 @@ from __future__ import unicode_literals import inspect -from django.core import serializers +from django.core import exceptions, serializers from django.db import connection from django.test import TestCase from .fields import Small, CustomTypedField -from .models import DataModel, MyModel, OtherModel +from .models import ChoicesModel, DataModel, MyModel, OtherModel class CustomField(TestCase): @@ -106,6 +106,16 @@ class CustomField(TestCase): self.assertIn('__module__', data) self.assertEqual(data['__module__'], 'field_subclassing.models') + def test_validation_of_choices_for_custom_field(self): + # a valid choice + o = ChoicesModel.objects.create(data=Small('a', 'b')) + o.full_clean() + + # an invalid choice + o = ChoicesModel.objects.create(data=Small('d', 'e')) + with self.assertRaises(exceptions.ValidationError): + o.full_clean() + class TestDbType(TestCase): |