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([])
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)
def test_to_representation(self): instance = fields.ListField(child=fields.IntegerField()) self.assertEqual(instance.to_representation([1, 2, 3]), [1, 2, 3])
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'])
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] )
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])
def test_to_internal_value_empty_list(self): instance = fields.ListField(child=fields.IntegerField()) self.assertEqual(instance.to_internal_value([]), [])
def test_init_with_max_value(self): instance = fields.IntegerField(max_value=10) self.assertIsNone(instance.min_value) self.assertEqual(instance.max_value, 10)
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])
def test_init_raises_assertion_error_for_child_without_source(self): with self.assertRaises(AssertionError): fields.ListField(child=fields.IntegerField(source='value'))
def test_to_to_representation(self): instance = fields.IntegerField() self.assertEqual(instance.to_representation('1'), 1)
def test_to_internal_value_raises_validate_exception(self): instance = fields.IntegerField() with self.assertRaises(ValidationError): instance.to_internal_value('object')
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)
def test_to_internal_value(self): instance = fields.IntegerField() self.assertEqual(instance.to_internal_value(1), 1)
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)
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] )
class FakeSerializer(Serializer): default_list_serializer = ListSerializer pk = fields.IntegerField() def run_validation(self, data=empty): return self.to_internal_value(data)
def test_init_without_borders(self): instance = fields.IntegerField() self.assertIsNone(instance.max_value) self.assertIsNone(instance.min_value)