Skip to content

Commit a8e2576

Browse files
josernestodavilaDávila Pantoja, José Ernesto
authored andcommitted
Improvements to the test suite.
- Added mocks for the Vimeo and Embedly API calls. - Removed extra argument from the return value of the `render_html` method for the `Vimeo` provider.
1 parent aef6a95 commit a8e2576

10 files changed

Lines changed: 46 additions & 32 deletions

.coverage

7.15 KB
Binary file not shown.

magicembed/providers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ def __init__(self, url, size=(640, 480)):
4949

5050
def render_video(self):
5151
html = '''<iframe title="Vimeo video player" width="%d" height="%d" src="http://player.vimeo.com/video/%s" frameborder="0" allowfullscreen></iframe>''' # noqa
52-
return html % (self.video_id, self.size[0],
53-
self.size[1], self.video_id)
52+
return html % (self.size[0], self.size[1], self.video_id)
5453

5554
def render_thumbnail(self, link_to="#"):
5655
api_response = json.loads(urllib.urlopen(self.api_url).read())
-137 Bytes
Loading

magicembed/tests/coverage/errors.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<body>
4545

4646
<div id="content-header">
47-
<h1>Test Coverage Report: error packages and modules</h1><p>Generated: Wed 2015-08-05 15:26 CST</p></div>
47+
<h1>Test Coverage Report: error packages and modules</h1><p>Generated: Sat 2015-08-29 16:25 CST</p></div>
4848
<div id="result-list">
4949
<p> <code>django_coverage.utils.module_tools.find_or_load_module</code> had
5050
problems importing these packages and modules:

magicembed/tests/coverage/excludes.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<body>
4545

4646
<div id="content-header">
47-
<h1>Test Coverage Report: excluded packages and modules</h1><p>Generated: Wed 2015-08-05 15:26 CST</p></div>
47+
<h1>Test Coverage Report: excluded packages and modules</h1><p>Generated: Sat 2015-08-29 16:25 CST</p></div>
4848
<div id="result-list">
4949
<p> These packages and modules were excluded from the coverage analysis in
5050
<code>django.conf.settings.COVERAGE_MODULE_EXCLUDES</code> or they do

magicembed/tests/coverage/index.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116

117117
<div id="content-header">
118118
<h1>Test Coverage Report</h1>
119-
<p>Generated: Wed 2015-08-05 15:26 CST</p>
119+
<p>Generated: Sat 2015-08-29 16:25 CST</p>
120120
<p><img src="coverage_status.png"></p>
121121
</div>
122122

@@ -139,9 +139,9 @@ <h1>Test Coverage Report</h1>
139139
<tr>
140140
<td class="module-name">Total</td>
141141
<td>72</td>
142-
<td>47</td>
142+
<td>48</td>
143143
<td>10</td>
144-
<td>75.8%</td>
144+
<td>77.4%</td>
145145
</tr>
146146
</tfoot>
147147
<tbody>
@@ -156,9 +156,9 @@ <h1>Test Coverage Report</h1>
156156
<tr>
157157
<td class="module-name"><a href="modules/magicembed.providers.html">magicembed.providers</a></td>
158158
<td>59</td>
159-
<td>47</td>
159+
<td>48</td>
160160
<td>6</td>
161-
<td class="normal">88.7%</td>
161+
<td class="normal">90.6%</td>
162162
</tr>
163163

164164
<tr>

magicembed/tests/coverage/modules/magicembed.__init__.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ <h1>
129129
1 total statements,
130130
<span class="normal">100.0% covered</span>
131131
</h1>
132-
<p>Generated: Wed 2015-08-05 15:26 CST</p>
133-
<p>Source file: /Users/oscarmcm/Code/Django/django-magicembed/magicembed/__init__.py</p>
132+
<p>Generated: Sat 2015-08-29 16:25 CST</p>
133+
<p>Source file: /home/jdavila/workspace/django/Proyectos/django-magicembed/magicembed/__init__.py</p>
134134
<p>
135135
Stats:
136136
<span class="executed">0 executed</span>,

magicembed/tests/coverage/modules/magicembed.providers.html

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,16 @@
128128
<h1>
129129
<span id="module-name">magicembed.providers</span>:
130130
59 total statements,
131-
<span class="normal">88.7% covered</span>
131+
<span class="normal">90.6% covered</span>
132132
</h1>
133-
<p>Generated: Wed 2015-08-05 15:26 CST</p>
134-
<p>Source file: /Users/oscarmcm/Code/Django/django-magicembed/magicembed/providers.py</p>
133+
<p>Generated: Sat 2015-08-29 16:25 CST</p>
134+
<p>Source file: /home/jdavila/workspace/django/Proyectos/django-magicembed/magicembed/providers.py</p>
135135
<p>
136136
Stats:
137-
<span class="executed">47 executed</span>,
138-
<span class="missed">6 missed</span>,
137+
<span class="executed">48 executed</span>,
138+
<span class="missed">5 missed</span>,
139139
<span class="excluded">6 excluded</span>,
140-
<span class="ignored">32 ignored</span>
140+
<span class="ignored">31 ignored</span>
141141
</p>
142142
</div>
143143

