Exemplo n.º 1
0
 def test_nested_array_mismatch(self):
     field = ArrayField(ArrayField(models.IntegerField()))
     field.clean([[1, 2], [3, 4]], None)
     with self.assertRaises(exceptions.ValidationError) as cm:
         field.clean([[1, 2], [3, 4, 5]], None)
     self.assertEqual(cm.exception.code, 'nested_array_mismatch')
     self.assertEqual(cm.exception.messages[0], 'Nested arrays must have the same length.')
Exemplo n.º 2
0
 def test_nested_array_mismatch(self):
     field = ArrayField(ArrayField(models.IntegerField()))
     field.clean([[1, 2], [3, 4]], None)
     with self.assertRaises(exceptions.ValidationError) as cm:
         field.clean([[1, 2], [3, 4, 5]], None)
     self.assertEqual(cm.exception.code, 'nested_array_mismatch')
     self.assertEqual(cm.exception.messages[0], 'Nested arrays must have the same length.')
Exemplo n.º 3
0
 def test_with_validators(self):
     field = ArrayField(models.IntegerField(validators=[validators.MinValueValidator(1)]))
     field.clean([1, 2], None)
     with self.assertRaises(exceptions.ValidationError) as cm:
         field.clean([0], None)
     self.assertEqual(cm.exception.code, 'item_invalid')
     self.assertEqual(cm.exception.messages[0], 'Item 0 in the array did not validate: Ensure this value is greater than or equal to 1.')
Exemplo n.º 4
0
 def test_with_validators(self):
     field = ArrayField(models.IntegerField(validators=[validators.MinValueValidator(1)]))
     field.clean([1, 2], None)
     with self.assertRaises(exceptions.ValidationError) as cm:
         field.clean([0], None)
     self.assertEqual(cm.exception.code, 'item_invalid')
     self.assertEqual(cm.exception.messages[0], 'Item 0 in the array did not validate: Ensure this value is greater than or equal to 1.')
Exemplo n.º 5
0
 def test_with_size(self):
     field = ArrayField(models.IntegerField(), size=3)
     field.clean([1, 2, 3], None)
     with self.assertRaises(exceptions.ValidationError) as cm:
         field.clean([1, 2, 3, 4], None)
     self.assertEqual(
         cm.exception.messages[0],
         'List contains 4 items, it should contain no more than 3.')
Exemplo n.º 6
0
 def test_unbounded(self):
     field = ArrayField(models.IntegerField())
     with self.assertRaises(exceptions.ValidationError) as cm:
         field.clean([1, None], None)
     self.assertEqual(cm.exception.code, 'item_invalid')
     self.assertEqual(
         cm.exception.message % cm.exception.params,
         'Item 1 in the array did not validate: This field cannot be null.')
Exemplo n.º 7
0
 def test_unbounded(self):
     field = ArrayField(models.IntegerField())
     with self.assertRaises(exceptions.ValidationError) as cm:
         field.clean([1, None], None)
     self.assertEqual(cm.exception.code, 'item_invalid')
     self.assertEqual(
         cm.exception.message % cm.exception.params,
         'Item 1 in the array did not validate: This field cannot be null.'
     )
Exemplo n.º 8
0
 def test_with_base_field_error_params(self):
     field = ArrayField(models.CharField(max_length=2))
     with self.assertRaises(exceptions.ValidationError) as cm:
         field.clean(['abc'], None)
     self.assertEqual(len(cm.exception.error_list), 1)
     exception = cm.exception.error_list[0]
     self.assertEqual(
         exception.message,
         'Item 0 in the array did not validate: Ensure this value has at most 2 characters (it has 3).'
     )
     self.assertEqual(exception.code, 'item_invalid')
     self.assertEqual(exception.params, {'nth': 0, 'value': 'abc', 'limit_value': 2, 'show_value': 3})
