BVB Source Codes

django-rest-framework Show test_description.py Source code

Return Download django-rest-framework: download test_description.py Source code - Download django-rest-framework Source code - Type:.py
  1. # -- coding: utf-8 --
  2.  
  3. from __future__ import unicode_literals
  4.  
  5. from django.test import TestCase
  6. from django.utils.encoding import python_2_unicode_compatible
  7.  
  8. from rest_framework.compat import apply_markdown
  9. from rest_framework.utils.formatting import dedent
  10. from rest_framework.views import APIView
  11.  
  12.  
  13. # We check that docstrings get nicely un-indented.
  14. DESCRIPTION = """an example docstring
  15. ====================
  16.  
  17. * list
  18. * list
  19.  
  20. another header
  21. --------------
  22.  
  23.    code block
  24.  
  25. indented
  26.  
  27. # hash style header #"""
  28.  
  29. # If markdown is installed we also test it's working
  30. # (and that our wrapped forces '=' to h2 and '-' to h3)
  31.  
  32. # We support markdown < 2.1 and markdown >= 2.1
  33. MARKED_DOWN_lt_21 = """<h2>an example docstring</h2>
  34. <ul>
  35. <li>list</li>
  36. <li>list</li>
  37. </ul>
  38. <h3>another header</h3>
  39. <pre><code>code block
  40. </code></pre>
  41. <p>indented</p>
  42. <h2 id="hash_style_header">hash style header</h2>"""
  43.  
  44. MARKED_DOWN_gte_21 = """<h2 id="an-example-docstring">an example docstring</h2>
  45. <ul>
  46. <li>list</li>
  47. <li>list</li>
  48. </ul>
  49. <h3 id="another-header">another header</h3>
  50. <pre><code>code block
  51. </code></pre>
  52. <p>indented</p>
  53. <h2 id="hash-style-header">hash style header</h2>"""
  54.  
  55.  
  56. class TestViewNamesAndDescriptions(TestCase):
  57.     def test_view_name_uses_class_name(self):
  58.         """
  59.        Ensure view names are based on the class name.
  60.        """
  61.         class MockView(APIView):
  62.             pass
  63.         assert MockView().get_view_name() == 'Mock'
  64.  
  65.     def test_view_description_uses_docstring(self):
  66.         """Ensure view descriptions are based on the docstring."""
  67.         class MockView(APIView):
  68.             """an example docstring
  69.            ====================
  70.  
  71.            * list
  72.            * list
  73.  
  74.            another header
  75.            --------------
  76.  
  77.                code block
  78.  
  79.            indented
  80.  
  81.            # hash style header #"""
  82.  
  83.         assert MockView().get_view_description() == DESCRIPTION
  84.  
  85.     def test_view_description_can_be_empty(self):
  86.         """
  87.        Ensure that if a view has no docstring,
  88.        then it's description is the empty string.
  89.        """
  90.         class MockView(APIView):
  91.             pass
  92.         assert MockView().get_view_description() == ''
  93.  
  94.     def test_view_description_can_be_promise(self):
  95.         """
  96.        Ensure a view may have a docstring that is actually a lazily evaluated
  97.        class that can be converted to a string.
  98.  
  99.        See: https://github.com/encode/django-rest-framework/issues/1708
  100.        """
  101.         # use a mock object instead of gettext_lazy to ensure that we can't end
  102.         # up with a test case string in our l10n catalog
  103.         @python_2_unicode_compatible
  104.         class MockLazyStr(object):
  105.             def __init__(self, string):
  106.                 self.s = string
  107.  
  108.             def __str__(self):
  109.                 return self.s
  110.  
  111.         class MockView(APIView):
  112.             __doc__ = MockLazyStr("a gettext string")
  113.  
  114.         assert MockView().get_view_description() == 'a gettext string'
  115.  
  116.     def test_markdown(self):
  117.         """
  118.        Ensure markdown to HTML works as expected.
  119.        """
  120.         if apply_markdown:
  121.             gte_21_match = apply_markdown(DESCRIPTION) == MARKED_DOWN_gte_21
  122.             lt_21_match = apply_markdown(DESCRIPTION) == MARKED_DOWN_lt_21
  123.             assert gte_21_match or lt_21_match
  124.  
  125.  
  126. def test_dedent_tabs():
  127.     result = 'first string\n\nsecond string'
  128.     assert dedent("    first string\n\n    second string") == result
  129.     assert dedent("first string\n\n    second string") == result
  130.     assert dedent("\tfirst string\n\n\tsecond string") == result
  131.     assert dedent("first string\n\n\tsecond string") == result
  132.  
downloadtest_description.py Source code - Download django-rest-framework Source code
Related Source Codes/Software:
lectures - Oxford Deep NLP 2017 course 2017-06-10
algorithms - Minimal examples of data structures and algorithms... 2017-06-10
Awesome-Hacking - A collection of various awesome lists for hackers,... 2017-06-09
lottie-ios - An iOS library to natively render After Effects ve... 2017-06-09
Public-APIs - 2017-06-09
angular-starter - 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11

 Back to top