Embedly unit tests

pull/287/head
Tom Talbot 2014-06-04 16:19:34 +01:00
rodzic c4e7848264
commit 3b8ca10fdb
3 zmienionych plików z 107 dodań i 5 usunięć

Wyświetl plik

@ -1,3 +1,4 @@
# For coverage and PEP8 linting
coverage==3.7.1
flake8==2.1.0
mock==1.0.1

Wyświetl plik

@ -16,10 +16,14 @@ import json
class EmbedNotFoundException(Exception): pass
class EmbedlyException(Exception): pass
class AccessDeniedEmbedlyException(EmbedlyException): pass
# This is here for unit testing. It is used if Embedly is not installed.
class MockEmbedly():
def oembed():
pass
# Pinched from django 1.7 source code.
# TODO: Replace this with "from django.utils.module_loading import import_string" when django 1.7 is released
@ -45,14 +49,16 @@ def import_string(dotted_path):
def embedly(url, max_width=None, key=None):
from embedly import Embedly
# Get embedly key
if key is None:
key = settings.EMBEDLY_KEY
# Get embedly client
client = Embedly(key=settings.EMBEDLY_KEY)
try:
from embedly import Embedly
# Get embedly client
client = Embedly(key)
except ImportError:
client = MockEmbedly(key)
# Call embedly
if max_width is not None:

Wyświetl plik

@ -1,7 +1,23 @@
try:
from embedly import Embedly
patch_me = 'embedly.Embedly.oembed'
except ImportError:
patch_me = 'wagtail.wagtailembeds.embeds.MockEmbedly.oembed'
from django.test import TestCase
from django.test.client import Client
from mock import patch
from wagtail.tests.utils import login
from wagtail.wagtailembeds import get_embed
from wagtail.wagtailembeds.embeds import (
embedly,
EmbedNotFoundException,
EmbedlyException,
AccessDeniedEmbedlyException,
MockEmbedly
)
class TestEmbeds(TestCase):
@ -73,3 +89,82 @@ class TestChooser(TestCase):
self.assertEqual(r.status_code, 200)
# TODO: Test submitting
class TestEmbedly(TestCase):
@patch(patch_me)
def test_embedly_oembed_called_with_correct_arguments(self, oembed):
oembed.return_value = {'type': 'photo',
'url': 'http://www.example.com'}
embedly('http://www.example.com', key='foo')
oembed.assert_called_with('http://www.example.com', better=False)
embedly('http://www.example.com', max_width=100, key='foo')
oembed.assert_called_with('http://www.example.com', maxwidth=100, better=False)
@patch(patch_me)
def test_embedly_errors(self, oembed):
oembed.return_value = {'type': 'photo',
'url': 'http://www.example.com',
'error': True,
'error_code': 401}
self.assertRaises(AccessDeniedEmbedlyException,
embedly, 'http://www.example.com', key='foo')
oembed.return_value['error_code'] = 403
self.assertRaises(AccessDeniedEmbedlyException,
embedly, 'http://www.example.com', key='foo')
oembed.return_value['error_code'] = 404
self.assertRaises(EmbedNotFoundException,
embedly, 'http://www.example.com', key='foo')
oembed.return_value['error_code'] = 999
self.assertRaises(EmbedlyException, embedly,
'http://www.example.com', key='foo')
@patch(patch_me)
def test_embedly_html_conversion(self, oembed):
oembed.return_value = {'type': 'photo',
'url': 'http://www.example.com'}
result = embedly('http://www.example.com', key='foo')
self.assertEqual(result['html'], '<img src="http://www.example.com" />')
oembed.return_value = {'type': 'something else',
'html': '<foo>bar</foo>'}
result = embedly('http://www.example.com', key='foo')
self.assertEqual(result['html'], '<foo>bar</foo>')
@patch(patch_me)
def test_embedly_return_value(self, oembed):
oembed.return_value = {'type': 'something else',
'html': '<foo>bar</foo>'}
result = embedly('http://www.example.com', key='foo')
self.assertEqual(result, {
'title': '',
'author_name': '',
'provider_name': '',
'type': 'something else',
'thumbnail_url': None,
'width': None,
'height': None,
'html': '<foo>bar</foo>'})
oembed.return_value = {'type': 'something else',
'author_name': 'Alice',
'provider_name': 'Bob',
'title': 'foo',
'thumbnail_url': 'http://www.example.com',
'width': 100,
'height': 100,
'html': '<foo>bar</foo>'}
result = embedly('http://www.example.com', key='foo')
self.assertEqual(result, {'type': 'something else',
'author_name': 'Alice',
'provider_name': 'Bob',
'title': 'foo',
'thumbnail_url': 'http://www.example.com',
'width': 100,
'height': 100,
'html': '<foo>bar</foo>'})