0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-24 10:58:52 +01:00

Merge pull request #375 from mope/even-more-unit-tests

Add unit tests for image formats and embeds template tags
This commit is contained in:
Karl Hobley 2014-06-27 15:32:48 +01:00
commit e8ee5a347b
2 changed files with 134 additions and 2 deletions

View File

@ -7,6 +7,7 @@ try:
except ImportError:
no_embedly = True
from django import template
from django.test import TestCase
from wagtail.tests.utils import WagtailTestUtils, unittest
@ -18,7 +19,8 @@ from wagtail.wagtailembeds.embeds import (
AccessDeniedEmbedlyException,
)
from wagtail.wagtailembeds.embeds import embedly as wagtail_embedly, oembed as wagtail_oembed
from wagtail.wagtailembeds.templatetags.embed_filters import embed as embed_filter
from wagtail.wagtailembeds.templatetags.embed_filters import embedly as embedly_filter
class TestEmbeds(TestCase):
@ -258,3 +260,81 @@ class TestOembed(TestCase):
'height': 'test_height',
'html': 'test_html'
})
class TestEmbedFilter(TestCase):
def setUp(self):
class DummyResponse(object):
def read(self):
return "foo"
self.dummy_response = DummyResponse()
@patch('urllib2.urlopen')
@patch('json.loads')
def test_valid_embed(self, loads, urlopen):
urlopen.return_value = self.dummy_response
loads.return_value = {'type': 'photo',
'url': 'http://www.example.com'}
result = embed_filter('http://www.youtube.com/watch/')
self.assertEqual(result, '<img src="http://www.example.com" />')
@patch('urllib2.urlopen')
@patch('json.loads')
def test_render_filter(self, loads, urlopen):
urlopen.return_value = self.dummy_response
loads.return_value = {'type': 'photo',
'url': 'http://www.example.com'}
temp = template.Template('{% load embed_filters %}{{ "http://www.youtube.com/watch/"|embed }}')
context = template.Context()
result = temp.render(context)
self.assertEqual(result, '<img src="http://www.example.com" />')
@patch('urllib2.urlopen')
@patch('json.loads')
def test_render_filter_nonexistent_type(self, loads, urlopen):
urlopen.return_value = self.dummy_response
loads.return_value = {'type': 'foo',
'url': 'http://www.example.com'}
temp = template.Template('{% load embed_filters %}{{ "http://www.youtube.com/watch/"|embed }}')
context = template.Context()
result = temp.render(context)
self.assertEqual(result, '')
class TestEmbedlyFilter(TestEmbedFilter):
def setUp(self):
class DummyResponse(object):
def read(self):
return "foo"
self.dummy_response = DummyResponse()
@patch('urllib2.urlopen')
@patch('json.loads')
def test_valid_embed(self, loads, urlopen):
urlopen.return_value = self.dummy_response
loads.return_value = {'type': 'photo',
'url': 'http://www.example.com'}
result = embedly_filter('http://www.youtube.com/watch/')
self.assertEqual(result, '<img src="http://www.example.com" />')
@patch('urllib2.urlopen')
@patch('json.loads')
def test_render_filter(self, loads, urlopen):
urlopen.return_value = self.dummy_response
loads.return_value = {'type': 'photo',
'url': 'http://www.example.com'}
temp = template.Template('{% load embed_filters %}{{ "http://www.youtube.com/watch/"|embedly }}')
context = template.Context()
result = temp.render(context)
self.assertEqual(result, '<img src="http://www.example.com" />')
@patch('urllib2.urlopen')
@patch('json.loads')
def test_render_filter_nonexistent_type(self, loads, urlopen):
urlopen.return_value = self.dummy_response
loads.return_value = {'type': 'foo',
'url': 'http://www.example.com'}
temp = template.Template('{% load embed_filters %}{{ "http://www.youtube.com/watch/"|embedly }}')
context = template.Context()
result = temp.render(context)
self.assertEqual(result, '')

View File

@ -1,3 +1,5 @@
from mock import MagicMock
from django.test import TestCase
from django import template
from django.contrib.auth.models import User, Group, Permission
@ -6,7 +8,11 @@ from django.core.files.uploadedfile import SimpleUploadedFile
from wagtail.tests.utils import unittest, WagtailTestUtils
from wagtail.wagtailimages.models import get_image_model
from wagtail.wagtailimages.templatetags import image_tags
from wagtail.wagtailimages.formats import (
Format,
get_image_format,
register_image_format
)
from wagtail.wagtailimages.backends import get_image_backend
from wagtail.wagtailimages.backends.pillow import PillowBackend
@ -419,3 +425,49 @@ class TestImageChooserUploadView(TestCase, WagtailTestUtils):
self.assertTemplateUsed(response, 'wagtailimages/chooser/chooser.js')
# TODO: Test uploading through chooser
class TestFormat(TestCase):
def setUp(self):
# test format
self.format = Format(
'test name',
'test label',
'test classnames',
'test filter spec'
)
# test image
self.image = MagicMock()
self.image.id = 0
def test_editor_attributes(self):
result = self.format.editor_attributes(
self.image,
'test alt text'
)
self.assertEqual(result,
'data-embedtype="image" data-id="0" data-format="test name" data-alt="test alt text" ')
def test_image_to_editor_html(self):
result = self.format.image_to_editor_html(
self.image,
'test alt text'
)
self.assertRegexpMatches(
result,
'<img data-embedtype="image" data-id="0" data-format="test name" data-alt="test alt text" class="test classnames" src="[^"]+" width="1" height="1" alt="test alt text">',
)
def test_image_to_html_no_classnames(self):
self.format.classnames = None
result = self.format.image_to_html(self.image, 'test alt text')
self.assertRegexpMatches(
result,
'<img src="[^"]+" width="1" height="1" alt="test alt text">'
)
self.format.classnames = 'test classnames'
def test_get_image_format(self):
register_image_format(self.format)
result = get_image_format('test name')
self.assertEqual(result, self.format)