diff --git a/IPython/core/tests/test_display.py b/IPython/core/tests/test_display.py index 15cae21..b88bee8 100644 --- a/IPython/core/tests/test_display.py +++ b/IPython/core/tests/test_display.py @@ -7,7 +7,7 @@ import warnings from unittest import mock -import nose.tools as nt +import pytest from IPython import display from IPython.core.getipython import get_ipython @@ -22,15 +22,15 @@ def test_image_size(): """Simple test for display.Image(args, width=x,height=y)""" thisurl = 'http://www.google.fr/images/srpr/logo3w.png' img = display.Image(url=thisurl, width=200, height=200) - nt.assert_equal(u'' % (thisurl), img._repr_html_()) + assert '' % (thisurl) == img._repr_html_() img = display.Image(url=thisurl, metadata={'width':200, 'height':200}) - nt.assert_equal(u'' % (thisurl), img._repr_html_()) + assert '' % (thisurl) == img._repr_html_() img = display.Image(url=thisurl, width=200) - nt.assert_equal(u'' % (thisurl), img._repr_html_()) + assert '' % (thisurl) == img._repr_html_() img = display.Image(url=thisurl) - nt.assert_equal(u'' % (thisurl), img._repr_html_()) + assert '' % (thisurl) == img._repr_html_() img = display.Image(url=thisurl, unconfined=True) - nt.assert_equal(u'' % (thisurl), img._repr_html_()) + assert '' % (thisurl) == img._repr_html_() def test_image_mimes(): @@ -39,7 +39,7 @@ def test_image_mimes(): mime = display.Image._MIMETYPES[format] img = display.Image(b'garbage', format=format) data, metadata = fmt(img) - nt.assert_equal(sorted(data), sorted([mime, 'text/plain'])) + assert sorted(data) == sorted([mime, "text/plain"]) def test_geojson(): @@ -60,17 +60,20 @@ def test_geojson(): "attribution": "Celestia/praesepe", "minZoom": 0, "maxZoom": 18, - }) - nt.assert_equal(u'', str(gj)) + }, + ) + assert "" == str(gj) + def test_retina_png(): here = os.path.dirname(__file__) img = display.Image(os.path.join(here, "2x2.png"), retina=True) - nt.assert_equal(img.height, 1) - nt.assert_equal(img.width, 1) + assert img.height == 1 + assert img.width == 1 data, md = img._repr_png_() - nt.assert_equal(md['width'], 1) - nt.assert_equal(md['height'], 1) + assert md["width"] == 1 + assert md["height"] == 1 + def test_embed_svg_url(): import gzip @@ -102,18 +105,20 @@ def test_embed_svg_url(): with mock.patch('urllib.request.urlopen', side_effect=mocked_urlopen): svg = display.SVG(url=url) - nt.assert_true(svg._repr_svg_().startswith('') - nt.assert_equal(p.data, 'This is a simple test') + p = display.Pretty("This is a simple test") + assert repr(p) == "" + assert p.data == "This is a simple test" + + p._show_mem_addr = True + assert repr(p) == object.__repr__(p) - p._show_mem_addr = True - nt.assert_equal(repr(p), object.__repr__(p)) def test_displayobject_repr(): - h = display.HTML('
') - nt.assert_equal(repr(h), '') + h = display.HTML("
") + assert repr(h) == "" h._show_mem_addr = True - nt.assert_equal(repr(h), object.__repr__(h)) + assert repr(h) == object.__repr__(h) h._show_mem_addr = False - nt.assert_equal(repr(h), '') + assert repr(h) == "" - j = display.Javascript('') - nt.assert_equal(repr(j), '') + j = display.Javascript("") + assert repr(j) == "" j._show_mem_addr = True - nt.assert_equal(repr(j), object.__repr__(j)) + assert repr(j) == object.__repr__(j) j._show_mem_addr = False - nt.assert_equal(repr(j), '') + assert repr(j) == "" @mock.patch('warnings.warn') def test_encourage_iframe_over_html(m_warn): @@ -255,18 +273,22 @@ def test_encourage_iframe_over_html(m_warn): def test_progress(): p = display.ProgressBar(10) - nt.assert_in('0/10',repr(p)) - p.html_width = '100%' + assert "0/10" in repr(p) + p.html_width = "100%" p.progress = 5 - nt.assert_equal(p._repr_html_(), "") + assert ( + p._repr_html_() == "" + ) + def test_progress_iter(): with capture_output(display=False) as captured: for i in display.ProgressBar(5): out = captured.stdout - nt.assert_in('{0}/5'.format(i), out) + assert "{0}/5".format(i) in out out = captured.stdout - nt.assert_in('5/5', out) + assert "5/5" in out + def test_json(): d = {'a': 5} @@ -284,13 +306,13 @@ def test_json(): display.JSON(d, expanded=True, root='custom'), ] for j, md in zip(json_objs, metadata): - nt.assert_equal(j._repr_json_(), (d, md)) + assert j._repr_json_() == (d, md) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") j = display.JSON(json.dumps(d)) - nt.assert_equal(len(w), 1) - nt.assert_equal(j._repr_json_(), (d, metadata[0])) + assert len(w) == 1 + assert j._repr_json_() == (d, metadata[0]) json_objs = [ display.JSON(lis), @@ -299,23 +321,24 @@ def test_json(): display.JSON(lis, expanded=True, root='custom'), ] for j, md in zip(json_objs, metadata): - nt.assert_equal(j._repr_json_(), (lis, md)) + assert j._repr_json_() == (lis, md) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") j = display.JSON(json.dumps(lis)) - nt.assert_equal(len(w), 1) - nt.assert_equal(j._repr_json_(), (lis, metadata[0])) + assert len(w) == 1 + assert j._repr_json_() == (lis, metadata[0]) + def test_video_embedding(): """use a tempfile, with dummy-data, to ensure that video embedding doesn't crash""" v = display.Video("http://ignored") assert not v.embed html = v._repr_html_() - nt.assert_not_in('src="data:', html) - nt.assert_in('src="http://ignored"', html) + assert 'src="data:' not in html + assert 'src="http://ignored"' in html - with nt.assert_raises(ValueError): + with pytest.raises(ValueError): v = display.Video(b'abc') with NamedFileInTemporaryDirectory('test.mp4') as f: @@ -325,52 +348,53 @@ def test_video_embedding(): v = display.Video(f.name) assert not v.embed html = v._repr_html_() - nt.assert_not_in('src="data:', html) + assert 'src="data:' not in html v = display.Video(f.name, embed=True) html = v._repr_html_() - nt.assert_in('src="data:video/mp4;base64,YWJj"',html) + assert 'src="data:video/mp4;base64,YWJj"' in html v = display.Video(f.name, embed=True, mimetype='video/other') html = v._repr_html_() - nt.assert_in('src="data:video/other;base64,YWJj"',html) + assert 'src="data:video/other;base64,YWJj"' in html v = display.Video(b'abc', embed=True, mimetype='video/mp4') html = v._repr_html_() - nt.assert_in('src="data:video/mp4;base64,YWJj"',html) + assert 'src="data:video/mp4;base64,YWJj"' in html v = display.Video(u'YWJj', embed=True, mimetype='video/xyz') html = v._repr_html_() - nt.assert_in('src="data:video/xyz;base64,YWJj"',html) + assert 'src="data:video/xyz;base64,YWJj"' in html def test_html_metadata(): s = "

Test

" h = display.HTML(s, metadata={"isolated": True}) - nt.assert_equal(h._repr_html_(), (s, {"isolated": True})) + assert h._repr_html_() == (s, {"isolated": True}) + def test_display_id(): ip = get_ipython() with mock.patch.object(ip.display_pub, 'publish') as pub: handle = display.display('x') - nt.assert_is(handle, None) + assert handle is None handle = display.display('y', display_id='secret') - nt.assert_is_instance(handle, display.DisplayHandle) + assert isinstance(handle, display.DisplayHandle) handle2 = display.display('z', display_id=True) - nt.assert_is_instance(handle2, display.DisplayHandle) - nt.assert_not_equal(handle.display_id, handle2.display_id) + assert isinstance(handle2, display.DisplayHandle) + assert handle.display_id != handle2.display_id - nt.assert_equal(pub.call_count, 3) + assert pub.call_count == 3 args, kwargs = pub.call_args_list[0] - nt.assert_equal(args, ()) - nt.assert_equal(kwargs, { + assert args == () + assert kwargs == { 'data': { 'text/plain': repr('x') }, 'metadata': {}, - }) + } args, kwargs = pub.call_args_list[1] - nt.assert_equal(args, ()) - nt.assert_equal(kwargs, { + assert args == () + assert kwargs == { 'data': { 'text/plain': repr('y') }, @@ -378,10 +402,10 @@ def test_display_id(): 'transient': { 'display_id': handle.display_id, }, - }) + } args, kwargs = pub.call_args_list[2] - nt.assert_equal(args, ()) - nt.assert_equal(kwargs, { + assert args == () + assert kwargs == { 'data': { 'text/plain': repr('z') }, @@ -389,19 +413,19 @@ def test_display_id(): 'transient': { 'display_id': handle2.display_id, }, - }) + } def test_update_display(): ip = get_ipython() with mock.patch.object(ip.display_pub, 'publish') as pub: - with nt.assert_raises(TypeError): + with pytest.raises(TypeError): display.update_display('x') display.update_display('x', display_id='1') display.update_display('y', display_id='2') args, kwargs = pub.call_args_list[0] - nt.assert_equal(args, ()) - nt.assert_equal(kwargs, { + assert args == () + assert kwargs == { 'data': { 'text/plain': repr('x') }, @@ -410,10 +434,10 @@ def test_update_display(): 'display_id': '1', }, 'update': True, - }) + } args, kwargs = pub.call_args_list[1] - nt.assert_equal(args, ()) - nt.assert_equal(kwargs, { + assert args == () + assert kwargs == { 'data': { 'text/plain': repr('y') }, @@ -422,22 +446,22 @@ def test_update_display(): 'display_id': '2', }, 'update': True, - }) + } def test_display_handle(): ip = get_ipython() handle = display.DisplayHandle() - nt.assert_is_instance(handle.display_id, str) - handle = display.DisplayHandle('my-id') - nt.assert_equal(handle.display_id, 'my-id') - with mock.patch.object(ip.display_pub, 'publish') as pub: - handle.display('x') - handle.update('y') + assert isinstance(handle.display_id, str) + handle = display.DisplayHandle("my-id") + assert handle.display_id == "my-id" + with mock.patch.object(ip.display_pub, "publish") as pub: + handle.display("x") + handle.update("y") args, kwargs = pub.call_args_list[0] - nt.assert_equal(args, ()) - nt.assert_equal(kwargs, { + assert args == () + assert kwargs == { 'data': { 'text/plain': repr('x') }, @@ -445,10 +469,10 @@ def test_display_handle(): 'transient': { 'display_id': handle.display_id, } - }) + } args, kwargs = pub.call_args_list[1] - nt.assert_equal(args, ()) - nt.assert_equal(kwargs, { + assert args == () + assert kwargs == { 'data': { 'text/plain': repr('y') }, @@ -457,34 +481,31 @@ def test_display_handle(): 'display_id': handle.display_id, }, 'update': True, - }) + } def test_image_alt_tag(): """Simple test for display.Image(args, alt=x,)""" thisurl = "http://example.com/image.png" img = display.Image(url=thisurl, alt="an image") - nt.assert_equal(u'an image' % (thisurl), img._repr_html_()) + assert 'an image' % (thisurl) == img._repr_html_() img = display.Image(url=thisurl, unconfined=True, alt="an image") - nt.assert_equal( - u'an image' % (thisurl), - img._repr_html_(), + assert ( + 'an image' % (thisurl) + == img._repr_html_() ) img = display.Image(url=thisurl, alt='>"& <') - nt.assert_equal( - u'>"& <' % (thisurl), img._repr_html_() - ) + assert '>"& <' % (thisurl) == img._repr_html_() img = display.Image(url=thisurl, metadata={"alt": "an image"}) - nt.assert_equal(img.alt, "an image") - + assert img.alt == "an image" here = os.path.dirname(__file__) img = display.Image(os.path.join(here, "2x2.png"), alt="an image") - nt.assert_equal(img.alt, "an image") + assert img.alt == "an image" _, md = img._repr_png_() - nt.assert_equal(md["alt"], "an image") + assert md["alt"] == "an image" -@nt.raises(FileNotFoundError) def test_image_bad_filename_raises_proper_exception(): - display.Image("/this/file/does/not/exist/")._repr_png_() + with pytest.raises(FileNotFoundError): + display.Image("/this/file/does/not/exist/")._repr_png_()