python 3.x - Django filter testing -
class businesspartnerfilter(silbasefilter): active = django_filters.booleanfilter( name='date_deactivated', lookup_expr='isnull') parent_name = django_filters.charfilter(name='parent__name') unmapped = django_filters.booleanfilter(method='check_if_unmapped')
i have added field 'unmapped'
above , created method filter
below. can please me write tests filter. i'm stuck.
class meta(object): model = models.businesspartner fields = [ 'name', 'bp_type', 'slade_code', 'parent', 'national_identifier', 'active', 'parent_name', 'unmapped' ] def check_if_unmapped(self, queryset, field, value): if value: exclude_bps = [record.id record in queryset if record.mapped == 0 , record.unmapped == 0] return queryset.exclude(id__in=exclude_bps) return queryset
you can either test filter method in isolation, or test evaluation of filterset.qs
.
to test filter method, don't need initialized filterset
.
qs = businesspartner.objects.all() f = businesspartnerfilter() result = f.check_if_unmapped(qs, 'unmapped', true) # assert result
that said, it's not more difficult initialize filterset
, check .qs
.
qs = businesspartner.objects.all() f = businesspartnerfilter(data={'unmapped': 'true'}, queryset=qs) result = f.qs # assert result
Comments
Post a Comment