~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to tests/regressiontests/decorators/tests.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from unittest import TestCase
2
2
from sys import version_info
 
3
try:
 
4
    from functools import wraps
 
5
except ImportError:
 
6
    from django.utils.functional import wraps  # Python 2.4 fallback.
3
7
 
4
 
from django.http import HttpResponse
 
8
from django.http import HttpResponse, HttpRequest
5
9
from django.utils.functional import allow_lazy, lazy, memoize
6
10
from django.views.decorators.http import require_http_methods, require_GET, require_POST
7
11
from django.views.decorators.vary import vary_on_headers, vary_on_cookie
8
12
from django.views.decorators.cache import cache_page, never_cache, cache_control
 
13
from django.utils.decorators import method_decorator
9
14
from django.contrib.auth.decorators import login_required, permission_required, user_passes_test
10
15
from django.contrib.admin.views.decorators import staff_member_required
11
16
 
42
47
fully_decorated = allow_lazy(fully_decorated)
43
48
fully_decorated = lazy(fully_decorated)
44
49
 
 
50
 
45
51
class DecoratorsTest(TestCase):
46
52
 
47
53
    def test_attributes(self):
84
90
        response = callback(request)
85
91
        
86
92
        self.assertEqual(response, ['test2', 'test1'])
87
 
        
 
93
 
 
94
    def test_cache_page_new_style(self):
 
95
        """
 
96
        Test that we can call cache_page the new way
 
97
        """
 
98
        def my_view(request):
 
99
            return "response"
 
100
        my_view_cached = cache_page(123)(my_view)
 
101
        self.assertEqual(my_view_cached(HttpRequest()), "response")
 
102
        my_view_cached2 = cache_page(123, key_prefix="test")(my_view)
 
103
        self.assertEqual(my_view_cached2(HttpRequest()), "response")
 
104
 
 
105
    def test_cache_page_old_style(self):
 
106
        """
 
107
        Test that we can call cache_page the old way
 
108
        """
 
109
        def my_view(request):
 
110
            return "response"
 
111
        my_view_cached = cache_page(my_view, 123)
 
112
        self.assertEqual(my_view_cached(HttpRequest()), "response")
 
113
        my_view_cached2 = cache_page(my_view, 123, key_prefix="test")
 
114
        self.assertEqual(my_view_cached2(HttpRequest()), "response")
 
115
 
 
116
 
 
117
# For testing method_decorator, a decorator that assumes a single argument.
 
118
# We will get type arguments if there is a mismatch in the number of arguments.
 
119
def simple_dec(func):
 
120
    def wrapper(arg):
 
121
        return func("test:" + arg)
 
122
    return wraps(func)(wrapper)
 
123
 
 
124
simple_dec_m = method_decorator(simple_dec)
 
125
 
 
126
 
 
127
class MethodDecoratorTests(TestCase):
 
128
    """
 
129
    Tests for method_decorator
 
130
    """
 
131
    def test_method_decorator(self):
 
132
        class Test(object):
 
133
            @simple_dec_m
 
134
            def say(self, arg):
 
135
                return arg
 
136
 
 
137
        self.assertEqual("test:hello", Test().say("hello"))