コード例 #1
0
    def test_to_internal_value_raises_validation_error_for_empty_list(self):
        instance = fields.ListField(
            child=fields.IntegerField(), allow_empty=False
        )

        with self.assertRaises(ValidationError):
            instance.to_internal_value([])
コード例 #2
0
    def test_get_value_returns_empty_value(self):

        class FakeModelSerializer(object):
            partial = True
            parent = None

        instance = fields.ListField(child=fields.IntegerField())
        instance.bind('test', FakeModelSerializer())
        self.assertEqual(instance.get_value({'key': [1, 2, 3]}), empty)
コード例 #3
0
 def test_to_representation(self):
     instance = fields.ListField(child=fields.IntegerField())
     self.assertEqual(instance.to_representation([1, 2, 3]), [1, 2, 3])
コード例 #4
0
    def test_to_internal_value_raises_validation_error_for_wrong_value(self):
        instance = fields.ListField(child=fields.IntegerField())

        with self.assertRaises(ValidationError):
            instance.to_internal_value([1, 2, 'error'])
コード例 #5
0
 def test_to_internal_value_for_list_with_integer_as_a_string(self):
     instance = fields.ListField(child=fields.IntegerField())
     self.assertEqual(
         instance.to_internal_value(['1', '2', '3']),
         [1, 2, 3]
     )
コード例 #6
0
 def test_to_internal_value_for_integer_list(self):
     instance = fields.ListField(child=fields.IntegerField())
     self.assertEqual(instance.to_internal_value([1, 2, 3]), [1, 2, 3])
コード例 #7
0
 def test_to_internal_value_empty_list(self):
     instance = fields.ListField(child=fields.IntegerField())
     self.assertEqual(instance.to_internal_value([]), [])
コード例 #8
0
 def test_init_with_max_value(self):
     instance = fields.IntegerField(max_value=10)
     self.assertIsNone(instance.min_value)
     self.assertEqual(instance.max_value, 10)
コード例 #9
0
 def test_get_value(self):
     instance = fields.ListField(child=fields.IntegerField())
     instance.bind('test', None)
     self.assertEqual(instance.get_value({'test': [1, 2, 3]}), [1, 2, 3])
コード例 #10
0
    def test_init_raises_assertion_error_for_child_without_source(self):

        with self.assertRaises(AssertionError):
            fields.ListField(child=fields.IntegerField(source='value'))
コード例 #11
0
 def test_to_to_representation(self):
     instance = fields.IntegerField()
     self.assertEqual(instance.to_representation('1'), 1)
コード例 #12
0
    def test_to_internal_value_raises_validate_exception(self):
        instance = fields.IntegerField()

        with self.assertRaises(ValidationError):
            instance.to_internal_value('object')
コード例 #13
0
    def test_to_internal_value_raises_max_string_length_exception(self):
        instance = fields.IntegerField()

        with self.assertRaises(ValidationError):
            data = 'value' * 250
            instance.to_internal_value(data)
コード例 #14
0
 def test_to_internal_value(self):
     instance = fields.IntegerField()
     self.assertEqual(instance.to_internal_value(1), 1)
コード例 #15
0
 def test_init_with_min_and_max_values(self):
     instance = fields.IntegerField(min_value=1, max_value=10)
     self.assertEqual(instance.min_value, 1)
     self.assertEqual(instance.max_value, 10)
コード例 #16
0
 def test_to_representation_for_string_list(self):
     instance = fields.ListField(child=fields.IntegerField())
     self.assertEqual(
         instance.to_representation(['1', '2', '3']),
         [1, 2, 3]
     )
コード例 #17
0
    class FakeSerializer(Serializer):
        default_list_serializer = ListSerializer
        pk = fields.IntegerField()

        def run_validation(self, data=empty):
            return self.to_internal_value(data)
コード例 #18
0
 def test_init_without_borders(self):
     instance = fields.IntegerField()
     self.assertIsNone(instance.max_value)
     self.assertIsNone(instance.min_value)