Fix tests, force reevaluate field queryset

This commit is contained in:
Aurélien Delobelle 2017-10-25 14:28:24 +02:00
parent a7dbc64e2b
commit 6d90311ae1

View file

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