@@ -177,7 +177,7 @@ <h1>
177177
<li class="executed"><code> self.video_id = parse_qs(qs[1])['v'][0]</code></li>
178178
<li class="ignored"><code></code></li>
179179
<li class="executed"><code> def render_video(self):</code></li>
180-
<li class="executed"><code> html = '''&lt;iframe title="YouTube video player" width="%d" height="%d" src="http://www.youtube.com/embed/%s" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;''' # noqa</code></li>
180+
<li class="executed"><code> html = '''&lt;iframe title="YouTube video player" width="%d" height="%d" src="https://www.youtube.com/embed/%s" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;''' # noqa</code></li>
181181
<li class="executed"><code> return html % (self.size[0], self.size[1], self.video_id)</code></li>
182182
<li class="ignored"><code></code></li>
183183
<li class="executed"><code> def render_thumbnail(self, link_to='#'):</code></li>
@@ -193,9 +193,8 @@ <h1>
193193
<li class="executed"><code> self.api_url = 'http://vimeo.com/api/v2/video/%s.json' % self.video_id</code></li>
194194
<li class="ignored"><code></code></li>
195195
<li class="executed"><code> def render_video(self):</code></li>
196-
<li class="executed"><code> html = '''&lt;iframe src="http://player.vimeo.com/video/%s" width="%d" height="%d" frameborder="0"&gt;&lt;/iframe&gt;&lt;p&gt;&lt;a href="http://vimeo.com/%s"&gt;Das Pop: The Game&lt;/a&gt; from &lt;a href="http://vimeo.com/bigactive"&gt;Big Active&lt;/a&gt; on &lt;a href="http://vimeo.com"&gt;Vimeo&lt;/a&gt;.&lt;/p&gt;''' # noqa</code></li>
197-
<li class="executed"><code> return html % (self.video_id, self.size[0],</code></li>
198-
<li class="ignored"><code> self.size[1], self.video_id)</code></li>
196+
<li class="executed"><code> html = '''&lt;iframe title="Vimeo video player" width="%d" height="%d" src="http://player.vimeo.com/video/%s" frameborder="0" allowfullscreen&gt;&lt;/iframe&gt;''' # noqa</code></li>
197+
<li class="executed"><code> return html % (self.size[0], self.size[1], self.video_id)</code></li>
199198
<li class="ignored"><code></code></li>
200199
<li class="executed"><code> def render_thumbnail(self, link_to="#"):</code></li>
201200
<li class="executed"><code> api_response = json.loads(urllib.urlopen(self.api_url).read())</code></li>
@@ -216,14 +215,14 @@ <h1>
216215
<li class="executed"><code> return self._call_api()['html']</code></li>
217216
<li class="ignored"><code></code></li>
218217
<li class="executed"><code> def render_thumbnail(self):</code></li>
219-
<li class="missed"><code> return self._call_api()['thumbnail_url']</code></li>
218+
<li class="executed"><code> return self._call_api()['thumbnail_url']</code></li>
220219
<li class="ignored"><code></code></li>
221220
<li class="executed"><code> def _call_api(self):</code></li>
222221
<li class="executed"><code> try:</code></li>
223222
<li class="executed"><code> data = json.loads(urllib.urlopen(self.api_url).read())</code></li>
224-
<li class="executed"><code> except ImproperlyConfigured:</code></li>
223+
<li class="missed"><code> except ImproperlyConfigured:</code></li>
225224
<li class="missed"><code> raise ImproperlyConfigured("Please set the Embedly api key correctly")</code></li>
226-
<li class="missed"><code> return data</code></li>
225+
<li class="executed"><code> return data</code></li>
227226
<li class="ignored"><code></code></li>
228227
<li class="ignored"><code></code></li>
229228
<li class="executed"><code>def get_provider(url, size=None):</code></li>

magicembed/tests/coverage/modules/magicembed.templatetags.magicembed_tags.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ <h1>
129129
12 total statements,
130130
<span class="critical">0.0% covered</span>
131131
</h1>
132-
<p>Generated: Wed 2015-08-05 15:26 CST</p>
133-
<p>Source file: /Users/oscarmcm/Code/Django/django-magicembed/magicembed/templatetags/magicembed_tags.py</p>
132+
<p>Generated: Sat 2015-08-29 16:25 CST</p>
133+
<p>Source file: /home/jdavila/workspace/django/Proyectos/django-magicembed/magicembed/templatetags/magicembed_tags.py</p>
134134
<p>
135135
Stats:
136136
<span class="executed">0 executed</span>,

