Skip to content

Commit

Permalink
use prefetch if django version > 1.4 and updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
justquick committed Apr 19, 2012
1 parent b0b6382 commit 1549809
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 3 additions & 1 deletion actstream/gfk.py
@@ -1,3 +1,4 @@
import django
from django.conf import settings
from django.db.models import Manager
from django.db.models.query import QuerySet, EmptyQuerySet
Expand All @@ -6,7 +7,8 @@
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.generic import GenericForeignKey

USE_PREFETCH = getattr(settings, 'USE_PREFETCH', False)
USE_PREFETCH = getattr(settings, 'USE_PREFETCH',
django.VERSION[0] == 1 and django.VERSION[1] >= 4)
FETCH_RELATIONS = getattr(settings, 'FETCH_RELATIONS', True)


Expand Down
21 changes: 16 additions & 5 deletions actstream/tests.py
Expand Up @@ -16,6 +16,17 @@
from actstream.signals import action
from actstream import settings as actstream_settings

class LTE(int):
def __new__(cls, n):
obj = super(LTE, cls).__new__(cls, n)
obj.n = n
return obj

def __eq__(self, other):
return other <= self.n

def __repr__(self):
return "<= %s" % self.n

class ActivityBaseTestCase(TestCase):
actstream_models = ()
Expand Down Expand Up @@ -331,9 +342,9 @@ def test_fetch_generic_relations(self):
n = actions().count()

# compare to fetching only 1 generic relation
self.assertNumQueries(n + 1,
self.assertNumQueries(LTE(n + 1),
lambda: [a.target for a in actions()])
self.assertNumQueries(num_content_types + 2,
self.assertNumQueries(LTE(num_content_types + 2),
lambda: [a.target for a in
actions().fetch_generic_relations('target')])

Expand All @@ -345,9 +356,9 @@ def test_fetch_generic_relations(self):
# compare to fetching all generic relations
num_content_types = len(set(sum(actions().values_list(
'actor_content_type_id', 'target_content_type_id'), ())))
self.assertNumQueries(2 * n + 1,
self.assertNumQueries(LTE(2 * n + 1),
lambda: [(a.actor, a.target) for a in actions()])
self.assertNumQueries(num_content_types + 2,
self.assertNumQueries(LTE(num_content_types + 2),
lambda: [(a.actor, a.target) for a in
actions().fetch_generic_relations()])

Expand All @@ -359,7 +370,7 @@ def test_fetch_generic_relations(self):
action_actor_targets_fetch_generic_all)

# fetch only 1 generic relation, but access both gfks
self.assertNumQueries(n + num_content_types + 2,
self.assertNumQueries(LTE(n + num_content_types + 2),
lambda: [(a.actor, a.target) for a in
actions().fetch_generic_relations('target')])
action_actor_targets_fetch_generic_target = [
Expand Down
4 changes: 2 additions & 2 deletions example_project/settings.py
Expand Up @@ -124,7 +124,7 @@ def user_override(user):

FETCH_RELATIONS = True

USE_PREFETCH = False
USE_PREFETCH = True

DEBUG_TOOLBAR_PANELS = (
'debug_toolbar.panels.template.TemplateDebugPanel',
Expand All @@ -134,4 +134,4 @@ def user_override(user):

DEBUG_TOOLBAR_CONFIG = {
'INTERCEPT_REDIRECTS': False,
}
}

0 comments on commit 1549809

Please sign in to comment.