import unittest from django.contrib.admindocs.utils import ( docutils_is_available, parse_docstring, parse_rst, trim_docstring, ) from .tests import AdminDocsTestCase @unittest.skipUnless(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 = 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 = 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 = parse_docstring(self.docstring) title_output = 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 = parse_docstring(self.docstring) description_output = parse_rst(description, 'model', 'model:admindocs') description_rendered = ( 'Display an individual myapp.MyModel.
\n' 'Context
\n' 'RequestContext
\nTemplate:
' '\nmyapp/my_template.html (DESCRIPTION)' '
\n' ) self.assertHTMLEqual(description_output, description_rendered) def test_initial_header_level(self): header = 'should be h3...\n\nHeader\n------\n' output = parse_rst(header, 'header') self.assertIn('reST, interpreted text, default role.
\n' parts = docutils.core.publish_parts(source=source, writer_name="html4css1") self.assertEqual(parts['fragment'], markup)