import mock
from django.core.files import File
from markdown.test_tools import TestCase as MarkdownTestCase
class TestEmbed(MarkdownTestCase):
default_kwargs = {
'extensions': ['uploads.extensions.embed'],
}
maxDiff = None
def test_invalid_pdf(self):
self.assertMarkdownRenders(
self.dedent(
"""
[pdf:hello]
"""
),
self.dedent(
"""
INVALID FILE: hello
"""
),
output_format='html'
)
def test_valid_pdf(self):
with mock.patch('uploads.models.PdfUpload') as pdf_upload_mock:
mock_upload = mock.Mock()
mock_upload.file.url = '/media/pdfs/uploads/url.pdf'
mock_querylist = mock.Mock()
mock_querylist.first.return_value = mock_upload
pdf_upload_mock.objects.filter.return_value = mock_querylist
self.assertMarkdownRenders(
self.dedent(
"""
[pdf:hello]
"""
),
self.dedent(
"""
![](/media/pdfs/uploads/url.pdf.png)
Don't print this page, print the PDF itself!
"""
),
output_format='html'
)
def test_invalid_image(self):
self.assertMarkdownRenders(
self.dedent(
"""
[img:bye]
"""
),
self.dedent(
"""
INVALID FILE: bye
"""
),
output_format='html'
)
def test_valid_image(self):
with mock.patch('uploads.models.ImageUpload') as image_upload_mock:
mock_upload = mock.Mock()
mock_upload.file.url = '/media/images/uploads/bye.png'
mock_querylist = mock.Mock()
mock_querylist.first.return_value = mock_upload
image_upload_mock.objects.filter.return_value = mock_querylist
self.assertMarkdownRenders(
self.dedent(
"""
[img:bye]
"""
),
self.dedent(
"""
"""
),
output_format='html'
)
def test_image_with_caption(self):
with mock.patch('uploads.models.ImageUpload') as image_upload_mock:
mock_upload = mock.Mock()
mock_upload.file.url = '/media/images/uploads/bye.png'
mock_querylist = mock.Mock()
mock_querylist.first.return_value = mock_upload
image_upload_mock.objects.filter.return_value = mock_querylist
self.assertMarkdownRenders(
self.dedent(
"""
[img:bye Bye]
"""
),
self.dedent(
"""
Bye![](/media/images/uploads/bye.png)
"""
),
output_format='html'
)
def test_double_image(self):
with mock.patch('uploads.models.ImageUpload') as image_upload_mock:
mock_upload_1 = mock.Mock()
mock_upload_1.file.url = '/media/images/uploads/hello.png'
mock_upload_2 = mock.Mock()
mock_upload_2.file.url = '/media/images/uploads/bye.png'
mock_querylist = mock.Mock()
mock_querylist.first.side_effect = [mock_upload_1, mock_upload_2]
image_upload_mock.objects.filter.return_value = mock_querylist
self.assertMarkdownRenders(
self.dedent(
"""
[img2:hello bye]
"""
),
self.dedent(
"""
"""
),
output_format='html'
)
def test_images(self):
with mock.patch('uploads.models.ImageUpload') as image_upload_mock:
mock_upload_1 = mock.Mock()
mock_upload_1.file.url = '/media/images/uploads/one.png'
mock_upload_2 = mock.Mock()
mock_upload_2.file.url = '/media/images/uploads/two.png'
mock_upload_3 = mock.Mock()
mock_upload_3.file.url = '/media/images/uploads/three.png'
mock_querylist = mock.Mock()
mock_querylist.first.side_effect = [
mock_upload_1, mock_upload_2, mock_upload_3
]
image_upload_mock.objects.filter.return_value = mock_querylist
self.assertMarkdownRenders(
self.dedent(
"""
[images:one two three]
"""
),
self.dedent(
"""
"""
),
output_format='html'
)
image_upload_mock.objects.filter.assert_any_call(slug='one')
image_upload_mock.objects.filter.assert_any_call(slug='two')
image_upload_mock.objects.filter.assert_any_call(slug='three')
self.assertEqual(image_upload_mock.objects.filter.call_count, 3)