forked from jacobian/django-pjax
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
153 lines (122 loc) · 5.28 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Django bootstrap, sigh.
from django.conf import settings; settings.configure()
import djpjax
from django.template.response import TemplateResponse
from django.http import HttpResponse
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"
def test_pjaxtend_default():
resp = view_default_pjaxtend(regular_request)
assert resp.template_name == "template.html"
assert resp.context_data['parent'] == "base.html"
resp = view_default_pjaxtend(pjax_request)
assert resp.template_name == "template.html"
assert resp.context_data['parent'] == "pjax.html"
def test_pjaxtend_default_parent():
resp = view_default_parent_pjaxtend(regular_request)
assert resp.template_name == "template.html"
assert resp.context_data['parent'] == "parent.html"
resp = view_default_parent_pjaxtend(pjax_request)
assert resp.template_name == "template.html"
assert resp.context_data['parent'] == "pjax.html"
def test_pjaxtend_custom_parent():
resp = view_custom_parent_pjaxtend(regular_request)
assert resp.template_name == "template.html"
assert resp.context_data['parent'] == "parent.html"
resp = view_custom_parent_pjaxtend(pjax_request)
assert resp.template_name == "template.html"
assert resp.context_data['parent'] == "parent-pjax.html"
def test_pjaxtend_custom_context():
resp = view_custom_context_pjaxtend(regular_request)
assert resp.template_name == "template.html"
assert resp.context_data['my_parent'] == "parent.html"
resp = view_custom_context_pjaxtend(pjax_request)
assert resp.template_name == "template.html"
assert resp.context_data['my_parent'] == "parent-pjax.html"
def test_handle_httpresponse():
resp = view_httpresponse(regular_request)
assert resp.status_code == 200
# 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"), {})
@djpjax.pjaxtend()
def view_default_pjaxtend(request):
return TemplateResponse(request, "template.html", {})
@djpjax.pjaxtend('parent.html')
def view_default_parent_pjaxtend(request):
return TemplateResponse(request, "template.html", {})
@djpjax.pjaxtend('parent.html', 'parent-pjax.html')
def view_custom_parent_pjaxtend(request):
return TemplateResponse(request, "template.html", {})
@djpjax.pjaxtend('parent.html', 'parent-pjax.html', 'my_parent')
def view_custom_context_pjaxtend(request):
return TemplateResponse(request, "template.html", {})
@djpjax.pjaxtend()
def view_httpresponse(request):
return HttpResponse("This is a test.")
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({})