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