mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
Fixed #5405 -- Added admindocs support for reStructured text in model docstrings
Thanks elvard and gkmngrgn for work on the patch and Markus H. for review.
This commit is contained in:
@@ -291,3 +291,110 @@ class TestModelDetailView(AdminDocsTestCase):
|
||||
|
||||
fields = response.context_data.get('fields')
|
||||
self.assertEqual(len(fields), 2)
|
||||
|
||||
def test_model_docstring_renders_correctly(self):
|
||||
summary = (
|
||||
'<h2 class="subhead"><p>Stores information about a person, related to <a class="reference external" '
|
||||
'href="/admindocs/models/myapp.company/">myapp.Company</a>.</p></h2>'
|
||||
)
|
||||
subheading = '<p><strong>Notes</strong></p>'
|
||||
body = '<p>Use <tt class="docutils literal">save_changes()</tt> when saving this object.</p>'
|
||||
model_body = (
|
||||
'<dl class="docutils"><dt><tt class="'
|
||||
'docutils literal">company</tt></dt><dd>Field storing <a class="'
|
||||
'reference external" href="/admindocs/models/myapp.company/">'
|
||||
'myapp.Company</a> where the person works.</dd></dl>'
|
||||
)
|
||||
self.assertContains(self.response, 'DESCRIPTION')
|
||||
self.assertContains(self.response, summary, html=True)
|
||||
self.assertContains(self.response, subheading, html=True)
|
||||
self.assertContains(self.response, body, html=True)
|
||||
self.assertContains(self.response, model_body, html=True)
|
||||
|
||||
|
||||
@unittest.skipUnless(utils.docutils_is_available, "no docutils installed.")
|
||||
class TestUtils(AdminDocsTestCase):
|
||||
"""
|
||||
This __doc__ output is required for testing. I copied this example from
|
||||
`admindocs` documentation. (TITLE)
|
||||
|
||||
Display an individual :model:`myapp.MyModel`.
|
||||
|
||||
**Context**
|
||||
|
||||
``RequestContext``
|
||||
|
||||
``mymodel``
|
||||
An instance of :model:`myapp.MyModel`.
|
||||
|
||||
**Template:**
|
||||
|
||||
:template:`myapp/my_template.html` (DESCRIPTION)
|
||||
|
||||
some_metadata: some data
|
||||
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.docstring = self.__doc__
|
||||
|
||||
def test_trim_docstring(self):
|
||||
trim_docstring_output = utils.trim_docstring(self.docstring)
|
||||
trimmed_docstring = (
|
||||
'This __doc__ output is required for testing. I copied this '
|
||||
'example from\n`admindocs` documentation. (TITLE)\n\n'
|
||||
'Display an individual :model:`myapp.MyModel`.\n\n'
|
||||
'**Context**\n\n``RequestContext``\n\n``mymodel``\n'
|
||||
' An instance of :model:`myapp.MyModel`.\n\n'
|
||||
'**Template:**\n\n:template:`myapp/my_template.html` '
|
||||
'(DESCRIPTION)\n\nsome_metadata: some data'
|
||||
)
|
||||
self.assertEqual(trim_docstring_output, trimmed_docstring)
|
||||
|
||||
def test_parse_docstring(self):
|
||||
title, description, metadata = utils.parse_docstring(self.docstring)
|
||||
docstring_title = (
|
||||
'This __doc__ output is required for testing. I copied this example from\n'
|
||||
'`admindocs` documentation. (TITLE)'
|
||||
)
|
||||
docstring_description = (
|
||||
'Display an individual :model:`myapp.MyModel`.\n\n'
|
||||
'**Context**\n\n``RequestContext``\n\n``mymodel``\n'
|
||||
' An instance of :model:`myapp.MyModel`.\n\n'
|
||||
'**Template:**\n\n:template:`myapp/my_template.html` '
|
||||
'(DESCRIPTION)'
|
||||
)
|
||||
self.assertEqual(title, docstring_title)
|
||||
self.assertEqual(description, docstring_description)
|
||||
self.assertEqual(metadata, {'some_metadata': 'some data'})
|
||||
|
||||
def test_title_output(self):
|
||||
title, description, metadata = utils.parse_docstring(self.docstring)
|
||||
title_output = utils.parse_rst(title, 'model', 'model:admindocs')
|
||||
self.assertIn('TITLE', title_output)
|
||||
|
||||
title_rendered = (
|
||||
'<p>This __doc__ output is required for testing. I copied this '
|
||||
'example from\n<a class="reference external" '
|
||||
'href="/admindocs/models/admindocs/">admindocs</a> documentation. '
|
||||
'(TITLE)</p>\n'
|
||||
)
|
||||
self.assertHTMLEqual(title_output, title_rendered)
|
||||
|
||||
def test_description_output(self):
|
||||
title, description, metadata = utils.parse_docstring(self.docstring)
|
||||
description_output = utils.parse_rst(description, 'model', 'model:admindocs')
|
||||
|
||||
description_rendered = (
|
||||
'<p>Display an individual <a class="reference external" '
|
||||
'href="/admindocs/models/myapp.mymodel/">myapp.MyModel</a>.</p>\n'
|
||||
'<p><strong>Context</strong></p>\n<p><tt class="docutils literal">'
|
||||
'RequestContext</tt></p>\n<dl class="docutils">\n<dt><tt class="'
|
||||
'docutils literal">mymodel</tt></dt>\n<dd>An instance of <a class="'
|
||||
'reference external" href="/admindocs/models/myapp.mymodel/">'
|
||||
'myapp.MyModel</a>.</dd>\n</dl>\n<p><strong>Template:</strong></p>'
|
||||
'\n<p><a class="reference external" href="/admindocs/templates/'
|
||||
'myapp/my_template.html/">myapp/my_template.html</a> (DESCRIPTION)'
|
||||
'</p>\n'
|
||||
)
|
||||
self.assertHTMLEqual(description_output, description_rendered)
|
||||
|
||||
Reference in New Issue
Block a user