magicembed/tests/test.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,48 @@
11
# -*- coding: utf-8 -*-
22
import unittest
33

4+
from mock import MagicMock, patch
5+
46
from magicembed.providers import (Youtube, Vimeo, Embedly, get_provider)
57

68

79
class ProvidersTest(unittest.TestCase):
810
def testYoutube(self):
911
video = 'http://www.youtube.com/watch?v=693m7iCh-TE'
1012
yt = Youtube(video, (640, 510))
11-
embed = '''<iframe title="YouTube video player" width="640" height="510" src="http://www.youtube.com/embed/693m7iCh-TE" frameborder="0" allowfullscreen></iframe>'''
13+
embed = '''<iframe title="YouTube video player" width="640" height="510" src="https://www.youtube.com/embed/693m7iCh-TE" frameborder="0" allowfullscreen></iframe>'''
1214
thumbnail = '''http://img.youtube.com/vi/693m7iCh-TE/0.jpg'''
1315
self.assertEqual(yt.render_video(), embed)
1416
self.assertEqual(yt.render_thumbnail("http://google.com"), thumbnail)
1517

1618
def testVimeo(self):
1719
video = 'http://vimeo.com/21443752'
1820
vimeo = Vimeo(video, (400, 225))
19-
embed = '''<iframe src="http://player.vimeo.com/video/21443752" width="400" height="225" frameborder="0"></iframe><p><a href="http://vimeo.com/21443752">Das Pop: The Game</a> from <a href="http://vimeo.com/bigactive">Big Active</a> on <a href="http://vimeo.com">Vimeo</a>.</p>'''
21+
embed = '''<iframe title="Vimeo video player" width="400" height="225" src="http://player.vimeo.com/video/21443752" frameborder="0" allowfullscreen></iframe>'''
2022
thumbnail = '''http://i.vimeocdn.com/video/137933005_200x150.jpg'''
2123
self.assertEqual(vimeo.render_video(), embed)
22-
self.assertEqual(vimeo.render_thumbnail(), thumbnail)
2324

24-
def testEmbedly(self):
25+
api_call_mock = MagicMock(
26+
return_value=[{ "thumbnail_medium": thumbnail }])
27+
with patch('magicembed.providers.json.loads', api_call_mock):
28+
self.assertEqual(vimeo.render_thumbnail(), thumbnail)
29+
30+
@patch('magicembed.providers.urllib')
31+
def testEmbedly(self, urllib_mock):
2532
blip = Embedly('https://vine.co/v/eHHOtXV5lxT', (600, 400))
2633
embed = '<iframe class="embedly-embed" src="//cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fvine.co%2Fv%2FeHHOtXV5lxT%2Fembed%2Fsimple&url=https%3A%2F%2Fvine.co%2Fv%2FeHHOtXV5lxT&image=https%3A%2F%2Fv.cdn.vine.co%2Fr%2Fvideos%2FB3FA3B51771240096733128749056_39a5c18c0e0.2.1.15446276152990570361.mp4.jpg%3FversionId%3DnM.tB7FoIhn4z059SNsYgmz.2RmLKV4x&key=6d7c04d32bab45e8b7d7e2ad09d10917&type=text%2Fhtml&schema=vine" width="500" height="500" scrolling="no" frameborder="0" allowfullscreen></iframe>'
2734
thumbnail = 'https://v.cdn.vine.co/r/videos/B3FA3B51771240096733128749056_39a5c18c0e0.2.1.15446276152990570361.mp4.jpg?versionId=nM.tB7FoIhn4z059SNsYgmz.2RmLKV4x'
28-
self.assertNotEqual(blip.render_video(), embed)
29-
self.assertEqual(blip.render_thumbnail(), thumbnail)
35+
36+
with patch('magicembed.providers.Embedly._call_api', MagicMock()):
37+
self.assertNotEqual(blip.render_video(), embed)
38+
39+
api_call_mock = MagicMock(
40+
return_value={
41+
"thumbnail_url": thumbnail
42+
}
43+
)
44+
with patch('magicembed.providers.json.loads', api_call_mock):
45+
self.assertEqual(blip.render_thumbnail(), thumbnail)
3046

3147
def test_return_provider(self):
3248
yt = 'http://www.youtube.com/watch?v=693m7iCh-TE'

0 commit comments

Comments
 (0)