Skip to content

Commit

Permalink
Merge pull request #1149 from ojii/develop
Browse files Browse the repository at this point in the history
#1145 with tests
  • Loading branch information
chrisglass committed Jan 21, 2012
2 parents ca9c886 + 52b5e6e commit 1aa8ab0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
38 changes: 25 additions & 13 deletions cms/tests/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 %}")
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions menus/menu_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 1aa8ab0

Please sign in to comment.