mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-11-04 09:57:55 +03:00 
			
		
		
		
	Add unittests for ChoiceField metadata.
Rename 'name' to 'display_name'.
This commit is contained in:
		
							parent
							
								
									2484fc9141
								
							
						
					
					
						commit
						8d09f56061
					
				| 
						 | 
				
			
			@ -516,7 +516,7 @@ class ChoiceField(WritableField):
 | 
			
		|||
 | 
			
		||||
    def metadata(self):
 | 
			
		||||
        data = super(ChoiceField, self).metadata()
 | 
			
		||||
        data['choices'] = [{'value': v, 'name': n} for v, n in self.choices]
 | 
			
		||||
        data['choices'] = [{'value': v, 'display_name': n} for v, n in self.choices]
 | 
			
		||||
        return data
 | 
			
		||||
 | 
			
		||||
    def validate(self, value):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -714,7 +714,8 @@ class ChoiceFieldTests(TestCase):
 | 
			
		|||
 | 
			
		||||
    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': ''})
 | 
			
		||||
        self.assertTrue(s.is_valid())
 | 
			
		||||
| 
						 | 
				
			
			@ -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):
 | 
			
		||||
    """
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user