Skip to content

Commit 878d001

Browse files
author
Ellery Newcomer
committed
add tests for marcgibbons#190
found some unintended behavior
1 parent 40f0678 commit 878d001

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

rest_framework_swagger/compat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def handle_data(self, d):
3333
self.fed.append(d)
3434

3535
def get_data(self):
36-
return ''.join(self.fed)
36+
return ''.join(self.fed) + self.rawdata
3737

3838

3939
def strip_tags(html):

rest_framework_swagger/tests.py

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from rest_framework.routers import DefaultRouter
1818
from rest_framework.viewsets import ModelViewSet
1919
from rest_framework.decorators import api_view
20+
from rest_framework.settings import api_settings
21+
from rest_framework_swagger.compat import strip_tags
2022
import rest_framework
2123

2224
from .decorators import wrapper_to_func, func_to_wrapper
@@ -1919,6 +1921,15 @@ def my_view_mocker2(view):
19191921

19201922
@override_settings(**reST_SETTINGS)
19211923
class RESTDocstringTests(TestCase):
1924+
def setUp(self):
1925+
from rest_framework_swagger.views import get_restructuredtext
1926+
self.view_func = api_settings.VIEW_DESCRIPTION_FUNCTION
1927+
api_settings.VIEW_DESCRIPTION_FUNCTION = get_restructuredtext
1928+
self.assertEqual(get_restructuredtext, api_settings.VIEW_DESCRIPTION_FUNCTION)
1929+
1930+
def tearDown(self):
1931+
api_settings.VIEW_DESCRIPTION_FUNCTION = self.view_func
1932+
19221933
def test_get_summary_empty(self):
19231934
class MyViewSet(ModelViewSet):
19241935
model = User
@@ -1935,6 +1946,7 @@ def test_get_summary_view(self):
19351946
class MyViewSet(ModelViewSet):
19361947
"""
19371948
Oh yes this is reST
1949+
Oh good there are waffles
19381950
============
19391951
"""
19401952
model = User
@@ -1945,7 +1957,7 @@ class MyViewSet(ModelViewSet):
19451957
class_introspector = make_viewset_introspector(MyViewSet)
19461958
introspector = get_introspectors(class_introspector)['create']
19471959
summary = introspector.get_summary()
1948-
self.assertEqual("Oh yes this is reST", summary)
1960+
self.assertEqual("Oh yes this is reST", summary.strip())
19491961

19501962
def test_get_summary_raw(self):
19511963
class MyViewSet(ModelViewSet):
@@ -1964,7 +1976,7 @@ class MyViewSet(ModelViewSet):
19641976
tx = IntrospectorHelper.get_summary(
19651977
introspector.callback,
19661978
'My comment\n---\nomit_serializer: true')
1967-
self.assertEqual("My comment", tx)
1979+
self.assertEqual("My comment", tx.strip())
19681980

19691981
def test_get_yaml_viewset(self):
19701982
class MyViewSet(ModelViewSet):
@@ -2043,3 +2055,61 @@ class MyViewSet(ModelViewSet):
20432055
docs = introspector.get_notes()
20442056
self.assertIn('Oh yes this is reST', docs)
20452057
self.assertIn('Oh yes so is this', docs)
2058+
2059+
2060+
def get_custom_description(view_cls, html=False):
2061+
description = view_cls.__doc__ or ''
2062+
description = description.strip()
2063+
if html:
2064+
description = description.replace('\n', '<tacos />')
2065+
return description
2066+
2067+
2068+
custom_SETTINGS = {
2069+
'REST_FRAMEWORK': {
2070+
'VIEW_DESCRIPTION_FUNCTION': get_custom_description
2071+
}
2072+
}
2073+
2074+
2075+
@override_settings(**custom_SETTINGS)
2076+
class CustomDocstringTests(TestCase):
2077+
def setUp(self):
2078+
self.view_func = api_settings.VIEW_DESCRIPTION_FUNCTION
2079+
api_settings.VIEW_DESCRIPTION_FUNCTION = get_custom_description
2080+
self.assertEqual(get_custom_description, api_settings.VIEW_DESCRIPTION_FUNCTION)
2081+
2082+
def tearDown(self):
2083+
api_settings.VIEW_DESCRIPTION_FUNCTION = self.view_func
2084+
2085+
def test_custom_function_works(self):
2086+
from rest_framework_swagger.introspectors import get_view_description
2087+
s = get_view_description(
2088+
CommentSerializer, html=True,
2089+
docstring="hiya bad boy\ntacos")
2090+
self.assertEqual("hiya bad boy<tacos />tacos", s)
2091+
2092+
def test_get_summary_view(self):
2093+
class MyViewSet(ModelViewSet):
2094+
"""
2095+
Oh yes this is reST
2096+
Oh good there are waffles
2097+
============
2098+
"""
2099+
model = User
2100+
serializer_class = CommentSerializer
2101+
paginate_by = 20
2102+
paginate_by_param = 'page_this_by'
2103+
2104+
class_introspector = make_viewset_introspector(MyViewSet)
2105+
introspector = get_introspectors(class_introspector)['create']
2106+
summary = introspector.get_summary()
2107+
self.assertEqual("Oh yes this is reST", summary)
2108+
2109+
2110+
class TestStripTags(TestCase):
2111+
def test1(self):
2112+
self.assertEqual('tacos', strip_tags('tacos'))
2113+
self.assertEqual('tacos', strip_tags('<p>tacos</p>'))
2114+
self.assertEqual('tacobeans', strip_tags('<p>taco</p>beans'))
2115+
self.assertEqual('tacobeans', strip_tags('taco<p>beans</p>'))

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ deps =
1919
py26-django{1.5,1.6}-drf{2.3.8,2.4.3,3.0.0,3.0.1}: Markdown==2.1.1
2020
py26-django{1.5,1.6}-drf{2.3.8,2.4.3}: importlib==1.0.1
2121
py26-django{1.5,1.6}-drf{2.3.8,2.4.3}: ordereddict==1.1
22+
docutils==0.11
2223
argparse==1.2.1
2324
argh==0.23.2
2425
nose==1.3.0

0 commit comments

Comments
 (0)