Show More
@@ -7,7 +7,7 b' import warnings' | |||
|
7 | 7 | |
|
8 | 8 | from unittest import mock |
|
9 | 9 | |
|
10 | import nose.tools as nt | |
|
10 | import pytest | |
|
11 | 11 | |
|
12 | 12 | from IPython import display |
|
13 | 13 | from IPython.core.getipython import get_ipython |
@@ -22,15 +22,15 b' def test_image_size():' | |||
|
22 | 22 | """Simple test for display.Image(args, width=x,height=y)""" |
|
23 | 23 | thisurl = 'http://www.google.fr/images/srpr/logo3w.png' |
|
24 | 24 | img = display.Image(url=thisurl, width=200, height=200) |
|
25 |
|
|
|
25 | assert '<img src="%s" width="200" height="200"/>' % (thisurl) == img._repr_html_() | |
|
26 | 26 | img = display.Image(url=thisurl, metadata={'width':200, 'height':200}) |
|
27 |
|
|
|
27 | assert '<img src="%s" width="200" height="200"/>' % (thisurl) == img._repr_html_() | |
|
28 | 28 | img = display.Image(url=thisurl, width=200) |
|
29 |
|
|
|
29 | assert '<img src="%s" width="200"/>' % (thisurl) == img._repr_html_() | |
|
30 | 30 | img = display.Image(url=thisurl) |
|
31 |
|
|
|
31 | assert '<img src="%s"/>' % (thisurl) == img._repr_html_() | |
|
32 | 32 | img = display.Image(url=thisurl, unconfined=True) |
|
33 |
|
|
|
33 | assert '<img src="%s" class="unconfined"/>' % (thisurl) == img._repr_html_() | |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | def test_image_mimes(): |
@@ -39,7 +39,7 b' def test_image_mimes():' | |||
|
39 | 39 | mime = display.Image._MIMETYPES[format] |
|
40 | 40 | img = display.Image(b'garbage', format=format) |
|
41 | 41 | data, metadata = fmt(img) |
|
42 |
|
|
|
42 | assert sorted(data) == sorted([mime, "text/plain"]) | |
|
43 | 43 | |
|
44 | 44 | |
|
45 | 45 | def test_geojson(): |
@@ -60,17 +60,20 b' def test_geojson():' | |||
|
60 | 60 | "attribution": "Celestia/praesepe", |
|
61 | 61 | "minZoom": 0, |
|
62 | 62 | "maxZoom": 18, |
|
63 |
} |
|
|
64 | nt.assert_equal(u'<IPython.core.display.GeoJSON object>', str(gj)) | |
|
63 | }, | |
|
64 | ) | |
|
65 | assert "<IPython.core.display.GeoJSON object>" == str(gj) | |
|
66 | ||
|
65 | 67 | |
|
66 | 68 | def test_retina_png(): |
|
67 | 69 | here = os.path.dirname(__file__) |
|
68 | 70 | img = display.Image(os.path.join(here, "2x2.png"), retina=True) |
|
69 |
|
|
|
70 |
|
|
|
71 | assert img.height == 1 | |
|
72 | assert img.width == 1 | |
|
71 | 73 | data, md = img._repr_png_() |
|
72 |
|
|
|
73 |
|
|
|
74 | assert md["width"] == 1 | |
|
75 | assert md["height"] == 1 | |
|
76 | ||
|
74 | 77 | |
|
75 | 78 | def test_embed_svg_url(): |
|
76 | 79 | import gzip |
@@ -102,18 +105,20 b' def test_embed_svg_url():' | |||
|
102 | 105 | |
|
103 | 106 | with mock.patch('urllib.request.urlopen', side_effect=mocked_urlopen): |
|
104 | 107 | svg = display.SVG(url=url) |
|
105 |
|
|
|
106 |
svg = display.SVG(url=url + |
|
|
107 |
|
|
|
108 | assert svg._repr_svg_().startswith("<svg") is True | |
|
109 | svg = display.SVG(url=url + "z") | |
|
110 | assert svg._repr_svg_().startswith("<svg") is True | |
|
111 | ||
|
108 | 112 | |
|
109 | 113 | def test_retina_jpeg(): |
|
110 | 114 | here = os.path.dirname(__file__) |
|
111 | 115 | img = display.Image(os.path.join(here, "2x2.jpg"), retina=True) |
|
112 |
|
|
|
113 |
|
|
|
116 | assert img.height == 1 | |
|
117 | assert img.width == 1 | |
|
114 | 118 | data, md = img._repr_jpeg_() |
|
115 |
|
|
|
116 |
|
|
|
119 | assert md["width"] == 1 | |
|
120 | assert md["height"] == 1 | |
|
121 | ||
|
117 | 122 | |
|
118 | 123 | def test_base64image(): |
|
119 | 124 | display.Image("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAWJLR0QAiAUdSAAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB94BCRQnOqNu0b4AAAAKSURBVAjXY2AAAAACAAHiIbwzAAAAAElFTkSuQmCC") |
@@ -121,18 +126,30 b' def test_base64image():' | |||
|
121 | 126 | def test_image_filename_defaults(): |
|
122 | 127 | '''test format constraint, and validity of jpeg and png''' |
|
123 | 128 | tpath = ipath.get_ipython_package_dir() |
|
124 | nt.assert_raises(ValueError, display.Image, filename=os.path.join(tpath, 'testing/tests/badformat.zip'), | |
|
125 | embed=True) | |
|
126 | nt.assert_raises(ValueError, display.Image) | |
|
127 | nt.assert_raises(ValueError, display.Image, data='this is not an image', format='badformat', embed=True) | |
|
128 | # check both paths to allow packages to test at build and install time | |
|
129 | pytest.raises( | |
|
130 | ValueError, | |
|
131 | display.Image, | |
|
132 | filename=os.path.join(tpath, "testing/tests/badformat.zip"), | |
|
133 | embed=True, | |
|
134 | ) | |
|
135 | pytest.raises(ValueError, display.Image) | |
|
136 | pytest.raises( | |
|
137 | ValueError, | |
|
138 | display.Image, | |
|
139 | data="this is not an image", | |
|
140 | format="badformat", | |
|
141 | embed=True, | |
|
142 | ) | |
|
143 | # check boths paths to allow packages to test at build and install time | |
|
129 | 144 | imgfile = os.path.join(tpath, 'core/tests/2x2.png') |
|
130 | 145 | img = display.Image(filename=imgfile) |
|
131 |
|
|
|
132 |
|
|
|
133 | img = display.Image(filename=os.path.join(tpath, 'testing/tests/logo.jpg'), embed=False) | |
|
134 | nt.assert_equal('jpeg', img.format) | |
|
135 | nt.assert_is_none(img._repr_jpeg_()) | |
|
146 | assert "png" == img.format | |
|
147 | assert img._repr_png_() is not None | |
|
148 | img = display.Image( | |
|
149 | filename=os.path.join(tpath, "testing/tests/logo.jpg"), embed=False | |
|
150 | ) | |
|
151 | assert "jpeg" == img.format | |
|
152 | assert img._repr_jpeg_() is None | |
|
136 | 153 | |
|
137 | 154 | def _get_inline_config(): |
|
138 | 155 | from matplotlib_inline.config import InlineBackend |
@@ -171,9 +188,9 b' def test_set_matplotlib_formats():' | |||
|
171 | 188 | display.set_matplotlib_formats(*formats) |
|
172 | 189 | for mime, f in formatters.items(): |
|
173 | 190 | if mime in active_mimes: |
|
174 |
|
|
|
191 | assert Figure in f | |
|
175 | 192 | else: |
|
176 |
|
|
|
193 | assert Figure not in f | |
|
177 | 194 | |
|
178 | 195 | |
|
179 | 196 | @dec.skip_without("ipykernel") |
@@ -192,7 +209,7 b' def test_set_matplotlib_formats_kwargs():' | |||
|
192 | 209 | expected["base64"] = True |
|
193 | 210 | expected["fmt"] = "png" |
|
194 | 211 | expected.update(cfg.print_figure_kwargs) |
|
195 |
|
|
|
212 | assert formatter_kwargs == expected | |
|
196 | 213 | |
|
197 | 214 | def test_display_available(): |
|
198 | 215 | """ |
@@ -213,27 +230,28 b' def test_display_available():' | |||
|
213 | 230 | ip.run_cell('display') |
|
214 | 231 | |
|
215 | 232 | def test_textdisplayobj_pretty_repr(): |
|
216 |
|
|
|
217 |
|
|
|
218 |
|
|
|
233 | p = display.Pretty("This is a simple test") | |
|
234 | assert repr(p) == "<IPython.core.display.Pretty object>" | |
|
235 | assert p.data == "This is a simple test" | |
|
236 | ||
|
237 | p._show_mem_addr = True | |
|
238 | assert repr(p) == object.__repr__(p) | |
|
219 | 239 | |
|
220 | p._show_mem_addr = True | |
|
221 | nt.assert_equal(repr(p), object.__repr__(p)) | |
|
222 | 240 | |
|
223 | 241 | def test_displayobject_repr(): |
|
224 |
h = display.HTML( |
|
|
225 |
|
|
|
242 | h = display.HTML("<br />") | |
|
243 | assert repr(h) == "<IPython.core.display.HTML object>" | |
|
226 | 244 | h._show_mem_addr = True |
|
227 |
|
|
|
245 | assert repr(h) == object.__repr__(h) | |
|
228 | 246 | h._show_mem_addr = False |
|
229 |
|
|
|
247 | assert repr(h) == "<IPython.core.display.HTML object>" | |
|
230 | 248 | |
|
231 |
j = display.Javascript( |
|
|
232 |
|
|
|
249 | j = display.Javascript("") | |
|
250 | assert repr(j) == "<IPython.core.display.Javascript object>" | |
|
233 | 251 | j._show_mem_addr = True |
|
234 |
|
|
|
252 | assert repr(j) == object.__repr__(j) | |
|
235 | 253 | j._show_mem_addr = False |
|
236 |
|
|
|
254 | assert repr(j) == "<IPython.core.display.Javascript object>" | |
|
237 | 255 | |
|
238 | 256 | @mock.patch('warnings.warn') |
|
239 | 257 | def test_encourage_iframe_over_html(m_warn): |
@@ -255,18 +273,22 b' def test_encourage_iframe_over_html(m_warn):' | |||
|
255 | 273 | |
|
256 | 274 | def test_progress(): |
|
257 | 275 | p = display.ProgressBar(10) |
|
258 |
|
|
|
259 |
p.html_width = |
|
|
276 | assert "0/10" in repr(p) | |
|
277 | p.html_width = "100%" | |
|
260 | 278 | p.progress = 5 |
|
261 | nt.assert_equal(p._repr_html_(), "<progress style='width:100%' max='10' value='5'></progress>") | |
|
279 | assert ( | |
|
280 | p._repr_html_() == "<progress style='width:100%' max='10' value='5'></progress>" | |
|
281 | ) | |
|
282 | ||
|
262 | 283 | |
|
263 | 284 | def test_progress_iter(): |
|
264 | 285 | with capture_output(display=False) as captured: |
|
265 | 286 | for i in display.ProgressBar(5): |
|
266 | 287 | out = captured.stdout |
|
267 |
|
|
|
288 | assert "{0}/5".format(i) in out | |
|
268 | 289 | out = captured.stdout |
|
269 |
|
|
|
290 | assert "5/5" in out | |
|
291 | ||
|
270 | 292 | |
|
271 | 293 | def test_json(): |
|
272 | 294 | d = {'a': 5} |
@@ -284,13 +306,13 b' def test_json():' | |||
|
284 | 306 | display.JSON(d, expanded=True, root='custom'), |
|
285 | 307 | ] |
|
286 | 308 | for j, md in zip(json_objs, metadata): |
|
287 |
|
|
|
309 | assert j._repr_json_() == (d, md) | |
|
288 | 310 | |
|
289 | 311 | with warnings.catch_warnings(record=True) as w: |
|
290 | 312 | warnings.simplefilter("always") |
|
291 | 313 | j = display.JSON(json.dumps(d)) |
|
292 |
|
|
|
293 |
|
|
|
314 | assert len(w) == 1 | |
|
315 | assert j._repr_json_() == (d, metadata[0]) | |
|
294 | 316 | |
|
295 | 317 | json_objs = [ |
|
296 | 318 | display.JSON(lis), |
@@ -299,23 +321,24 b' def test_json():' | |||
|
299 | 321 | display.JSON(lis, expanded=True, root='custom'), |
|
300 | 322 | ] |
|
301 | 323 | for j, md in zip(json_objs, metadata): |
|
302 |
|
|
|
324 | assert j._repr_json_() == (lis, md) | |
|
303 | 325 | |
|
304 | 326 | with warnings.catch_warnings(record=True) as w: |
|
305 | 327 | warnings.simplefilter("always") |
|
306 | 328 | j = display.JSON(json.dumps(lis)) |
|
307 |
|
|
|
308 |
|
|
|
329 | assert len(w) == 1 | |
|
330 | assert j._repr_json_() == (lis, metadata[0]) | |
|
331 | ||
|
309 | 332 | |
|
310 | 333 | def test_video_embedding(): |
|
311 | 334 | """use a tempfile, with dummy-data, to ensure that video embedding doesn't crash""" |
|
312 | 335 | v = display.Video("http://ignored") |
|
313 | 336 | assert not v.embed |
|
314 | 337 | html = v._repr_html_() |
|
315 |
|
|
|
316 |
|
|
|
338 | assert 'src="data:' not in html | |
|
339 | assert 'src="http://ignored"' in html | |
|
317 | 340 | |
|
318 |
with |
|
|
341 | with pytest.raises(ValueError): | |
|
319 | 342 | v = display.Video(b'abc') |
|
320 | 343 | |
|
321 | 344 | with NamedFileInTemporaryDirectory('test.mp4') as f: |
@@ -325,52 +348,53 b' def test_video_embedding():' | |||
|
325 | 348 | v = display.Video(f.name) |
|
326 | 349 | assert not v.embed |
|
327 | 350 | html = v._repr_html_() |
|
328 |
|
|
|
351 | assert 'src="data:' not in html | |
|
329 | 352 | |
|
330 | 353 | v = display.Video(f.name, embed=True) |
|
331 | 354 | html = v._repr_html_() |
|
332 |
|
|
|
355 | assert 'src="data:video/mp4;base64,YWJj"' in html | |
|
333 | 356 | |
|
334 | 357 | v = display.Video(f.name, embed=True, mimetype='video/other') |
|
335 | 358 | html = v._repr_html_() |
|
336 |
|
|
|
359 | assert 'src="data:video/other;base64,YWJj"' in html | |
|
337 | 360 | |
|
338 | 361 | v = display.Video(b'abc', embed=True, mimetype='video/mp4') |
|
339 | 362 | html = v._repr_html_() |
|
340 |
|
|
|
363 | assert 'src="data:video/mp4;base64,YWJj"' in html | |
|
341 | 364 | |
|
342 | 365 | v = display.Video(u'YWJj', embed=True, mimetype='video/xyz') |
|
343 | 366 | html = v._repr_html_() |
|
344 |
|
|
|
367 | assert 'src="data:video/xyz;base64,YWJj"' in html | |
|
345 | 368 | |
|
346 | 369 | def test_html_metadata(): |
|
347 | 370 | s = "<h1>Test</h1>" |
|
348 | 371 | h = display.HTML(s, metadata={"isolated": True}) |
|
349 |
|
|
|
372 | assert h._repr_html_() == (s, {"isolated": True}) | |
|
373 | ||
|
350 | 374 | |
|
351 | 375 | def test_display_id(): |
|
352 | 376 | ip = get_ipython() |
|
353 | 377 | with mock.patch.object(ip.display_pub, 'publish') as pub: |
|
354 | 378 | handle = display.display('x') |
|
355 |
|
|
|
379 | assert handle is None | |
|
356 | 380 | handle = display.display('y', display_id='secret') |
|
357 |
|
|
|
381 | assert isinstance(handle, display.DisplayHandle) | |
|
358 | 382 | handle2 = display.display('z', display_id=True) |
|
359 |
|
|
|
360 |
|
|
|
383 | assert isinstance(handle2, display.DisplayHandle) | |
|
384 | assert handle.display_id != handle2.display_id | |
|
361 | 385 | |
|
362 |
|
|
|
386 | assert pub.call_count == 3 | |
|
363 | 387 | args, kwargs = pub.call_args_list[0] |
|
364 |
|
|
|
365 |
|
|
|
388 | assert args == () | |
|
389 | assert kwargs == { | |
|
366 | 390 | 'data': { |
|
367 | 391 | 'text/plain': repr('x') |
|
368 | 392 | }, |
|
369 | 393 | 'metadata': {}, |
|
370 |
} |
|
|
394 | } | |
|
371 | 395 | args, kwargs = pub.call_args_list[1] |
|
372 |
|
|
|
373 |
|
|
|
396 | assert args == () | |
|
397 | assert kwargs == { | |
|
374 | 398 | 'data': { |
|
375 | 399 | 'text/plain': repr('y') |
|
376 | 400 | }, |
@@ -378,10 +402,10 b' def test_display_id():' | |||
|
378 | 402 | 'transient': { |
|
379 | 403 | 'display_id': handle.display_id, |
|
380 | 404 | }, |
|
381 |
} |
|
|
405 | } | |
|
382 | 406 | args, kwargs = pub.call_args_list[2] |
|
383 |
|
|
|
384 |
|
|
|
407 | assert args == () | |
|
408 | assert kwargs == { | |
|
385 | 409 | 'data': { |
|
386 | 410 | 'text/plain': repr('z') |
|
387 | 411 | }, |
@@ -389,19 +413,19 b' def test_display_id():' | |||
|
389 | 413 | 'transient': { |
|
390 | 414 | 'display_id': handle2.display_id, |
|
391 | 415 | }, |
|
392 |
} |
|
|
416 | } | |
|
393 | 417 | |
|
394 | 418 | |
|
395 | 419 | def test_update_display(): |
|
396 | 420 | ip = get_ipython() |
|
397 | 421 | with mock.patch.object(ip.display_pub, 'publish') as pub: |
|
398 |
with |
|
|
422 | with pytest.raises(TypeError): | |
|
399 | 423 | display.update_display('x') |
|
400 | 424 | display.update_display('x', display_id='1') |
|
401 | 425 | display.update_display('y', display_id='2') |
|
402 | 426 | args, kwargs = pub.call_args_list[0] |
|
403 |
|
|
|
404 |
|
|
|
427 | assert args == () | |
|
428 | assert kwargs == { | |
|
405 | 429 | 'data': { |
|
406 | 430 | 'text/plain': repr('x') |
|
407 | 431 | }, |
@@ -410,10 +434,10 b' def test_update_display():' | |||
|
410 | 434 | 'display_id': '1', |
|
411 | 435 | }, |
|
412 | 436 | 'update': True, |
|
413 |
} |
|
|
437 | } | |
|
414 | 438 | args, kwargs = pub.call_args_list[1] |
|
415 |
|
|
|
416 |
|
|
|
439 | assert args == () | |
|
440 | assert kwargs == { | |
|
417 | 441 | 'data': { |
|
418 | 442 | 'text/plain': repr('y') |
|
419 | 443 | }, |
@@ -422,22 +446,22 b' def test_update_display():' | |||
|
422 | 446 | 'display_id': '2', |
|
423 | 447 | }, |
|
424 | 448 | 'update': True, |
|
425 |
} |
|
|
449 | } | |
|
426 | 450 | |
|
427 | 451 | |
|
428 | 452 | def test_display_handle(): |
|
429 | 453 | ip = get_ipython() |
|
430 | 454 | handle = display.DisplayHandle() |
|
431 |
|
|
|
432 |
handle = display.DisplayHandle( |
|
|
433 |
|
|
|
434 |
with mock.patch.object(ip.display_pub, |
|
|
435 |
handle.display( |
|
|
436 |
handle.update( |
|
|
455 | assert isinstance(handle.display_id, str) | |
|
456 | handle = display.DisplayHandle("my-id") | |
|
457 | assert handle.display_id == "my-id" | |
|
458 | with mock.patch.object(ip.display_pub, "publish") as pub: | |
|
459 | handle.display("x") | |
|
460 | handle.update("y") | |
|
437 | 461 | |
|
438 | 462 | args, kwargs = pub.call_args_list[0] |
|
439 |
|
|
|
440 |
|
|
|
463 | assert args == () | |
|
464 | assert kwargs == { | |
|
441 | 465 | 'data': { |
|
442 | 466 | 'text/plain': repr('x') |
|
443 | 467 | }, |
@@ -445,10 +469,10 b' def test_display_handle():' | |||
|
445 | 469 | 'transient': { |
|
446 | 470 | 'display_id': handle.display_id, |
|
447 | 471 | } |
|
448 |
} |
|
|
472 | } | |
|
449 | 473 | args, kwargs = pub.call_args_list[1] |
|
450 |
|
|
|
451 |
|
|
|
474 | assert args == () | |
|
475 | assert kwargs == { | |
|
452 | 476 | 'data': { |
|
453 | 477 | 'text/plain': repr('y') |
|
454 | 478 | }, |
@@ -457,34 +481,31 b' def test_display_handle():' | |||
|
457 | 481 | 'display_id': handle.display_id, |
|
458 | 482 | }, |
|
459 | 483 | 'update': True, |
|
460 |
} |
|
|
484 | } | |
|
461 | 485 | |
|
462 | 486 | |
|
463 | 487 | def test_image_alt_tag(): |
|
464 | 488 | """Simple test for display.Image(args, alt=x,)""" |
|
465 | 489 | thisurl = "http://example.com/image.png" |
|
466 | 490 | img = display.Image(url=thisurl, alt="an image") |
|
467 |
|
|
|
491 | assert '<img src="%s" alt="an image"/>' % (thisurl) == img._repr_html_() | |
|
468 | 492 | img = display.Image(url=thisurl, unconfined=True, alt="an image") |
|
469 |
|
|
|
470 |
|
|
|
471 |
img._repr_html_() |
|
|
493 | assert ( | |
|
494 | '<img src="%s" class="unconfined" alt="an image"/>' % (thisurl) | |
|
495 | == img._repr_html_() | |
|
472 | 496 | ) |
|
473 | 497 | img = display.Image(url=thisurl, alt='>"& <') |
|
474 | nt.assert_equal( | |
|
475 | u'<img src="%s" alt=">"& <"/>' % (thisurl), img._repr_html_() | |
|
476 | ) | |
|
498 | assert '<img src="%s" alt=">"& <"/>' % (thisurl) == img._repr_html_() | |
|
477 | 499 | |
|
478 | 500 | img = display.Image(url=thisurl, metadata={"alt": "an image"}) |
|
479 |
|
|
|
480 | ||
|
501 | assert img.alt == "an image" | |
|
481 | 502 | here = os.path.dirname(__file__) |
|
482 | 503 | img = display.Image(os.path.join(here, "2x2.png"), alt="an image") |
|
483 |
|
|
|
504 | assert img.alt == "an image" | |
|
484 | 505 | _, md = img._repr_png_() |
|
485 |
|
|
|
506 | assert md["alt"] == "an image" | |
|
486 | 507 | |
|
487 | 508 | |
|
488 | @nt.raises(FileNotFoundError) | |
|
489 | 509 | def test_image_bad_filename_raises_proper_exception(): |
|
490 | display.Image("/this/file/does/not/exist/")._repr_png_() | |
|
510 | with pytest.raises(FileNotFoundError): | |
|
511 | display.Image("/this/file/does/not/exist/")._repr_png_() |
General Comments 0
You need to be logged in to leave comments.
Login now