#1145 with tests #1149

Merged
merged 3 commits into from Jan 21, 2012
Jump to file or symbol
Failed to load files and symbols.
+27 −15
Split
View
@@ -10,15 +10,15 @@
from cms.test_utils.util.context_managers import (SettingsOverride,
LanguageOverride)
from cms.test_utils.util.mock import AttributeObject
-from menus.base import NavigationNode
-from menus.menu_pool import menu_pool, _build_nodes_inner_for_one_menu
-from menus.utils import mark_descendants, find_selected, cut_levels
-
from django.conf import settings
from django.contrib.auth.models import AnonymousUser, User, Permission, Group
-from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site
from django.template import Template, TemplateSyntaxError
+from menus.base import NavigationNode
+from menus.menu_pool import menu_pool, _build_nodes_inner_for_one_menu
+from menus.models import CacheKey
+from menus.utils import mark_descendants, find_selected, cut_levels
+
class BaseMenuTest(SettingsOverrideTestCase):
@@ -112,16 +112,26 @@ def test_show_menu(self):
def test_show_menu_num_queries(self):
context = self.get_context()
# test standard show_menu
- with self.assertNumQueries(4):
+ with self.assertNumQueries(5):
"""
- The 4 queries should be:
+ The queries should be:
get all pages
get all page permissions
get all titles
+ get the menu cache key
set the menu cache key
"""
tpl = Template("{% load menu_tags %}{% show_menu %}")
tpl.render(context)
+
+ def test_show_menu_cache_key_leak(self):
+ context = self.get_context()
+ tpl = Template("{% load menu_tags %}{% show_menu %}")
+ self.assertEqual(CacheKey.objects.count(), 0)
+ tpl.render(context)
+ self.assertEqual(CacheKey.objects.count(), 1)
+ tpl.render(context)
+ self.assertEqual(CacheKey.objects.count(), 1)
def test_only_active_tree(self):
context = self.get_context()
@@ -666,12 +676,13 @@ def test_show_submenu_num_queries(self):
page = Page.objects.get(title_set__title='P6')
context = self.get_context(page.get_absolute_url())
# test standard show_menu
- with self.assertNumQueries(4):
+ with self.assertNumQueries(5):
"""
- The 4 queries should be:
+ The queries should be:
get all pages
get all page permissions
get all titles
+ get the menu cache key
set the menu cache key
"""
tpl = Template("{% load menu_tags %}{% show_sub_menu %}")
@@ -691,7 +702,7 @@ def test_not_in_navigation(self):
"""
a = create_page('A', 'nav_playground.html', 'en', published=True,
in_navigation=True, reverse_id='a')
- b =create_page('B', 'nav_playground.html', 'en', parent=a,
+ b = create_page('B', 'nav_playground.html', 'en', parent=a,
published=True, in_navigation=True)
c = create_page('C', 'nav_playground.html', 'en', parent=b,
published=True, in_navigation=True)
@@ -724,18 +735,19 @@ def test_not_in_navigation_num_queries(self):
in_navigation=True, reverse_id='a')
b =create_page('B', 'nav_playground.html', 'en', parent=a,
published=True, in_navigation=True)
- c = create_page('C', 'nav_playground.html', 'en', parent=b,
+ create_page('C', 'nav_playground.html', 'en', parent=b,
published=True, in_navigation=True)
create_page('D', 'nav_playground.html', 'en', parent=self.reload(b),
published=True, in_navigation=False)
with LanguageOverride('en'):
context = self.get_context(a.get_absolute_url())
- with self.assertNumQueries(4):
+ with self.assertNumQueries(5):
"""
- The 4 queries should be:
+ The queries should be:
get all pages
get all page permissions
get all titles
+ get the menu cache key
set the menu cache key
"""
# Actually seems to run:
View
@@ -84,7 +84,7 @@ def clear(self, site_id=None, language=None, all=False):
cache_keys = CacheKey.objects.get_keys()
else:
cache_keys = CacheKey.objects.get_keys(site_id, language)
- to_be_deleted = [obj.key for obj in cache_keys]
+ to_be_deleted = cache_keys.distinct().values_list('key', flat=True)
cache.delete_many(to_be_deleted)
cache_keys.delete()
@@ -140,7 +140,7 @@ def _build_nodes(self, request, site_id):
# the database. It's still cheaper than recomputing every time!
# This way we can selectively invalidate per-site and per-language,
# since the cache shared but the keys aren't
- CacheKey.objects.create(key=key, language=lang, site=site_id)
+ CacheKey.objects.get_or_create(key=key, language=lang, site=site_id)
return final_nodes
def apply_modifiers(self, nodes, request, namespace=None, root_id=None, post_cut=False, breadcrumb=False):