Skip to content
This repository was archived by the owner on Dec 7, 2023. It is now read-only.

Commit 7a84750

Browse files
authored
Merge pull request #18 from dipcode-software/feat/tests
added tests to all functionalities
2 parents 45860c6 + 94743cc commit 7a84750

File tree

3 files changed

+72
-15
lines changed

3 files changed

+72
-15
lines changed

ajax_views/mixins.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from __future__ import unicode_literals
22

3-
from django.template.loader import render_to_string
4-
from django.http import JsonResponse, Http404
53
from django.conf import settings
4+
from django.http import Http404, JsonResponse
5+
from django.template.loader import render_to_string
66

77

88
class AjaxResponseAction():
@@ -119,13 +119,17 @@ class PartialAjaxMixin(object):
119119
def get_partial_title(self):
120120
return self.partial_title
121121

122-
def render_to_response(self, context, **response_kwargs):
123-
""" Returns the rendered template in JSON format """
122+
def get_context_data(self, **kwargs):
123+
context = super(PartialAjaxMixin, self).get_context_data(**kwargs)
124124
partial_title = self.get_partial_title()
125125
if partial_title:
126126
context.update({
127-
'title': self.get_partial_title()
127+
'title': partial_title
128128
})
129+
return context
130+
131+
def render_to_response(self, context, **response_kwargs):
132+
""" Returns the rendered template in JSON format """
129133
if self.request.is_ajax():
130134
data = {
131135
"content": render_to_string(

ajax_views/tests/test_mixins.py

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
from __future__ import unicode_literals
22

33
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
94

105
from ajax_views.mixins import (
11-
AjaxResponseAction, AjaxResponseMixin, FormAjaxMixin, AjaxResponseStatus,
6+
AjaxResponseAction, AjaxResponseMixin, AjaxResponseStatus, FormAjaxMixin,
127
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
1314

1415

1516
class DummyForm(Form):
@@ -71,9 +72,7 @@ class DummyFormView(FormAjaxMixin, FormView):
7172
""" """
7273
template_name = 'unit.html'
7374
prefix = 'unit'
74-
75-
def get_success_url(self):
76-
return "/example/"
75+
success_url = "/example/"
7776

7877
def setUp(self):
7978
self.view = self.DummyFormView()
@@ -119,11 +118,19 @@ def test_add_prefix_with_prefix(self):
119118
result = self.view.add_prefix({'field_1': 'invalid'}, 'test')
120119
self.assertEqual(result['test-field_1'], "invalid")
121120

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+
122129

123130
class PartialAjaxMixinTest(SimpleTestCase):
124131
""" """
125132

126-
class DummyView(PartialAjaxMixin):
133+
class DummyView(PartialAjaxMixin, TemplateView):
127134
""" """
128135

129136
def get_template_names(self):
@@ -133,6 +140,21 @@ def setUp(self):
133140
self.view = self.DummyView()
134141
self.view.request = MagicMock()
135142

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+
136158
@patch('ajax_views.mixins.render_to_string',
137159
return_value="<html></html>")
138160
def test_render_to_response(self, render_to_string):
@@ -141,3 +163,14 @@ def test_render_to_response(self, render_to_string):
141163
self.assertEqual(content['content'], "<html></html>")
142164
render_to_string.assert_called_with(
143165
"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)

ajax_views/tests/test_views.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from __future__ import unicode_literals
2+
3+
from ajax_views.views import DeleteAjaxView
4+
from django.test import RequestFactory, SimpleTestCase
5+
from mock import Mock, patch
6+
7+
8+
class DeleteAjaxViewTest(SimpleTestCase):
9+
"""docstring for ClassName"""
10+
11+
def setUp(self):
12+
# Every test needs access to the request factory.
13+
self.factory = RequestFactory()
14+
15+
@patch.object(DeleteAjaxView, 'get_object', return_value=Mock())
16+
def test_delete(self, mget_object):
17+
request = self.factory.delete('/unit/')
18+
response = DeleteAjaxView.as_view()(request)
19+
mget_object().delete.assert_called_with()
20+
self.assertEqual(response.status_code, 200)

0 commit comments

Comments
 (0)