Debian Codegraph @

tests/test_description.py

raw ◀ ▶
1 Debian patch touch this file
1 import pytest
2 from django.test import TestCase
3
4 from rest_framework.compat import apply_markdown
5 from rest_framework.utils.formatting import dedent
6 from rest_framework.views import APIView
7
8 # We check that docstrings get nicely un-indented.
9 DESCRIPTION = """an example docstring
10 ====================
11
12 * list
13 * list
14
15 another header
16 --------------
17
18 code block
19
20 indented
21
22 # hash style header #
23
24 ```json
25 [{
26 "alpha": 1,
27 "beta": "this is a string"
28 }]
29 ```"""
30
31
32 # If markdown is installed we also test it's working
33 # (and that our wrapped forces '=' to h2 and '-' to h3)
34 MARKDOWN_DOCSTRING = """<h2 id="an-example-docstring">an example docstring</h2>
35 <ul>
36 <li>list</li>
37 <li>list</li>
38 </ul>
39 <h3 id="another-header">another header</h3>
40 <pre><code>code block
41 </code></pre>
42 <p>indented</p>
43 <h2 id="hash-style-header">hash style header</h2>
•44 <div class="highlight"><pre><span></span><span class="p">[{</span><br /><span class="w"> </span><span class="nt">&quot;alpha&quot;</span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><br /><span class="w"> </span><span class="nt">&quot;beta&quot;</span><span class="p">:</span><span class="w"> </span><span class="s2">&quot;this is a string&quot;</span><br /><span class="p">}]</span><br /></pre></div>
45 <p><br /></p>"""
46
47
48 class TestViewNamesAndDescriptions(TestCase):
49 def test_view_name_uses_class_name(self):
50 """
51 Ensure view names are based on the class name.
52 """
53 class MockView(APIView):
54 pass
55 assert MockView().get_view_name() == 'Mock'
56
57 def test_view_name_uses_name_attribute(self):
58 class MockView(APIView):
59 name = 'Foo'
60 assert MockView().get_view_name() == 'Foo'
61
62 def test_view_name_uses_suffix_attribute(self):
63 class MockView(APIView):
64 suffix = 'List'
65 assert MockView().get_view_name() == 'Mock List'
66
67 def test_view_name_preferences_name_over_suffix(self):
68 class MockView(APIView):
69 name = 'Foo'
70 suffix = 'List'
71 assert MockView().get_view_name() == 'Foo'
72
73 def test_view_description_uses_docstring(self):
74 """Ensure view descriptions are based on the docstring."""
75 class MockView(APIView):
76 """an example docstring
77 ====================
78
79 * list
80 * list
81
82 another header
83 --------------
84
85 code block
86
87 indented
88
89 # hash style header #
90
91 ```json
92 [{
93 "alpha": 1,
94 "beta": "this is a string"
95 }]
96 ```"""
97
98 assert MockView().get_view_description() == DESCRIPTION
99
100 def test_view_description_uses_description_attribute(self):
101 class MockView(APIView):
102 description = 'Foo'
103 assert MockView().get_view_description() == 'Foo'
104
105 def test_view_description_allows_empty_description(self):
106 class MockView(APIView):
107 """Description."""
108 description = ''
109 assert MockView().get_view_description() == ''
110
111 def test_view_description_can_be_empty(self):
112 """
113 Ensure that if a view has no docstring,
114 then it's description is the empty string.
115 """
116 class MockView(APIView):
117 pass
118 assert MockView().get_view_description() == ''
119
120 def test_view_description_can_be_promise(self):
121 """
122 Ensure a view may have a docstring that is actually a lazily evaluated
123 class that can be converted to a string.
124
125 See: https://github.com/encode/django-rest-framework/issues/1708
126 """
127 # use a mock object instead of gettext_lazy to ensure that we can't end
128 # up with a test case string in our l10n catalog
129
130 class MockLazyStr:
131 def __init__(self, string):
132 self.s = string
133
134 def __str__(self):
135 return self.s
136
137 class MockView(APIView):
138 __doc__ = MockLazyStr("a gettext string")
139
140 assert MockView().get_view_description() == 'a gettext string'
141
142 @pytest.mark.skipif(not apply_markdown, reason="Markdown is not installed")
143 def test_markdown(self):
144 """
145 Ensure markdown to HTML works as expected.
146 """
147 assert apply_markdown(DESCRIPTION) == MARKDOWN_DOCSTRING
148
149
150 def test_dedent_tabs():
151 result = 'first string\n\nsecond string'
152 assert dedent(" first string\n\n second string") == result
153 assert dedent("first string\n\n second string") == result
154 assert dedent("\tfirst string\n\n\tsecond string") == result
155 assert dedent("first string\n\n\tsecond string") == result