Paul McLanahan avatar Paul McLanahan committed b090f55

Added tests for PassThroughManager.

Comments (0)

Files changed (3)

model_utils/managers.py

     
     def __init__(self, queryset_cls=None):
         self._queryset_cls = queryset_cls
-        super(PassthroughManager, self).__init__()
+        super(PassThroughManager, self).__init__()
     
     def __getattr__(self, name):
         if name in self._deny_methods:

model_utils/tests/models.py

 from django.utils.translation import ugettext_lazy as _
 
 from model_utils.models import InheritanceCastModel, TimeStampedModel, StatusModel, TimeFramedModel
-from model_utils.managers import QueryManager, manager_from, InheritanceManager
+from model_utils.managers import QueryManager, manager_from, InheritanceManager, PassThroughManager
 from model_utils.fields import SplitField, MonitorField
 from model_utils import Choices
 
     featured = manager_from(PublishedMixin,
                             manager_cls=FeaturedManager,
                             queryset_cls=ByAuthorQuerySet)
+
+class DudeQuerySet(models.query.QuerySet):
+    def abiding(self):
+        return self.filter(abides=True)
+    
+    def rug_positive(self):
+        return self.filter(has_rug=True)
+    
+    def rug_negative(self):
+        return self.filter(has_rug=False)
+    
+    def by_name(self, name):
+        return self.filter(name__iexact=name)
+
+class AbidingManager(PassThroughManager):
+    def get_query_set(self):
+        return DudeQuerySet(self.model, using=self._db).abiding()
+    
+    def get_stats(self):
+        return {
+            'abiding_count': self.count(),
+            'rug_count': self.rug_positive().count(),
+        }
+
+class Dude(models.Model):
+    abides = models.BooleanField(default=True)
+    name = models.CharField(max_length=20)
+    has_rug = models.BooleanField()
+    
+    objects = PassThroughManager(DudeQuerySet)
+    abiders = AbidingManager()

model_utils/tests/tests.py

+try:
+    import cPickle as pickle
+except ImportError:
+    import pickle
+
 from datetime import datetime, timedelta
 
 from django.test import TestCase
     InheritParent, InheritChild, InheritChild2, InheritanceManagerTestParent,
     InheritanceManagerTestChild1, InheritanceManagerTestChild2,
     TimeStamp, Post, Article, Status, StatusPlainTuple, TimeFrame, Monitored,
-    StatusManagerAdded, TimeFrameManagerAdded,  Entry)
+    StatusManagerAdded, TimeFrameManagerAdded, Entry, Dude)
 
 
 class GetExcerptTests(TestCase):
                               NoRendered._meta.get_field,
                               '_body_excerpt')
 
+
 class ManagerFromTests(TestCase):
     def setUp(self):
         Entry.objects.create(author='George', published=True)
 
     def test_cant_reconcile_qs_class(self):
         self.assertRaises(TypeError, Entry.broken.all)
+    
+    def test_queryset_pickling_fails(self):
+        qs = Entry.objects.all()
+        def dump_load():
+            pqs = pickle.dumps(qs)
+            upqs = pickle.loads(pqs)
+        self.assertRaises(pickle.PicklingError, dump_load)
+
+
+class PassThroughManagerTests(TestCase):
+    def setUp(self):
+        Dude.objects.create(name='The Dude', abides=True, has_rug=False)
+        Dude.objects.create(name='His Dudeness', abides=False, has_rug=True)
+        Dude.objects.create(name='Duder', abides=False, has_rug=False)
+        Dude.objects.create(name='El Duderino', abides=True, has_rug=True)
+
+    def test_chaining(self):
+        self.assertEqual(Dude.objects.by_name('Duder').count(), 1)
+        self.assertEqual(Dude.objects.all().by_name('Duder').count(), 1)
+        self.assertEqual(Dude.abiders.rug_positive().count(), 1)
+        self.assertEqual(Dude.abiders.all().rug_positive().count(), 1)
+    
+    def test_manager_only_methods(self):
+        stats = Dude.abiders.get_stats()
+        self.assertEqual(stats['rug_count'], 1)
+        def notonqs():
+            Dude.abiders.all().get_stats()
+        self.assertRaises(AttributeError, notonqs)
+    
+    def test_queryset_pickling(self):
+        qs = Dude.objects.all()
+        saltyqs = pickle.dumps(qs)
+        unqs = pickle.loads(saltyqs)
+        self.assertEqual(unqs.by_name('The Dude').count(), 1)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.