PageRenderTime 93ms CodeModel.GetById 51ms app.highlight 12ms RepoModel.GetById 18ms app.codeStats 0ms

/django/contrib/flatpages/tests/templatetags.py

https://code.google.com/p/mango-py/
Python | 134 lines | 121 code | 13 blank | 0 comment | 3 complexity | 0aa31653cdfdd9fc3fd888fd90b51246 MD5 | raw file
  1import os
  2from django.conf import settings
  3from django.contrib.auth.models import AnonymousUser, User
  4from django.template import Template, Context, TemplateSyntaxError
  5from django.test import TestCase
  6
  7class FlatpageTemplateTagTests(TestCase):
  8    fixtures = ['sample_flatpages']
  9    urls = 'django.contrib.flatpages.tests.urls'
 10
 11    def setUp(self):
 12        self.old_MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
 13        flatpage_middleware_class = 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
 14        if flatpage_middleware_class not in settings.MIDDLEWARE_CLASSES:
 15            settings.MIDDLEWARE_CLASSES += (flatpage_middleware_class,)
 16        self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
 17        settings.TEMPLATE_DIRS = (
 18            os.path.join(
 19                os.path.dirname(__file__),
 20                'templates'
 21            ),
 22        )
 23        self.me = User.objects.create_user('testuser', 'test@example.com', 's3krit')
 24
 25    def tearDown(self):
 26        settings.MIDDLEWARE_CLASSES = self.old_MIDDLEWARE_CLASSES
 27        settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
 28
 29    def test_get_flatpages_tag(self):
 30        "The flatpage template tag retrives unregistered prefixed flatpages by default"
 31        out = Template(
 32                "{% load flatpages %}"
 33                "{% get_flatpages as flatpages %}"
 34                "{% for page in flatpages %}"
 35                "{{ page.title }},"
 36                "{% endfor %}"
 37            ).render(Context())
 38        self.assertEqual(out, "A Flatpage,A Nested Flatpage,")
 39
 40    def test_get_flatpages_tag_for_anon_user(self):
 41        "The flatpage template tag retrives unregistered flatpages for an anonymous user"
 42        out = Template(
 43                "{% load flatpages %}"
 44                "{% get_flatpages for anonuser as flatpages %}"
 45                "{% for page in flatpages %}"
 46                "{{ page.title }},"
 47                "{% endfor %}"
 48            ).render(Context({
 49                'anonuser': AnonymousUser()
 50            }))
 51        self.assertEqual(out, "A Flatpage,A Nested Flatpage,")
 52
 53    def test_get_flatpages_tag_for_user(self):
 54        "The flatpage template tag retrives all flatpages for an authenticated user"
 55        out = Template(
 56                "{% load flatpages %}"
 57                "{% get_flatpages for me as flatpages %}"
 58                "{% for page in flatpages %}"
 59                "{{ page.title }},"
 60                "{% endfor %}"
 61            ).render(Context({
 62                'me': self.me
 63            }))
 64        self.assertEqual(out, "A Flatpage,A Nested Flatpage,Sekrit Nested Flatpage,Sekrit Flatpage,")
 65
 66    def test_get_flatpages_with_prefix(self):
 67        "The flatpage template tag retrives unregistered prefixed flatpages by default"
 68        out = Template(
 69                "{% load flatpages %}"
 70                "{% get_flatpages '/location/' as location_flatpages %}"
 71                "{% for page in location_flatpages %}"
 72                "{{ page.title }},"
 73                "{% endfor %}"
 74            ).render(Context())
 75        self.assertEqual(out, "A Nested Flatpage,")
 76
 77    def test_get_flatpages_with_prefix_for_anon_user(self):
 78        "The flatpage template tag retrives unregistered prefixed flatpages for an anonymous user"
 79        out = Template(
 80                "{% load flatpages %}"
 81                "{% get_flatpages '/location/' for anonuser as location_flatpages %}"
 82                "{% for page in location_flatpages %}"
 83                "{{ page.title }},"
 84                "{% endfor %}"
 85            ).render(Context({
 86                'anonuser': AnonymousUser()
 87            }))
 88        self.assertEqual(out, "A Nested Flatpage,")
 89
 90    def test_get_flatpages_with_prefix_for_user(self):
 91        "The flatpage template tag retrive prefixed flatpages for an authenticated user"
 92        out = Template(
 93                "{% load flatpages %}"
 94                "{% get_flatpages '/location/' for me as location_flatpages %}"
 95                "{% for page in location_flatpages %}"
 96                "{{ page.title }},"
 97                "{% endfor %}"
 98            ).render(Context({
 99                'me': self.me
100            }))
101        self.assertEqual(out, "A Nested Flatpage,Sekrit Nested Flatpage,")
102
103    def test_get_flatpages_with_variable_prefix(self):
104        "The prefix for the flatpage template tag can be a template variable"
105        out = Template(
106                "{% load flatpages %}"
107                "{% get_flatpages location_prefix as location_flatpages %}"
108                "{% for page in location_flatpages %}"
109                "{{ page.title }},"
110                "{% endfor %}"
111            ).render(Context({
112                'location_prefix': '/location/'
113            }))
114        self.assertEqual(out, "A Nested Flatpage,")
115
116    def test_parsing_errors(self):
117        "There are various ways that the flatpages template tag won't parse"
118        render = lambda t: Template(t).render(Context())
119
120        self.assertRaises(TemplateSyntaxError, render,
121                          "{% load flatpages %}{% get_flatpages %}")
122        self.assertRaises(TemplateSyntaxError, render,
123                          "{% load flatpages %}{% get_flatpages as %}")
124        self.assertRaises(TemplateSyntaxError, render,
125                          "{% load flatpages %}{% get_flatpages cheesecake flatpages %}")
126        self.assertRaises(TemplateSyntaxError, render,
127                          "{% load flatpages %}{% get_flatpages as flatpages asdf%}")
128        self.assertRaises(TemplateSyntaxError, render,
129                          "{% load flatpages %}{% get_flatpages cheesecake user as flatpages %}")
130        self.assertRaises(TemplateSyntaxError, render,
131                          "{% load flatpages %}{% get_flatpages for user as flatpages asdf%}")
132        self.assertRaises(TemplateSyntaxError, render,
133                          "{% load flatpages %}{% get_flatpages prefix for user as flatpages asdf%}")
134