|
1 | 1 | from __future__ import unicode_literals |
| 2 | +import re |
2 | 3 | import json |
3 | 4 |
|
4 | | -try: |
5 | | - import unittest2 as unittest # Python 2.6 |
6 | | -except ImportError: |
7 | | - import unittest |
| 5 | +try: # pragma: no cover |
| 6 | + import unittest2 as unittest # Python 2.6 # pragma: no cover |
| 7 | +except ImportError: # pragma: no cover |
| 8 | + import unittest # pragma: no cover |
8 | 9 |
|
9 | 10 | from embedly.client import Embedly |
10 | 11 | from embedly.models import Url |
11 | 12 |
|
12 | 13 |
|
13 | | -class EmbedlyTestCase(unittest.TestCase): |
14 | | - def setUp(self): |
15 | | - self.key = 'internal' |
16 | | - |
| 14 | +class UrlTestCase(unittest.TestCase): |
17 | 15 | def test_model(self): |
18 | 16 | data = { |
19 | 17 | 'provider_url': 'http://www.google.com/', |
@@ -75,6 +73,24 @@ def test_model_data_can_serialize(self): |
75 | 73 | unserialzed = json.loads(json.dumps(obj.data)) |
76 | 74 | self.assertDictEqual(obj.data, unserialzed) |
77 | 75 |
|
| 76 | + |
| 77 | +class EmbedlyTestCase(unittest.TestCase): |
| 78 | + def setUp(self): |
| 79 | + self.key = 'internal' |
| 80 | + |
| 81 | + def test_requires_api_key(self): |
| 82 | + with self.assertRaises(ValueError): |
| 83 | + Embedly()._get(1, "test", "http://fake") |
| 84 | + |
| 85 | + def test_requires_url(self): |
| 86 | + with self.assertRaises(ValueError): |
| 87 | + Embedly(self.key)._get(1, "test", None) |
| 88 | + |
| 89 | + def test_exception_on_too_many_urls(self): |
| 90 | + urls = ['http://embed.ly'] * 21 |
| 91 | + with self.assertRaises(ValueError): |
| 92 | + Embedly(self.key)._get(1, "test", urls) |
| 93 | + |
78 | 94 | def test_provider(self): |
79 | 95 | http = Embedly(self.key) |
80 | 96 |
|
@@ -139,13 +155,68 @@ def test_multi_errors(self): |
139 | 155 | self.assertEqual(objs[0]['type'], 'photo') |
140 | 156 | self.assertEqual(objs[1]['type'], 'error') |
141 | 157 |
|
142 | | - def test_exception_on_too_many_urls(self): |
143 | | - http = Embedly(self.key) |
144 | | - urls = ['http://embed.ly'] * 21 |
| 158 | + def test_raw_content_in_request(self): |
| 159 | + client = Embedly(self.key) |
| 160 | + response = client.oembed( |
| 161 | + 'http://www.scribd.com/doc/13994900/Easter', |
| 162 | + raw=True) |
145 | 163 |
|
146 | | - with self.assertRaises(ValueError): |
147 | | - http.oembed(urls) |
| 164 | + self.assertEqual(response['raw'], response.data['raw']) |
| 165 | + |
| 166 | + parsed = json.loads(response['raw'].decode('utf-8')) |
| 167 | + self.assertEqual(response['type'], parsed['type']) |
| 168 | + |
| 169 | + def test_regex_url_matches(self): |
| 170 | + regex = [ |
| 171 | + 'http://.*youtube\\.com/watch.*', |
| 172 | + 'http://www\\.vimeo\\.com/.*'] |
| 173 | + client = Embedly(self.key) |
| 174 | + client._regex = re.compile('|'.join(regex)) |
| 175 | + |
| 176 | + self.assertTrue( |
| 177 | + client.is_supported('http://www.youtube.com/watch?v=Zk7dDekYej0')) |
| 178 | + self.assertTrue( |
| 179 | + client.is_supported('http://www.vimeo.com/18150336')) |
| 180 | + self.assertFalse( |
| 181 | + client.is_supported('http://vimeo.com/18150336')) |
| 182 | + self.assertFalse( |
| 183 | + client.is_supported('http://yfrog.com/h22eu4j')) |
| 184 | + |
| 185 | + def test_services_can_be_manually_configured(self): |
| 186 | + client = Embedly(self.key) |
| 187 | + client.services = ['nothing', 'like', 'real', 'response', 'data'] |
| 188 | + |
| 189 | + self.assertTrue('nothing' in client.get_services()) |
| 190 | + self.assertEqual(len(client.get_services()), 5) |
| 191 | + |
| 192 | + def test_get_services_retrieves_data_and_builds_regex(self): |
| 193 | + client = Embedly(self.key) |
| 194 | + client.get_services() |
| 195 | + |
| 196 | + self.assertGreater(len(client.services), 0) |
| 197 | + self.assertTrue(client.regex.match('http://yfrog.com/h22eu4j')) |
| 198 | + |
| 199 | + def test_extract(self): |
| 200 | + client = Embedly(self.key) |
| 201 | + response = client.extract('http://vimeo.com/18150336') |
| 202 | + |
| 203 | + self.assertEqual(response.method, 'extract') |
| 204 | + self.assertEqual(response['provider_name'], 'Vimeo') |
| 205 | + |
| 206 | + def test_preview(self): |
| 207 | + client = Embedly(self.key) |
| 208 | + response = client.preview('http://vimeo.com/18150336') |
| 209 | + |
| 210 | + self.assertEqual(response.method, 'preview') |
| 211 | + self.assertEqual(response['provider_name'], 'Vimeo') |
| 212 | + |
| 213 | + def test_objectify(self): |
| 214 | + client = Embedly(self.key) |
| 215 | + response = client.objectify('http://vimeo.com/18150336') |
| 216 | + |
| 217 | + self.assertEqual(response.method, 'objectify') |
| 218 | + self.assertEqual(response['provider_name'], 'Vimeo') |
148 | 219 |
|
149 | 220 |
|
150 | | -if __name__ == '__main__': |
151 | | - unittest.main() |
| 221 | +if __name__ == '__main__': # pragma: no cover |
| 222 | + unittest.main() # pragma: no cover |
0 commit comments