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