##// END OF EJS Templates
fix tests
Marius van Niekerk -
Show More
@@ -1,378 +1,380 b''
1 # Copyright (c) IPython Development Team.
1 # Copyright (c) IPython Development Team.
2 # Distributed under the terms of the Modified BSD License.
2 # Distributed under the terms of the Modified BSD License.
3
3
4 import json
4 import json
5 import os
5 import os
6 import warnings
6 import warnings
7
7
8 from unittest import mock
8 from unittest import mock
9
9
10 import nose.tools as nt
10 import nose.tools as nt
11
11
12 from IPython.core import display
12 from IPython.core import display
13 from IPython.core.getipython import get_ipython
13 from IPython.core.getipython import get_ipython
14 from IPython.utils.tempdir import NamedFileInTemporaryDirectory
14 from IPython.utils.tempdir import NamedFileInTemporaryDirectory
15 from IPython import paths as ipath
15 from IPython import paths as ipath
16 from IPython.testing.tools import AssertPrints, AssertNotPrints
16 from IPython.testing.tools import AssertPrints, AssertNotPrints
17
17
18 import IPython.testing.decorators as dec
18 import IPython.testing.decorators as dec
19
19
20 def test_image_size():
20 def test_image_size():
21 """Simple test for display.Image(args, width=x,height=y)"""
21 """Simple test for display.Image(args, width=x,height=y)"""
22 thisurl = 'http://www.google.fr/images/srpr/logo3w.png'
22 thisurl = 'http://www.google.fr/images/srpr/logo3w.png'
23 img = display.Image(url=thisurl, width=200, height=200)
23 img = display.Image(url=thisurl, width=200, height=200)
24 nt.assert_equal(u'<img src="%s" width="200" height="200"/>' % (thisurl), img._repr_html_())
24 nt.assert_equal(u'<img src="%s" width="200" height="200"/>' % (thisurl), img._repr_html_())
25 img = display.Image(url=thisurl, metadata={'width':200, 'height':200})
25 img = display.Image(url=thisurl, metadata={'width':200, 'height':200})
26 nt.assert_equal(u'<img src="%s" width="200" height="200"/>' % (thisurl), img._repr_html_())
26 nt.assert_equal(u'<img src="%s" width="200" height="200"/>' % (thisurl), img._repr_html_())
27 img = display.Image(url=thisurl, width=200)
27 img = display.Image(url=thisurl, width=200)
28 nt.assert_equal(u'<img src="%s" width="200"/>' % (thisurl), img._repr_html_())
28 nt.assert_equal(u'<img src="%s" width="200"/>' % (thisurl), img._repr_html_())
29 img = display.Image(url=thisurl)
29 img = display.Image(url=thisurl)
30 nt.assert_equal(u'<img src="%s"/>' % (thisurl), img._repr_html_())
30 nt.assert_equal(u'<img src="%s"/>' % (thisurl), img._repr_html_())
31 img = display.Image(url=thisurl, unconfined=True)
31 img = display.Image(url=thisurl, unconfined=True)
32 nt.assert_equal(u'<img src="%s" class="unconfined"/>' % (thisurl), img._repr_html_())
32 nt.assert_equal(u'<img src="%s" class="unconfined"/>' % (thisurl), img._repr_html_())
33
33
34
34
35 def test_image_mimes():
35 def test_image_mimes():
36 fmt = get_ipython().display_formatter.format
36 fmt = get_ipython().display_formatter.format
37 for format in display.Image._ACCEPTABLE_EMBEDDINGS:
37 for format in display.Image._ACCEPTABLE_EMBEDDINGS:
38 mime = display.Image._MIMETYPES[format]
38 mime = display.Image._MIMETYPES[format]
39 img = display.Image(b'garbage', format=format)
39 img = display.Image(b'garbage', format=format)
40 data, metadata = fmt(img)
40 data, metadata = fmt(img)
41 nt.assert_equal(sorted(data), sorted([mime, 'text/plain']))
41 nt.assert_equal(sorted(data), sorted([mime, 'text/plain']))
42
42
43
43
44 def test_geojson():
44 def test_geojson():
45
45
46 gj = display.GeoJSON(data={
46 gj = display.GeoJSON(data={
47 "type": "Feature",
47 "type": "Feature",
48 "geometry": {
48 "geometry": {
49 "type": "Point",
49 "type": "Point",
50 "coordinates": [-81.327, 296.038]
50 "coordinates": [-81.327, 296.038]
51 },
51 },
52 "properties": {
52 "properties": {
53 "name": "Inca City"
53 "name": "Inca City"
54 }
54 }
55 },
55 },
56 url_template="http://s3-eu-west-1.amazonaws.com/whereonmars.cartodb.net/{basemap_id}/{z}/{x}/{y}.png",
56 url_template="http://s3-eu-west-1.amazonaws.com/whereonmars.cartodb.net/{basemap_id}/{z}/{x}/{y}.png",
57 layer_options={
57 layer_options={
58 "basemap_id": "celestia_mars-shaded-16k_global",
58 "basemap_id": "celestia_mars-shaded-16k_global",
59 "attribution": "Celestia/praesepe",
59 "attribution": "Celestia/praesepe",
60 "minZoom": 0,
60 "minZoom": 0,
61 "maxZoom": 18,
61 "maxZoom": 18,
62 })
62 })
63 nt.assert_equal(u'<IPython.core.display.GeoJSON object>', str(gj))
63 nt.assert_equal(u'<IPython.core.display.GeoJSON object>', str(gj))
64
64
65 def test_retina_png():
65 def test_retina_png():
66 here = os.path.dirname(__file__)
66 here = os.path.dirname(__file__)
67 img = display.Image(os.path.join(here, "2x2.png"), retina=True)
67 img = display.Image(os.path.join(here, "2x2.png"), retina=True)
68 nt.assert_equal(img.height, 1)
68 nt.assert_equal(img.height, 1)
69 nt.assert_equal(img.width, 1)
69 nt.assert_equal(img.width, 1)
70 data, md = img._repr_png_()
70 data, md = img._repr_png_()
71 nt.assert_equal(md['width'], 1)
71 nt.assert_equal(md['width'], 1)
72 nt.assert_equal(md['height'], 1)
72 nt.assert_equal(md['height'], 1)
73
73
74 def test_retina_jpeg():
74 def test_retina_jpeg():
75 here = os.path.dirname(__file__)
75 here = os.path.dirname(__file__)
76 img = display.Image(os.path.join(here, "2x2.jpg"), retina=True)
76 img = display.Image(os.path.join(here, "2x2.jpg"), retina=True)
77 nt.assert_equal(img.height, 1)
77 nt.assert_equal(img.height, 1)
78 nt.assert_equal(img.width, 1)
78 nt.assert_equal(img.width, 1)
79 data, md = img._repr_jpeg_()
79 data, md = img._repr_jpeg_()
80 nt.assert_equal(md['width'], 1)
80 nt.assert_equal(md['width'], 1)
81 nt.assert_equal(md['height'], 1)
81 nt.assert_equal(md['height'], 1)
82
82
83 def test_base64image():
83 def test_base64image():
84 display.Image("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAWJLR0QAiAUdSAAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB94BCRQnOqNu0b4AAAAKSURBVAjXY2AAAAACAAHiIbwzAAAAAElFTkSuQmCC")
84 display.Image("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAWJLR0QAiAUdSAAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB94BCRQnOqNu0b4AAAAKSURBVAjXY2AAAAACAAHiIbwzAAAAAElFTkSuQmCC")
85
85
86 def test_image_filename_defaults():
86 def test_image_filename_defaults():
87 '''test format constraint, and validity of jpeg and png'''
87 '''test format constraint, and validity of jpeg and png'''
88 tpath = ipath.get_ipython_package_dir()
88 tpath = ipath.get_ipython_package_dir()
89 nt.assert_raises(ValueError, display.Image, filename=os.path.join(tpath, 'testing/tests/badformat.zip'),
89 nt.assert_raises(ValueError, display.Image, filename=os.path.join(tpath, 'testing/tests/badformat.zip'),
90 embed=True)
90 embed=True)
91 nt.assert_raises(ValueError, display.Image)
91 nt.assert_raises(ValueError, display.Image)
92 nt.assert_raises(ValueError, display.Image, data='this is not an image', format='badformat', embed=True)
92 nt.assert_raises(ValueError, display.Image, data='this is not an image', format='badformat', embed=True)
93 # check boths paths to allow packages to test at build and install time
93 # check boths paths to allow packages to test at build and install time
94 imgfile = os.path.join(tpath, 'core/tests/2x2.png')
94 imgfile = os.path.join(tpath, 'core/tests/2x2.png')
95 img = display.Image(filename=imgfile)
95 img = display.Image(filename=imgfile)
96 nt.assert_equal('png', img.format)
96 nt.assert_equal('png', img.format)
97 nt.assert_is_not_none(img._repr_png_())
97 nt.assert_is_not_none(img._repr_png_())
98 img = display.Image(filename=os.path.join(tpath, 'testing/tests/logo.jpg'), embed=False)
98 img = display.Image(filename=os.path.join(tpath, 'testing/tests/logo.jpg'), embed=False)
99 nt.assert_equal('jpeg', img.format)
99 nt.assert_equal('jpeg', img.format)
100 nt.assert_is_none(img._repr_jpeg_())
100 nt.assert_is_none(img._repr_jpeg_())
101
101
102 def _get_inline_config():
102 def _get_inline_config():
103 from ipykernel.pylab.config import InlineBackend
103 from ipykernel.pylab.config import InlineBackend
104 return InlineBackend.instance()
104 return InlineBackend.instance()
105
105
106 @dec.skip_without('matplotlib')
106 @dec.skip_without('matplotlib')
107 def test_set_matplotlib_close():
107 def test_set_matplotlib_close():
108 cfg = _get_inline_config()
108 cfg = _get_inline_config()
109 cfg.close_figures = False
109 cfg.close_figures = False
110 display.set_matplotlib_close()
110 display.set_matplotlib_close()
111 assert cfg.close_figures
111 assert cfg.close_figures
112 display.set_matplotlib_close(False)
112 display.set_matplotlib_close(False)
113 assert not cfg.close_figures
113 assert not cfg.close_figures
114
114
115 _fmt_mime_map = {
115 _fmt_mime_map = {
116 'png': 'image/png',
116 'png': 'image/png',
117 'jpeg': 'image/jpeg',
117 'jpeg': 'image/jpeg',
118 'pdf': 'application/pdf',
118 'pdf': 'application/pdf',
119 'retina': 'image/png',
119 'retina': 'image/png',
120 'svg': 'image/svg+xml',
120 'svg': 'image/svg+xml',
121 }
121 }
122
122
123 @dec.skip_without('matplotlib')
123 @dec.skip_without('matplotlib')
124 def test_set_matplotlib_formats():
124 def test_set_matplotlib_formats():
125 from matplotlib.figure import Figure
125 from matplotlib.figure import Figure
126 formatters = get_ipython().display_formatter.formatters
126 formatters = get_ipython().display_formatter.formatters
127 for formats in [
127 for formats in [
128 ('png',),
128 ('png',),
129 ('pdf', 'svg'),
129 ('pdf', 'svg'),
130 ('jpeg', 'retina', 'png'),
130 ('jpeg', 'retina', 'png'),
131 (),
131 (),
132 ]:
132 ]:
133 active_mimes = {_fmt_mime_map[fmt] for fmt in formats}
133 active_mimes = {_fmt_mime_map[fmt] for fmt in formats}
134 display.set_matplotlib_formats(*formats)
134 display.set_matplotlib_formats(*formats)
135 for mime, f in formatters.items():
135 for mime, f in formatters.items():
136 if mime in active_mimes:
136 if mime in active_mimes:
137 nt.assert_in(Figure, f)
137 nt.assert_in(Figure, f)
138 else:
138 else:
139 nt.assert_not_in(Figure, f)
139 nt.assert_not_in(Figure, f)
140
140
141 @dec.skip_without('matplotlib')
141 @dec.skip_without('matplotlib')
142 def test_set_matplotlib_formats_kwargs():
142 def test_set_matplotlib_formats_kwargs():
143 from matplotlib.figure import Figure
143 from matplotlib.figure import Figure
144 ip = get_ipython()
144 ip = get_ipython()
145 cfg = _get_inline_config()
145 cfg = _get_inline_config()
146 cfg.print_figure_kwargs.update(dict(foo='bar'))
146 cfg.print_figure_kwargs.update(dict(foo='bar'))
147 kwargs = dict(quality=10)
147 kwargs = dict(quality=10)
148 display.set_matplotlib_formats('png', **kwargs)
148 display.set_matplotlib_formats('png', **kwargs)
149 formatter = ip.display_formatter.formatters['image/png']
149 formatter = ip.display_formatter.formatters['image/png']
150 f = formatter.lookup_by_type(Figure)
150 f = formatter.lookup_by_type(Figure)
151 cell = f.__closure__[0].cell_contents
151 cell = f.__closure__[0].cell_contents
152 expected = kwargs
152 expected = kwargs
153 expected.update(cfg.print_figure_kwargs)
153 expected.update(cfg.print_figure_kwargs)
154 nt.assert_equal(cell, expected)
154 nt.assert_equal(cell, expected)
155
155
156 def test_display_available():
156 def test_display_available():
157 """
157 """
158 Test that display is available without import
158 Test that display is available without import
159
159
160 We don't really care if it's in builtin or anything else, but it should
160 We don't really care if it's in builtin or anything else, but it should
161 always be available.
161 always be available.
162 """
162 """
163 ip = get_ipython()
163 ip = get_ipython()
164 with AssertNotPrints('NameError'):
164 with AssertNotPrints('NameError'):
165 ip.run_cell('display')
165 ip.run_cell('display')
166 try:
166 try:
167 ip.run_cell('del display')
167 ip.run_cell('del display')
168 except NameError:
168 except NameError:
169 pass # it's ok, it might be in builtins
169 pass # it's ok, it might be in builtins
170 # even if deleted it should be back
170 # even if deleted it should be back
171 with AssertNotPrints('NameError'):
171 with AssertNotPrints('NameError'):
172 ip.run_cell('display')
172 ip.run_cell('display')
173
173
174 def test_textdisplayobj_pretty_repr():
174 def test_textdisplayobj_pretty_repr():
175 p = display.Pretty("This is a simple test")
175 p = display.Pretty("This is a simple test")
176 nt.assert_equal(repr(p), '<IPython.core.display.Pretty object>')
176 nt.assert_equal(repr(p), '<IPython.core.display.Pretty object>')
177 nt.assert_equal(p.data, 'This is a simple test')
177 nt.assert_equal(p.data, 'This is a simple test')
178
178
179 p._show_mem_addr = True
179 p._show_mem_addr = True
180 nt.assert_equal(repr(p), object.__repr__(p))
180 nt.assert_equal(repr(p), object.__repr__(p))
181
181
182 def test_displayobject_repr():
182 def test_displayobject_repr():
183 h = display.HTML('<br />')
183 h = display.HTML('<br />')
184 nt.assert_equal(repr(h), '<IPython.core.display.HTML object>')
184 nt.assert_equal(repr(h), '<IPython.core.display.HTML object>')
185 h._show_mem_addr = True
185 h._show_mem_addr = True
186 nt.assert_equal(repr(h), object.__repr__(h))
186 nt.assert_equal(repr(h), object.__repr__(h))
187 h._show_mem_addr = False
187 h._show_mem_addr = False
188 nt.assert_equal(repr(h), '<IPython.core.display.HTML object>')
188 nt.assert_equal(repr(h), '<IPython.core.display.HTML object>')
189
189
190 j = display.Javascript('')
190 j = display.Javascript('')
191 nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>')
191 nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>')
192 j._show_mem_addr = True
192 j._show_mem_addr = True
193 nt.assert_equal(repr(j), object.__repr__(j))
193 nt.assert_equal(repr(j), object.__repr__(j))
194 j._show_mem_addr = False
194 j._show_mem_addr = False
195 nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>')
195 nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>')
196
196
197 def test_progress():
197 def test_progress():
198 p = display.ProgressBar(10)
198 p = display.ProgressBar(10)
199 nt.assert_true('0/10' in repr(p))
199 nt.assert_true('0/10' in repr(p))
200 nt.assert_equal(p._repr_html_(), "<progress style='width:100%' max='10' value='0'></progress>")
200 p.html_width = '100%'
201 p.progress = 5
202 nt.assert_equal(p._repr_html_(), "<progress style='width:100%' max='10' value='5'></progress>")
201
203
202 def test_json():
204 def test_json():
203 d = {'a': 5}
205 d = {'a': 5}
204 lis = [d]
206 lis = [d]
205 md = {'expanded': False}
207 md = {'expanded': False}
206 md2 = {'expanded': True}
208 md2 = {'expanded': True}
207 j = display.JSON(d)
209 j = display.JSON(d)
208 j2 = display.JSON(d, expanded=True)
210 j2 = display.JSON(d, expanded=True)
209 nt.assert_equal(j._repr_json_(), (d, md))
211 nt.assert_equal(j._repr_json_(), (d, md))
210 nt.assert_equal(j2._repr_json_(), (d, md2))
212 nt.assert_equal(j2._repr_json_(), (d, md2))
211
213
212 with warnings.catch_warnings(record=True) as w:
214 with warnings.catch_warnings(record=True) as w:
213 warnings.simplefilter("always")
215 warnings.simplefilter("always")
214 j = display.JSON(json.dumps(d))
216 j = display.JSON(json.dumps(d))
215 nt.assert_equal(len(w), 1)
217 nt.assert_equal(len(w), 1)
216 nt.assert_equal(j._repr_json_(), (d, md))
218 nt.assert_equal(j._repr_json_(), (d, md))
217 nt.assert_equal(j2._repr_json_(), (d, md2))
219 nt.assert_equal(j2._repr_json_(), (d, md2))
218
220
219 j = display.JSON(lis)
221 j = display.JSON(lis)
220 j2 = display.JSON(lis, expanded=True)
222 j2 = display.JSON(lis, expanded=True)
221 nt.assert_equal(j._repr_json_(), (lis, md))
223 nt.assert_equal(j._repr_json_(), (lis, md))
222 nt.assert_equal(j2._repr_json_(), (lis, md2))
224 nt.assert_equal(j2._repr_json_(), (lis, md2))
223
225
224 with warnings.catch_warnings(record=True) as w:
226 with warnings.catch_warnings(record=True) as w:
225 warnings.simplefilter("always")
227 warnings.simplefilter("always")
226 j = display.JSON(json.dumps(lis))
228 j = display.JSON(json.dumps(lis))
227 nt.assert_equal(len(w), 1)
229 nt.assert_equal(len(w), 1)
228 nt.assert_equal(j._repr_json_(), (lis, md))
230 nt.assert_equal(j._repr_json_(), (lis, md))
229 nt.assert_equal(j2._repr_json_(), (lis, md2))
231 nt.assert_equal(j2._repr_json_(), (lis, md2))
230
232
231 def test_video_embedding():
233 def test_video_embedding():
232 """use a tempfile, with dummy-data, to ensure that video embedding doesn't crash"""
234 """use a tempfile, with dummy-data, to ensure that video embedding doesn't crash"""
233 v = display.Video("http://ignored")
235 v = display.Video("http://ignored")
234 assert not v.embed
236 assert not v.embed
235 html = v._repr_html_()
237 html = v._repr_html_()
236 nt.assert_not_in('src="data:', html)
238 nt.assert_not_in('src="data:', html)
237 nt.assert_in('src="http://ignored"', html)
239 nt.assert_in('src="http://ignored"', html)
238
240
239 with nt.assert_raises(ValueError):
241 with nt.assert_raises(ValueError):
240 v = display.Video(b'abc')
242 v = display.Video(b'abc')
241
243
242 with NamedFileInTemporaryDirectory('test.mp4') as f:
244 with NamedFileInTemporaryDirectory('test.mp4') as f:
243 f.write(b'abc')
245 f.write(b'abc')
244 f.close()
246 f.close()
245
247
246 v = display.Video(f.name)
248 v = display.Video(f.name)
247 assert not v.embed
249 assert not v.embed
248 html = v._repr_html_()
250 html = v._repr_html_()
249 nt.assert_not_in('src="data:', html)
251 nt.assert_not_in('src="data:', html)
250
252
251 v = display.Video(f.name, embed=True)
253 v = display.Video(f.name, embed=True)
252 html = v._repr_html_()
254 html = v._repr_html_()
253 nt.assert_in('src="data:video/mp4;base64,YWJj"',html)
255 nt.assert_in('src="data:video/mp4;base64,YWJj"',html)
254
256
255 v = display.Video(f.name, embed=True, mimetype='video/other')
257 v = display.Video(f.name, embed=True, mimetype='video/other')
256 html = v._repr_html_()
258 html = v._repr_html_()
257 nt.assert_in('src="data:video/other;base64,YWJj"',html)
259 nt.assert_in('src="data:video/other;base64,YWJj"',html)
258
260
259 v = display.Video(b'abc', embed=True, mimetype='video/mp4')
261 v = display.Video(b'abc', embed=True, mimetype='video/mp4')
260 html = v._repr_html_()
262 html = v._repr_html_()
261 nt.assert_in('src="data:video/mp4;base64,YWJj"',html)
263 nt.assert_in('src="data:video/mp4;base64,YWJj"',html)
262
264
263 v = display.Video(u'YWJj', embed=True, mimetype='video/xyz')
265 v = display.Video(u'YWJj', embed=True, mimetype='video/xyz')
264 html = v._repr_html_()
266 html = v._repr_html_()
265 nt.assert_in('src="data:video/xyz;base64,YWJj"',html)
267 nt.assert_in('src="data:video/xyz;base64,YWJj"',html)
266
268
267
269
268 def test_display_id():
270 def test_display_id():
269 ip = get_ipython()
271 ip = get_ipython()
270 with mock.patch.object(ip.display_pub, 'publish') as pub:
272 with mock.patch.object(ip.display_pub, 'publish') as pub:
271 handle = display.display('x')
273 handle = display.display('x')
272 nt.assert_is(handle, None)
274 nt.assert_is(handle, None)
273 handle = display.display('y', display_id='secret')
275 handle = display.display('y', display_id='secret')
274 nt.assert_is_instance(handle, display.DisplayHandle)
276 nt.assert_is_instance(handle, display.DisplayHandle)
275 handle2 = display.display('z', display_id=True)
277 handle2 = display.display('z', display_id=True)
276 nt.assert_is_instance(handle2, display.DisplayHandle)
278 nt.assert_is_instance(handle2, display.DisplayHandle)
277 nt.assert_not_equal(handle.display_id, handle2.display_id)
279 nt.assert_not_equal(handle.display_id, handle2.display_id)
278
280
279 nt.assert_equal(pub.call_count, 3)
281 nt.assert_equal(pub.call_count, 3)
280 args, kwargs = pub.call_args_list[0]
282 args, kwargs = pub.call_args_list[0]
281 nt.assert_equal(args, ())
283 nt.assert_equal(args, ())
282 nt.assert_equal(kwargs, {
284 nt.assert_equal(kwargs, {
283 'data': {
285 'data': {
284 'text/plain': repr('x')
286 'text/plain': repr('x')
285 },
287 },
286 'metadata': {},
288 'metadata': {},
287 })
289 })
288 args, kwargs = pub.call_args_list[1]
290 args, kwargs = pub.call_args_list[1]
289 nt.assert_equal(args, ())
291 nt.assert_equal(args, ())
290 nt.assert_equal(kwargs, {
292 nt.assert_equal(kwargs, {
291 'data': {
293 'data': {
292 'text/plain': repr('y')
294 'text/plain': repr('y')
293 },
295 },
294 'metadata': {},
296 'metadata': {},
295 'transient': {
297 'transient': {
296 'display_id': handle.display_id,
298 'display_id': handle.display_id,
297 },
299 },
298 })
300 })
299 args, kwargs = pub.call_args_list[2]
301 args, kwargs = pub.call_args_list[2]
300 nt.assert_equal(args, ())
302 nt.assert_equal(args, ())
301 nt.assert_equal(kwargs, {
303 nt.assert_equal(kwargs, {
302 'data': {
304 'data': {
303 'text/plain': repr('z')
305 'text/plain': repr('z')
304 },
306 },
305 'metadata': {},
307 'metadata': {},
306 'transient': {
308 'transient': {
307 'display_id': handle2.display_id,
309 'display_id': handle2.display_id,
308 },
310 },
309 })
311 })
310
312
311
313
312 def test_update_display():
314 def test_update_display():
313 ip = get_ipython()
315 ip = get_ipython()
314 with mock.patch.object(ip.display_pub, 'publish') as pub:
316 with mock.patch.object(ip.display_pub, 'publish') as pub:
315 with nt.assert_raises(TypeError):
317 with nt.assert_raises(TypeError):
316 display.update_display('x')
318 display.update_display('x')
317 display.update_display('x', display_id='1')
319 display.update_display('x', display_id='1')
318 display.update_display('y', display_id='2')
320 display.update_display('y', display_id='2')
319 args, kwargs = pub.call_args_list[0]
321 args, kwargs = pub.call_args_list[0]
320 nt.assert_equal(args, ())
322 nt.assert_equal(args, ())
321 nt.assert_equal(kwargs, {
323 nt.assert_equal(kwargs, {
322 'data': {
324 'data': {
323 'text/plain': repr('x')
325 'text/plain': repr('x')
324 },
326 },
325 'metadata': {},
327 'metadata': {},
326 'transient': {
328 'transient': {
327 'display_id': '1',
329 'display_id': '1',
328 },
330 },
329 'update': True,
331 'update': True,
330 })
332 })
331 args, kwargs = pub.call_args_list[1]
333 args, kwargs = pub.call_args_list[1]
332 nt.assert_equal(args, ())
334 nt.assert_equal(args, ())
333 nt.assert_equal(kwargs, {
335 nt.assert_equal(kwargs, {
334 'data': {
336 'data': {
335 'text/plain': repr('y')
337 'text/plain': repr('y')
336 },
338 },
337 'metadata': {},
339 'metadata': {},
338 'transient': {
340 'transient': {
339 'display_id': '2',
341 'display_id': '2',
340 },
342 },
341 'update': True,
343 'update': True,
342 })
344 })
343
345
344
346
345 def test_display_handle():
347 def test_display_handle():
346 ip = get_ipython()
348 ip = get_ipython()
347 handle = display.DisplayHandle()
349 handle = display.DisplayHandle()
348 nt.assert_is_instance(handle.display_id, str)
350 nt.assert_is_instance(handle.display_id, str)
349 handle = display.DisplayHandle('my-id')
351 handle = display.DisplayHandle('my-id')
350 nt.assert_equal(handle.display_id, 'my-id')
352 nt.assert_equal(handle.display_id, 'my-id')
351 with mock.patch.object(ip.display_pub, 'publish') as pub:
353 with mock.patch.object(ip.display_pub, 'publish') as pub:
352 handle.display('x')
354 handle.display('x')
353 handle.update('y')
355 handle.update('y')
354
356
355 args, kwargs = pub.call_args_list[0]
357 args, kwargs = pub.call_args_list[0]
356 nt.assert_equal(args, ())
358 nt.assert_equal(args, ())
357 nt.assert_equal(kwargs, {
359 nt.assert_equal(kwargs, {
358 'data': {
360 'data': {
359 'text/plain': repr('x')
361 'text/plain': repr('x')
360 },
362 },
361 'metadata': {},
363 'metadata': {},
362 'transient': {
364 'transient': {
363 'display_id': handle.display_id,
365 'display_id': handle.display_id,
364 }
366 }
365 })
367 })
366 args, kwargs = pub.call_args_list[1]
368 args, kwargs = pub.call_args_list[1]
367 nt.assert_equal(args, ())
369 nt.assert_equal(args, ())
368 nt.assert_equal(kwargs, {
370 nt.assert_equal(kwargs, {
369 'data': {
371 'data': {
370 'text/plain': repr('y')
372 'text/plain': repr('y')
371 },
373 },
372 'metadata': {},
374 'metadata': {},
373 'transient': {
375 'transient': {
374 'display_id': handle.display_id,
376 'display_id': handle.display_id,
375 },
377 },
376 'update': True,
378 'update': True,
377 })
379 })
378
380
General Comments 0
You need to be logged in to leave comments. Login now