def test_convert(self, lookup, get_comment_slug): from allura.lib.app_globals import ForgeMarkdown shortlink = mock.Mock(url='/p/project/tool/artifact/') shortlink.ref.artifact.deleted = False lookup.return_value = shortlink get_comment_slug.return_value = 'abc' app = mock.Mock(url='/p/project/tool/') text = """\ # Not A Heading # --- * #100, r2 * ticket:100 * comment:13:ticket:2 * source:test.py@2#L3 Not *strong* or _underlined_.""" expected_html = """\ <div class="markdown_content"><p># Not A Heading #<br> ---<br> * <a href="/p/project/tool/artifact/">#100</a>, <a href="/p/project/tool/artifact/">r2</a><br> * <a href="/p/project/tool/artifact/">ticket:100</a><br> * <a href="/p/project/tool/artifact/#abc">comment:13:ticket:2</a><br> * <a href="/p/project/tool/2/tree/test.py#l3">source:test.py@2#L3</a></p> <p>Not *strong* or _underlined_.</div>""" md = ForgeMarkdown( extensions=[mde.CommitMessageExtension(app), 'nl2br'], output_format='html4') self.assertEqual(md.convert(text), expected_html)
def test_convert(self, lookup, get_comment_slug): from allura.lib.app_globals import ForgeMarkdown shortlink = mock.Mock(url='/p/project/tool/artifact/') shortlink.ref.artifact.deleted = False lookup.return_value = shortlink get_comment_slug.return_value = 'abc' app = mock.Mock(url='/p/project/tool/') text = """\ # Not A Heading # --- * #100, r2 * ticket:100 * comment:13:ticket:2 * source:test.py@2#L3 Not *strong* or _underlined_.""" expected_html = """\ <div class="markdown_content"><p># Not A Heading #<br> ---<br> * <a href=/p/project/tool/artifact/>#100</a>, <a href=/p/project/tool/artifact/>r2</a><br> * <a href=/p/project/tool/artifact/>ticket:100</a><br> * <a href=/p/project/tool/artifact/#abc>comment:13:ticket:2</a><br> * <a href=/p/project/tool/2/tree/test.py#l3>source:test.py@2#L3</a></p> <p>Not *strong* or _underlined_.</div>""" md = ForgeMarkdown( extensions=[mde.CommitMessageExtension(app), 'nl2br'], output_format='html4') self.assertEqual(md.convert(text), expected_html)
def setUp(self): self.md = ForgeMarkdown() self.post = M.Post() self.post.text = u'**bold**' self.expected_html = u'<p><strong>bold</strong></p>'
class TestCachedMarkdown(unittest.TestCase): def setUp(self): self.md = ForgeMarkdown() self.post = M.Post() self.post.text = u'**bold**' self.expected_html = u'<p><strong>bold</strong></p>' def test_bad_source_field_name(self): self.assertRaises(AttributeError, self.md.cached_convert, self.post, 'no_such_field') def test_missing_cache_field(self): delattr(self.post, 'text_cache') html = self.md.cached_convert(self.post, 'text') self.assertEqual(html, self.expected_html) @patch.dict('allura.lib.app_globals.config', markdown_cache_threshold='0') def test_non_ascii(self): self.post.text = u'å∫ç' expected = u'<p>å∫ç</p>' # test with empty cache self.assertEqual(expected, self.md.cached_convert(self.post, 'text')) # test with primed cache self.assertEqual(expected, self.md.cached_convert(self.post, 'text')) @patch.dict('allura.lib.app_globals.config', markdown_cache_threshold='0') def test_empty_cache(self): html = self.md.cached_convert(self.post, 'text') self.assertEqual(html, self.expected_html) self.assertEqual(html, self.post.text_cache.html) self.assertEqual( hashlib.md5(self.post.text).hexdigest(), self.post.text_cache.md5) self.assertTrue(self.post.text_cache.render_time > 0) @patch.dict('allura.lib.app_globals.config', markdown_cache_threshold='0') def test_stale_cache(self): old = self.md.cached_convert(self.post, 'text') self.post.text = u'new, different source text' html = self.md.cached_convert(self.post, 'text') self.assertNotEqual(old, html) self.assertEqual(html, self.post.text_cache.html) self.assertEqual( hashlib.md5(self.post.text).hexdigest(), self.post.text_cache.md5) self.assertTrue(self.post.text_cache.render_time > 0) @patch.dict('allura.lib.app_globals.config', markdown_cache_threshold='0') def test_valid_cache(self): from jinja2 import Markup self.md.cached_convert(self.post, 'text') with patch.object(self.md, 'convert') as convert_func: html = self.md.cached_convert(self.post, 'text') self.assertEqual(html, self.expected_html) self.assertIsInstance(html, Markup) self.assertFalse(convert_func.called) self.post.text = u"text [[macro]] pass" html = self.md.cached_convert(self.post, 'text') self.assertTrue(convert_func.called) @patch.dict('allura.lib.app_globals.config', {}) def test_no_threshold_defined(self): html = self.md.cached_convert(self.post, 'text') self.assertEqual(html, self.expected_html) self.assertIsNone(self.post.text_cache.md5) self.assertIsNone(self.post.text_cache.html) self.assertIsNone(self.post.text_cache.render_time) @patch.dict('allura.lib.app_globals.config', markdown_cache_threshold='foo') def test_invalid_threshold(self): html = self.md.cached_convert(self.post, 'text') self.assertEqual(html, self.expected_html) self.assertIsNone(self.post.text_cache.md5) self.assertIsNone(self.post.text_cache.html) self.assertIsNone(self.post.text_cache.render_time) @patch.dict('allura.lib.app_globals.config', markdown_cache_threshold='99999') def test_render_time_below_threshold(self): html = self.md.cached_convert(self.post, 'text') self.assertEqual(html, self.expected_html) self.assertIsNone(self.post.text_cache.md5) self.assertIsNone(self.post.text_cache.html) self.assertIsNone(self.post.text_cache.render_time) @patch.dict('allura.lib.app_globals.config', {}) def test_all_expected_keys_exist_in_cache(self): self.md.cached_convert(self.post, 'text') required_keys = ['fix7528', 'html', 'md5', 'render_time'] keys = sorted(self.post.text_cache.keys()) self.assertEqual(required_keys, keys)
class TestCachedMarkdown(unittest.TestCase): def setUp(self): self.md = ForgeMarkdown() self.post = M.Post() self.post.text = u'**bold**' self.expected_html = u'<p><strong>bold</strong></p>' def test_bad_source_field_name(self): self.assertRaises(AttributeError, self.md.cached_convert, self.post, 'no_such_field') def test_missing_cache_field(self): delattr(self.post, 'text_cache') html = self.md.cached_convert(self.post, 'text') self.assertEqual(html, self.expected_html) @patch.dict('allura.lib.app_globals.config', markdown_cache_threshold='0') def test_non_ascii(self): self.post.text = u'å∫ç' expected = u'<p>å∫ç</p>' # test with empty cache self.assertEqual(expected, self.md.cached_convert(self.post, 'text')) # test with primed cache self.assertEqual(expected, self.md.cached_convert(self.post, 'text')) @patch.dict('allura.lib.app_globals.config', markdown_cache_threshold='0') def test_empty_cache(self): html = self.md.cached_convert(self.post, 'text') self.assertEqual(html, self.expected_html) self.assertEqual(html, self.post.text_cache.html) self.assertEqual(hashlib.md5(self.post.text).hexdigest(), self.post.text_cache.md5) self.assertTrue(self.post.text_cache.render_time > 0) @patch.dict('allura.lib.app_globals.config', markdown_cache_threshold='0') def test_stale_cache(self): old = self.md.cached_convert(self.post, 'text') self.post.text = u'new, different source text' html = self.md.cached_convert(self.post, 'text') self.assertNotEqual(old, html) self.assertEqual(html, self.post.text_cache.html) self.assertEqual(hashlib.md5(self.post.text).hexdigest(), self.post.text_cache.md5) self.assertTrue(self.post.text_cache.render_time > 0) @patch.dict('allura.lib.app_globals.config', markdown_cache_threshold='0') def test_valid_cache(self): from jinja2 import Markup self.md.cached_convert(self.post, 'text') with patch.object(self.md, 'convert') as convert_func: html = self.md.cached_convert(self.post, 'text') self.assertEqual(html, self.expected_html) self.assertIsInstance(html, Markup) self.assertFalse(convert_func.called) self.post.text = u"text [[macro]] pass" html = self.md.cached_convert(self.post, 'text') self.assertTrue(convert_func.called) @patch.dict('allura.lib.app_globals.config', {}) def test_no_threshold_defined(self): html = self.md.cached_convert(self.post, 'text') self.assertEqual(html, self.expected_html) self.assertIsNone(self.post.text_cache.md5) self.assertIsNone(self.post.text_cache.html) self.assertIsNone(self.post.text_cache.render_time) @patch.dict('allura.lib.app_globals.config', markdown_cache_threshold='foo') def test_invalid_threshold(self): html = self.md.cached_convert(self.post, 'text') self.assertEqual(html, self.expected_html) self.assertIsNone(self.post.text_cache.md5) self.assertIsNone(self.post.text_cache.html) self.assertIsNone(self.post.text_cache.render_time) @patch.dict('allura.lib.app_globals.config', markdown_cache_threshold='99999') def test_render_time_below_threshold(self): html = self.md.cached_convert(self.post, 'text') self.assertEqual(html, self.expected_html) self.assertIsNone(self.post.text_cache.md5) self.assertIsNone(self.post.text_cache.html) self.assertIsNone(self.post.text_cache.render_time) @patch.dict('allura.lib.app_globals.config', {}) def test_all_expected_keys_exist_in_cache(self): self.md.cached_convert(self.post, 'text') required_keys = ['fix7528', 'html', 'md5', 'render_time'] keys = sorted(self.post.text_cache.keys()) self.assertEqual(required_keys, keys)