forked from jacobian/django-pjax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
96 lines (75 loc) · 3.09 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Django bootstrap, sigh.
from django.conf import settings; settings.configure()
import djpjax
from django.template.response import TemplateResponse
from django.test.client import RequestFactory
from django.views.generic import View
# A couple of request objects - one PJAX, one not.
rf = RequestFactory()
regular_request = rf.get('/')
pjax_request = rf.get('/', HTTP_X_PJAX=True)
# Tests.
def test_pjax_sans_template():
resp = view_sans_pjax_template(regular_request)
assert resp.template_name == "template.html"
resp = view_sans_pjax_template(pjax_request)
assert resp.template_name == "template-pjax.html"
def test_view_with_silly_template():
resp = view_with_silly_template(regular_request)
assert resp.template_name == "silly"
resp = view_with_silly_template(pjax_request)
assert resp.template_name == "silly-pjax"
def test_view_with_pjax_template():
resp = view_with_pjax_template(regular_request)
assert resp.template_name == "template.html"
resp = view_with_pjax_template(pjax_request)
assert resp.template_name == "pjax.html"
def test_view_with_template_tuple():
resp = view_with_template_tuple(regular_request)
assert resp.template_name == ("template.html", "other_template.html")
resp = view_with_template_tuple(pjax_request)
assert resp.template_name == ("template-pjax.html", "other_template-pjax.html")
def test_class_pjax_sans_template():
view = NoPJAXTemplateVew.as_view()
resp = view(regular_request)
assert resp.template_name[0] == "template.html"
resp = view(pjax_request)
assert resp.template_name[0] == "template-pjax.html"
def test_class_with_silly_template():
view = SillyTemplateNameView.as_view()
resp = view(regular_request)
assert resp.template_name[0] == "silly"
resp = view(pjax_request)
assert resp.template_name[0] == "silly-pjax"
def test_class_with_pjax_template():
view = PJAXTemplateView.as_view()
resp = view(regular_request)
assert resp.template_name[0] == "template.html"
resp = view(pjax_request)
assert resp.template_name[0] == "pjax.html"
# The test "views" themselves.
@djpjax.pjax()
def view_sans_pjax_template(request):
return TemplateResponse(request, "template.html", {})
@djpjax.pjax()
def view_with_silly_template(request):
return TemplateResponse(request, "silly", {})
@djpjax.pjax("pjax.html")
def view_with_pjax_template(request):
return TemplateResponse(request, "template.html", {})
@djpjax.pjax()
def view_with_template_tuple(request):
return TemplateResponse(request, ("template.html", "other_template.html"), {})
class NoPJAXTemplateVew(djpjax.PJAXResponseMixin, View):
template_name = 'template.html'
def get(self, request):
return self.render_to_response({})
class SillyTemplateNameView(djpjax.PJAXResponseMixin, View):
template_name = 'silly'
def get(self, request):
return self.render_to_response({})
class PJAXTemplateView(djpjax.PJAXResponseMixin, View):
template_name = 'template.html'
pjax_template_name = 'pjax.html'
def get(self, request):
return self.render_to_response({})