fixed test on Python3.5

This commit is contained in:
David Smith 2020-10-12 21:27:30 +01:00
parent 529d48cee2
commit 40761b9093

View File

@ -1,161 +1,180 @@
import pytest import sys
from django.test import TestCase
import pytest
from rest_framework.compat import apply_markdown from django.test import TestCase
from rest_framework.utils.formatting import dedent
from rest_framework.views import APIView from rest_framework.compat import apply_markdown
from rest_framework.utils.formatting import dedent
# We check that docstrings get nicely un-indented. from rest_framework.views import APIView
DESCRIPTION = """an example docstring
==================== # We check that docstrings get nicely un-indented.
DESCRIPTION = """an example docstring
* list ====================
* list
* list
another header * list
--------------
another header
code block --------------
indented code block
# hash style header # indented
``` json # hash style header #
[{
"alpha": 1, ``` json
"beta: "this is a string" [{
}] "alpha": 1,
```""" "beta: "this is a string"
}]
```"""
# If markdown is installed we also test it's working
# (and that our wrapped forces '=' to h2 and '-' to h3)
MARKED_DOWN_HILITE = """<h2 id="an-example-docstring">an example docstring</h2> # If markdown is installed we also test it's working
<ul> # (and that our wrapped forces '=' to h2 and '-' to h3)
<li>list</li> MARKDOWN_BASE = """<h2 id="an-example-docstring">an example docstring</h2>
<li>list</li> <ul>
</ul> <li>list</li>
<h3 id="another-header">another header</h3> <li>list</li>
<pre><code>code block </ul>
</code></pre> <h3 id="another-header">another header</h3>
<p>indented</p> <pre><code>code block
<h2 id="hash-style-header">hash style header</h2> </code></pre>
<div class="highlight"><pre><span></span><span class="p">[{</span><br />\ <p>indented</p>
<span class="nt">&quot;alpha&quot;</span><span class="p">:</span>\ <h2 id="hash-style-header">hash style header</h2>%s"""
<span class="mi">1</span><span class="p">,</span><br />\
<span class="nt">&quot;beta: &quot;</span><span class="err">this\ MARKDOWN_gte_33 = """
</span> <span class="err">is</span> <span class="err">a</span> \ <div class="highlight"><pre><span></span><span class="p">[{</span><br />\
<span class="err">string&quot;</span><br /><span class="p">}]</span>\ <span class="nt">&quot;alpha&quot;</span><span class="p">:</span>\
<br /></pre></div> <span class="mi">1</span><span class="p">,</span><br />\
<p><br /></p>""" <span class="nt">&quot;beta: &quot;</span><span class="err">this\
</span> <span class="err">is</span> <span class="err">a</span> \
<span class="err">string&quot;</span><br /><span class="p">}]</span>\
class TestViewNamesAndDescriptions(TestCase): <br /></pre></div>
def test_view_name_uses_class_name(self): <p><br /></p>"""
"""
Ensure view names are based on the class name. MARKDOWN_lt_33 = """
""" <div class="highlight"><pre><span></span><span class="p">[{</span><br />\
class MockView(APIView): <span class="nt">&quot;alpha&quot;</span><span class="p">:</span>\
pass <span class="mi">1</span><span class="p">,</span><br />\
assert MockView().get_view_name() == 'Mock' <span class="nt">&quot;beta: &quot;</span><span class="err">this\
</span> <span class="err">is</span> <span class="err">a</span>\
def test_view_name_uses_name_attribute(self): <span class="err">string&quot;</span><br /><span class="p">}]</span>\
class MockView(APIView): <br /></pre></div>
name = 'Foo'
assert MockView().get_view_name() == 'Foo' <p><br /></p>"""
def test_view_name_uses_suffix_attribute(self):
class MockView(APIView): class TestViewNamesAndDescriptions(TestCase):
suffix = 'List' def test_view_name_uses_class_name(self):
assert MockView().get_view_name() == 'Mock List' """
Ensure view names are based on the class name.
def test_view_name_preferences_name_over_suffix(self): """
class MockView(APIView): class MockView(APIView):
name = 'Foo' pass
suffix = 'List' assert MockView().get_view_name() == 'Mock'
assert MockView().get_view_name() == 'Foo'
def test_view_name_uses_name_attribute(self):
def test_view_description_uses_docstring(self): class MockView(APIView):
"""Ensure view descriptions are based on the docstring.""" name = 'Foo'
class MockView(APIView): assert MockView().get_view_name() == 'Foo'
"""an example docstring
==================== def test_view_name_uses_suffix_attribute(self):
class MockView(APIView):
* list suffix = 'List'
* list assert MockView().get_view_name() == 'Mock List'
another header def test_view_name_preferences_name_over_suffix(self):
-------------- class MockView(APIView):
name = 'Foo'
code block suffix = 'List'
assert MockView().get_view_name() == 'Foo'
indented
def test_view_description_uses_docstring(self):
# hash style header # """Ensure view descriptions are based on the docstring."""
class MockView(APIView):
``` json """an example docstring
[{ ====================
"alpha": 1,
"beta: "this is a string" * list
}] * list
```"""
another header
assert MockView().get_view_description() == DESCRIPTION --------------
def test_view_description_uses_description_attribute(self): code block
class MockView(APIView):
description = 'Foo' indented
assert MockView().get_view_description() == 'Foo'
# hash style header #
def test_view_description_allows_empty_description(self):
class MockView(APIView): ``` json
"""Description.""" [{
description = '' "alpha": 1,
assert MockView().get_view_description() == '' "beta: "this is a string"
}]
def test_view_description_can_be_empty(self): ```"""
"""
Ensure that if a view has no docstring, assert MockView().get_view_description() == DESCRIPTION
then it's description is the empty string.
""" def test_view_description_uses_description_attribute(self):
class MockView(APIView): class MockView(APIView):
pass description = 'Foo'
assert MockView().get_view_description() == '' assert MockView().get_view_description() == 'Foo'
def test_view_description_can_be_promise(self): def test_view_description_allows_empty_description(self):
""" class MockView(APIView):
Ensure a view may have a docstring that is actually a lazily evaluated """Description."""
class that can be converted to a string. description = ''
assert MockView().get_view_description() == ''
See: https://github.com/encode/django-rest-framework/issues/1708
""" def test_view_description_can_be_empty(self):
# use a mock object instead of gettext_lazy to ensure that we can't end """
# up with a test case string in our l10n catalog Ensure that if a view has no docstring,
then it's description is the empty string.
class MockLazyStr: """
def __init__(self, string): class MockView(APIView):
self.s = string pass
assert MockView().get_view_description() == ''
def __str__(self):
return self.s def test_view_description_can_be_promise(self):
"""
class MockView(APIView): Ensure a view may have a docstring that is actually a lazily evaluated
__doc__ = MockLazyStr("a gettext string") class that can be converted to a string.
assert MockView().get_view_description() == 'a gettext string' See: https://github.com/encode/django-rest-framework/issues/1708
"""
@pytest.mark.skipif(not apply_markdown, reason="Markdown is not installed") # use a mock object instead of gettext_lazy to ensure that we can't end
def test_markdown(self): # up with a test case string in our l10n catalog
"""
Ensure markdown to HTML works as expected. class MockLazyStr:
""" def __init__(self, string):
assert apply_markdown(DESCRIPTION) == MARKED_DOWN_HILITE self.s = string
def __str__(self):
def test_dedent_tabs(): return self.s
result = 'first string\n\nsecond string'
assert dedent(" first string\n\n second string") == result class MockView(APIView):
assert dedent("first string\n\n second string") == result __doc__ = MockLazyStr("a gettext string")
assert dedent("\tfirst string\n\n\tsecond string") == result
assert dedent("first string\n\n\tsecond string") == result assert MockView().get_view_description() == 'a gettext string'
@pytest.mark.skipif(not apply_markdown, reason="Markdown is not installed")
def test_markdown(self):
"""
Ensure markdown to HTML works as expected.
"""
# Markdown 3.3 is only supported on Python 3.6 and higher
if sys.version_info >= (3, 6):
assert apply_markdown(DESCRIPTION) == MARKDOWN_BASE % MARKDOWN_gte_33
else:
assert apply_markdown(DESCRIPTION) == MARKDOWN_BASE % MARKDOWN_lt_33
def test_dedent_tabs():
result = 'first string\n\nsecond string'
assert dedent(" first string\n\n second string") == result
assert dedent("first string\n\n second string") == result
assert dedent("\tfirst string\n\n\tsecond string") == result
assert dedent("first string\n\n\tsecond string") == result