1
1
from unittest import TestCase
2
2
from sys import version_info
4
from functools import wraps
6
from django.utils.functional import wraps # Python 2.4 fallback.
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
84
90
response = callback(request)
86
92
self.assertEqual(response, ['test2', 'test1'])
94
def test_cache_page_new_style(self):
96
Test that we can call cache_page the new way
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")
105
def test_cache_page_old_style(self):
107
Test that we can call cache_page the old way
109
def my_view(request):
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")
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):
121
return func("test:" + arg)
122
return wraps(func)(wrapper)
124
simple_dec_m = method_decorator(simple_dec)
127
class MethodDecoratorTests(TestCase):
129
Tests for method_decorator
131
def test_method_decorator(self):
137
self.assertEqual("test:hello", Test().say("hello"))