From 7b420367524ad9f29b8bf0284f4b40bd6cfc8b93 Mon Sep 17 00:00:00 2001 From: Zan Anderle Date: Mon, 3 Nov 2014 20:16:28 +0100 Subject: 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. --- tests/admin_docs/models.py | 12 +++++ tests/admin_docs/tests.py | 107 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) (limited to 'tests/admin_docs') diff --git a/tests/admin_docs/models.py b/tests/admin_docs/models.py index 9b8f388f4e..7e8b6c37e8 100644 --- a/tests/admin_docs/models.py +++ b/tests/admin_docs/models.py @@ -18,6 +18,18 @@ class Family(models.Model): class Person(models.Model): + """ + Stores information about a person, related to :model:`myapp.Company`. + + **Notes** + + Use ``save_changes()`` when saving this object. + + ``company`` + Field storing :model:`myapp.Company` where the person works. + + (DESCRIPTION) + """ first_name = models.CharField(max_length=200, help_text="The person's first name") last_name = models.CharField(max_length=200, help_text="The person's last name") company = models.ForeignKey(Company, help_text="place of work") diff --git a/tests/admin_docs/tests.py b/tests/admin_docs/tests.py index 6234834955..2b0f6dc065 100644 --- a/tests/admin_docs/tests.py +++ b/tests/admin_docs/tests.py @@ -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 = ( + '

Stores information about a person, related to myapp.Company.

' + ) + subheading = '

Notes

' + body = '

Use save_changes() when saving this object.

' + model_body = ( + '
company
Field storing ' + 'myapp.Company where the person works.
' + ) + 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 = ( + '

This __doc__ output is required for testing. I copied this ' + 'example from\nadmindocs documentation. ' + '(TITLE)

\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 = ( + '

Display an individual myapp.MyModel.

\n' + '

Context

\n

' + 'RequestContext

\n
\n
mymodel
\n
An instance of ' + 'myapp.MyModel.
\n
\n

Template:

' + '\n

myapp/my_template.html (DESCRIPTION)' + '

\n' + ) + self.assertHTMLEqual(description_output, description_rendered) -- cgit v1.3