1
1
from __future__ import unicode_literals
2
2
3
3
import json
4
- from mock import MagicMock , patch
5
-
6
- from django .views .generic .edit import FormView
7
- from django .forms import Form
8
- from django .test import SimpleTestCase
9
4
10
5
from ajax_views .mixins import (
11
- AjaxResponseAction , AjaxResponseMixin , FormAjaxMixin , AjaxResponseStatus ,
6
+ AjaxResponseAction , AjaxResponseMixin , AjaxResponseStatus , FormAjaxMixin ,
12
7
PartialAjaxMixin )
8
+ from django .forms import Form
9
+ from django .http import Http404
10
+ from django .test import SimpleTestCase
11
+ from django .views .generic import TemplateView
12
+ from django .views .generic .edit import FormView
13
+ from mock import MagicMock , patch
13
14
14
15
15
16
class DummyForm (Form ):
@@ -71,9 +72,7 @@ class DummyFormView(FormAjaxMixin, FormView):
71
72
""" """
72
73
template_name = 'unit.html'
73
74
prefix = 'unit'
74
-
75
- def get_success_url (self ):
76
- return "/example/"
75
+ success_url = "/example/"
77
76
78
77
def setUp (self ):
79
78
self .view = self .DummyFormView ()
@@ -119,11 +118,19 @@ def test_add_prefix_with_prefix(self):
119
118
result = self .view .add_prefix ({'field_1' : 'invalid' }, 'test' )
120
119
self .assertEqual (result ['test-field_1' ], "invalid" )
121
120
121
+ def test_get_success_url (self ):
122
+ self .view .request .is_ajax .return_value = False
123
+ self .assertEqual (self .view .get_success_url (), '/example/' )
124
+
125
+ def test_get_success_url_with_ajax (self ):
126
+ self .view .request .is_ajax .return_value = True
127
+ self .assertIsNone (self .view .get_success_url ())
128
+
122
129
123
130
class PartialAjaxMixinTest (SimpleTestCase ):
124
131
""" """
125
132
126
- class DummyView (PartialAjaxMixin ):
133
+ class DummyView (PartialAjaxMixin , TemplateView ):
127
134
""" """
128
135
129
136
def get_template_names (self ):
@@ -133,6 +140,21 @@ def setUp(self):
133
140
self .view = self .DummyView ()
134
141
self .view .request = MagicMock ()
135
142
143
+ def test_get_partial_title (self ):
144
+ self .view .partial_title = 'Unit Test'
145
+ result = self .view .get_partial_title ()
146
+ self .assertEqual (result , 'Unit Test' )
147
+
148
+ def test_get_context_data (self ):
149
+ self .view .partial_title = 'Unit'
150
+ result = self .view .get_context_data ()
151
+ self .assertEqual (result ['title' ], 'Unit' )
152
+
153
+ def test_get_context_data_without_partial_title (self ):
154
+ self .view .partial_title = None
155
+ context = self .view .get_context_data ()
156
+ self .assertFalse ('title' in context )
157
+
136
158
@patch ('ajax_views.mixins.render_to_string' ,
137
159
return_value = "<html></html>" )
138
160
def test_render_to_response (self , render_to_string ):
@@ -141,3 +163,14 @@ def test_render_to_response(self, render_to_string):
141
163
self .assertEqual (content ['content' ], "<html></html>" )
142
164
render_to_string .assert_called_with (
143
165
"example.html" , {}, request = self .view .request )
166
+
167
+ def test_render_to_response_without_ajax (self ):
168
+ self .view .request .is_ajax .return_value = False
169
+ with self .assertRaises (Http404 ):
170
+ self .view .render_to_response ({})
171
+
172
+ def test_render_to_response_without_ajax_debug (self ):
173
+ self .view .request .is_ajax .return_value = False
174
+ with self .settings (DEBUG = True ):
175
+ result = self .view .render_to_response ({})
176
+ self .assertEqual (result .status_code , 200 )
0 commit comments