Commits

Andy Mikhailenko  committed 75e02ef

Added tests for filtering by custom field of related model.

  • Participants
  • Parent commits 5812922

Comments (0)

Files changed (1)

File view_shortcuts/tests.py

 # -*- coding: utf-8 -*-
 
 __doc__="""
->>> c1 = Category.objects.create(title='News')
->>> c2 = Category.objects.create(title='Misc')
+>>> c1 = Category.objects.create(title='News', slug='news')
+>>> c2 = Category.objects.create(title='Misc', slug='misc')
 >>> a1 = Author.objects.create(name='John')
 >>> a2 = Author.objects.create(name='Mary')
 >>> a3 = Author.objects.create(name='joe')
 >>> qs = Story.objects.all()
 >>> from view_shortcuts.filters import FilterList, facet, RelationFilter
 >>> filter_settings = (
-...     facet('category'),
-...     facet('author__id', 'author', RelationFilter), # redundant manual setting
+...     facet('category__slug', 'category'),
+...     facet('author'),
 ...     facet('status'),
 ...     facet('paid')
 ... )
 [<Story: s1>, <Story: s2>]
 >>> filters.object_list    # intersection between the two
 [<Story: s1>]
+>>> request = mock_request(category='news')
+>>> filters = FilterList(request, qs, filter_settings)
+>>> filters
+[<RelationFilter "category": True>, <RelationFilter "author": False>, <AllValuesFilter "status": False>, <BooleanFilter "paid": False>]
+>>> filters.active
+[<RelationFilter "category": True>]
+>>> filters.object_list
+[<Story: s1>, <Story: s2>]
 
 # Test custom filters
 
 from django.core.handlers.wsgi import WSGIRequest
 from django.core.urlresolvers import reverse
 from django.db.models import BooleanField, CharField, ForeignKey, IntegerField, \
-                             ManyToManyField, Model, TextField
+                             ManyToManyField, Model, SlugField, TextField
 from django.utils.translation import ugettext_lazy as _
 
 
 
 class Category(Model):
     title = CharField(max_length=255)
+    slug     = SlugField()
 
     __unicode__ = lambda s: s.title