##// END OF EJS Templates
add some light tests
Marius van Niekerk -
Show More
@@ -1,373 +1,379 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():
198 p = display.ProgressBar(10)
199 nt.assert_equal(repr(p), '<IPython.core.display.ProgressBar object>')
200 p._show_mem_addr = True
201 nt.assert_equal(p._repr_html_(), "<progress style='width:100%' max='10' value='0'></progress>")
202
197 def test_json():
203 def test_json():
198 d = {'a': 5}
204 d = {'a': 5}
199 lis = [d]
205 lis = [d]
200 md = {'expanded': False}
206 md = {'expanded': False}
201 md2 = {'expanded': True}
207 md2 = {'expanded': True}
202 j = display.JSON(d)
208 j = display.JSON(d)
203 j2 = display.JSON(d, expanded=True)
209 j2 = display.JSON(d, expanded=True)
204 nt.assert_equal(j._repr_json_(), (d, md))
210 nt.assert_equal(j._repr_json_(), (d, md))
205 nt.assert_equal(j2._repr_json_(), (d, md2))
211 nt.assert_equal(j2._repr_json_(), (d, md2))
206
212
207 with warnings.catch_warnings(record=True) as w:
213 with warnings.catch_warnings(record=True) as w:
208 warnings.simplefilter("always")
214 warnings.simplefilter("always")
209 j = display.JSON(json.dumps(d))
215 j = display.JSON(json.dumps(d))
210 nt.assert_equal(len(w), 1)
216 nt.assert_equal(len(w), 1)
211 nt.assert_equal(j._repr_json_(), (d, md))
217 nt.assert_equal(j._repr_json_(), (d, md))
212 nt.assert_equal(j2._repr_json_(), (d, md2))
218 nt.assert_equal(j2._repr_json_(), (d, md2))
213
219
214 j = display.JSON(lis)
220 j = display.JSON(lis)
215 j2 = display.JSON(lis, expanded=True)
221 j2 = display.JSON(lis, expanded=True)
216 nt.assert_equal(j._repr_json_(), (lis, md))
222 nt.assert_equal(j._repr_json_(), (lis, md))
217 nt.assert_equal(j2._repr_json_(), (lis, md2))
223 nt.assert_equal(j2._repr_json_(), (lis, md2))
218
224
219 with warnings.catch_warnings(record=True) as w:
225 with warnings.catch_warnings(record=True) as w:
220 warnings.simplefilter("always")
226 warnings.simplefilter("always")
221 j = display.JSON(json.dumps(lis))
227 j = display.JSON(json.dumps(lis))
222 nt.assert_equal(len(w), 1)
228 nt.assert_equal(len(w), 1)
223 nt.assert_equal(j._repr_json_(), (lis, md))
229 nt.assert_equal(j._repr_json_(), (lis, md))
224 nt.assert_equal(j2._repr_json_(), (lis, md2))
230 nt.assert_equal(j2._repr_json_(), (lis, md2))
225
231
226 def test_video_embedding():
232 def test_video_embedding():
227 """use a tempfile, with dummy-data, to ensure that video embedding doesn't crash"""
233 """use a tempfile, with dummy-data, to ensure that video embedding doesn't crash"""
228 v = display.Video("http://ignored")
234 v = display.Video("http://ignored")
229 assert not v.embed
235 assert not v.embed
230 html = v._repr_html_()
236 html = v._repr_html_()
231 nt.assert_not_in('src="data:', html)
237 nt.assert_not_in('src="data:', html)
232 nt.assert_in('src="http://ignored"', html)
238 nt.assert_in('src="http://ignored"', html)
233
239
234 with nt.assert_raises(ValueError):
240 with nt.assert_raises(ValueError):
235 v = display.Video(b'abc')
241 v = display.Video(b'abc')
236
242
237 with NamedFileInTemporaryDirectory('test.mp4') as f:
243 with NamedFileInTemporaryDirectory('test.mp4') as f:
238 f.write(b'abc')
244 f.write(b'abc')
239 f.close()
245 f.close()
240
246
241 v = display.Video(f.name)
247 v = display.Video(f.name)
242 assert not v.embed
248 assert not v.embed
243 html = v._repr_html_()
249 html = v._repr_html_()
244 nt.assert_not_in('src="data:', html)
250 nt.assert_not_in('src="data:', html)
245
251
246 v = display.Video(f.name, embed=True)
252 v = display.Video(f.name, embed=True)
247 html = v._repr_html_()
253 html = v._repr_html_()
248 nt.assert_in('src="data:video/mp4;base64,YWJj"',html)
254 nt.assert_in('src="data:video/mp4;base64,YWJj"',html)
249
255
250 v = display.Video(f.name, embed=True, mimetype='video/other')
256 v = display.Video(f.name, embed=True, mimetype='video/other')
251 html = v._repr_html_()
257 html = v._repr_html_()
252 nt.assert_in('src="data:video/other;base64,YWJj"',html)
258 nt.assert_in('src="data:video/other;base64,YWJj"',html)
253
259
254 v = display.Video(b'abc', embed=True, mimetype='video/mp4')
260 v = display.Video(b'abc', embed=True, mimetype='video/mp4')
255 html = v._repr_html_()
261 html = v._repr_html_()
256 nt.assert_in('src="data:video/mp4;base64,YWJj"',html)
262 nt.assert_in('src="data:video/mp4;base64,YWJj"',html)
257
263
258 v = display.Video(u'YWJj', embed=True, mimetype='video/xyz')
264 v = display.Video(u'YWJj', embed=True, mimetype='video/xyz')
259 html = v._repr_html_()
265 html = v._repr_html_()
260 nt.assert_in('src="data:video/xyz;base64,YWJj"',html)
266 nt.assert_in('src="data:video/xyz;base64,YWJj"',html)
261
267
262
268
263 def test_display_id():
269 def test_display_id():
264 ip = get_ipython()
270 ip = get_ipython()
265 with mock.patch.object(ip.display_pub, 'publish') as pub:
271 with mock.patch.object(ip.display_pub, 'publish') as pub:
266 handle = display.display('x')
272 handle = display.display('x')
267 nt.assert_is(handle, None)
273 nt.assert_is(handle, None)
268 handle = display.display('y', display_id='secret')
274 handle = display.display('y', display_id='secret')
269 nt.assert_is_instance(handle, display.DisplayHandle)
275 nt.assert_is_instance(handle, display.DisplayHandle)
270 handle2 = display.display('z', display_id=True)
276 handle2 = display.display('z', display_id=True)
271 nt.assert_is_instance(handle2, display.DisplayHandle)
277 nt.assert_is_instance(handle2, display.DisplayHandle)
272 nt.assert_not_equal(handle.display_id, handle2.display_id)
278 nt.assert_not_equal(handle.display_id, handle2.display_id)
273
279
274 nt.assert_equal(pub.call_count, 3)
280 nt.assert_equal(pub.call_count, 3)
275 args, kwargs = pub.call_args_list[0]
281 args, kwargs = pub.call_args_list[0]
276 nt.assert_equal(args, ())
282 nt.assert_equal(args, ())
277 nt.assert_equal(kwargs, {
283 nt.assert_equal(kwargs, {
278 'data': {
284 'data': {
279 'text/plain': repr('x')
285 'text/plain': repr('x')
280 },
286 },
281 'metadata': {},
287 'metadata': {},
282 })
288 })
283 args, kwargs = pub.call_args_list[1]
289 args, kwargs = pub.call_args_list[1]
284 nt.assert_equal(args, ())
290 nt.assert_equal(args, ())
285 nt.assert_equal(kwargs, {
291 nt.assert_equal(kwargs, {
286 'data': {
292 'data': {
287 'text/plain': repr('y')
293 'text/plain': repr('y')
288 },
294 },
289 'metadata': {},
295 'metadata': {},
290 'transient': {
296 'transient': {
291 'display_id': handle.display_id,
297 'display_id': handle.display_id,
292 },
298 },
293 })
299 })
294 args, kwargs = pub.call_args_list[2]
300 args, kwargs = pub.call_args_list[2]
295 nt.assert_equal(args, ())
301 nt.assert_equal(args, ())
296 nt.assert_equal(kwargs, {
302 nt.assert_equal(kwargs, {
297 'data': {
303 'data': {
298 'text/plain': repr('z')
304 'text/plain': repr('z')
299 },
305 },
300 'metadata': {},
306 'metadata': {},
301 'transient': {
307 'transient': {
302 'display_id': handle2.display_id,
308 'display_id': handle2.display_id,
303 },
309 },
304 })
310 })
305
311
306
312
307 def test_update_display():
313 def test_update_display():
308 ip = get_ipython()
314 ip = get_ipython()
309 with mock.patch.object(ip.display_pub, 'publish') as pub:
315 with mock.patch.object(ip.display_pub, 'publish') as pub:
310 with nt.assert_raises(TypeError):
316 with nt.assert_raises(TypeError):
311 display.update_display('x')
317 display.update_display('x')
312 display.update_display('x', display_id='1')
318 display.update_display('x', display_id='1')
313 display.update_display('y', display_id='2')
319 display.update_display('y', display_id='2')
314 args, kwargs = pub.call_args_list[0]
320 args, kwargs = pub.call_args_list[0]
315 nt.assert_equal(args, ())
321 nt.assert_equal(args, ())
316 nt.assert_equal(kwargs, {
322 nt.assert_equal(kwargs, {
317 'data': {
323 'data': {
318 'text/plain': repr('x')
324 'text/plain': repr('x')
319 },
325 },
320 'metadata': {},
326 'metadata': {},
321 'transient': {
327 'transient': {
322 'display_id': '1',
328 'display_id': '1',
323 },
329 },
324 'update': True,
330 'update': True,
325 })
331 })
326 args, kwargs = pub.call_args_list[1]
332 args, kwargs = pub.call_args_list[1]
327 nt.assert_equal(args, ())
333 nt.assert_equal(args, ())
328 nt.assert_equal(kwargs, {
334 nt.assert_equal(kwargs, {
329 'data': {
335 'data': {
330 'text/plain': repr('y')
336 'text/plain': repr('y')
331 },
337 },
332 'metadata': {},
338 'metadata': {},
333 'transient': {
339 'transient': {
334 'display_id': '2',
340 'display_id': '2',
335 },
341 },
336 'update': True,
342 'update': True,
337 })
343 })
338
344
339
345
340 def test_display_handle():
346 def test_display_handle():
341 ip = get_ipython()
347 ip = get_ipython()
342 handle = display.DisplayHandle()
348 handle = display.DisplayHandle()
343 nt.assert_is_instance(handle.display_id, str)
349 nt.assert_is_instance(handle.display_id, str)
344 handle = display.DisplayHandle('my-id')
350 handle = display.DisplayHandle('my-id')
345 nt.assert_equal(handle.display_id, 'my-id')
351 nt.assert_equal(handle.display_id, 'my-id')
346 with mock.patch.object(ip.display_pub, 'publish') as pub:
352 with mock.patch.object(ip.display_pub, 'publish') as pub:
347 handle.display('x')
353 handle.display('x')
348 handle.update('y')
354 handle.update('y')
349
355
350 args, kwargs = pub.call_args_list[0]
356 args, kwargs = pub.call_args_list[0]
351 nt.assert_equal(args, ())
357 nt.assert_equal(args, ())
352 nt.assert_equal(kwargs, {
358 nt.assert_equal(kwargs, {
353 'data': {
359 'data': {
354 'text/plain': repr('x')
360 'text/plain': repr('x')
355 },
361 },
356 'metadata': {},
362 'metadata': {},
357 'transient': {
363 'transient': {
358 'display_id': handle.display_id,
364 'display_id': handle.display_id,
359 }
365 }
360 })
366 })
361 args, kwargs = pub.call_args_list[1]
367 args, kwargs = pub.call_args_list[1]
362 nt.assert_equal(args, ())
368 nt.assert_equal(args, ())
363 nt.assert_equal(kwargs, {
369 nt.assert_equal(kwargs, {
364 'data': {
370 'data': {
365 'text/plain': repr('y')
371 'text/plain': repr('y')
366 },
372 },
367 'metadata': {},
373 'metadata': {},
368 'transient': {
374 'transient': {
369 'display_id': handle.display_id,
375 'display_id': handle.display_id,
370 },
376 },
371 'update': True,
377 'update': True,
372 })
378 })
373
379
General Comments 0
You need to be logged in to leave comments. Login now