diff options
| author | Ian Foote | 2013-11-27 11:00:15 +0000 | 
|---|---|---|
| committer | Ian Foote | 2013-11-27 11:06:42 +0000 | 
| commit | 8d09f56061a3ee82e31fb646cfa84484ae525f88 (patch) | |
| tree | c5e51e6b5bac4cc9b0b919022fe26e57c450ed9a /rest_framework/tests | |
| parent | 2484fc914159571a3867c2dae2d9b51314f4581d (diff) | |
| download | django-rest-framework-8d09f56061a3ee82e31fb646cfa84484ae525f88.tar.bz2 | |
Add unittests for ChoiceField metadata.
Rename 'name' to 'display_name'.
Diffstat (limited to 'rest_framework/tests')
| -rw-r--r-- | rest_framework/tests/test_fields.py | 26 | 
1 files changed, 22 insertions, 4 deletions
diff --git a/rest_framework/tests/test_fields.py b/rest_framework/tests/test_fields.py index ab2cceac..5c96bce9 100644 --- a/rest_framework/tests/test_fields.py +++ b/rest_framework/tests/test_fields.py @@ -707,20 +707,21 @@ class ChoiceFieldTests(TestCase):          self.assertEqual(f.choices, models.fields.BLANK_CHOICE_DASH + SAMPLE_CHOICES)      def test_invalid_choice_model(self): -        s = ChoiceFieldModelSerializer(data={'choice' : 'wrong_value'}) +        s = ChoiceFieldModelSerializer(data={'choice': 'wrong_value'})          self.assertFalse(s.is_valid())          self.assertEqual(s.errors,  {'choice': ['Select a valid choice. wrong_value is not one of the available choices.']})          self.assertEqual(s.data['choice'], '')      def test_empty_choice_model(self):          """ -        Test that the 'empty' value is correctly passed and used depending on the 'null' property on the model field. +        Test that the 'empty' value is correctly passed and used depending on +        the 'null' property on the model field.          """ -        s = ChoiceFieldModelSerializer(data={'choice' : ''}) +        s = ChoiceFieldModelSerializer(data={'choice': ''})          self.assertTrue(s.is_valid())          self.assertEqual(s.data['choice'], '') -        s = ChoiceFieldModelWithNullSerializer(data={'choice' : ''}) +        s = ChoiceFieldModelWithNullSerializer(data={'choice': ''})          self.assertTrue(s.is_valid())          self.assertEqual(s.data['choice'], None) @@ -740,6 +741,23 @@ class ChoiceFieldTests(TestCase):          self.assertEqual(f.from_native(''), None)          self.assertEqual(f.from_native(None), None) +    def test_metadata_choices(self): +        """ +        Make sure proper choices are included in the field's metadata. +        """ +        choices = [{'value': v, 'display_name': n} for v, n in SAMPLE_CHOICES] +        f = serializers.ChoiceField(choices=SAMPLE_CHOICES) +        self.assertEqual(f.metadata()['choices'], choices) + +    def test_metadata_choices_not_required(self): +        """ +        Make sure proper choices are included in the field's metadata. +        """ +        choices = [{'value': v, 'display_name': n} +                   for v, n in models.fields.BLANK_CHOICE_DASH + SAMPLE_CHOICES] +        f = serializers.ChoiceField(required=False, choices=SAMPLE_CHOICES) +        self.assertEqual(f.metadata()['choices'], choices) +  class EmailFieldTests(TestCase):      """  | 
