more robust tests

This commit is contained in:
Aurélien Delobelle 2017-05-14 22:19:25 +02:00
parent 4ac7b30bdd
commit e0b1db1e1e
2 changed files with 11 additions and 4 deletions

View file

@ -2,7 +2,6 @@
from datetime import timedelta from datetime import timedelta
from decimal import Decimal from decimal import Decimal
from itertools import chain
from django import forms from django import forms
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
@ -145,7 +144,7 @@ class UserGroupForm(forms.ModelForm):
def clean_groups(self): def clean_groups(self):
kfet_groups = self.cleaned_data.get('groups') kfet_groups = self.cleaned_data.get('groups')
other_groups = self.instance.groups.exclude(name__icontains='K-Fêt') other_groups = self.instance.groups.exclude(name__icontains='K-Fêt')
return chain(kfet_groups, other_groups) return list(kfet_groups) + list(other_groups)
class Meta: class Meta:
model = User model = User

View file

@ -28,7 +28,11 @@ class UserGroupFormTests(TestCase):
"""Only K-Fêt groups are selectable.""" """Only K-Fêt groups are selectable."""
form = UserGroupForm(instance=self.user) form = UserGroupForm(instance=self.user)
groups_field = form.fields['groups'] groups_field = form.fields['groups']
self.assertEqual(len(groups_field.choices), len(self.kfet_groups)) self.assertQuerysetEqual(
groups_field.queryset,
[repr(g) for g in self.kfet_groups],
ordered=False,
)
def test_keep_others(self): def test_keep_others(self):
"""User stays in its non-K-Fêt groups.""" """User stays in its non-K-Fêt groups."""
@ -45,4 +49,8 @@ class UserGroupFormTests(TestCase):
form.is_valid() form.is_valid()
form.save() form.save()
self.assertEqual(len(user.groups.all()), 1+len(self.kfet_groups)) self.assertQuerysetEqual(
user.groups.all(),
[repr(g) for g in [self.other_group] + self.kfet_groups],
ordered=False,
)