Code

Ticket #19671: test_m2m_validator_called-2.diff

File test_m2m_validator_called-2.diff, 2.2 KB (added by claudep, 13 months ago)

Same test updated to master

Line 
1diff --git a/tests/validation/models.py b/tests/validation/models.py
2index db08329..6944293 100644
3--- a/tests/validation/models.py
4+++ b/tests/validation/models.py
5@@ -11,6 +11,10 @@ def validate_answer_to_universe(value):
6     if value != 42:
7         raise ValidationError('This is not the answer to life, universe and everything!', code='not42')
8 
9+def validate_m2m_fied(value):
10+    raise ValidationError('Many to many validator called')
11+
12+
13 class ModelToValidate(models.Model):
14     name = models.CharField(max_length=100)
15     created = models.DateTimeField(default=datetime.now)
16@@ -97,6 +101,9 @@ class GenericIPAddressTestModel(models.Model):
17 class GenericIPAddrUnpackUniqueTest(models.Model):
18     generic_v4unpack_ip = models.GenericIPAddressField(blank=True, unique=True, unpack_ipv4=True)
19 
20+class M2mModel(models.Model):
21+    authors = models.ManyToManyField(Author, validators=[validate_m2m_fied])
22+
23 
24 # A model can't have multiple AutoFields
25 # Refs #12467.
26diff --git a/tests/validation/tests.py b/tests/validation/tests.py
27index 58b7b94..65678b9 100644
28--- a/tests/validation/tests.py
29+++ b/tests/validation/tests.py
30@@ -6,7 +6,7 @@ from django.test import TestCase
31 
32 from . import ValidationTestCase
33 from .models import (Author, Article, ModelToValidate,
34-    GenericIPAddressTestModel, GenericIPAddrUnpackUniqueTest)
35+    GenericIPAddressTestModel, GenericIPAddrUnpackUniqueTest, M2mModel)
36 
37 # Import other tests for this package.
38 from .test_custom_messages import CustomMessagesTest
39@@ -67,6 +67,12 @@ class ArticleForm(forms.ModelForm):
40         model = Article
41         exclude = ['author']
42 
43+
44+class M2mForm(forms.ModelForm):
45+    class Meta:
46+        model = M2mModel
47+
48+
49 class ModelFormsTests(TestCase):
50     def setUp(self):
51         self.author = Author.objects.create(name='Joseph Kocherhans')
52@@ -110,6 +116,11 @@ class ModelFormsTests(TestCase):
53         form = ArticleForm(data, instance=article)
54         self.assertEqual(list(form.errors), ['pub_date'])
55 
56+    def test_m2m_validator_gets_called(self):
57+        # checks if the custom validator on the m2m field authors gets called
58+        form = M2mForm({'authors': [self.author.pk]})
59+        self.assertEqual(list(form.errors), ['authors'])
60+
61 
62 class GenericIPAddressFieldTests(ValidationTestCase):
63