Fix tests, force reevaluate field queryset

This commit is contained in:
Aurélien Delobelle 2017-10-25 14:28:24 +02:00
parent 09290131d5
commit d36a813e15

View file

@ -11,6 +11,11 @@ User = get_user_model()
class KeepUnselectableModelFormMixinTests(TestCase):
def _get_form_cls(self):
"""
We recreate a new form class for each test, because `queryset` may not
be reevaluate and miss the permissions created by `setUp`.
"""
class ExampleForm(KeepUnselectableModelFormMixin, forms.ModelForm):
user_permissions = forms.ModelMultipleChoiceField(
queryset=Permission.objects.filter(
@ -22,31 +27,33 @@ class KeepUnselectableModelFormMixinTests(TestCase):
class Meta:
model = User
fields = ('username', 'user_permissions')
return ExampleForm
def setUp(self):
ct = ContentType.objects.get_for_model(Permission)
self.unselec_perm1 = Permission.objects.create(
content_type=ct, codename='unselec_perm1')
content_type=ct, codename='unselec_perm1', name='Unselectable 1')
self.unselec_perm2 = Permission.objects.create(
content_type=ct, codename='unselec_perm2')
content_type=ct, codename='unselec_perm2', name='Unselectable 2')
# These two perms are the only selectable permissions from
# 'permissions' field of ExampleForm.
self.selec_perm1 = Permission.objects.create(
content_type=ct, codename='selec_perm1')
content_type=ct, codename='selec_perm1', name='Selectable 1')
self.selec_perm2 = Permission.objects.create(
content_type=ct, codename='selec_perm2')
content_type=ct, codename='selec_perm2', name='Selectable 2')
def test_creation(self):
"""
The mixin functions properly when instance is being created.
"""
ExampleForm = self._get_form_cls()
data = {
'username': 'user',
'user_permissions': [self.selec_perm1.pk],
}
form = self.ExampleForm(data)
form = ExampleForm(data)
instance = form.save()
@ -62,11 +69,12 @@ class KeepUnselectableModelFormMixinTests(TestCase):
https://docs.djangoproject.com/en/1.11/topics/forms/modelforms/#the-save-method
"""
ExampleForm = self._get_form_cls()
data = {
'username': 'user',
'user_permissions': [self.selec_perm1.pk],
}
form = self.ExampleForm(data)
form = ExampleForm(data)
instance = form.save(commit=False)
@ -89,11 +97,12 @@ class KeepUnselectableModelFormMixinTests(TestCase):
# Link instance with an unselectable and a selectable permissions.
instance.user_permissions.add(self.unselec_perm1, self.selec_perm2)
ExampleForm = self._get_form_cls()
data = {
'username': 'user',
'user_permissions': [self.selec_perm1.pk],
}
form = self.ExampleForm(data, instance=instance)
form = ExampleForm(data, instance=instance)
instance = form.save()
@ -111,11 +120,12 @@ class KeepUnselectableModelFormMixinTests(TestCase):
# Link instance with an unselectable and a selectable permissions.
instance.user_permissions.add(self.unselec_perm1, self.selec_perm2)
ExampleForm = self._get_form_cls()
data = {
'username': 'changed',
'user_permissions': [self.selec_perm1.pk],
}
form = self.ExampleForm(data, instance=instance)
form = ExampleForm(data, instance=instance)
instance = form.save(commit=False)