Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test a custom filter in admin.py

I've the following filter in my admin.py file:

class parentCategoryFilter(admin.SimpleListFilter):
    title = 'parent category'
    parameter_name = 'parent_category'

    def lookups(self, request, model_admin):
        first_level_categories = model_admin.get_queryset(request).filter(parent_category__isnull=True)

        if first_level_categories:

            lookups = (('none', 'None'),)

            for first_level_category in first_level_categories:
                lookups += ((first_level_category.id, first_level_category.name),)

            return lookups

    def queryset(self, request, queryset):
        if self.value() == 'none':
            return queryset.filter(parent_category__isnull=True)
        elif self.value():
            try:
                return queryset.filter(parent_category=int(self.value()))
            except (ValueError, TypeError):
                return queryset.none()
        else:
            pass

And I want to test it in my tests.py file, however while trying to instantiate the class it starts asking me about 5 __init__ parameters. Is it possible to test this filter functionality?

like image 415
Ben Avatar asked May 25 '13 15:05

Ben


2 Answers

Thanks to karthikr and lots of reading I've came up with the following solution (using Django's Pool example):

The filter method is defined as follows:

class WasPublishedRecentlyFilter(admin.SimpleListFilter):
    title = 'Was published recently'
    parameter_name = 'published_recently'

    def lookups(self, request, model_admin):
        return (
            ('yes', 'Yes',),
            ('no', 'No',),
        )

    def queryset(self, request, queryset):
        if self.value() == 'yes':
            #filter logic
        elif self.value() == 'no':
            #filter logic
        elif self.value():
            return queryset.none() 

And the test for the filter:

def test_filter(self):
    Poll.objects.create(question='Sup?', pub_date=timezone.now())
    Poll.objects.create(question='How you doing?', pub_date=timezone.now() - datetime.timedelta(days=1))

    filter = admin.WasPublishedRecentlyFilter(None, {'published_recently':'yes'}, Poll, admin.PollAdmin)
    poll = filter.queryset(None, Poll.objects.all())[0]
    self.assertEqual(poll.question, 'Sup?')

    filter = admin.WasPublishedRecentlyFilter(None, {'published_recently':'no'}, Poll, admin.PollAdmin)
    poll = filter.queryset(None, Poll.objects.all())[0]
    self.assertEqual(poll.question, 'How you doing?')

And the output:

Creating test database for alias 'default'...
.
----------------------------------------------------------------------
Ran 1 test in 0.124s

OK
Destroying test database for alias 'default'...
like image 198
Ben Avatar answered Nov 14 '22 11:11

Ben


The 5 parameters that __init__ needs is from admin.SimpleListFilter whose __init__ is defined as: (Source)

def __init__(self, request, params, model, model_admin):

You can pass in those parameters to test your filter

like image 44
karthikr Avatar answered Nov 14 '22 12:11

karthikr