Exemplo n.º 9
0
 def test_with_base_field_error_params(self):
     field = ArrayField(models.CharField(max_length=2))
     with self.assertRaises(exceptions.ValidationError) as cm:
         field.clean(['abc'], None)
     self.assertEqual(len(cm.exception.error_list), 1)
     exception = cm.exception.error_list[0]
     self.assertEqual(
         exception.message,
         'Item 0 in the array did not validate: Ensure this value has at most 2 characters (it has 3).'
     )
     self.assertEqual(exception.code, 'item_invalid')
     self.assertEqual(exception.params, {'nth': 0, 'value': 'abc', 'limit_value': 2, 'show_value': 3})
Exemplo n.º 10
0
 def test_with_validators(self):
     field = ArrayField(models.IntegerField(validators=[validators.MinValueValidator(1)]))
     field.clean([1, 2], None)
     with self.assertRaises(exceptions.ValidationError) as cm:
         field.clean([0], None)
     self.assertEqual(len(cm.exception.error_list), 1)
     exception = cm.exception.error_list[0]
     self.assertEqual(
         exception.message, "Item 0 in the array did not validate: Ensure this value is greater than or equal to 1."
     )
     self.assertEqual(exception.code, "item_invalid")
     self.assertEqual(exception.params, {"nth": 0, "value": 0, "limit_value": 1, "show_value": 0})
Exemplo n.º 11
0
 def test_with_base_field_error_params(self):
     field = ArrayField(models.CharField(max_length=2))
     with self.assertRaises(exceptions.ValidationError) as cm:
         field.clean(["abc"], None)
     self.assertEqual(len(cm.exception.error_list), 1)
     exception = cm.exception.error_list[0]
     self.assertEqual(
         exception.message,
         "Item 0 in the array did not validate: Ensure this value has at most 2 characters (it has 3).",
     )
     self.assertEqual(exception.code, "item_invalid")
     self.assertEqual(exception.params, {"nth": 0, "value": "abc", "limit_value": 2, "show_value": 3})
Exemplo n.º 12
0
 def test_with_validators(self):
     field = ArrayField(models.IntegerField(validators=[validators.MinValueValidator(1)]))
     field.clean([1, 2], None)
     with self.assertRaises(exceptions.ValidationError) as cm:
         field.clean([0], None)
     self.assertEqual(len(cm.exception.error_list), 1)
     exception = cm.exception.error_list[0]
     self.assertEqual(
         exception.message,
         'Item 0 in the array did not validate: Ensure this value is greater than or equal to 1.'
     )
     self.assertEqual(exception.code, 'item_invalid')
     self.assertEqual(exception.params, {'nth': 0, 'value': 0, 'limit_value': 1, 'show_value': 0})
Exemplo n.º 13
0
 def test_with_base_field_error_params(self):
     field = ArrayField(models.CharField(max_length=2))
     with self.assertRaises(exceptions.ValidationError) as cm:
         field.clean(["abc"], None)
     self.assertEqual(len(cm.exception.error_list), 1)
     exception = cm.exception.error_list[0]
     self.assertEqual(
         exception.message,
         "Item 1 in the array did not validate: Ensure this value has at most 2 "
         "characters (it has 3).",
     )
     self.assertEqual(exception.code, "item_invalid")
     self.assertEqual(
         exception.params,
         {"nth": 1, "value": "abc", "limit_value": 2, "show_value": 3},
     )
Exemplo n.º 14
0
 def test_with_size(self):
     field = ArrayField(models.IntegerField(), size=3)
     field.clean([1, 2, 3], None)
     with self.assertRaises(exceptions.ValidationError) as cm:
         field.clean([1, 2, 3, 4], None)
     self.assertEqual(cm.exception.messages[0], 'List contains 4 items, it should contain no more than 3.')
Exemplo n.º 15
0
 def test_blank_true(self):
     field = ArrayField(models.IntegerField(blank=True, null=True))
     # This should not raise a validation error
     field.clean([1, None], None)
Exemplo n.º 16
0
 def test_blank_true(self):
     field = ArrayField(models.IntegerField(blank=True, null=True))
     # This should not raise a validation error
     field.clean([1, None], None)